Module: Locomotive::Mounter::Fields

Extended by:
ActiveSupport::Concern
Included in:
Models::Base
Defined in:
lib/locomotive/mounter/fields.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributesHash

Return the fields with their values



69
70
71
72
73
74
75
# File 'lib/locomotive/mounter/fields.rb', line 69

def attributes
  {}.tap do |_attributes|
    self.class._fields.each do |name, options|
      _attributes[name] = self.send(name.to_sym)
    end
  end
end

#attributes_with_translationsHash

Return the fields with their values and their translations



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/locomotive/mounter/fields.rb', line 81

def attributes_with_translations
  {}.tap do |_attributes|
    self.class._fields.each do |name, options|
      next if options[:association]

      if options[:localized]
        value = self.send(:"#{name}_translations")

        value = value.values.first if value.size == 1

        value = nil if value.respond_to?(:empty?) && value.empty?

        _attributes[name] = value
      else
        _attributes[name] = self.send(name.to_sym)
      end
    end
  end
end

#initialize(attributes = {}) ⇒ Object

Default constructor



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/locomotive/mounter/fields.rb', line 22

def initialize(attributes = {})
  run_callbacks :initialize do
    _attributes = attributes.symbolize_keys

    # set default values
    self.class._fields.each do |name, options|
      next if !options.key?(:default) || _attributes.key?(name)

      _attributes[name] = options[:default]
    end

    # set default translation
    self.add_locale(Locomotive::Mounter.locale)

    self.write_attributes(_attributes)
  end
end

#localized_field?(name) ⇒ Boolean

Check if the field specified by the argument is localized



107
108
109
110
# File 'lib/locomotive/mounter/fields.rb', line 107

def localized_field?(name)
  method_name = :"#{name}_localized?"
  self.respond_to?(method_name) && self.send(method_name)
end

#to_hash(translations = true) ⇒ Hash

Return a Hash of all the non blank attributes of the object. It also performs a couple of modifications: stringify keys and convert Symbol to String.



139
140
141
142
143
144
145
146
147
# File 'lib/locomotive/mounter/fields.rb', line 139

def to_hash(translations = true)
  hash = translations ? self.attributes_with_translations : self.attributes

  hash.delete_if { |k, v| (!v.is_a?(FalseClass) && v.blank?) }

  hash.each { |k, v| hash[k] = v.to_s if v.is_a?(Symbol) }

  hash.deep_stringify_keys
end

#to_yamlString

Provide a better output of the default to_yaml method



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/locomotive/mounter/fields.rb', line 153

def to_yaml
  # get the attributes with their translations and get rid of all the symbols
  object = self.to_hash

  object.each do |key, value|
    if value.is_a?(Array)
      object[key] = if value.first.is_a?(String)
        StyledYAML.inline(value) # inline array
      else
        value.map(&:to_hash)
      end
    end
  end

  StyledYAML.dump object
end

#translated_inList

List all the translations done on that model



116
117
118
# File 'lib/locomotive/mounter/fields.rb', line 116

def translated_in
  self._locales.map(&:to_sym)
end

#translated_in?(locale) ⇒ Boolean

Tell if the object has been translated in the locale passed in parameter.



127
128
129
# File 'lib/locomotive/mounter/fields.rb', line 127

def translated_in?(locale)
  self.translated_in.include?(locale.to_sym)
end

#write_attributes(attributes) ⇒ Object Also known as: attributes=

Set or replace the attributes of the current instance by the ones passed as parameter. It raises an exception if one of the keys is not included in the list of fields.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/locomotive/mounter/fields.rb', line 46

def write_attributes(attributes)
  return if attributes.blank?

  attributes.each do |name, value|
    unless self.class._fields.key?(name.to_sym) || self.respond_to?(:"#{name}=")
      next if name.to_s == 'id'
      raise FieldDoesNotExistException.new "[#{self.class.inspect}] setting an unknown attribute '#{name}' with the value '#{value.inspect}'"
    end

    if self.localized_field?(name) && value.is_a?(Hash)
      self.send(:"#{name}_translations=", value)
    else
      self.send(:"#{name}=", value)
    end
  end
end