Module: Protip::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Validations
Defined in:
lib/protip/resource.rb,
lib/protip/resource/creatable.rb,
lib/protip/resource/updateable.rb,
lib/protip/resource/destroyable.rb,
lib/protip/resource/extra_methods.rb,
lib/protip/resource/search_methods.rb,
lib/protip/resource/associations/association.rb,
lib/protip/resource/associations/belongs_to_association.rb,
lib/protip/resource/associations/belongs_to_polymorphic_association.rb

Defined Under Namespace

Modules: Associations, ClassMethods, Creatable, Destroyable, ExtraMethods, SearchMethods, Updatable Classes: RecordInvalid

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attributes) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/protip/resource.rb', line 289

def assign_attributes(attributes)
  old_attributes = {}
  descriptor = message.class.descriptor
  keys = attributes.keys.map(&:to_s)
  keys.each do |key|
    field = descriptor.lookup(key)
    value = message[key]
    # If the current value is a message, we need to clone it to get a reasonable comparison later,
    # since we might just assign attributes to the current instance of the message directly
    old_attributes[key] = field && field.type == :message && value ? value.clone : value
  end
  @decorator.assign_attributes attributes
  keys.each do |key|
    old_value = old_attributes[key]
    new_value = message[key]
    changed = !(old_value.class == new_value.class && old_value == new_value)

    if changed
      send "#{key}_will_change!"
    end
  end
  nil # return nil to match ActiveRecord behavior
end

#attributesObject



356
357
358
359
360
361
362
# File 'lib/protip/resource.rb', line 356

def attributes
  # Like `.as_json`, but includes nil fields to match ActiveRecord behavior.
  self.class.message.descriptor.map{|field| field.name}.inject({}) do |hash, attribute_name|
    hash[attribute_name] = public_send(attribute_name)
    hash
  end
end

#errorsObject



364
365
366
# File 'lib/protip/resource.rb', line 364

def errors
  @errors ||= ActiveModel::Errors.new(self)
end

#initialize(message_or_attributes = {}) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/protip/resource.rb', line 275

def initialize(message_or_attributes = {})
  if self.class.message == nil
    raise RuntimeError.new('Must define a message class using `resource`')
  end
  if message_or_attributes.is_a?(self.class.message)
    self.message = message_or_attributes
  else
    self.message = self.class.message.new
    assign_attributes message_or_attributes
  end

  super()
end

#message=(message) ⇒ Object



313
314
315
316
# File 'lib/protip/resource.rb', line 313

def message=(message)
  @decorator = Protip::Decorator.new(message,
    self.class.transformer, self.class.nested_resources)
end

#persisted?Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/protip/resource.rb', line 352

def persisted?
  id != nil
end

#saveObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/protip/resource.rb', line 318

def save
  success = true
  begin
    if persisted?
      # TODO: use `ActiveModel::Dirty` to only send changed attributes?
      update!
    else
      create!
    end
    changes_applied
  rescue Protip::UnprocessableEntityError => error
    success = false
    error.errors.messages.each do |message|
      errors.add :base, message
    end
    error.errors.field_errors.each do |field_error|
      errors.add field_error.field, field_error.message
    end
  end
  success
end

#save!Object



343
344
345
346
347
348
349
350
# File 'lib/protip/resource.rb', line 343

def save!
  success = save
  if !success
    error_messages = errors.full_messages.join(", ")
    raise RecordInvalid.new("Validation failed: #{error_messages}")
  end
  success
end