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
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 |
#fae_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 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
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 |
#for_fae_index ⇒ Object
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
86 87 88 89 90 91 92 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 86 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
78 79 80 81 82 83 84 |
# File 'app/models/concerns/fae/base_model_concern.rb', line 78 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
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_csv ⇒ Object
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 |