Module: FmRest::Spyke::Model::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Dirty, ActiveModel::ForbiddenAttributesProtection, Orm
Included in:
FmRest::Spyke::Model
Defined in:
lib/fmrest/spyke/model/attributes.rb

Overview

Extends Spyke models with support for mapped attributes, ActiveModel::Dirty and forbidden attributes (e.g. Rails' params.permit).

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Orm

all, create!, #destroy, fetch, #save, #save!, #update!, #validate!

Class Method Details

.attributes(*attrs) ⇒ Object

Spyke override

Similar to Spyke::Base.attributes, but allows defining attribute methods that map to FM attributes with different names.

Examples:


class Person < Spyke::Base
  include FmRest::Spyke::Model

  attributes first_name: "FstName", last_name: "LstName"
end

p = Person.new
p.first_name = "Jojo"
p.attributes # => { "FstName" => "Jojo" }


61
62
63
64
65
66
67
# File 'lib/fmrest/spyke/model/attributes.rb', line 61

def attributes(*attrs)
  if attrs.length == 1 && attrs.first.kind_of?(Hash)
    attrs.first.each { |from, to| _fmrest_define_attribute(from, to) }
  else
    attrs.each { |attr| _fmrest_define_attribute(attr, attr) }
  end
end

Instance Method Details

#attributes=(new_attributes) ⇒ Object

Spyke override -- Adds support for forbidden attributes (i.e. Rails' params.permit, etc.)



136
137
138
139
140
# File 'lib/fmrest/spyke/model/attributes.rb', line 136

def attributes=(new_attributes)
  @spyke_attributes ||= ::Spyke::Attributes.new(scope.params)
  return unless new_attributes && !new_attributes.empty?
  use_setters(sanitize_for_mass_assignment(new_attributes))
end

#reload(*args) ⇒ Object

Spyke override -- Adds AM::Dirty support



129
130
131
# File 'lib/fmrest/spyke/model/attributes.rb', line 129

def reload(*args)
  super.tap { |r| clear_changes_information }
end