class BettyResource::Model::Record

Attributes

id[R]
model[R]

Public Class Methods

new(model, attributes = {}) click to toggle source
Calls superclass method DirtyAttributes::InstanceMethods::new
# File lib/betty_resource/model/record.rb, line 57
def initialize(model, attributes = {})
  @model = model
  @id = attributes.delete(:id) || attributes.delete('id')
  @errors = {}
  super(self)
  self.attributes = Hash[model.attributes.map { |x| [x, nil] }].merge attributes
  self.attributes.instance_variable_set(:@model, model)
end

Public Instance Methods

_class()
Alias for: class
as_json(options = {}) click to toggle source

TODO: Test this update

# File lib/betty_resource/model/record.rb, line 108
def as_json(options = {})
  attributes_as_json(options).merge! 'id' => id
end
attributes=(other) click to toggle source

TODO: Test this

# File lib/betty_resource/model/record.rb, line 71
def attributes=(other)
  other.each do |key, value|
    send "#{key}=", value
  end
end
class() click to toggle source
# File lib/betty_resource/model/record.rb, line 48
def class
  model
end
Also aliased as: _class
errors() click to toggle source
# File lib/betty_resource/model/record.rb, line 77
def errors
  @errors.dup
end
inspect() click to toggle source
# File lib/betty_resource/model/record.rb, line 101
def inspect
  inspection = "id: #{id.inspect}, #{attributes.map { |key, value| "#{key}: #{value.inspect}"}.join(", ")}"
  "#<#{model.name} #{inspection}>"
end
Also aliased as: to_s
new_record?() click to toggle source
# File lib/betty_resource/model/record.rb, line 66
def new_record?
  @id.nil?
end
save() click to toggle source
# File lib/betty_resource/model/record.rb, line 81
def save
  @errors.clear

  result = begin
    if new_record?
      Api.post("/models/#{model.id}/records/new", to_params)
    else
      Api.put("/models/#{model.id}/records/#{id}", to_params)
    end
  end

  result.success?.tap do |success|
    if success
      model.send :load, result.parsed_response, self
    else
      @errors = result.parsed_response ? result.parsed_response['errors'] : { '' => ['Er is iets mis gegaan met het verwerken van het formulier. Probeer u het later nog eens. Onze excuses voor het ongemak'] }
    end
  end
end
to_s()
Alias for: inspect
typecast(key, value) click to toggle source
# File lib/betty_resource/model/record.rb, line 52
def typecast(key, value)
  property = self.class.property(key)
  property ? property.typecast(self, value) : value
end

Private Instance Methods

attributes_as_json(options = {}) click to toggle source

TODO: Test this update

# File lib/betty_resource/model/record.rb, line 119
def attributes_as_json(options = {})
  attributes.reduce({}) do |h, (k, v)|
    h.merge! k => (v.respond_to?(:as_json) ? v.as_json(options) : v) if v
    h
  end
end
to_params() click to toggle source
# File lib/betty_resource/model/record.rb, line 114
def to_params
  { body: { record: attributes_as_json } }
end