Module: Hoodoo::ActiveRecord::Creator::ClassMethods

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#new_in(context, attributes = nil, &block) ⇒ Object

Create an instance of this model with knowledge of the wider request context. This may lead to important things like support of inbound “dated_from” values, depending upon the Hoodoo mixins included (or not) by this class - see Hoodoo::ActiveRecord::Dated.

You use this exactly as you would for ActiveRecord::Core#new, but an additional, mandatory first parameter providing the request context must be supplied. For example, instead of this:

instance = SomeActiveRecordSubclass.new( attrs )

…do this inside a resource implementation:

instance = SomeActiveRecordSubclass.new_in( context, attrs )

See also:

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

Optional model attributes Hash, passed through to ActiveRecord::Core#new.

&block

Optional block for initialisation, passed through to ActiveRecord::Core#new.

Returns a new model instance which may have context-derived values set for some attributes, in addition to anything set through the attributes or &block parameters, if present.

Note that context-dependent data is set AFTER attribute or block based values, so takes precedence over anything you might set up using those parameters.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hoodoo/active/active_record/creator.rb', line 114

def new_in( context, attributes = nil, &block )

  instance = self.new( attributes, &block )

  # TODO: Refactor this to use the scope chain plugin approach in due
  #       course, but for now, pragmatic implementation does the only
  #       things we currently require - set "created_at"/"updated_at".
  #
  unless context.request.dated_from.nil?
    instance.created_at = instance.updated_at = context.request.dated_from
  end

  return instance
end