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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 39

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 additional associations on this model ?
  searchable_options[:search_custom_fields] = reflect_on_association(:custom_values).present?
  searchable_options[:search_attachments] = reflect_on_association(:attachments).present?
  searchable_options[:search_journals] = reflect_on_association(:journals).present?

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