Sha256: 08610b18bec0498d39c696a84028ef87e1f9a52aaa0672b16d1e73506bd0b4c6

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 KB

Contents

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

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

          def description
            "dependent => #{dependent}"
          end

          def matches?(subject)
            self.subject = ModelReflector.new(subject, name)

            if option_matches?
              true
            else
              self.missing_option = generate_missing_option
              false
            end
          end

          protected

          attr_accessor :subject, :dependent, :name

          private

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

          def option_matches?
            option_verifier.correct_for?(option_type, :dependent, dependent)
          end

          def option_type
            case dependent
            when true, false then :boolean
            else :string
            end
          end

          def generate_missing_option
            [
              "#{name} should have",
              (dependent == true ? 'a' : dependent),
              'dependency',
            ].join(' ')
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-6.0.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-5.3.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-5.2.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-5.1.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-5.0.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-5.0.0.rc1 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-4.5.1 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb
shoulda-matchers-4.5.0 lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb