Module: Fae::BaseModelConcern::ClassMethods
- Defined in:
- app/models/concerns/fae/base_model_concern.rb
Instance Method Summary collapse
- #fae_search(query) ⇒ Object
- #fae_translate(*attributes) ⇒ Object
- #filter(params) ⇒ Object
- #for_fae_index ⇒ Object
- #has_fae_file(file_name_symbol) ⇒ Object
- #has_fae_image(image_name_symbol) ⇒ Object
- #order_method ⇒ Object
- #to_csv ⇒ Object
Instance Method Details
#fae_search(query) ⇒ Object
62 63 64 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 62 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 |
#fae_translate(*attributes) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 75 def fae_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 |
#filter(params) ⇒ Object
57 58 59 60 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 57 def filter(params) # override this method in your model for_fae_index end |
#for_fae_index ⇒ Object
40 41 42 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 40 def for_fae_index order(order_method) end |
#has_fae_file(file_name_symbol) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 95 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
87 88 89 90 91 92 93 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 87 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_method ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 44 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_csv ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 66 def to_csv CSV.generate do |csv| csv << column_names all.each do |item| csv << item.attributes.values_at(*column_names) end end end |