Class: Releaf::DefaultSearchableFields

Inherits:
Object
  • Object
show all
Defined in:
app/lib/releaf/default_searchable_fields.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ DefaultSearchableFields

Returns a new instance of DefaultSearchableFields.


5
6
7
# File 'app/lib/releaf/default_searchable_fields.rb', line 5

def initialize(klass)
  self.klass = klass
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass


3
4
5
# File 'app/lib/releaf/default_searchable_fields.rb', line 3

def klass
  @klass
end

Instance Method Details

#findObject


9
10
11
12
13
# File 'app/lib/releaf/default_searchable_fields.rb', line 9

def find
  search_columns = possible_field_names & string_columns
  search_columns << { translations: searchable_translated_string_columns } if has_searchable_translated_string_columns?
  search_columns
end

#has_searchable_translated_string_columns?Boolean

Returns:

  • (Boolean)

23
24
25
26
# File 'app/lib/releaf/default_searchable_fields.rb', line 23

def has_searchable_translated_string_columns?
  return false unless klass.translates?
  searchable_translated_string_columns.present?
end

#possible_field_namesObject


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/lib/releaf/default_searchable_fields.rb', line 28

def possible_field_names
  %w[
    email
    first_name
    forename
    last_name
    login
    middle_name
    name
    surname
    title
    username
  ]
end

#searchable_translated_string_columnsObject


19
20
21
# File 'app/lib/releaf/default_searchable_fields.rb', line 19

def searchable_translated_string_columns
  @searchable_translated_string_columns ||= self.class.new(klass::Translation).find
end

#string_columnsObject


15
16
17
# File 'app/lib/releaf/default_searchable_fields.rb', line 15

def string_columns
  klass.columns.select { |column| column.type == :string }.map(&:name)
end