Module: CustomFields::Types::Select::Target::ClassMethods

Defined in:
lib/custom_fields/types/select.rb

Instance Method Summary collapse

Instance Method Details

#_order_select_entries(list, order_by = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/custom_fields/types/select.rb', line 177

def _order_select_entries(list, order_by = nil)
  return list if order_by.nil?

  column, direction = order_by.flatten

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

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

  list
end

#_select_options(name) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/custom_fields/types/select.rb', line 160

def _select_options(name)
  self.send(:"_raw_#{name}_options").map do |option|

    locale = Mongoid::Fields::I18n.locale.to_s

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

    { '_id' => option['_id'], 'name' => name }
  end
end

#apply_select_custom_field(klass, rule) ⇒ Object

Adds a select field

Parameters:

  • klass (Class)

    The class to modify

  • rule (Hash)

    It contains the name of the field and if it is required or not



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/custom_fields/types/select.rb', line 67

def apply_select_custom_field(klass, rule)
  name, base_collection_name = rule['name'], "#{rule['name']}_options".to_sym

  klass.field :"#{name}_id", type: Moped::BSON::ObjectId, localize: rule['localized'] || false

  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

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

  EOV

  if rule['required']
    klass.validates_presence_of name
  end
end

#group_by_select_option(name, order_by = nil) ⇒ Array

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

Parameters:

  • klass (Class)

    The class to modify

Returns:

  • (Array)

    An array of hashes (keys: select option and related documents)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/custom_fields/types/select.rb', line 135

def group_by_select_option(name, order_by = nil)
  name_id = "#{name}_id"
  groups = self.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: self._order_select_entries(list, order_by) }.with_indifferent_access
  end.tap do |array|
    if not groups.empty? # orphan entries ?
      empty = { name: nil, entries: [] }.with_indifferent_access
      groups.each do |group|
        empty[:entries] += group['group']
      end
      empty[:entries] = self._order_select_entries(empty[:entries], order_by)
      array << empty
    end
  end
end

#select_attribute_get(instance, name) ⇒ Hash

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

Parameters:

  • instance (Object)

    An instance of the class enhanced by the custom_fields

  • name (String)

    The name of the select custom field

Returns:

  • (Hash)

    fields: <name>: option name, <name>_id: id of the option



100
101
102
103
104
105
106
107
108
109
# File 'lib/custom_fields/types/select.rb', line 100

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) ⇒ Object

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

Parameters:

  • instance (Object)

    An instance of the class enhanced by the custom_fields

  • name (String)

    The name of the select custom field

  • attributes (Hash)

    The attributes used to fetch the values



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/custom_fields/types/select.rb', line 118

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