Sha256: eb15703efd214eeb0ef81cbd478fc81e0488a9c02c338f8d9374d0b17db07298

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

module Mongoid
  module Matchers # :nodoc:

    # Ensures that the model can accept nested attributes for the specified
    # association.
    #
    # Example:
    #   it { should accept_nested_attributes_for(:articles) }
    #
    def accept_nested_attributes_for(attribute)
      AcceptNestedAttributesForMatcher.new(attribute)
    end

    class AcceptNestedAttributesForMatcher

      def initialize(attribute)
        @attribute = attribute.to_s
        @options = {}
      end

      def matches?(subject)
        @subject = subject
        match?
      end

      def failure_message
        "Expected #{expectation} (#{@problem})"
      end

      def negative_failure_message
        "Did not expect #{expectation}"
      end

      alias :failure_message_when_negated :negative_failure_message

      def description
        description = "accepts_nested_attributes_for :#{@attribute}"
      end

      protected
        def match?
          exists?
        end

        def exists?
          if config
            true
          else
            @problem = 'is not declared'
            false
          end
        end

        def config
          model_class.nested_attributes["#{@attribute}_attributes"]
        end

        def model_class
          @subject.class
        end

        def expectation
          "#{model_class.name} to accept nested attributes for #{@attribute}"
        end
    end
  end
end

Version data entries

9 entries across 9 versions & 4 rubygems

Version Path
mongoid-spec-4.0.1 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-3.0.0 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-2.2.0 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-2.1.0 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-1.13.0 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-2.0.0 lib/matchers/accept_nested_attributes.rb
mongoid-rspec-2.0.0.rc1 lib/matchers/accept_nested_attributes.rb
glebtv-mongoid-rspec-3.0.0 lib/matchers/accept_nested_attributes.rb
mongoid4-rspec-1.11.0 lib/matchers/accept_nested_attributes.rb