Sha256: 0d92a37644e59a05dc46aeef06ad171867907786a8a46365b63cfd779aa3c3a7

Contents?: true

Size: 1.67 KB

Versions: 29

Compression:

Stored size: 1.67 KB

Contents

require 'resourceful/builder'

module Resourceful
  module Default
    # This module is mostly meant to be used by the make_resourceful default actions.
    # It provides various methods that declare where callbacks set in the +make_resourceful+ block,
    # like Builder#before and Builder#response_for,
    # should be called.
    module Callbacks
      # Calls any +before+ callbacks set in the +make_resourceful+ block for the given event.
      def before(event)
        resourceful_fire(:before, event.to_sym)
      end

      # Calls any +after+ callbacks set in the +make_resourceful+ block for the given event.
      def after(event)
        resourceful_fire(:after, event.to_sym)
      end

      # Calls any +response_for+ callbacks set in the +make_resourceful+ block for the given event.
      # Note that these aren't called directly,
      # but instead passed along to Rails' respond_to method.
      def response_for(event)
        if responses = self.class.resourceful_responses[event.to_sym]
          respond_to do |format|
            responses.each do |key, value|
              format.send(key, &scope(value))
            end
          end
        end
      end

      # Returns a block identical to the given block,
      # but in the context of the current controller.
      # The returned block accepts no arguments,
      # even if the given block accepted them.
      def scope(block)
        proc do
          instance_eval(&(block || proc {}))
        end
      end

      private

      def resourceful_fire(type, name)
        callbacks = self.class.resourceful_callbacks[type][name] || []
        callbacks.each { |callback| scope(callback).call }
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
make_resourceful-2.0.0.pre.beta lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.5 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.4 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.3 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.2 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.1 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.8.0 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.6 lib/resourceful/default/callbacks.rb
make_resourceful-1.0.2 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.5 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.4 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.3 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.2 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.1 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.7.0 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.5.15 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.5.14 lib/resourceful/default/callbacks.rb
easy_admin_ui-0.6.1 lib/resourceful/default/callbacks.rb
make_resourceful_rails2-1.0.1.1 lib/resourceful/default/callbacks.rb
make_resourceful_rails2-1.0.1 lib/resourceful/default/callbacks.rb