Module: Fae::BaseModelConcern::ClassMethods

Defined in:
app/models/concerns/fae/base_model_concern.rb

Instance Method Summary collapse

Instance Method Details

#fae_display_fieldObject



22
23
24
# File 'app/models/concerns/fae/base_model_concern.rb', line 22

def fae_display_field
  # override this method in your model
end

#fae_search(query) ⇒ Object



53
54
55
# File 'app/models/concerns/fae/base_model_concern.rb', line 53

def fae_search(query)
  all.to_a.keep_if { |i| i.fae_display_field.present? && i.fae_display_field.to_s.downcase.include?(query.downcase) }
end

#filter(params) ⇒ Object



48
49
50
51
# File 'app/models/concerns/fae/base_model_concern.rb', line 48

def filter(params)
  # override this method in your model
  for_fae_index
end

#filter_allObject



43
44
45
46
# File 'app/models/concerns/fae/base_model_concern.rb', line 43

def filter_all
  # override this method in your model
  for_fae_index
end

#for_fae_indexObject



26
27
28
# File 'app/models/concerns/fae/base_model_concern.rb', line 26

def for_fae_index
  order(order_method)
end

#order_methodObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/concerns/fae/base_model_concern.rb', line 30

def order_method
  klass = name.constantize
  if klass.column_names.include? 'position'
    return :position
  elsif klass.column_names.include? 'name'
    return :name
  elsif klass.column_names.include? 'title'
    return :title
  else
    raise "No order_method found, please define for_fae_index as a #{name} class method to set a custom scope."
  end
end

#to_csvObject



57
58
59
60
61
62
63
64
# File 'app/models/concerns/fae/base_model_concern.rb', line 57

def to_csv
  CSV.generate do |csv|
    csv << column_names
    all.each do |item|
      csv << item.attributes.values_at(*column_names)
    end
  end
end

#translate(*attributes) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/concerns/fae/base_model_concern.rb', line 66

def translate(*attributes)
  attributes.each do |attribute|
    define_method attribute.to_s do
      self.send "#{attribute}_#{I18n.locale}"
    end

    define_singleton_method "find_by_#{attribute}" do |val|
      self.send("find_by_#{attribute}_#{I18n.locale}", val)
    end
  end
end