Sha256: d3b9ec057cc20d198e1cc43caa5d71004d2fc63d2572f32f44879ec6f98e8462

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

module Locomotive
  class CustomFieldService < Struct.new(:field, :locale)

    # Update the options of a "select" field.
    #
    # @param [ Hash ] options It includes the following keys: name, _id and _destroyed (if persisted)
    #
    # @return [ Array ] The new list of options
    #
    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

    private

    def save_field
      # save the content type so that all the content entries get a fresh version
      # of the custom fields rules
      self.field.save
      self.field._parent.save
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotivecms-4.1.1 app/services/locomotive/custom_field_service.rb
locomotivecms-4.1.0 app/services/locomotive/custom_field_service.rb
locomotivecms-4.1.0.rc1 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.3 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.2 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.1 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.0 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.0.rc0 app/services/locomotive/custom_field_service.rb
locomotivecms-4.0.0.alpha3 app/services/locomotive/custom_field_service.rb