Class: Locomotive::CustomFieldService

Inherits:
Struct
  • Object
show all
Defined in:
app/services/locomotive/custom_field_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldObject

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



2
3
4
# File 'app/services/locomotive/custom_field_service.rb', line 2

def field
  @field
end

#localeObject

Returns the value of attribute locale

Returns:

  • (Object)

    the current value of locale



2
3
4
# File 'app/services/locomotive/custom_field_service.rb', line 2

def locale
  @locale
end

Instance Method Details

#update_select_options(options) ⇒ Array

Update the options of a “select” field.

Parameters:

  • options (Hash)

    It includes the following keys: name, _id and _destroyed (if persisted)

Returns:

  • (Array)

    The new list of options



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/locomotive/custom_field_service.rb', line 10

def update_select_options(options)
  return nil if options.blank?

  default_locale      = field._parent.site.default_locale.to_sym
  include_new_options = false

  # set the right position
  options.each_with_index do |option, position|
    option['position'] = position
    include_new_options = true if option['_id'].blank?
  end

  self.field.select_options_attributes = options

  save_field

  # make sure the new options are also available in the default locale
  if include_new_options && locale != default_locale
    ::Mongoid::Fields::I18n.with_locale(default_locale) do
      self.field.reload.select_options.each do |option|
        next unless option.attributes[:name][default_locale].blank?

        # force the name in the default locale
        option.name = option.attributes[:name].values.first
      end
    end

    save_field # we have to save it again and that's okay
  end

  self.field.select_options
end