Commit c282a3dec9b67fab8d65736256ed4032254e67aa
1 parent
00d5c1ac
Exists in
master
and in
29 other branches
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.
Showing
3 changed files
with
26 additions
and
11 deletions
Show diff stats
app/controllers/application_controller.rb
| @@ -181,7 +181,7 @@ class ApplicationController < ActionController::Base | @@ -181,7 +181,7 @@ class ApplicationController < ActionController::Base | ||
| 181 | private | 181 | private |
| 182 | 182 | ||
| 183 | def fallback_find_by_contents(asset, scope, query, paginate_options, options) | 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 | scope = scope.send(options[:filter]) unless options[:filter].blank? | 185 | scope = scope.send(options[:filter]) unless options[:filter].blank? |
| 186 | {:results => scope.paginate(paginate_options)} | 186 | {:results => scope.paginate(paginate_options)} |
| 187 | end | 187 | end |
app/controllers/public/invite_controller.rb
| @@ -78,10 +78,7 @@ class InviteController < PublicController | @@ -78,10 +78,7 @@ class InviteController < PublicController | ||
| 78 | 78 | ||
| 79 | def search_friend | 79 | def search_friend |
| 80 | fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s } | 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 | end | 82 | end |
| 86 | 83 | ||
| 87 | protected | 84 | protected |
lib/noosfero/core_ext/active_record.rb
| @@ -13,14 +13,32 @@ class ActiveRecord::Base | @@ -13,14 +13,32 @@ class ActiveRecord::Base | ||
| 13 | key.join('/') | 13 | key.join('/') |
| 14 | end | 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 | query = query.downcase.strip | 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 | end.join(' OR ') | 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 | else | 42 | else |
| 25 | raise "No searchable fields defined for #{self.name}" | 43 | raise "No searchable fields defined for #{self.name}" |
| 26 | end | 44 | end |