Module: Fae::BaseModelConcern::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#fae_display_fieldObject



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

def fae_display_field
  # override this method in your model
end

#fae_search(query) ⇒ Object



58
59
60
# File 'app/models/concerns/fae/base_model_concern.rb', line 58

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



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

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

#filter_allObject



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

def filter_all
  # override this method in your model
  for_fae_index
end

#for_fae_indexObject



31
32
33
# File 'app/models/concerns/fae/base_model_concern.rb', line 31

def for_fae_index
  order(order_method)
end

#order_methodObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/concerns/fae/base_model_concern.rb', line 35

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



62
63
64
65
66
67
68
69
# File 'app/models/concerns/fae/base_model_concern.rb', line 62

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



71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/concerns/fae/base_model_concern.rb', line 71

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