Class: Dao::Gateway::ActiveRecord::Base

Inherits:
Base
  • Object
show all
Includes:
ActiveSupport::Rescuable
Defined in:
lib/dao/gateway/active_record/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, transformer) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/dao/gateway/active_record/base.rb', line 7

def initialize(source, transformer)
  super
  @black_list_attributes += source_relations
end

Instance Method Details

#add_relations(scope, relations, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/dao/gateway/active_record/base.rb', line 38

def add_relations(scope, relations, options)
  case options[:strategy]
    when :preload
      scope.preload(*relations)
    when :includes
      scope.includes(*relations)
    else
      scope.eager_load(*relations)
  end
end

#chain(scope, method_name, args, &block) ⇒ Object



32
33
34
35
36
# File 'lib/dao/gateway/active_record/base.rb', line 32

def chain(scope, method_name, args, &block)
  scope.public_send(method_name, *args, &block)
rescue ::ActiveRecord::RecordNotFound => e
  raise Dao::Gateway::RecordNotFound, e.message
end

#delete(domain_id) ⇒ Object



28
29
30
# File 'lib/dao/gateway/active_record/base.rb', line 28

def delete(domain_id)
  record(domain_id).destroy if domain_id.present?
end

#save!(domain, attributes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dao/gateway/active_record/base.rb', line 12

def save!(domain, attributes)
  record = export(domain, record(domain.id))
  record.assign_attributes(attributes)
  record.save!
  domain.attributes = import(record, domain.initialized_with).attributes
  domain
rescue ::ActiveRecord::RecordInvalid
  raise Dao::Gateway::InvalidRecord.new(record.errors.to_hash)
rescue ::ActiveRecord::RecordNotFound => e
  raise Dao::Gateway::RecordNotFound, e.message
rescue ::ActiveRecord::RecordNotUnique => e
  raise Dao::Gateway::RecordNotUnique, e.message
rescue Exception => e
  rescue_with_handler(e) || raise
end

#with_lock(id, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/dao/gateway/active_record/base.rb', line 53

def with_lock(id, *args, &block)
  source.transaction do
    source.lock(*args).find(id)
    block.call
  end

rescue ::ActiveRecord::StatementInvalid => e
  raise Dao::Gateway::StatementInvalid, e.message
end

#with_transaction(&block) ⇒ Object



49
50
51
# File 'lib/dao/gateway/active_record/base.rb', line 49

def with_transaction(&block)
  source.transaction(&block)
end