Skip to content

Commit

Permalink
Change action_item option to priority
Browse files Browse the repository at this point in the history
  • Loading branch information
andreslemik committed Jan 29, 2018
1 parent 7d75c59 commit 72c613a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/active_admin/resource/action_items.rb
Expand Up @@ -24,10 +24,8 @@ def action_items
# this action item on.
# :except: A single or array of controller actions not to
# display this action item on.
# :first: Place action item to the beginning of action_items array.
def add_action_item(name, options = {}, &block)
method_name = options.delete(:first) ? :unshift : :<<
action_items.public_send method_name, ActiveAdmin::ActionItem.new(name, options, &block)
self.action_items << ActiveAdmin::ActionItem.new(name, options, &block)
end

def remove_action_item(name)
Expand All @@ -40,7 +38,7 @@ def remove_action_item(name)
#
# @return [Array] Array of ActionItems for the controller actions
def action_items_for(action, render_context = nil)
action_items.select{ |item| item.display_on? action, render_context }
action_items.select { |item| item.display_on? action, render_context }.sort_by(&:priority)
end

# Clears all the existing action items for this resource
Expand Down Expand Up @@ -112,6 +110,10 @@ def initialize(name, options = {}, &block)
def html_class
"action_item #{@options[:class]}".rstrip
end

def priority
@options[:priority] || 10
end
end

end
9 changes: 6 additions & 3 deletions spec/unit/resource/action_items_spec.rb
Expand Up @@ -32,11 +32,14 @@
expect(resource.action_items.first.html_class).to eq("action_item test")
end

it 'should be first when specified' do
resource.add_action_item :first_item, first: true do
it 'should be ordered by priority' do
resource.add_action_item :first, priority: 0 do
# Empty ...
end
expect(resource.action_items.first.name).to eq :first_item
resource.add_action_item :second, priority: 1 do
# Empty ...
end
expect(resource.action_items_for(:index).collect(&:name)).to eq [:first, :second, :empty]
end

end
Expand Down

0 comments on commit 72c613a

Please sign in to comment.