Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search scopes #276

Closed
wants to merge 95 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
df6e8cb
colon at last char could mean use a model scope
nando Nov 26, 2015
1747a5a
new feature: admin searches using a model scope
nando Nov 27, 2015
c84dd78
gems upgrade to fix nokogiri vulnerability
nando Nov 27, 2015
77dc494
avoid hound violations
nando Nov 27, 2015
e2c52a2
hopefully last Houndci fixes...
nando Nov 27, 2015
022590f
spec to ignore malicious scope searches (fix pending)
nando Dec 2, 2015
23104a7
Search::BLACKLISTED_WORDS avoids malicious scopes
nando Dec 3, 2015
948305d
petting HoundCI
nando Dec 3, 2015
6916c97
Search splits the scope and the term in the query (wip)
nando Dec 4, 2015
71796b8
Merge branch 'master' into search_scopes
nando Dec 6, 2015
3d4d80f
Admin searches *into* a model scope
nando Dec 6, 2015
43c79af
Merge branch 'master' into search_scopes
nando Dec 6, 2015
4aee009
Merge branch 'master' into search_scopes
nando Dec 7, 2015
5de68c5
Merge branch 'master' into search_scopes
nando Dec 9, 2015
ce096e7
Merge branch 'master' into search_scopes
nando Dec 10, 2015
fc749c5
Merge branch 'master' into search_scopes
nando Dec 13, 2015
104b23c
double-quoting a string
nando Dec 13, 2015
a37e91b
Merge branch 'master' into search_scopes
nando Dec 15, 2015
cb6dc90
much better than send anything is public_send it
nando Dec 16, 2015
8802956
Merge branch 'master' into search_scopes
nando Dec 17, 2015
797d99c
Dashboard's COLLECTION_SCOPES for the index page (WIP)
nando Dec 17, 2015
fce4e5b
link_to_unless_current in index scope buttons
nando Dec 18, 2015
92d3855
Merge branch 'master' into collection_scopes
nando Dec 22, 2015
2a375ec
Merge branch 'master' into search_scopes
nando Dec 22, 2015
b73de10
Dashboard's COLLECTION_SCOPES for the index page (2/2)
nando Dec 22, 2015
14bcced
Merge branch 'collection_scopes' into search_scopes
nando Dec 22, 2015
cc634fc
debug change reverted
nando Dec 22, 2015
86f3e8e
Hound fixes
nando Dec 22, 2015
063b503
search into scope syntax changed
nando Dec 23, 2015
d508ad7
refactorization finished
nando Dec 23, 2015
cd48500
regexp refactor
nando Dec 23, 2015
c71f75a
now i feel bad about this stupid joke
nando Dec 24, 2015
47e6128
scenario "admin clicks a scope button"
nando Dec 24, 2015
dcdcbf9
scope button link fixed
nando Dec 24, 2015
7fbf27e
COLLECTION_SCOPES defined for the feature specs
nando Dec 24, 2015
fffdd86
Hound fix
nando Dec 25, 2015
42bb6ce
Search#new extracts #scopes&#words from the query
nando Dec 25, 2015
a95b716
trailing whitespaces removed
nando Dec 25, 2015
9c3819b
admin searches a word inside two model scopes
nando Dec 25, 2015
5c4966c
Scopes with an argument (feature spec pending)
nando Dec 27, 2015
ba68fdf
Scopes with argument feature spec
nando Dec 28, 2015
e0d3fb6
*if* indentation like hound says it should be
nando Dec 28, 2015
9e29782
COLLECTION_SCOPES accepts strings with (argument)
nando Dec 29, 2015
9742470
Hound fixes
nando Dec 29, 2015
320d239
COLLECTION_SCOPES as a hash w/ scopes' arrays
nando Dec 30, 2015
2616bbd
scope-buttons class style
nando Jan 7, 2016
8f67444
Merge branch 'master' into search_scopes
nando Jan 8, 2016
a48b4e4
avoided needless SQL query for only scope search
nando Jan 9, 2016
318f40f
Hound fixes
nando Jan 9, 2016
d57583c
fix in #scope_groups
nando Jan 18, 2016
c026d41
Merge branch 'master' into search_scopes
nando Jan 18, 2016
45165b1
Merge branch 'master' into search_scopes
nando Jan 20, 2016
8649b87
buttons-group style&component + use in scope groups
nando Jan 20, 2016
73ab8eb
Merge remote-tracking branch 'upstream/master' into search_scopes
nando Jan 21, 2016
5b94dda
calling #freeze in the BLACKLISTED_WORDS value
nando Jan 21, 2016
413e94e
several Hound tweaks
nando Jan 21, 2016
1466905
raw user's Search#scopes_with_arguments inputs
nando Jan 22, 2016
ff776ed
Search#new(resolver, query) cleans the users query
nando Jan 22, 2016
917f256
partial for _scope_buttons_group
nando Jan 22, 2016
2c62488
Collection#scoped_with?(scope) + use in partial
nando Jan 22, 2016
d307ebb
Page::Collection's #scope_groups&#scope_names spec
nando Jan 22, 2016
846208a
Page::Collection#scope_group(scope)
nando Jan 22, 2016
eedbd9e
Collection#scoped_groups in the current query
nando Jan 23, 2016
6b9478b
Mock dashboards moved to spec_helper to be shared
nando Jan 23, 2016
c939b84
Page::Colletion's specs refactorization
nando Jan 23, 2016
61c56d9
Collection#current_scope_of(group)
nando Jan 23, 2016
b1cf880
Collection#term_using_scope adds/updates the query term with the give…
nando Jan 23, 2016
f126156
Hound tweaks
nando Jan 24, 2016
433409f
Merge branch 'master' into search_scopes
nando Jan 24, 2016
0f6ff85
use of page.term_using_scope in the index view
nando Jan 24, 2016
cfa40cc
ApplicationHelper#translated_scope(scope, i18n_scope)
nando Jan 25, 2016
be85f0f
"Filter" translations added (only zh-CN&zh-TW missing)
nando Jan 25, 2016
679b358
missing Chinese translations added
nando Jan 25, 2016
2ea34a8
check scope's arity while searching them in the query
nando Jan 27, 2016
5f9cb3b
Hound tweaks
nando Jan 27, 2016
5f448de
hound fix
nando Jan 27, 2016
cbffcdb
Scope's arity hack removed
nando Jan 28, 2016
46cfe4a
Bourbon button-group style for scope buttons groups
nando Jan 28, 2016
6e808cd
"wildcarded" scopes to search "<scope>:<argument>"
nando Jan 28, 2016
360ec3f
Hound fixes
nando Jan 29, 2016
5735a19
oops! restoring the missed buttons background
nando Jan 29, 2016
c3f7293
ending multiline array with a comma
nando Jan 29, 2016
5ef54fa
use #casecmp instead of (innecesary) #downcase.eq (thanks hound!)
nando Jan 29, 2016
0140edc
double-quoting a string
nando Jan 29, 2016
63a9054
"Disable scopes" feature and used by default (in the generated dashbo…
nando Feb 6, 2016
b6dcb80
extra line removed
nando Feb 6, 2016
a4264f1
explanations in generated dashboard improved
nando Feb 6, 2016
254140c
Merge branch 'master' into search_scopes
nando Feb 14, 2016
cc62974
extra space removed
nando Feb 16, 2016
8c672e3
Merge branch 'master' into search_scopes
nando Feb 21, 2016
b99024e
Merge branch 'master' into search_scopes
nando Mar 10, 2016
e8fddd0
Button to disable active scope in Dashboard index
nando Mar 10, 2016
d877b91
Collection#term_without_scope don't care if defined
nando Mar 10, 2016
368dd2b
Merge branch 'master' into search_scopes
nando Apr 23, 2016
e76877a
Merge branch 'master' into search_scopes
nando May 18, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 55 additions & 50 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.2)
actionpack (= 4.2.2)
actionview (= 4.2.2)
activejob (= 4.2.2)
actionmailer (4.2.5)
actionpack (= 4.2.5)
actionview (= 4.2.5)
activejob (= 4.2.5)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.2)
actionview (= 4.2.2)
activesupport (= 4.2.2)
actionpack (4.2.5)
actionview (= 4.2.5)
activesupport (= 4.2.5)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
actionview (4.2.2)
activesupport (= 4.2.2)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
activejob (4.2.2)
activesupport (= 4.2.2)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (4.2.5)
activesupport (= 4.2.5)
globalid (>= 0.3.0)
activemodel (4.2.2)
activesupport (= 4.2.2)
activemodel (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
activerecord (4.2.2)
activemodel (= 4.2.2)
activesupport (= 4.2.2)
activerecord (4.2.5)
activemodel (= 4.2.5)
activesupport (= 4.2.5)
arel (~> 6.0)
activesupport (4.2.2)
activesupport (4.2.5)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
Expand All @@ -62,7 +62,8 @@ GEM
rake
thor (>= 0.14.0)
arel (6.0.3)
autoprefixer-rails (6.1.0.1)
ast (2.1.0)
autoprefixer-rails (6.1.1)
execjs
json
awesome_print (1.6.1)
Expand All @@ -75,7 +76,7 @@ GEM
bundler-audit (0.4.0)
bundler (~> 1.2)
thor (~> 0.18)
byebug (6.0.2)
byebug (8.2.1)
capybara (2.5.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand Down Expand Up @@ -111,7 +112,7 @@ GEM
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.5.0)
faker (1.6.1)
i18n (~> 0.5)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
Expand All @@ -124,17 +125,19 @@ GEM
ruby-progressbar (~> 1.4)
globalid (0.3.6)
activesupport (>= 4.1.0)
hashdiff (0.2.2)
hashdiff (0.2.3)
high_voltage (2.4.0)
highline (1.7.8)
httpclient (2.7.0)
httpclient (2.7.0.1)
i18n (0.7.0)
i18n-tasks (0.8.7)
activesupport (>= 2.3.18)
i18n-tasks (0.9.2)
activesupport (>= 4.0.2)
ast (>= 2.1.0)
easy_translate (>= 0.5.0)
erubis
highline (>= 1.7.3)
i18n
parser (>= 2.2.3.0)
term-ansicolor (>= 1.3.2)
terminal-table (>= 1.5.1)
inline_svg (0.6.1)
Expand All @@ -157,26 +160,28 @@ GEM
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.2)
mime-types (2.99)
mini_portile (0.6.2)
minitest (5.8.1)
minitest (5.8.3)
momentjs-rails (2.10.6)
railties (>= 3.1)
multi_json (1.11.2)
multipart-post (2.0.0)
neat (1.7.2)
bourbon (>= 4.0)
sass (>= 3.3)
nokogiri (1.6.6.2)
nokogiri (1.6.6.4)
mini_portile (~> 0.6.0)
normalize-rails (3.0.3)
percy-capybara (1.0.0)
percy-client (>= 1.0.0)
percy-client (1.2.0)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
percy-capybara (1.1.0)
percy-client (>= 1.3.0)
percy-client (1.3.0)
faraday (>= 0.9)
httpclient (>= 2.6)
pg (0.18.3)
poltergeist (1.7.0)
pg (0.18.4)
poltergeist (1.8.1)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
Expand All @@ -191,16 +196,16 @@ GEM
rack-test (0.6.3)
rack (>= 1.0)
rack-timeout (0.3.2)
rails (4.2.2)
actionmailer (= 4.2.2)
actionpack (= 4.2.2)
actionview (= 4.2.2)
activejob (= 4.2.2)
activemodel (= 4.2.2)
activerecord (= 4.2.2)
activesupport (= 4.2.2)
rails (4.2.5)
actionmailer (= 4.2.5)
actionpack (= 4.2.5)
actionview (= 4.2.5)
activejob (= 4.2.5)
activemodel (= 4.2.5)
activerecord (= 4.2.5)
activesupport (= 4.2.5)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.2)
railties (= 4.2.5)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
Expand All @@ -211,9 +216,9 @@ GEM
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rails_stdout_logging (0.0.4)
railties (4.2.2)
actionpack (= 4.2.2)
activesupport (= 4.2.2)
railties (4.2.5)
actionpack (= 4.2.5)
activesupport (= 4.2.5)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
Expand Down Expand Up @@ -251,7 +256,7 @@ GEM
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
slop (3.6.0)
sprockets (3.4.0)
sprockets (3.4.1)
rack (> 1, < 3)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
Expand All @@ -265,13 +270,13 @@ GEM
thread_safe (0.3.5)
tilt (2.0.1)
timecop (0.8.0)
tins (1.6.0)
tins (1.7.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.7.2)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicorn (4.9.0)
unicorn (5.0.1)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
Expand All @@ -280,7 +285,7 @@ GEM
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
webmock (1.22.1)
webmock (1.22.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
Expand Down
9 changes: 9 additions & 0 deletions lib/administrate/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ def initialize(resolver, term)
def run
if @term.blank?
resource_class.all
elsif (@scope = search_scope)
resource_class.send @scope
else
resource_class.where(query, *search_terms)
end
end

def search_scope
if (@term[-1, 1] == ":")
possible_scope = @term[0..-2]
possible_scope if resource_class.respond_to?(possible_scope)
end
end

private

delegate :resource_class, to: :resolver
Expand Down
2 changes: 2 additions & 0 deletions spec/example_app/app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Customer < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true

scope :subscribed, -> { where(email_subscriber: true) }

def lifetime_value
orders.map(&:total_price).reduce(0, :+)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/features/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@
expect(page).to have_content(email_match.email)
expect(page).not_to have_content(mismatch.email)
end

scenario "admin searches using a model scope", :js do
query = "subscribed:"
subscribed_customer = create(:customer,
name: "Dan Croak",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the parameters of a method call if they span more than one line.

email_subscriber: true)
other_customer = create(:customer,
name: "Foo Bar",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the parameters of a method call if they span more than one line.

email_subscriber: false)

visit admin_customers_path
fill_in :search, with: query
page.execute_script("$('.search').submit()")

expect(page).to have_content(subscribed_customer.name)
expect(page).not_to have_content(other_customer.name)
end
end