Sha256: 3f5cb5ba31ea72fbd7d6ba7187d401579f73735f2c1599ef4231ba5cfc8e7421

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

module Remarkable
  module ActiveRecord
    module Matchers
      class ValidateAcceptanceOfMatcher < Remarkable::ActiveRecord::Base #:nodoc:
        arguments :collection => :attributes, :as => :attribute

        optional :accept, :message
        optional :allow_nil, :default => true

        collection_assertions :requires_acceptance?, :accept_is_valid?, :allow_nil?

        default_options :message => :accepted

        protected

          def requires_acceptance?
            bad?(false)
          end

          def accept_is_valid?
            return true unless @options.key?(:accept)
            good?(@options[:accept])
          end

      end

      # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
      #
      # == Options
      #
      # * <tt>:accept</tt> - the expected value to be accepted.
      # * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
      # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
      #   Regexp, string or symbol.  Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
      #
      # == Examples
      #
      #   should_validate_acceptance_of :eula, :terms
      #   should_validate_acceptance_of :eula, :terms, :accept => true
      #
      #   it { should validate_acceptance_of(:eula, :terms) }
      #   it { should validate_acceptance_of(:eula, :terms, :accept => true) }
      #
      def validate_acceptance_of(*attributes, &block)
        ValidateAcceptanceOfMatcher.new(*attributes, &block).spec(self)
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remarkable_activerecord-3.1.13 lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb
remarkable_activerecord-3.1.12 lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb
remarkable_activerecord-3.1.11 lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb
remarkable_activerecord-3.1.10 lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb
remarkable_activerecord-3.1.9 lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb