Class: ForeignKeyValidation::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/foreign_key_validation/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Collector

Returns a new instance of Collector.



6
7
8
# File 'lib/foreign_key_validation/collector.rb', line 6

def initialize(opt={})
  self.options = opt
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/foreign_key_validation/collector.rb', line 4

def options
  @options
end

Instance Method Details

#check!Object



10
11
12
13
14
15
# File 'lib/foreign_key_validation/collector.rb', line 10

def check!
  raise Errors::NoReleationFoundError.new(klass.name) if reflection_names.empty?
  raise Errors::NoForeignKeyFoundError.new(validate_against, klass.table_name) unless reflection_names.include?(validate_against)
  raise Errors::UnknownRelationError.new(validate_with) unless validate_with.all? {|k| reflection_names.include?(k)}
  true
end

#klassObject



17
18
19
# File 'lib/foreign_key_validation/collector.rb', line 17

def klass
  @klass ||= options[:klass]
end

#reflection_namesObject



33
34
35
# File 'lib/foreign_key_validation/collector.rb', line 33

def reflection_names
  @reflection_names ||= reflections.map {|r| r.name.to_s }
end

#reflectionsObject



29
30
31
# File 'lib/foreign_key_validation/collector.rb', line 29

def reflections
  @reflections ||= klass.reflect_on_all_associations(:belongs_to)
end

#validate_againstObject



21
22
23
# File 'lib/foreign_key_validation/collector.rb', line 21

def validate_against
  @validate_against ||= (options[:on] || ForeignKeyValidation.configuration.validate_against).to_s
end

#validate_against_keyObject



37
38
39
# File 'lib/foreign_key_validation/collector.rb', line 37

def validate_against_key
  @validate_against_key ||= reflections.select {|r| r.name.to_s == validate_against}.first.try(:foreign_key)
end

#validate_withObject



25
26
27
# File 'lib/foreign_key_validation/collector.rb', line 25

def validate_with
  @validate_with ||= ((Array(options[:with]).map(&:to_s) if options[:with]) || reflection_names).reject {|n| n == validate_against}
end