Module: Redmine::Acts::Searchable::ClassMethods

Defined in:
lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_searchable(options = {}) ⇒ Object

Adds the search methods to the class.

Options:

  • :columns - a column or an array of columns to search

  • :project_key - project foreign key (default to project_id)

  • :date_column - name of the datetime column used to sort results (default to :created_on)

  • :permission - permission required to search the model

  • :scope - scope used to search results

  • :preload - associations to preload when loading results for display


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 35

def acts_as_searchable(options = {})
  return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
  options.assert_valid_keys(:columns, :project_key, :date_column, :permission, :scope, :preload)

  cattr_accessor :searchable_options
  self.searchable_options = options

  if searchable_options[:columns].nil?
    raise 'No searchable column defined.'
  elsif !searchable_options[:columns].is_a?(Array)
    searchable_options[:columns] = [] << searchable_options[:columns]
  end

  searchable_options[:project_key] ||= "#{table_name}.project_id"
  searchable_options[:date_column] ||= :created_on

  # Should we search custom fields on this model ?
  searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil?

  send :include, Redmine::Acts::Searchable::InstanceMethods
end