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



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

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



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brainstem/presenter_validator.rb', line 63

def conditionals_exist(fields = presenter_class.configuration[:fields])
  fields.each do |name, field_or_fields|
    case field_or_fields
      when DSL::Field
        if field_or_fields.options[:if].present?
          if Array.wrap(field_or_fields.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
      when DSL::Configuration
        conditionals_exist(field_or_fields)
    end
  end
end

#default_sort_is_usedObject



78
79
80
81
82
# File 'lib/brainstem/presenter_validator.rb', line 78

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



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

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
# File 'lib/brainstem/presenter_validator.rb', line 35

def fields_exist(fields = presenter_class.configuration[:fields])
  fields.each do |name, field_or_fields|
    case field_or_fields
      when DSL::Field
        method_name = field_or_fields.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
      when DSL::Configuration
        fields_exist(field_or_fields)
    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