Skip to content

Commit

Permalink
Merge pull request #3091 from carnesmedia/name-action-items
Browse files Browse the repository at this point in the history
Name action items; allow removing them by name
  • Loading branch information
timoschilling committed Oct 24, 2014
2 parents 8b6586c + a73ed36 commit 3395dd8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 30 deletions.
4 changes: 2 additions & 2 deletions docs/10-custom-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Just like other resources, you can add action items. The difference here being t
`:only` and `:except` don't apply because there's only one page it could apply to.

```ruby
action_item do
action_item :view_site do
link_to "View Site", "/"
end
```
Expand All @@ -74,7 +74,7 @@ page_action :add_event, method: :post do
redirect_to admin_calendar_path, notice: "Your event was added"
end

action_item do
action_item :add do
link_to "Add Event", admin_calendar_add_event_path, method: :post
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/13-authorization-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ ActiveAdmin.register Post do
redirect_to [:admin, post]
end

action_item :only => :show do
action_item :publish, :only => :show do
if !post.published? && authorized?(:publish, post)
link_to "Publish", publish_admin_post_path(post), method: :post
end
Expand Down
7 changes: 4 additions & 3 deletions docs/8-custom-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,20 @@ end
# Action Items

To include your own action items (like the New, Edit and Delete buttons), add an
`action_item` block. For example, to add a "View on site" button to view a blog
`action_item` block. The first parameter is just a name to identify the action,
and is required. For example, to add a "View on site" button to view a blog
post:

```ruby
action_item only: :show do
action_item :view, only: :show do
link_to 'View on site', post_path(post) if post.published?
end
```

Actions items also accept the `:if` option to conditionally display them:

```ruby
action_item only: :show, if: proc{ current_admin_user.super_admin? } do
action_item :super_action, only: :show, if: proc{ current_admin_user.super_admin? } do
"Only display this to super admins on the show screen"
end
```
Expand Down
6 changes: 3 additions & 3 deletions features/action_item.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Action Item
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item do
action_item :embiggen do
link_to "Embiggen", '/'
end
end
Expand All @@ -32,7 +32,7 @@ Feature: Action Item
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item :if => proc{ !current_active_admin_user.nil? } do
action_item :embiggen, :if => proc{ !current_active_admin_user.nil? } do
link_to "Embiggen", '/'
end
end
Expand All @@ -54,7 +54,7 @@ Feature: Action Item
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item :if => proc{ current_active_admin_user.nil? } do
action_item :embiggen, :if => proc{ current_active_admin_user.nil? } do
link_to "Embiggen", '/'
end
end
Expand Down
2 changes: 1 addition & 1 deletion features/registering_pages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Feature: Registering Pages
Given a configuration of:
"""
ActiveAdmin.register_page "Status" do
action_item do
action_item :visit do
link_to "Visit", "/"
end
Expand Down
6 changes: 3 additions & 3 deletions features/specifying_actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Feature: Specifying Actions
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item(:only => :index) do
action_item(:import, :only => :index) do
link_to('Import Posts', import_admin_posts_path)
end
Expand All @@ -43,7 +43,7 @@ Feature: Specifying Actions
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item(:only => :show) do
action_item(:review, :only => :show) do
link_to('Review', review_admin_post_path)
end
Expand All @@ -69,7 +69,7 @@ Feature: Specifying Actions
Given a configuration of:
"""
ActiveAdmin.register Post do
action_item(:only => :show) do
action_item(:review, :only => :show) do
link_to('Review', review_admin_post_path)
end
Expand Down
12 changes: 10 additions & 2 deletions lib/active_admin/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,21 @@ def controller(&block)

# Add a new action item to the resource
#
# @param [Symbol] name
# @param [Hash] options valid keys include:
# :only: A single or array of controller actions to display
# this action item on.
# :except: A single or array of controller actions not to
# display this action item on.
def action_item(options = {}, &block)
config.add_action_item(options, &block)
def action_item(name = nil, options = {}, &block)
if name.is_a?(Hash)
options = name
name = nil
end

