Sha256: 9838d232a9c9dc35f64bf87a2ae83b79b36ba8ad4c67d36818d023d8efe247c0

Contents?: true

Size: 1.65 KB

Versions: 21

Compression:

Stored size: 1.65 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?(database_field_name(name))
      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 << database_field_name(name)
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 19 versions & 3 rubygems

Version Path
mongoid-5.4.1 lib/mongoid/attributes/readonly.rb
mongoid-5.4.0 lib/mongoid/attributes/readonly.rb
mongoid-5.2.1 lib/mongoid/attributes/readonly.rb
mongoid-5.2.0 lib/mongoid/attributes/readonly.rb
mongoid-5.2.0.rc0 lib/mongoid/attributes/readonly.rb
mongoid-5.1.6 lib/mongoid/attributes/readonly.rb
mongoid-5.1.5 lib/mongoid/attributes/readonly.rb
mongoid-5.1.4 lib/mongoid/attributes/readonly.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-5.1.3/lib/mongoid/attributes/readonly.rb
mongoid-5.1.3 lib/mongoid/attributes/readonly.rb
mongoid-5.1.2 lib/mongoid/attributes/readonly.rb
mongoid-5.1.1 lib/mongoid/attributes/readonly.rb
mongoid-5.1.0 lib/mongoid/attributes/readonly.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/attributes/readonly.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/attributes/readonly.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.1/lib/mongoid/attributes/readonly.rb
mongoid-5.0.2 lib/mongoid/attributes/readonly.rb
mongoid-5.0.1 lib/mongoid/attributes/readonly.rb
mongoid-5.0.0 lib/mongoid/attributes/readonly.rb
mongoid-5.0.0.rc0 lib/mongoid/attributes/readonly.rb