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