module CustomFields::Types::Select::Target::ClassMethods

Public Instance Methods

_order_select_entries(list, order_by = nil) click to toggle source
# File lib/custom_fields/types/select.rb, line 174
def _order_select_entries(list, order_by = nil)
  return list if order_by.nil?

  column, direction = order_by.flatten

  list = list.sort do |a, b|
    a.send(column) && b.send(column) ? (a.send(column) || 0) <=> (b.send(column) || 0) : 0
  end

  direction == 'asc' ? list : list.reverse

  list
end
_select_options(name) click to toggle source
# File lib/custom_fields/types/select.rb, line 156
def _select_options(name)
  send(:"_raw_#{name}_options").map do |option|
    locale = Mongoid::Fields::I18n.locale.to_s

    name = if !option['name'].respond_to?(:merge)
             option['name']
           elsif option['name'].key?(locale)
             option['name'][locale.to_s]
           elsif Mongoid::Fields::I18n.fallbacks?
             option['name'][Mongoid::Fields::I18n.fallbacks[locale.to_sym].map(&:to_s).find do |loc|
                              !option['name'][loc].nil?
                            end]
           end

    { '_id' => option['_id'], 'name' => name }
  end
end
apply_select_custom_field(klass, rule) click to toggle source

Adds a select field

@param [ Class ] klass The class to modify @param [ Hash ] rule It contains the name of the field and if it is required or not

# File lib/custom_fields/types/select.rb, line 58
          def apply_select_custom_field(klass, rule)
            name = rule['name']
            base_collection_name = "#{rule['name']}_options".to_sym

            klass.field :"#{name}_id", type: BSON::ObjectId, localize: rule['localized'] || false, default: lambda {
                                                                                                              _set_select_option(name, rule['default'])
                                                                                                            }

            klass.cattr_accessor "_raw_#{base_collection_name}"
            klass.send :"_raw_#{base_collection_name}=", rule['select_options'].sort { |a, b|
                                                           a['position'] <=> b['position']
                                                         }

            # other methods
            klass.send(:define_method, name.to_sym) { _get_select_option(name) }
            klass.send(:define_method, :"#{name}=") { |value| _set_select_option(name, value) }

            klass.class_eval <<-EOV, __FILE__, __LINE__ + 1

              def self.#{base_collection_name}
                self._select_options('#{name}')
              end

            EOV

            return unless rule['required']

            klass.validates_presence_of name
          end
group_by_select_option(name, order_by = nil) click to toggle source

Returns a list of documents groupes by select values defined in the custom fields recipe

@param [ Class ] klass The class to modify @return [ Array ] An array of hashes (keys: select option and related documents)

# File lib/custom_fields/types/select.rb, line 131
def group_by_select_option(name, order_by = nil)
  name_id = "#{name}_id"
  groups = each.group_by { |x| x.send(name_id) }.map do |(k, v)|
    { name_id => k, 'group' => v }
  end

  _select_options(name).map do |option|
    group = groups.detect { |g| g[name_id].to_s == option['_id'].to_s }
    list  = group ? group['group'] : []

    groups.delete(group) if group

    { name: option['name'], entries: _order_select_entries(list, order_by) }.with_indifferent_access
  end.tap do |array|
    unless groups.empty? # orphan entries ?
      empty = { name: nil, entries: [] }.with_indifferent_access
      groups.each do |group|
        empty[:entries] += group['group']
      end
      empty[:entries] = _order_select_entries(empty[:entries], order_by)
      array << empty
    end
  end
end
select_attribute_get(instance, name) click to toggle source

Build a hash storing the values (id and option name) for a select custom field of an instance.

@param [ Object ] instance An instance of the class enhanced by the custom_fields @param [ String ] name The name of the select custom field

@return [ Hash ] fields: <name>: option name, <name>_id: id of the option

# File lib/custom_fields/types/select.rb, line 96
def select_attribute_get(instance, name)
  if value = instance.send(name.to_sym)
    {
      name => value,
      "#{name}_id" => instance.send(:"#{name}_id")
    }
  else
    {}
  end
end
select_attribute_set(instance, name, attributes) click to toggle source

Set the value for the instance and the select field specified by the 2 params.

@param [ Object ] instance An instance of the class enhanced by the custom_fields @param [ String ] name The name of the select custom field @param [ Hash ] attributes The attributes used to fetch the values

# File lib/custom_fields/types/select.rb, line 114
def select_attribute_set(instance, name, attributes)
  id_or_name = attributes[name] || attributes["#{name}_id"]

  return if id_or_name.nil?

  option = _select_options(name).detect do |option|
    [option['_id'], option['name']].include?(id_or_name)
  end

  instance.send(:"#{name}_id=", option.try(:[], '_id'))
end