10000 Merge pull request #5359 from leio10/scopes_groups · stephancom/activeadmin@5cedfe5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cedfe5

Browse files
Merge pull request activeadmin#5359 from leio10/scopes_groups
Scopes groups
2 parents c9d3e9c + f4bbc4c commit 5cedfe5

File tree

8 files changed

+93
-9
lines changed

8 files changed

+93
-9
lines changed

app/assets/stylesheets/active_admin/components/_scopes.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
font-size: 0.9em;
77
line-height: 10px;
88
}
9+
&:first-child a {
10+
margin-left: 10px;
11+
}
912
}
1013
}

docs/3-index-pages.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,23 @@ end
214214
Scopes can be labelled with a translation, e.g.
215215
`activerecord.scopes.invoice.expired`.
216216

217+
### Scopes groups
218+
219+
You can assign group names to scopes to keep related scopes together and separate them from the rest.
220+
221+
```ruby
222+
# a scope in the default group
223+
scope :all
224+
225+
# two scopes used to filter by status
226+
scope :active, group: :status
227+
scope :inactive, group: :status
228+
229+
# two scopes used to filter by date
230+
scope :today, group: :date
231+
scope :tomorrow, group: :date
232+
```
233+
217234
## Index default sort order
218235

219236
You can define the default sort order for index pages:

features/index/index_scopes.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,27 @@ Feature: Index Scoping
283283
And I should see the scope "Published" with the count 1
284284
And I should see 1 posts in the table
285285
And I should see the current scope with label "Published"
286+
287+
Scenario: Viewing resources with grouped scopes
288+
Given 3 posts exist
289+
And an index configuration of:
290+
"""
291+
ActiveAdmin.register Post do
292+
scope :all
293+
scope "Published", group: :status do |posts|
294+
posts.where("published_date IS NOT NULL")
295+
end
296+
scope "Unpublished", group: :status do |posts|
297+
posts.where("published_date IS NULL")
298+
end
299+
scope "Today", group: :date do |posts|
300+
posts.where(["created_at > ? AND created_at < ?", ::Time.zone.now.beginning_of_day, ::Time.zone.now.end_of_day])
301+
end
302+
scope "Tomorrow", group: :date do |posts|
303+
posts.where(["created_at > ? AND created_at < ?", ::Time.zone.now.beginning_of_day + 1.day, ::Time.zone.now.end_of_day + 1.day])
304+
end
305+
end
306+
"""
307+
Then I should see an empty group with the scope "All"
308+
And I should see a group "status" with the scopes "Published" and "Unpublished"
309+
And I should see a group "date" with the scopes "Today" and "Tomorrow"

features/ 6D47 step_definitions/index_scope_steps.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@
2727
expect(page).to have_css ".scopes .#{name}"
2828
expect(page).to_not have_css ".scopes .#{name} .count"
2929
end
30+
31+
Then "I should see a group {string} with the scopes {string} and {string}" do |group, name1, name2|
32+
group = group.tr(" ", "").underscore.downcase
33+
name1 = name1.tr(" ", "").underscore.downcase
34+
name2 = name2.tr(" ", "").underscore.downcase
35+
expect(page).to have_css ".scopes .scope-group-#{group} .#{name1}"
36+
expect(page).to have_css ".scopes .scope-group-#{group} .#{name2}"
37+
end
38+
39+
Then"I should see an empty group with the scope {string}" do |name|
40+
name = name.tr(" ", "").underscore.downcase
41+
expect(page).to have_css ".scopes .scope-default-group .#{name}"
42+
end

lib/active_admin/scope.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ActiveAdmin
22
class Scope
33

4-
attr_reader :scope_method, :id, :scope_block, :display_if_block, :show_count, :default_block
4+
attr_reader :scope_method, :id, :scope_block, :display_if_block, :show_count, :default_block, :group
55

66
# Create a Scope
77
#
@@ -24,6 +24,9 @@ class Scope
2424
# Scope.new ->{Date.today.strftime '%A'}, :published_today
2525
# # => Scope with dynamic title using the :published_today scope method
2626
#
27+
# Scope.new :published, nil, group: :status
28+
# # => Scope with the group :status
29+
#
2730
def initialize(name, method = nil, options = {}, &block)
2831
@name, @scope_method = name, method.try(:to_sym)
2932

@@ -42,6 +45,7 @@ def initialize(name, method = nil, options = {}, &block)
4245
@show_count = options.fetch(:show_count, true)
4346
@display_if_block = options[:if] || proc{ true }
4447
@default_block = options[:default] || proc{ false }
48+
@group = options[:group].try(:to_sym)
4549
end
4650

4751
def name

lib/active_admin/views/components/scopes.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ class Scopes < ActiveAdmin::Component
1313
include ::ActiveAdmin::Helpers::Collection
1414

1515
def default_class_name
16-
"scopes table_tools_segmented_control"
16+
"scopes"
1717
end
1818

1919
def tag_name
20-
'ul'
20+
'div'
2121
end
2222

2323
def build(scopes, options = {})
24-
scopes.each do |scope|
25-
build_scope(scope, options) if call_method_or_proc_on(self, scope.display_if_block)
24+
scopes.group_by(&:group).each do |group, group_scopes|
25+
ul class: "table_tools_segmented_control #{group_class(group)}" do
26+
group_scopes.each do |scope|
27+
build_scope(scope, options) if call_method_or_proc_on(self, scope.display_if_block)
28+
end
29+
end
2630
end
2731
end
2832

@@ -60,6 +64,9 @@ def get_scope_count(scope)
6064
collection_size(scope_chain(scope, collection_before_scope))
6165
end
6266

67+
def group_class(group)
68+
group.present? ? "scope-group-#{group}" : "scope-default-group"
69+
end
6370
end
6471
end
6572
end

spec/support/rails_template_with_data.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@
143143
144144
scope :all, default: true
145145
146-
scope :drafts do |posts|
146+
scope :drafts, group: :status do |posts|
147147
posts.where(["published_date IS NULL"])
148148
end
149149
150-
scope :scheduled do |posts|
150+
scope :scheduled, group: :status do |posts|
151151
posts.where(["posts.published_date IS NOT NULL AND posts.published_date > ?", Time.now.utc])
152152
end
153153
154-
scope :published do |posts|
154+
scope :published, group: :status do |posts|
155155
posts.where(["posts.published_date IS NOT NULL AND posts.published_date < ?", Time.now.utc])
156156
end
157157
158-
scope :my_posts do |posts|
158+
scope :my_posts, group: :author do |posts|
159159
posts.where(author_id: current_admin_user.id)
160160
end
161161

spec/unit/scope_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,20 @@
206206
end
207207
end
208208

209+
describe "group" do
210+
it "should default to nil" do
211+
scope = ActiveAdmin::Scope.new(:default)
212+
expect(scope.group).to eq nil
213+
end
214+
215+
it "should accept a symbol to assign a group to the scope" do
216+
scope = ActiveAdmin::Scope.new(:default, nil, group: :test)
217+
expect(scope.group).to eq :test
218+
end
219+
220+
it "should accept a string to assign a group to the scope" do
221+
scope = ActiveAdmin::Scope.new(:default, nil, group: "test")
222+
expect(scope.group).to eq :test
223+
end
224+
end
209225
end

0 commit comments

Comments
 (0)
0