Sha256: 4eab6183f22f56bd1afc33006cdcaa24ec5b17c409de728978d1eb158b37fbb6

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module ForeignKeyValidation

  class Collector
    attr_accessor :options

    DEFAULT_VALIDATE_AGAINST = :user

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

    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

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

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

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

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

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

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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foreign_key_validation-1.1.0 lib/foreign_key_validation/collector.rb