Module: Fae::BaseModelConcern::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#fae_search(query) ⇒ Object



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

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



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

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

#filter_allObject



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

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

#has_fae_file(file_name_symbol) ⇒ Object



92
93
94
95
96
97
98
# File 'app/models/concerns/fae/base_model_concern.rb', line 92

def has_fae_file(file_name_symbol)
  has_one file_name_symbol, -> { where(attached_as: file_name_symbol.to_s) },
    as: :fileable,
    class_name: '::Fae::File',
    dependent: :destroy
  accepts_nested_attributes_for file_name_symbol, allow_destroy: true
end

#has_fae_image(image_name_symbol) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/concerns/fae/base_model_concern.rb', line 84

def has_fae_image(image_name_symbol)
  has_one image_name_symbol, -> { where(attached_as: image_name_symbol.to_s) },
    as: :imageable,
    class_name: '::Fae::Image',
    dependent: :destroy
  accepts_nested_attributes_for image_name_symbol, allow_destroy: true
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



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

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



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

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