Browse files

Merge pull request #1228 from genuitytech/active_admin

---

With this commit, setting the page title for collection and member actions is consistent with how it is done with the standard actions:

``` ruby
ActiveAdmin.register Post do
  collection_action :comments, :title => "My Awesome Comments" do
    # do stuff
  end
end
```
  • Loading branch information...
2 parents 1bff73d + 2ae297e commit 93141c867e82d737ee15389382a3ea2bd5709b4c @pcreux pcreux committed May 9, 2012
Showing with 44 additions and 0 deletions.
  1. +6 −0 lib/active_admin/resource_dsl.rb
  2. +38 −0 spec/unit/action_builder_spec.rb
View
6 lib/active_admin/resource_dsl.rb
@@ -93,14 +93,20 @@ def csv(&block)
#
def member_action(name, options = {}, &block)
config.member_actions << ControllerAction.new(name, options)
+ title = options.delete(:title)
+
controller do
+ before_filter :only => [name] { @page_title = title } if title
define_method(name, &block || Proc.new{})
end
end
def collection_action(name, options = {}, &block)
config.collection_actions << ControllerAction.new(name, options)
+ title = options.delete(:title)
+
controller do
+ before_filter :only => [name] { @page_title = title } if title
define_method(name, &block || Proc.new{})
end
end
View
38 spec/unit/action_builder_spec.rb
@@ -44,6 +44,18 @@
controller.public_instance_methods.collect(&:to_s).should include("comment")
end
end
+
+ context "with :title" do
+ let(:action!) do
+ ActiveAdmin.register Post do
+ member_action :comment, :title => "My Awesome Comment"
+ end
+ end
+
+ subject { find_before_filter controller, :comment }
+
+ it { should set_page_title_to "My Awesome Comment" }
+ end
end
describe "generate a new collection action" do
@@ -83,6 +95,32 @@
controller.public_instance_methods.collect(&:to_s).should include("comments")
end
end
+ context "with :title" do
+ let(:action!) do
+ ActiveAdmin.register Post do
+ collection_action :comments, :title => "My Awesome Comments"
+ end
+ end
+
+ subject { find_before_filter controller, :comments }
+
+ it { should set_page_title_to "My Awesome Comments" }
+ end
+ end
+
+ def find_before_filter(controller, action)
+ controller._process_action_callbacks.detect { |f| f.kind == :before && f.options[:only] == [action] }
end
+ RSpec::Matchers.define :set_page_title_to do |expected|
+ match do |filter|
+ filter.raw_filter.call
+ @actual = filter.klass.instance_variable_get(:@page_title)
+ @actual == expected
+ end
+
+ failure_message_for_should do |filter|
+ message = "expected before_filter to set the @page_title to '#{expected}', but was '#{@actual}'"
+ end
+ end
end

0 comments on commit 93141c8

Please sign in to comment.