ref: master
app/models/application_record.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
class ApplicationRecord < ActiveRecord::Base extend PostgresqlAttachmentFu::ClassMethods include UploadSanitizer self.abstract_class = true self.store_full_sti_class = true # an ActionView instance for rendering views on models def self.action_view @action_view ||= begin view_paths = ::ActionController::Base.view_paths action_view = ::ActionView::Base.new view_paths # for using Noosfero helpers inside render calls action_view.extend ::ApplicationHelper action_view end end # default value needed for the above ActionView def to_partial_path self.class.name.underscore end alias_method :meta_cache_key, :cache_key def cache_key key = [Noosfero::VERSION, meta_cache_key] key.unshift ApplicationRecord.connection.schema_search_path key.join('/') end def self.like_search(query, options={}) if defined?(self::SEARCHABLE_FIELDS) || options[:fields].present? fields_per_table = {} fields_per_table[table_name] = (options[:fields].present? ? options[:fields] : self::SEARCHABLE_FIELDS.keys.map(&:to_s)) & column_names if options[:joins].present? join_asset = options[:joins].to_s.classify.constantize if defined?(join_asset::SEARCHABLE_FIELDS) || options[:fields].present? fields_per_table[join_asset.table_name] = (options[:fields].present? ? options[:fields] : join_asset::SEARCHABLE_FIELDS.keys.map(&:to_s)) & join_asset.column_names end end query = query.downcase.strip fields_per_table.delete_if { |table,fields| fields.blank? } conditions = fields_per_table.map do |table,fields| fields.map do |field| "lower(#{table}.#{field}) LIKE '%#{query}%'" end.join(' OR ') end.join(' OR ') if options[:joins].present? joins(options[:joins]).where(conditions) else where(conditions) end else raise "No searchable fields defined for #{self.name}" end end end |