Class: Releaf::BuildErrorsHash

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/lib/releaf/build_errors_hash.rb

Instance Method Summary collapse

Instance Method Details

#association(association_name) ⇒ Object



77
78
79
# File 'app/lib/releaf/build_errors_hash.rb', line 77

def association(association_name)
  resource.class.reflect_on_association(association_name.to_sym)
end

#association_type(association_name) ⇒ Object



73
74
75
# File 'app/lib/releaf/build_errors_hash.rb', line 73

def association_type(association_name)
  association(association_name).macro
end

#attribute_error(attribute, message) ⇒ Object



22
23
24
25
26
27
28
# File 'app/lib/releaf/build_errors_hash.rb', line 22

def attribute_error(attribute, message)
  {
    field_name: field_name(attribute),
    error_code: message.error_code,
    message: message.to_s,
  }
end

#callObject



7
8
9
10
11
12
13
14
# File 'app/lib/releaf/build_errors_hash.rb', line 7

def call
  errors.inject({}) do |h, item|
    field_name = item.delete(:field_name)
    h[field_name] ||= []
    h[field_name] << item
    h
  end
end

#errorsObject



16
17
18
19
20
# File 'app/lib/releaf/build_errors_hash.rb', line 16

def errors
  resource.errors.map do |attribute, message|
    format_error(attribute, message)
  end.flatten
end

#field_name(attribute) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'app/lib/releaf/build_errors_hash.rb', line 62

def field_name(attribute)
  return field_name_prefix if attribute.to_sym == :base

  field = attribute
  if association(attribute).present?
    field = association(attribute).foreign_key.to_s
  end

  "#{field_name_prefix}[#{field}]"
end

#format_error(attribute, message) ⇒ Object



30
31
32
33
34
35
36
# File 'app/lib/releaf/build_errors_hash.rb', line 30

def format_error(attribute, message)
  if resource_attribute?(attribute)
    attribute_error(attribute, message)
  else
    nested_attribute_errors(attribute)
  end
end

#nested_attribute_errors(attribute) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/lib/releaf/build_errors_hash.rb', line 38

def nested_attribute_errors(attribute)
  association_name = attribute.to_s.split('.', 2).first
  if single_association?(association_name)
    prefix_template = "#{field_name_prefix}[#{association_name}_attributes]"
    nested_resources = [resource.send(association_name)]
  else
    prefix_template = "#{field_name_prefix}[#{association_name}_attributes][%d]"
    nested_resources = resource.send(association_name)
  end

  nested_resources.each_with_index.map do |nested_resource, i|
    prefix = prefix_template % i
    self.class.new(resource: nested_resource, field_name_prefix: prefix).errors
  end
end

#resource_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/lib/releaf/build_errors_hash.rb', line 58

def resource_attribute?(attribute)
  attribute !~ /\./
end

#single_association?(association_name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/lib/releaf/build_errors_hash.rb', line 54

def single_association?(association_name)
  [:belongs_to, :has_one].include? association_type(association_name)
end