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

#[](name) ⇒ Object



77
78
79
# File 'lib/locomotive/mounter/fields.rb', line 77

def [](name)
  self.attributes[name.to_sym]
end

#attributesHash

Return the fields with their values

Returns:

  • (Hash)

    The attributes



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

Returns:

  • (Hash)

    The attributes



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/locomotive/mounter/fields.rb', line 85

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

Parameters:

  • attributes (Hash) (defaults to: {})

    The new attributes



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

Parameters:

  • name (String)

    Name of the field

Returns:

  • (Boolean)

    True if the field is localized



111
112
113
114
# File 'lib/locomotive/mounter/fields.rb', line 111

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.

Parameters:

  • translation (Boolean)

    Flag (by default true) to get the translations too.

Returns:

  • (Hash)

    The non blank attributes



143
144
145
146
147
148
149
150
151
# File 'lib/locomotive/mounter/fields.rb', line 143

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

Returns:

  • (String)

    The YAML version of the object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/locomotive/mounter/fields.rb', line 157

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

Returns:

  • (List)

    List of locales



120
121
122
# File 'lib/locomotive/mounter/fields.rb', line 120

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.

Parameters:

Returns:

  • (Boolean)

    True if one of the fields has been translated.



131
132
133
# File 'lib/locomotive/mounter/fields.rb', line 131

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.

Parameters:

  • attributes (Hash)

    The new attributes



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