Module: Model

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/model.rb,
app/models/concerns/model/enum.rb,
app/models/concerns/model/has_one.rb,
app/models/concerns/model/has_many.rb,
app/models/concerns/model/belongs_to.rb

Defined Under Namespace

Modules: BelongsTo, Enum, HasMany, HasOne

Instance Method Summary collapse

Instance Method Details

#attributes(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/concerns/model.rb', line 21

def attributes(options = {})
  attrs = base_attributes

  (self.class.associations_registry || {}).each do |assoc, type|
    next unless (value = send(assoc)).present?

    attrs["#{assoc}_attributes"] = case type
    when :has_many
      if options[:as_params]
        Hash[value.each_with_index.map {|obj, i| [ i.to_s, obj.attributes(options) ] }]
      else
        value.map {|obj| obj.attributes(options) }
      end
    when :has_one
      value.attributes(options)
    else
      raise "invalid association type #{type} for #{assoc}"
    end
  end

  attrs
end

#base_attributesObject



17
18
19
# File 'app/models/concerns/model.rb', line 17

def base_attributes
  Hash[self.class.attributes_registry.map {|attr_name, attr_args| [ attr_name.to_s, send(attr_name).to_s ] }]
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/concerns/model.rb', line 48

def marked_for_destruction?
  false
end

#to_paramsObject



44
45
46
# File 'app/models/concerns/model.rb', line 44

def to_params
  attributes(as_params: true)
end