Module: Hoodoo::ActiveRecord::Writer::ClassMethods

Defined in:
lib/hoodoo/active/active_record/writer.rb

Overview

Collection of class methods that get defined on an including class via Hoodoo::ActiveRecord::Writer::included.

Instance Method Summary collapse

Instance Method Details

#persist_in(context, attributes) ⇒ Object

A class-based equivalent of the Hoodoo::ActiveRecord::Writer#persist_in method which creates a record using Hoodoo::ActiveRecord::Creator::ClassMethods::new_in, then calls Hoodoo::ActiveRecord::Writer#persist_in to persist the data; see that for full details.

As a class-based method, the return value and error handling semantics differ from the instance-based counterpart. Instead of checking the return value of persist_in for success or failure, use ActiveRecord’s “persisted?”:

def create( context )
  attributes = mapping_of( context.request.body )
  model_instance = Unique.persist_in( context, attributes )

  unless model_instance.persisted?

    # Error condition. If you're using the error handler mixin
    # in Hoodoo::ActiveRecord::ErrorMapping, do this:
    #
    context.response.add_errors( model_instance.platform_errors )
    return # Early exit

  end

  # ...any other processing...

  context.response.set_resource( rendering_of( context, model_instance ) )
end

Parameters

context

Hoodoo::Services::Context instance describing a call context. This is typically a value passed to one of the Hoodoo::Services::Implementation instance methods that a resource subclass implements.

attributes

Attributes hash to be passed to this model class’s constructor, via self.new( attributes ).

See also the Hoodoo::ActiveRecord::Writer#persist_in instance method equivalent of this class method.



341
342
343
344
345
346
# File 'lib/hoodoo/active/active_record/writer.rb', line 341

def persist_in( context, attributes )
  instance = self.new_in( context, attributes )
  instance.persist_in( context )

  return instance
end