Skip to content

Commit

Permalink
Merge 49f0f80 into a932d56
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Dec 6, 2016
2 parents a932d56 + 49f0f80 commit e8138c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
19 changes: 19 additions & 0 deletions features/index/index_scopes.feature
Expand Up @@ -14,6 +14,25 @@ Feature: Index Scoping
And I should see the scope "All" with the count 3
And I should see 3 posts in the table

Scenario: Viewing resources with one scope with dynamic name
Given 3 posts exist
And an index configuration of:
"""
ActiveAdmin.register Post do
scope -> { scope_title }, :all
controller do
def scope_title
'Neat scope'
end
helper_method :scope_title
end
end
"""
Then I should see the scope with label "Neat scope"
And I should see 3 posts in the table

Scenario: Viewing resources with one scope as the default
Given 3 posts exist
And an index configuration of:
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/index_scope_steps.rb
Expand Up @@ -14,6 +14,10 @@
step %{I should see "#{count}" within ".scopes .#{name} .count"}
end

Then /^I should see the scope with label "([^"]*)"$/ do |label|
expect(page).to have_link(label)
end

Then /^I should see the scope "([^"]*)" with no count$/ do |name|
name = name.tr(" ", "").underscore.downcase
expect(page).to have_css ".scopes .#{name}"
Expand Down
3 changes: 1 addition & 2 deletions lib/active_admin/scope.rb
Expand Up @@ -45,10 +45,9 @@ def initialize(name, method = nil, options = {}, &block)

def name
case @name
when Proc then @name.call.to_s
when String then @name
when Symbol then @name.to_s.titleize
else @name.to_s
else @name
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/active_admin/views/components/scopes.rb
Expand Up @@ -31,7 +31,7 @@ def build(scopes, options = {})

def build_scope(scope, options)
li class: classes_for_scope(scope) do
scope_name = I18n.t "active_admin.scopes.#{scope.id}", default: scope.name
scope_name = I18n.t "active_admin.scopes.#{scope.id}", default: name_for_scope(scope)
params = request.query_parameters.except :page, :scope, :commit, :format

a href: url_for(scope: scope.id, params: params), class: 'table_tools_button' do
Expand Down Expand Up @@ -61,6 +61,13 @@ def current_scope?(scope)
def get_scope_count(scope)
collection_size(scope_chain(scope, collection_before_scope))
end

def name_for_scope(scope)
case scope.name
when Proc then self.instance_exec(&scope.name).to_s
else scope.name.to_s
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/scope_spec.rb
Expand Up @@ -135,7 +135,7 @@

it "should properly render the proc" do
scope = ActiveAdmin::Scope.new proc{ Date.today.strftime '%A' }, :foobar
expect(scope.name).to eq Date.today.strftime '%A'
expect(scope.name.call).to eq Date.today.strftime '%A'
end
end

Expand Down

0 comments on commit e8138c0

Please sign in to comment.