8000 Merge pull request #3289 from ccallebs/2122-search-status-sidebar · stephancom/activeadmin@d59db03 · GitHub
[go: up one dir, main page]

Skip to content

Commit d59db03

Browse files
committed
Merge pull request activeadmin#3289 from ccallebs/2122-search-status-sidebar
Search Status Sidebar
2 parents e8157b7 + d3753a1 commit d59db03

File tree

6 files changed

+192
-1
lines changed

6 files changed

+192
-1
lines changed

lib/active_admin/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def initialize
8888
# A regex to detect unsupported browser, set to false to disable
8989
inheritable_setting :unsupported_browser_matcher, /MSIE [1-8]\.0/
9090

91+
# Whether to display 'Current Filters' on search screen
92+
inheritable_setting :current_filters, true
93+
9194
# Request parameters that are permitted by default
9295
inheritable_setting :permitted_params, [
9396
:utf8, :_method, :authenticity_token, :commit, :id

lib/active_admin/filters/active.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'active_admin/filters/humanized'
2+
3+
module ActiveAdmin
4+
module Filters
5+
6+
class Active
7+
attr_accessor :filters, :scope
8+
9+
def initialize(resource_class, params)
10+
@resource_class, @params = resource_class, params
11+
@scope = humanize_scope
12+
@filters = build_filters
13+
end
14+
15+
private
16+
17+
def build_filters
18+
@params[:q] ||= []
19+
@params[:q].map { |param| Humanized.new(param) }
20+
end
21+
22+
def humanize_scope
23+
scope = @params['scope']
24+
scope ? scope.humanize : "All"
25+
end
26+
end
27+
28+
end
29+
end

lib/active_admin/filters/humanized.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module ActiveAdmin
2+
module Filters
3+
4+
class Humanized
5+
include ActiveAdmin::ViewHelpers
6+
7+
def initialize(param)
8+
@body = param[0]
9+
@value = param[1]
10+
end
11+
12+
def value
13+
@value
14+
end
15+
16+
def body
17+
predicate = ransack_predicate_translation
18+
19+
if current_predicate.nil?
20+
predicate = @body.titleize
21+
elsif translation_missing?(predicate)
22+
predicate = active_admin_predicate_translation
23+
end
24+
25+
"#{parse_parameter_body} #{predicate}".strip
26+
end
27+
28+
private
29+
30+
def parse_parameter_body
31+
return if current_predicate.nil?
32+
33+
# Accounting for strings that might contain other predicates. Example:
34+
# 'requires_approval' contains the substring 'eq'
35+
split_string = "_#{current_predicate}"
36+
37+
@body.split(split_string)
38+
.first
39+
.gsub('_', ' ')
40+
.strip
41+
.titleize
42+
.gsub('Id', 'ID')
43+
end
44+
45+
def current_predicate
46+
@current_predicate ||= predicates.detect { |p| @body.include?(p) }
47+
end
48+
49+
def predicates
50+
Ransack::Predicate.names_by_decreasing_length
51+
end
52+
53+
def ransack_predicate_translation
54+
I18n.t("ransack.predicates.#{current_predicate}")
55+
end
56+
57+
def active_admin_predicate_translation
58+
translation = I18n.t("active_admin.filters.predicates.#{current_predicate}").downcase
59+
end
60+
61+
def translation_missing?(predicate)
62+
predicate.downcase.include?('translation missing')
63+
end
64+
65+
end
66+
67+
end
68+
end

lib/active_admin/filters/resource_extension.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_admin/filters/active'
2+
13
module ActiveAdmin
24
module Filters
35

@@ -12,6 +14,7 @@ module ResourceExtension
1214
def initialize(*)
1315
super
1416
add_filters_sidebar_section
17+
add_search_status_sidebar_section
1518
end
1619

1720
# Returns the filters for this resource. If filters are not enabled,
@@ -29,11 +32,23 @@ def filters=(bool)
2932
@filters_enabled = bool
3033
end
3134

35+
# Setter to enable/disable showing current filters on this resource.
36+
#
37+
# Set to `nil` to inherit the setting from the namespace
38+
def current_filters=(bool)
39+
@current_filters_enabled = bool
40+
end
41+
3242
# @return [Boolean] If filters are enabled for this resource
3343
def filters_enabled?
3444
@filters_enabled.nil? ? namespace.filters : @filters_enabled
3545
end
3646

47+
# @return [Boolean] If show current filters are enabled for this resource
48+
def current_filters_enabled?
49+
@current_filters_enabled.nil? ? namespace.current_filters : @current_filters_enabled
50+
end
51+
3752
def preserve_default_filters!
3853
@preserve_default_filters = true
3954
end
@@ -137,6 +152,38 @@ def filters_sidebar_section
137152
end
138153
end
139154

155+
def add_search_status_sidebar_section
156+
if current_filters_enabled?
157+
self.sidebar_sections << search_status_section
158+
end
159+
end
160+
161+
def search_status_section
162+
ActiveAdmin::SidebarSection.new :search_status, only: :index, if: -> { params[:q] || params[:scope] } do
163+
active = ActiveAdmin::Filters::Active.new(resource_class, params)
164+
165+
span do
166+
h4 "Scope:", style: 'display: inline'
167+
b active.scope, style: "display: inline"
168+
169+
div style: "margin-top: 10px" do
170+
h4 "Current filters:", style: 'margin-bottom: 10px'
171+
ul do
172+
if active.filters.blank?
173+
li "None"
174+
else
175+
active.filters.each do |filter|
176+
li do
177+
span filter.body
178+
b filter.value
179+
end
180+
end
181+
end
182+
end
183+
end
184+
end
185+
end
186+
end
140187
end
141188

142189
end

spec/unit/dsl_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def self.included(dsl)
9797
dsl.run_registration_block do
9898
sidebar :help
9999
end
100-
expect(dsl.config.sidebar_sections.map(&:name)).to match_array([:filters, :email, :help])
100+
expect(dsl.config.sidebar_sections.map(&:name)).to match_array([:filters, :search_status, :email, :help])
101101
end
102102

103103
end

spec/unit/filters/humanized_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'rails_helper'
2+
3+
describe ActiveAdmin::Filters::Humanized do
4+
let(:param) { ['category_id_eq', '1'] }
5+
6+
subject { ActiveAdmin::Filters::Humanized.new(param) }
7+
8+
describe '#value' do
9+
it 'should equal query string parameter' do
10+
expect(subject.value).to eq('1')
11+
end
12+
end
13+
14+
describe '#body' do
15+
context 'when Ransack predicate' do
16+
it 'parses language from Ransack' do
17+
expect(subject.body).to eq('Category ID equals')
18+
end
19+
20+
it 'handles strings with embedded predicates' do
21+
param = ['requires_approval_eq', '1']
22+
humanizer = ActiveAdmin::Filters::Humanized.new(param)
23+
expect(humanizer.value).to eq('1')
24+
expect(humanizer.body).to eq('Requires Approval equals')
25+
end
26+
end
27+
28+
context 'when ActiveAdmin predicate' do
29+
it 'parses language from ActiveAdmin' do
30+
param = ['name_starts_with', 'test']
31+
humanizer = ActiveAdmin::Filters::Humanized.new(param)
32+
expect(humanizer.body).to eq('Name starts with')
33+
end
34+
end
35+
36+
context 'when unknown predicate' do
37+
it 'uses raw predicate string' do
38+
param = ['name_predicate_does_not_exist', 'test']
39+
humanizer = ActiveAdmin::Filters::Humanized.new(param)
40+
expect(humanizer.body).to eq("Name Predicate Does Not Exist")
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)
0