Module: Perimeter::Repository::Adapters::ActiveRecord::ClassMethods

Defined in:
lib/perimeter/repository/adapters/active_record.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/perimeter/repository/adapters/active_record.rb', line 30

def create(attributes)
  record = attributes_to_record attributes

  if record.invalid?
    return Operations.failure(:validation_failed, object: record_to_entity(record))
  end

  id = record.id.presence || attributes[:id].presence || attributes['id'].presence
  if id && backend.find_by_id(id)
    return Operations.failure :record_already_exists
  end

  if record.save
    Operations.success :record_created, object: record_to_entity(record)
  else
    Operations.failure :creation_failed, object: record_to_entity(record)
  end

rescue => exception
  ::Trouble.notify exception
  Operations.failure :backend_error, object: exception
end

#destroy(id) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/perimeter/repository/adapters/active_record.rb', line 80

def destroy(id)
  unless record = backend.find_by_id(id)
    return Operations.success(:nothing_to_destroy)
  end

  if record.destroy
    Operations.success :record_destroyed, object: record_to_entity(record)
  else
    Operations.failure :destruction_failed
  end

rescue => exception
  ::Trouble.notify exception
  Operations.failure :backend_error, object: exception
end

#find(id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/perimeter/repository/adapters/active_record.rb', line 17

def find(id)
  record = backend.find id
  entity = record_to_entity record
  Operations.success :record_found, object: entity

rescue ::ActiveRecord::RecordNotFound => exception
  Operations.failure :record_not_found, object: exception

rescue => exception
  ::Trouble.notify exception
  Operations.failure :backend_error, object: exception
end

#update(id, attributes) ⇒ Object

––––––––Updating ––––––––



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/perimeter/repository/adapters/active_record.rb', line 57

def update(id, attributes)
  unless record = backend.find_by_id(id)
    return Operations.failure :record_not_found
  end

  record.send :assign_attributes, attributes

  if record.invalid?
    entity = record_to_entity record
    return Operations.failure(:validation_failed, object: entity)
  end

  if record.save
    Operations.success :record_updated, object: record_to_entity(record)
  else
    Operations.failure :update_failed
  end

rescue => exception
  ::Trouble.notify exception
  Operations.failure :backend_error, object: exception
end