Sha256: 541737b4d60bb24322a4027b3f3dbef892a37afe1192b64e564e72b1bc317c79

Contents?: true

Size: 1.51 KB

Versions: 17

Compression:

Stored size: 1.51 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

        attr_reader :failure_message_for_should, :failure_message_for_should_not

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

        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

17 entries across 16 versions & 2 rubygems

Version Path
shoulda-matchers-2.4.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-2.4.0.rc1 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-2.3.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-1.0.0 vendor/bundle/gems/shoulda-matchers-2.2.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-2.2.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-2.1.0/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-2.1.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-1.0.0.beta2 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-2.0.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.6 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.5 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.4 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.2 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.1 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
shoulda-matchers-1.5.0 lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb