Sha256: 3888bd91fcfe40ad30d25a86b17aff17e61798d41de32655a7e96a727c5cb2bf

Contents?: true

Size: 1.48 KB

Versions: 12

Compression:

Stored size: 1.48 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActiveRecord # :nodoc:

      # Ensures that the attribute cannot be changed once the record has been
      # created.
      #
      #   it { should have_readonly_attribute(:password) }
      #
      def have_readonly_attribute(value)
        HaveReadonlyAttributeMatcher.new(value)
      end

      class HaveReadonlyAttributeMatcher # :nodoc:

        def initialize(attribute)
          @attribute = attribute.to_s
        end

        def matches?(subject)
          @subject = subject
          if readonly_attributes.include?(@attribute)
            @negative_failure_message =
              "Did not expect #{@attribute} to be read-only"
            true
          else
            if readonly_attributes.empty?
              @failure_message = "#{class_name} attribute #{@attribute} " <<
                "is not read-only"
            else
              @failure_message = "#{class_name} is making " <<
                "#{readonly_attributes.to_a.to_sentence} " <<
                "read-only, but not #{@attribute}."
            end
            false
          end
        end

        attr_reader :failure_message, :negative_failure_message

        def description
          "make #{@attribute} read-only"
        end

        private

        def readonly_attributes
          @readonly_attributes ||= (@subject.class.readonly_attributes || [])
        end

        def class_name
          @subject.class.name
        end

      end

    end
  end
end

Version data entries

12 entries across 9 versions & 3 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.6.2 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.6.1 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.6.0 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.5.4 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.5.3 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.5.2 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-0.5.1 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.0.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb