Module: Locomotive::Mounter::Fields::ClassMethods

Defined in:
lib/locomotive/mounter/fields.rb

Instance Method Summary collapse

Instance Method Details

#field(name, options = {}) ⇒ Object

Add a field to the current instance. It creates getter/setter methods related to that field. A field can have translations if the option named localized is set to true.

Parameters:

  • name (String)

    The name of the field

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

    The options related to the field.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/locomotive/mounter/fields.rb', line 215

def field(name, options = {})
  options = { localized: false }.merge(options)

  @_fields ||= {} # initialize the list of fields if nil

  self._fields[name.to_sym] = options

  class_eval <<-EOV
    def #{name}
      self.getter '#{name}', self.class._fields[:#{name}]
    end

    def #{name}=(value)
      self.setter '#{name}', value, self.class._fields[:#{name}] #, #{options[:localized]}
    end

    def #{name}_localized?
      #{options[:localized]}
    end
  EOV

  if options[:localized]
    class_eval <<-EOV
      def #{name}_translations
        @#{name} || {}
      end

      def #{name}_translations=(translations)
        translations.each { |locale, value| self.add_locale(locale) }
        @#{name} = translations.symbolize_keys
      end
    EOV
  end
end