Class: Address::Creator

Inherits:
BaseService show all
Defined in:
app/services/address/creator.rb

Overview

Service object: creator.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ void

Parameters:

  • options (Hash) (defaults to: {})

    service options

Options Hash (options):

  • party_timezone_recorder (#process)

    recorder used to update the party timezone (defaults to PartyRecordTimezone.new)



21
22
23
# File 'app/services/address/creator.rb', line 21

def initialize(options = {})
  @party_timezone_recorder = options[:party_timezone_recorder] || PartyRecordTimezone.new
end

Instance Method Details

#process(attributes, context_object = nil) ⇒ Result

Creates an address, optionally nested under a context object's addresses,
and records the party timezone on success.

Parameters:

  • attributes (Hash)

    address attributes

  • context_object (ActiveRecord::Base, nil) (defaults to: nil)

    parent owning the addresses collection

Returns:

  • (Result)

    save outcome with the address record



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/address/creator.rb', line 30

def process(attributes, context_object = nil)
  messages = []
  address = context_object.nil? ? Address.new(attributes) : context_object.addresses.new(attributes)
  address.check_for_duplicates = true
  if address.save
    @party_timezone_recorder.process(address.party) if address.party

    Result.new(saved: true,
               autocorrect_message: address.autocorrect_message,
               messages: messages,
               address: address)

  else
    Result.new(saved: false,
               autocorrect_message: address.autocorrect_message,
               messages: address.errors.full_messages,
               address: address)
  end
end