Class: Fae::StaticPage

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BaseModelConcern, StaticPageConcern
Defined in:
app/models/fae/static_page.rb

Instance Attribute Summary

Attributes included from BaseModelConcern

#filter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModelConcern

#fae_nested_foreign_key, #fae_nested_parent, #fae_tracker_parent

Class Method Details

.define_association(name, type, locale_name = nil) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/fae/static_page.rb', line 54

def self.define_association(name, type, locale_name = nil)
  locale_name ||= name

  send :has_one, name.to_sym, -> { where(attached_as: locale_name.to_s)}, as: poly_sym(type), class_name: type.to_s, dependent: :destroy
  send :accepts_nested_attributes_for, name, allow_destroy: true
  send :define_method, :"#{name}_content", -> { send(name.to_sym).try(:content) }
end

.define_validations(name, type, validations) ⇒ Object



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

def self.define_validations(name, type, validations)
  unique_method_name = "is_#{self.name.underscore}_#{name.to_s}?".to_sym
  slug = @slug
  validations[:if] = unique_method_name
  type.validates(:content, validations)
  type.send(:define_method, unique_method_name) do
    contentable.slug == slug && attached_as == name.to_s if contentable.present?
  end
end

.fae_fieldsObject



18
19
20
# File 'app/models/fae/static_page.rb', line 18

def self.fae_fields
  {}
end

.instanceObject



11
12
13
14
15
16
# File 'app/models/fae/static_page.rb', line 11

def self.instance
  setup_dynamic_singleton
  row = includes(fae_fields.keys).references(fae_fields.keys).find_by_slug(@slug)
  row = create(title: @slug.titleize, slug: @slug) if row.blank?
  row
end

.poly_sym(assoc) ⇒ Object



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

def self.poly_sym(assoc)
  case assoc.name
  when 'Fae::TextField', 'Fae::TextArea'
    return :contentable
  when 'Fae::Image'
    return :imageable
  when 'Fae::File'
    return :fileable
  end
end

.setup_dynamic_singletonObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/fae/static_page.rb', line 28

def self.setup_dynamic_singleton
  return if @singleton_is_setup

  fae_fields.each do |name, value|
    type = value.is_a?(Hash) ? value[:type] : value
    languages = value.try(:[], :languages)

    if languages.present?
      languages.each do |lang|
        # Save with suffix for form fields
        define_association("#{name}_#{lang}", type)
        define_validations("#{name}_#{lang}", type, value[:validates]) if value.try(:[], :validates).present?
      end
      # Save with lookup to have default language return in front-end use (don't need to worry about validations here)
      default_language = Rails.application.config.i18n.default_locale || languages.first
      define_association(name, type, "#{name}_#{default_language}")
    else
      # Normal content_blocks
      define_association(name, type)
      define_validations(name, type, value[:validates]) if value.try(:[], :validates).present?
    end
  end

  @singleton_is_setup = true
end

Instance Method Details

#fae_display_fieldObject



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

def fae_display_field
  title
end