Sha256: e4cfc0acf4600d3a069d3334289727fd07f82201719c31a3f6d3e40ee51d98e8

Contents?: true

Size: 1.64 KB

Versions: 22

Compression:

Stored size: 1.64 KB

Contents

module Shoulda
  module Matchers
    module ActiveRecord
      module AssociationMatchers
        # @private
        class ThroughMatcher
          attr_accessor :missing_option

          def initialize(through, name)
            @through = through
            @name = name
            @missing_option = ''
          end

          def description
            "through #{through}"
          end

          def matches?(subject)
            self.subject = ModelReflector.new(subject, name)
            through.nil? || association_set_properly?
          end

          def association_set_properly?
            through_association_exists? && through_association_correct?
          end

          def through_association_exists?
            if through_reflection.present?
              true
            else
              self.missing_option = "#{name} does not have any relationship to #{through}"
              false
            end
          end

          def through_reflection
            @through_reflection ||= subject.reflect_on_association(through)
          end

          def through_association_correct?
            if option_verifier.correct_for_string?(:through, through)
              true
            else
              self.missing_option =
                "Expected #{name} to have #{name} through #{through}, " +
                "but got it through #{option_verifier.actual_value_for(:through)}"
              false
            end
          end

          protected

          attr_accessor :through, :name, :subject

          def option_verifier
            @option_verifier ||= OptionVerifier.new(subject)
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
shoulda-matchers-4.4.1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.4.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.3.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.2.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.1.2 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.1.1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.1.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.0.1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.1.3 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-4.0.0.rc1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.1.2 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.1.1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.1.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.0.1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.0.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-3.0.0.rc1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-2.8.0 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-2.8.0.rc2 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
shoulda-matchers-2.8.0.rc1 lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb