Class: Brainstem::PresenterValidator

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/brainstem/presenter_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(presenter_class) ⇒ PresenterValidator

Returns a new instance of PresenterValidator



13
14
15
# File 'lib/brainstem/presenter_validator.rb', line 13

def initialize(presenter_class)
  @presenter_class = presenter_class
end

Instance Attribute Details

#presenter_classObject

Returns the value of attribute presenter_class



11
12
13
# File 'lib/brainstem/presenter_validator.rb', line 11

def presenter_class
  @presenter_class
end

Instance Method Details

#associations_exist(associations = presenter_class.configuration[:associations]) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/brainstem/presenter_validator.rb', line 51

def associations_exist(associations = presenter_class.configuration[:associations])
  associations.each do |name, association|
    method_name = association.method_name

    if !association.polymorphic? && !Brainstem.presenter_collection.for(association.target_class)
      errors.add(:associations, "'#{name}' is not valid because no presenter could be found for the #{association.target_class} class")
    end

    if method_name && presenter_class.presents.any? { |klass| !klass.new.respond_to?(method_name) }
      errors.add(:associations, "'#{name}' is not valid because not all presented classes respond to '#{method_name}'")
    end
  end
end

#brainstem_key_is_providedObject



102
103
104
105
106
# File 'lib/brainstem/presenter_validator.rb', line 102

def brainstem_key_is_provided
  if !presenter_class.configuration[:brainstem_key] && presenter_class.presents.length > 1
    errors.add(:brainstem_key, "a brainstem_key must be provided when multiple classes are presented.")
  end
end

#conditionals_exist(fields = presenter_class.configuration[:fields]) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/brainstem/presenter_validator.rb', line 65

def conditionals_exist(fields = presenter_class.configuration[:fields])
  fields.each do |name, field|
    case field
      when DSL::HashBlockField
        if field.options[:if].present?
          if Array.wrap(field.options[:if]).any? { |conditional| presenter_class.configuration[:conditionals][conditional].nil? }
            errors.add(:fields, "'#{name}' is not valid because one or more of the specified conditionals does not exist")
          end
        end
        conditionals_exist(field) if presenter_class.presents.any? { |klass| !field.executable? }
      when DSL::Field
        if field.options[:if].present?
          if Array.wrap(field.options[:if]).any? { |conditional| presenter_class.configuration[:conditionals][conditional].nil? }
            errors.add(:fields, "'#{name}' is not valid because one or more of the specified conditionals does not exist")
          end
        end
      else
        errors.add(:fields, "'#{name}' is an unknown Brainstem field type")
    end
  end
end

#default_sort_is_usedObject



87
88
89
90
91
# File 'lib/brainstem/presenter_validator.rb', line 87

def default_sort_is_used
  if presenter_class.configuration[:sort_orders].length > 0 && presenter_class.configuration[:default_sort_order].blank?
    errors.add(:default_sort_order, "A default_sort_order is highly recommended if any sort_orders are declared")
  end
end

#default_sort_matches_sort_orderObject



93
94
95
96
97
98
99
100
# File 'lib/brainstem/presenter_validator.rb', line 93

def default_sort_matches_sort_order
  if presenter_class.configuration[:default_sort_order].present?
    default_sort_order = presenter_class.configuration[:default_sort_order].split(":").first.to_sym
    if !presenter_class.configuration[:sort_orders][default_sort_order]
      errors.add(:default_sort_order, "The declared default_sort_order ('#{default_sort_order}') does not match an existing sort_order")
    end
  end
end

#fields_exist(fields = presenter_class.configuration[:fields]) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brainstem/presenter_validator.rb', line 35

def fields_exist(fields = presenter_class.configuration[:fields])
  fields.each do |name, field|
    case field
      when DSL::HashBlockField
        fields_exist(field) if presenter_class.presents.any? { !field.executable? }
      when DSL::Field
        method_name = field.method_name
        if method_name && presenter_class.presents.any? { |klass| !klass.new.respond_to?(method_name) }
          errors.add(:fields, "'#{name}' is not valid because not all presented classes respond to '#{method_name}'")
        end
      else
        errors.add(:fields, "'#{name}' is an unknown Brainstem field type")
    end
  end
end

#preloads_existObject



25
26
27
28
29
30
31
32
33
# File 'lib/brainstem/presenter_validator.rb', line 25

def preloads_exist
  presenter_class.configuration[:preloads].each do |preload|
    Array(preload.is_a?(Hash) ? preload.keys : preload).each do |association_name|
      if presenter_class.presents.any? { |klass| !klass.new.respond_to?(association_name) }
        errors.add(:preload, "not all presented classes respond to '#{association_name}'")
      end
    end
  end
end