Commit c282a3dec9b67fab8d65736256ed4032254e67aa

Authored by Larissa Reis
1 parent 00d5c1ac

invite-members: uses the default find_by_content infra-structure

  Now search_friend uses the default infra-structure
  ApplicationController#find_by_contents.

  Because of that, this patch adds joins and specific search fields to
  find_by_contents.
app/controllers/application_controller.rb
... ... @@ -181,7 +181,7 @@ class ApplicationController < ActionController::Base
181 181 private
182 182  
183 183 def fallback_find_by_contents(asset, scope, query, paginate_options, options)
184   - scope = scope.like_search(query) unless query.blank?
  184 + scope = scope.like_search(query, options) unless query.blank?
185 185 scope = scope.send(options[:filter]) unless options[:filter].blank?
186 186 {:results => scope.paginate(paginate_options)}
187 187 end
... ...
app/controllers/public/invite_controller.rb
... ... @@ -78,10 +78,7 @@ class InviteController < PublicController
78 78  
79 79 def search_friend
80 80 fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }
81   - values = ["%#{params['q']}%"] * fields.count
82   - render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]).
83   - select {|person| !profile.members.include?(person) }.
84   - map {|person| {:id => person.id, :name => person.name} }.to_json
  81 + render :text => find_by_contents(:people, environment.people, params['q'], {:page => 1}, {:fields => fields, :joins => :user})[:results].select {|person| !profile.members.include?(person) }.map {|person| {:id => person.id, :name => person.name} }.to_json
85 82 end
86 83  
87 84 protected
... ...
lib/noosfero/core_ext/active_record.rb
... ... @@ -13,14 +13,32 @@ class ActiveRecord::Base
13 13 key.join('/')
14 14 end
15 15  
16   - def self.like_search(query)
17   - if defined?(self::SEARCHABLE_FIELDS)
18   - fields = self::SEARCHABLE_FIELDS.keys.map(&:to_s) & column_names
  16 + def self.like_search(query, options={})
  17 + if defined?(self::SEARCHABLE_FIELDS) || options[:fields].present?
  18 + fields_per_table = {}
  19 + fields_per_table[table_name] = (options[:fields].present? ? options[:fields] : self::SEARCHABLE_FIELDS.keys.map(&:to_s)) & column_names
  20 +
  21 + if options[:joins].present?
  22 + join_asset = options[:joins].to_s.classify.constantize
  23 + if defined?(join_asset::SEARCHABLE_FIELDS) || options[:fields].present?
  24 + fields_per_table[join_asset.table_name] = (options[:fields].present? ? options[:fields] : join_asset::SEARCHABLE_FIELDS.keys.map(&:to_s)) & join_asset.column_names
  25 + end
  26 + end
  27 +
19 28 query = query.downcase.strip
20   - conditions = fields.map do |field|
21   - "lower(#{table_name}.#{field}) LIKE '%#{query}%'"
  29 + fields_per_table.delete_if { |table,fields| fields.blank? }
  30 + conditions = fields_per_table.map do |table,fields|
  31 + fields.map do |field|
  32 + "lower(#{table}.#{field}) LIKE '%#{query}%'"
  33 + end.join(' OR ')
22 34 end.join(' OR ')
23   - where(conditions)
  35 +
  36 + if options[:joins].present?
  37 + joins(options[:joins]).where(conditions)
  38 + else
  39 + where(conditions)
  40 + end
  41 +
24 42 else
25 43 raise "No searchable fields defined for #{self.name}"
26 44 end
... ...