Deprecation.warn "using `action_item` without a name is deprecated! Use `action_item(:edit)`." unless name

config.add_action_item(name, options, &block)
end

# Add a new batch action item to the resource
Expand Down
23 changes: 15 additions & 8 deletions lib/active_admin/resource/action_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ def action_items

# Add a new action item to a resource
#
# @param [Symbol] name
# @param [Hash] options valid keys include:
# :only: A single or array of controller actions to display
# this action item on.
# :except: A single or array of controller actions not to
# display this action item on.
def add_action_item(options = {}, &block)
self.action_items << ActiveAdmin::ActionItem.new(options, &block)
def add_action_item(name, options = {}, &block)
self.action_items << ActiveAdmin::ActionItem.new(name, options, &block)
end

def remove_action_item(name)
self.action_items.delete_if { |item| item.name == name }
end

# Returns a set of action items to display for a specific controller action
Expand All @@ -51,21 +56,21 @@ def action_items?
# Adds the default action items to each resource
def add_default_action_items
# New link on index
add_action_item only: :index do
add_action_item :new, only: :index do
if controller.action_methods.include?('new') && authorized?(ActiveAdmin::Auth::CREATE, active_admin_config.resource_class)
link_to I18n.t('active_admin.new_model', model: active_admin_config.resource_label), new_resource_path
end
end

# Edit link on show
add_action_item only: :show do
add_action_item :show, only: :show do
if controller.action_methods.include?('edit') && authorized?(ActiveAdmin::Auth::UPDATE, resource)
link_to I18n.t('active_admin.edit_model', model: active_admin_config.resource_label), edit_resource_path(resource)
end
end

# Destroy link on show
add_action_item only: :show do
add_action_item :destroy, only: :show do
if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource)
link_to I18n.t('active_admin.delete_model', model: active_admin_config.resource_label), resource_path(resource),
method: :delete, data: {confirm: I18n.t('active_admin.delete_confirmation')}
Expand All @@ -80,10 +85,12 @@ def add_default_action_items
class ActionItem
include ActiveAdmin::OptionalDisplay

attr_accessor :block
attr_accessor :block, :name

def initialize(options = {}, &block)
@options, @block = options, block
def initialize(name, options = {}, &block)
@name = name
@options = options
@block = block
normalize_display_options!
end
end
Expand Down
24 changes: 21 additions & 3 deletions spec/unit/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,35 @@ def self.included(dsl)


describe '#action_item' do
before { @default_items_count = resource_config.action_items.size }
before do
@default_items_count = resource_config.action_items.size

it "add action_item to the action_items of config" do
dsl.run_registration_block do
action_item only: :show do
action_item :awesome, only: :show do
"Awesome ActionItem"
end
end
end

it "adds action_item to the action_items of config" do
expect(resource_config.action_items.size).to eq(@default_items_count + 1)
end

context 'DEPRECATED: when used without a name' do
before do
dsl.run_registration_block do
action_item only: :edit do
"Awesome ActionItem"
end
end
end

it "is configured for only the show action" do
item = resource_config.action_items.last
expect(item.display_on?(:edit)).to be true
expect(item.display_on?(:index)).to be false
end
end
end

describe "#menu" do
Expand Down
11 changes: 7 additions & 4 deletions spec/unit/resource/action_items_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

before do
resource.clear_action_items!
resource.add_action_item do
resource.add_action_item :empty do
# Empty ...
end
end
Expand All @@ -34,10 +34,10 @@

before do
resource.clear_action_items!
resource.add_action_item only: :index do
resource.add_action_item :new, only: :index do
raise StandardError
end
resource.add_action_item only: :show do
resource.add_action_item :edit, only: :show do
# Empty ...
end
end
Expand All @@ -52,11 +52,14 @@
end

describe "default action items" do

it "should have 3 action items" do
expect(resource.action_items.size).to eq 3
end

it 'can be removed by name' do
resource.remove_action_item :new
expect(resource.action_items.size).to eq 2
end
end

end

0 comments on commit 3395dd8

Please sign in to comment.