Sha256: de380818a62e13d1a635ce0a7f3d71252598f1d8a7951841bf9c3e9d604d99df

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module ActiveAdmin

  # Member Actions give you the functionality of defining both the
  # action and the route directly from your ActiveAdmin registration
  # block.
  #
  # For example:
  #
  #   ActiveAdmin.register Post do
  #     member_action :comments do
  #       @post = Post.find(params[:id]
  #       @comments = @post.comments
  #     end
  #   end
  #
  # Will create a new controller action comments and will hook it up to
  # the named route (comments_admin_post_path) /admin/posts/:id/comments
  #
  # You can treat everything within the block as a standard Rails controller
  # action.
  # 
  module ActionBuilder

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def member_action(name, options = {}, &block)
        active_admin_config.member_actions << ControllerAction.new(name, options)
        define_method(name, &block || Proc.new{})
      end

      def clear_member_actions!
        active_admin_config.clear_member_actions!
      end


      def collection_action(name, options = {}, &block)
        active_admin_config.collection_actions << ControllerAction.new(name, options)
        define_method(name, &block || Proc.new{})
      end

      def clear_collection_actions!
        active_admin_config.clear_collection_actions!
      end
    end

    class ControllerAction
      attr_reader :name
      def initialize(name, options = {})
        @name, @options = name, options
      end

      def http_verb
        @options[:method] ||= :get
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeadmin-0.1.1 lib/active_admin/action_builder.rb
activeadmin-0.1.0 lib/active_admin/action_builder.rb