Skip to content

Commit

Permalink
multi-column search
Browse files Browse the repository at this point in the history
  • Loading branch information
collin committed Mar 13, 2013
1 parent 6e06a94 commit 048424b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
21 changes: 18 additions & 3 deletions app/views/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,30 @@ def blank_slate

def body_content
# page_header
section class:'btn-toolbar' do
scope_selector
sort_selector
section class:'navbar-outer' do
section class:'navbar-inner' do
search
section class:'btn-toolbar' do
scope_selector
sort_selector
end
end
end

listing
end

delegate :scopes, :sortings, to: 'component'

def search
form class:'navbar-search', action:request.fullpath do
input type:'search', class:'search-query', name:'search', value:params[:search]
params[:scope] and input type:'hidden', name:'scope', value:params[:scope]
params[:sort] and input type:'hidden', name:'sort', value:params[:sort]
params[:direction] and input type:'hidden', name:'direction', value:params[:direction]
end
end

def scope_selector
return if scopes.none?
nav class:'scopes' do
Expand Down
13 changes: 11 additions & 2 deletions lib/alpha_simprini/admin/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ def resource_name

def collection
get_collection_ivar || begin
c = end_of_association_chain
paged = apply_pagination(c)
chain = end_of_association_chain
searched = apply_search(chain)
paged = apply_pagination(searched)
sorted = apply_sorting(paged)
scoped = apply_scoping(sorted)
set_collection_ivar(scoped.respond_to?(:scoped) ? scoped.scoped : scoped.all)
end
end

def apply_search(query)
if params[:search].present?
self.class.component.apply_search(query, params[:search])
else
query
end
end

def apply_pagination(query)
query.page(params[:page])
end
Expand Down
16 changes: 16 additions & 0 deletions lib/alpha_simprini/admin/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ def self.inherited(subclass)

subclass.class_attribute :sortings
subclass.sortings = []

subclass.class_attribute :search_fields
subclass.search_fields = []
end

def self.search(*fields)
self.search_fields += fields
end

def self.apply_search(query, search_string)
clause = model.arel_table[search_fields.first].matches("%#{search_string}%")
search_fields.each_with_index do |field, index|
next if index.zero?
clause = clause.or(model.arel_table[field].matches("%#{search_string}%"))
end
query.where(clause)
end

def self.sort(sort_name, display_name=nil, options={})
Expand Down

0 comments on commit 048424b

Please sign in to comment.