Skip to content

Instantly share code, notes, and snippets.

@HammerInTheNews
Created June 3, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HammerInTheNews/a56e89601e4c60517fa6 to your computer and use it in GitHub Desktop.
Save HammerInTheNews/a56e89601e4c60517fa6 to your computer and use it in GitHub Desktop.
Adding Advanced Search
## app/assets/javascripts/tenon/controllers/product_updates.js.coffee
Tenon.dispatcher.route 'tenon/product_updates#index', ->
new Tenon.features.AdvancedSearch
## app/controllers/tenon/product_updates_controller.rb
before_filter :initialize_advanced_search_params, only: :index
def index
params[:page] = 1 if params[:page].to_i == 0
respond_to do |format|
format.html
format.json do
self.collection = klass.all
add this-> self.collection = ProductUpdate.search(params, current_user)
self.collection = collection.paginate(per_page: 20, page: params[:page]).order('created_at desc')
self.collection = Tenon::PaginatingDecorator.decorate(collection)
end
private
~~~ and this ~~~
def initialize_advanced_search_params
params[:aq] ||= {}
end
## app/models/product_update.rb
include AdvancedSearch
def self.search(params, current_user = nil)
collection = all
collection = build_advanced_search(collection, params[:aq]) if params[:aq]
collection = sort(collection, params[:sort]) if params[:sort]
collection
end
private
def self.build_advanced_search(collection, aq)
collection = eq_query(collection, 'product_updates.product_id', aq[:product_id])
collection = ilike_query(collection, :title, aq[:title])
end
## app/views/tenon/product_updates/index.html.haml
- content_for :sidebar do
.sidebar
.content
%h2 Product Updates
- = link_to "New Product Update", new_product_update_path, class: 'btn btn-block btn-primary'
%header
%h1 Product Updates
.tools
= render "tenon/shared/section_header/sidebar_toggle"
~~~~ add stuff from here down ~~~~
.header-button
= link_to '#', id: 'search-button' do
.header-icon= fa_icon('fw search')
.toolbox
#advanced-search-container
= form_tag '#', authenticity_token: false, class: 'record-list-updater' do
%ul.search-fields
##### ADD FIELDS RIGHT BELOW HERE #####
%li= aq_select(:product_id, @products, params, label: '', prompt: 'Products...')
.search-actions
= submit_tag 'Search', class: 'btn-search btn btn-primary btn-block'
##### make sure the path below is the right one ######
= link_to 'Clear', product_updates_path, class: 'clear-link'
.space## app/assets/javascripts/tenon/controllers/product_updates.js.coffee
Tenon.dispatcher.route 'tenon/product_updates#index', ->
new Tenon.features.AdvancedSearch
## app/controllers/tenon/product_updates_controller.rb
before_filter :initialize_advanced_search_params, only: :index
def index
params[:page] = 1 if params[:page].to_i == 0
respond_to do |format|
format.html
format.json do
self.collection = klass.all
add this-> self.collection = ProductUpdate.search(params, current_user)
self.collection = collection.paginate(per_page: 20, page: params[:page]).order('created_at desc')
self.collection = Tenon::PaginatingDecorator.decorate(collection)
end
private
~~~ and this ~~~
def initialize_advanced_search_params
params[:aq] ||= {}
end
## app/models/product_update.rb
include AdvancedSearch
def self.search(params, current_user = nil)
collection = all
collection = build_advanced_search(collection, params[:aq]) if params[:aq]
collection = sort(collection, params[:sort]) if params[:sort]
collection
end
private
def self.build_advanced_search(collection, aq)
collection = eq_query(collection, 'product_updates.product_id', aq[:product_id])
collection = ilike_query(collection, :title, aq[:title])
end
## app/views/tenon/product_updates/index.html.haml
- content_for :sidebar do
.sidebar
.content
%h2 Product Updates
- = link_to "New Product Update", new_product_update_path, class: 'btn btn-block btn-primary'
%header
%h1 Product Updates
.tools
= render "tenon/shared/section_header/sidebar_toggle"
~~~~ add stuff from here down ~~~~
.header-button
= link_to '#', id: 'search-button' do
.header-icon= fa_icon('fw search')
.toolbox
#advanced-search-container
= form_tag '#', authenticity_token: false, class: 'record-list-updater' do
%ul.search-fields
##### ADD FIELDS RIGHT BELOW HERE #####
%li= aq_select(:product_id, @products, params, label: '', prompt: 'Products...')
.search-actions
= submit_tag 'Search', class: 'btn-search btn btn-primary btn-block'
##### make sure the path below is the right one ######
= link_to 'Clear', product_updates_path, class: 'clear-link'
.space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment