Skip to content

Commit

Permalink
browsermedia#729 - CMS - making job ad page filterable for users
Browse files Browse the repository at this point in the history
  • Loading branch information
censo committed May 6, 2015
1 parent da22f79 commit be4bf85
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
9 changes: 6 additions & 3 deletions app/controllers/cms/content_block_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ def load_blocks

scope = model_class.respond_to?(:list) ? model_class.list : model_class
if scope.searchable?
scope = scope.search(@search_filter.term)
if @search_filter.term.blank?
scope = scope.search(@search_filter.serializable_hash)
else
scope = scope.search(@search_filter.term)
end
end
if params[:section_id] && model_class.respond_to?(:with_parent_id)
if params[:section_id].present? && model_class.respond_to?(:with_parent_id)
scope = scope.with_parent_id(params[:section_id])
end

@total_number_of_items = scope.count
@blocks = scope.paginate(options)
check_permissions
Expand Down
8 changes: 7 additions & 1 deletion app/models/cms/search_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module Cms
# Captures values for the search form.
class SearchFilter
include ::ActiveModel::Model
include ::ActiveModel::Serialization

attr_accessor :model_class, :term
attr_accessor :model_class, :term, :responsible_person_email, :name, :filled, :job_type_id

def self.build(params_hash, model_class)
model = self.new(params_hash)
Expand All @@ -14,5 +15,10 @@ def self.build(params_hash, model_class)
def path
model_class
end

def attributes
{ 'name' => nil, 'responsible_person_email' => nil, 'filled' => nil, 'job_type_id' => nil }
end

end
end
20 changes: 18 additions & 2 deletions app/views/cms/content_block/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
<% use_page_title "Assets - #{content_type.display_name_plural}" %>
<div class="padded row-fluid clearfix">
<h1 class="span3">Assets</h1>

<%= form_for @search_filter, url: engine(content_type).url_for({}), html: {class: 'form-search'}, method: :get do |f| %>
<%= f.text_field :term, class: "span6 search-query right", placeholder: "Search #{content_type.display_name_plural}" %>
<% if content_type.name == "Freshminds::Advertisement" %>
<%= f.text_field :name, class: "span6 search-query right", placeholder: "Search #{content_type.display_name_plural}" %>
<%= f.label :responsible_person_email, :class => 'span6 right' %>
<%= f.select(:responsible_person_email, Freshminds::Advertisement.pluck(:responsible_person_email).uniq.reject!(&:blank?),{ :include_blank => true }, { :class => 'span6 right', :label => "Responsible person's email" }) %>
<%= f.label :filled, :class => 'span6 right' %>
<%= f.select(:filled, [['yes',true], ['no',false]],{ :include_blank => true }, { :class => 'span6 right', :label => "Responsible person's email" }) %>
<%= f.label :job_type_id, :class => 'span6 right' %>
<%= f.collection_select(:job_type_id, Freshminds::JobType.all,:id,:name,{ :include_blank => true }, { :class => 'span6 right', :label => "Job type" }) %>

<div class="btn-group span6 right">
<%= f.submit "Filter!", :class => 'span3 right btn btn-small btn-primary primary-ct' %>
</div>
<% else %>
<%= f.text_field :term, class: "span6 search-query right", placeholder: "Search #{content_type.display_name_plural}" %>
<% end %>
<% end %>
</div>

Expand Down
42 changes: 31 additions & 11 deletions lib/cms/behaviors/searching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,43 @@ def is_searchable(options={})

#This is in a method to allow classes to override it
scope :search, lambda{|search_params|
term = search_params.is_a?(Hash) ? search_params[:term] : search_params
conditions = []
unless term.blank?
if search_params.is_a?(Hash)
searchable_columns.each do |c|
if conditions.empty?
conditions = ["#{table_name}.#{c} like ?"]
else
conditions.first << "or #{table_name}.#{c} like ?"
c = c.to_s
bools = {'true' => 1, 'false' => 0}
unless search_params[c].blank?
operand = c == "name" ? "like" : "="
if conditions.empty?
conditions = ["#{table_name}.#{c} #{operand} ?"]
else
conditions.first << " AND #{table_name}.#{c} #{operand} ?"
end
if c == 'name'
conditions << "%#{search_params[c]}%"
elsif c.include?('id')
conditions << search_params[c].to_i
elsif bools.keys.include?(search_params[c])
conditions << bools[search_params[c]]
else
conditions << "#{search_params[c]}"
end
end
end
else
term = search_params
unless term.blank?
searchable_columns.each do |c|
if conditions.empty?
conditions = ["#{table_name}.#{c} like ?"]
else
conditions.first << "or #{table_name}.#{c} like ?"
end
conditions << "%#{term}%"
end
conditions << "%#{term}%"
end
#conditions[0] = "(#{conditions[0]})"
end
where(conditions)
#scope = {}
#scope[:conditions] = conditions if conditions
#scope
}
end
end
Expand Down

0 comments on commit be4bf85

Please sign in to comment.