Sha256: 3e1314421c7e11f343535d4004deb6dd59d1e9015b430c845fc5d4f7c0436b58

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'action_controller'

module Zertico
  class Controller < ActionController::Base
    def initialize
      begin
        extend "::#{self.class.name.chomp("Controller").concat("Service")}".constantize
      rescue NameError
        extend Zertico::Service
      end
      super
    end

    def index
      initialize_object all
      respond_with(instance_variable_get("@responder"))
    end

    def new
      initialize_object build
      respond_with(instance_variable_get("@responder"))
    end

    def show
      initialize_object find(params[:id])
      respond_with(instance_variable_get("@responder"))
    end

    def edit
      initialize_object find(params[:id])
      respond_with(instance_variable_get("@responder"))
    end

    def create
      initialize_object generate(params[interface_name.to_sym])
      respond_with(instance_variable_get("@responder"))
    end

    def update
      initialize_object modify(params[:id], params[interface_name.to_sym])
      respond_with(instance_variable_get("@responder"))
    end

    def destroy
      initialize_object delete(params[:id])
      respond_with(instance_variable_get("@responder"))
    end

    protected

    def initialize_object(objects = {})
      objects.each do |key, value|
        instance_variable_set("@#{key}", value)
        instance_variable_set("@responder", value) unless @responder
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zertico-0.4.0 lib/zertico/controller.rb