Module: HumanError::Persistable

Defined in:
lib/human_error/persistable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.human_error_resource_name(klass) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/human_error/persistable.rb', line 42

def self.human_error_resource_name(klass)
  last_part_of_class_name = /::(\w+)\z/

  klass.
    name[last_part_of_class_name, 1].
    underscore.
    humanize.
    downcase
end

.included(base) ⇒ Object



52
53
54
# File 'lib/human_error/persistable.rb', line 52

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#save!(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/human_error/persistable.rb', line 22

def save!(*args)
  super
rescue ActiveRecord::InvalidForeignKey => e
  association_info_pattern = /DETAIL:  Key \((.*)_id\)=\(([a-f0-9\-]+)\)/
  association_name, association_id = e.message.
                                       match(association_info_pattern) \
                                       [1..-1]

  raise HumanError::Errors::AssociationError.new(
    resource_name:    Persistable.human_error_resource_name(self.class),
    association_name: association_name,
    association_id:   association_id,
    attributes:       attributes)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
  raise HumanError::Errors::ResourcePersistenceError.new(
    resource_name: Persistable.human_error_resource_name(self.class),
    attributes:    attributes,
    errors:        errors.full_messages)
end