Sha256: 8915d8a3f0ce2a3a875b36f281fcf96b9e4092962fc6bff8594069ebb433c92e

Contents?: true

Size: 1.62 KB

Versions: 46

Compression:

Stored size: 1.62 KB

Contents

# encoding: utf-8
module Mongoid
  module Attributes

    # This module defines behaviour for readonly attributes.
    module Readonly
      extend ActiveSupport::Concern

      included do
        class_attribute :readonly_attributes
        self.readonly_attributes = ::Set.new
      end

      # Are we able to write the attribute with the provided name?
      #
      # @example Can we write the attribute?
      #   model.attribute_writable?(:title)
      #
      # @param [ String, Symbol ] name The name of the field.
      #
      # @return [ true, false ] If the document is new, or if the field is not
      #   readonly.
      #
      # @since 3.0.0
      def attribute_writable?(name)
        new_record? || !readonly_attributes.include?(name.to_s)
      end

      module ClassMethods

        # Defines an attribute as readonly. This will ensure that the value for
        # the attribute is only set when the document is new or we are
        # creating. In other cases, the field write will be ignored with the
        # exception of #remove_attribute and #update_attribute, where an error
        # will get raised.
        #
        # @example Flag fields as readonly.
        #   class Band
        #     include Mongoid::Document
        #     field :name, type: String
        #     field :genre, type: String
        #     attr_readonly :name, :genre
        #   end
        #
        # @param [ Array<Symbol> ] names The names of the fields.
        #
        # @since 3.0.0
        def attr_readonly(*names)
          names.each do |name|
            readonly_attributes << name.to_s
          end
        end
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 6 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/attributes/readonly.rb
mongoid-3.1.7 lib/mongoid/attributes/readonly.rb
mongoid-4.0.2 lib/mongoid/attributes/readonly.rb
mongoid-4.0.1 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.rc2 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.rc1 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.beta2 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.beta1 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.alpha2 lib/mongoid/attributes/readonly.rb
mongoid-4.0.0.alpha1 lib/mongoid/attributes/readonly.rb
mongoid-3.1.6 lib/mongoid/attributes/readonly.rb
sepastian-mongoid-rails4-4.0.1.alpha lib/mongoid/attributes/readonly.rb
sepastian-mongoid-rails4-4.0.0.alpha lib/mongoid/attributes/readonly.rb
mongoid-3.1.5 lib/mongoid/attributes/readonly.rb
mongoid_heroku_stable-4.0.0 lib/mongoid/attributes/readonly.rb
mongoid_rails4-4.0.0 lib/mongoid/attributes/readonly.rb
mongoid-3.1.4 lib/mongoid/attributes/readonly.rb
mongoid-3.1.3 lib/mongoid/attributes/readonly.rb
mongoid-3.1.2 lib/mongoid/attributes/readonly.rb