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



174
175
176
177
178
179
180
181
182
183
184
185
186
# 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) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# 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) ⇒ 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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ 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)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ 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



96
97
98
99
100
101
102
103
104
105
# 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) ⇒ 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



114
115
116
117
118
119
120
121
122
123
124
# 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