Module: Operable::Fields

Extended by:
ActiveSupport::Concern
Included in:
Operable
Defined in:
lib/operable/fields.rb

Instance Method Summary collapse

Instance Method Details

#operable_valuesObject

Return a list of attributes (with values) to be included in operations

Returns Hash of key (string) => value pairs



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/operable/fields.rb', line 9

def operable_values
  raise "Please specify one or more fields via operable_on in your model definition!" if self.class.operable_fields.nil?

  values = {}
  attributes.select {|k, v| self.class.operable_fields.include? k }.each do |k, v|
    values[k.to_s] = v
  end

  if respond_to?(:reflections)
    reflections.select {|k, v| self.class.operable_fields.include? k.to_s }.each do |k, v|
      if self.send(k).present?
        values[k.to_s] = send(k)
      end
    end
  end

  if respond_to?(:associations)
    associations.select {|k, v| self.class.operable_fields.include? k.to_s }.each do |k, v|
      values[k.to_s] = send(k)
    end
  end

  values
end