Sha256: e7305ca50629cb55588c03401bed38480eae2e9b73133dd1adcd26d9585d5c4a

Contents?: true

Size: 1.48 KB

Versions: 153

Compression:

Stored size: 1.48 KB

Contents

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

      # Ensures that the attribute cannot be changed once the record has been
      # created.
      #
      #   it { should have_readonly_attributes(: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_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

153 entries across 123 versions & 25 rubygems

Version Path
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-2.11.3/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/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-2.11.3/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
Flamefork-shoulda-2.10.1 lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
Flamefork-shoulda-2.10.2 lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.0 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.1 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.10 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.11 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.12 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.13 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.14 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.15 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.16 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.17 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.2 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.3 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.4 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb
auser-poolparty-1.3.5 vendor/gems/shoulda/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb