Class: Shamu::Services::Request

Inherits:
Object
  • Object
show all
Includes:
Attributes, Attributes::Assignment, Attributes::FluidAssignment, Attributes::Validation
Defined in:
lib/shamu/services/request.rb

Overview

Define the attributes and validations required to request a change by a Service. You can use the Request in place of an ActiveRecord model in rails forms_helpers.

module Document
  module Request
    class Change < Shamu::Services::Request
      attribute :title, presence: true
      attribute :author_id, presence: true
    end

    class Create < Change
    end

    class Update < Change
      attribute :id, presence: true
    end
  end
end

Direct Known Subclasses

Auditing::Transaction

Constant Summary collapse

REQUEST_ACTION_PATTERN =
/(Create|Update|New|Change|Delete)?(Request)?$/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::Validation

attribute, #valid?, #validated?

Methods included from Attributes::Assignment

attribute

Methods included from Attributes

#[], #assign_attributes, association, associations, attribute, #attribute?, attributes, #initialize, #set?, #to_attributes

Class Method Details

.coerce(params) ⇒ Request

Coerces a hash or params object to a proper Shamu::Services::Request object.

Parameters:

  • params (Object)

    to be coerced.

Returns:

  • (Request)

    the coerced request.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shamu/services/request.rb', line 60

def coerce( params )
  if params.is_a?( self )
    params
  elsif params.respond_to?( :to_h ) || params.respond_to?( :to_attributes )
    new( params )
  elsif params.nil?
    new
  else
    raise ArgumentError
  end
end

.coerce!(params) ⇒ Request

Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.

Parameters:

  • params (Object)

    to be coerced.

Returns:

  • (Request)

    the coerced request.

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/shamu/services/request.rb', line 76

def coerce!( params )
  coerced = coerce( params )
  raise ArgumentError unless coerced.valid?
  coerced
end

.model_nameActiveModel::Name

Returns used by url_helpers or form_helpers etc. when generating model specific names for this request.

Returns:

  • (ActiveModel::Name)

    used by url_helpers or form_helpers etc. when generating model specific names for this request.



86
87
88
89
90
91
92
93
94
95
# File 'lib/shamu/services/request.rb', line 86

def model_name
  @model_name ||= begin
    base_name = name || ""
    parts     = reduce_model_name_parts( base_name.split( "::" ) )
    parts     = ["Request"] if parts.empty?
    base_name = parts.join "::"

    ::ActiveModel::Name.new( self, nil, base_name )
  end
end

Instance Method Details

#apply_to(model) ⇒ model

Applies the attributes of the request to the given model. Only handles scalar attributes. For more complex associations, override in a custom Shamu::Services::Request class.

Parameters:

  • model (Object)

    or object to apply the attributes to.

Returns:

  • (model)


37
38
39
40
41
42
43
44
# File 'lib/shamu/services/request.rb', line 37

def apply_to( model )
  self.class.attributes.each do |name, _|
    method = :"#{ name }="
    model.send method, send( name ) if model.respond_to?( method ) && set?( name )
  end

  model
end

#persisted?Boolean

Entities are always immutable - so they are considered persisted. Use a Services::ChangeRequest to back a form instead.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/shamu/services/request.rb', line 48

def persisted?
  if respond_to?( :id )
    !!id
  else
    fail NotImplementedError, "override persisted? in #{ self.class.name }"
  end
end