Sha256: f1935bf10d73a2c4f39b3c6d278700ff45363dc1eff4b1fb14c47e97b0ac5f4d

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Protected
      module ClassMethods
        def attr_protected(*attrs)
          self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
        end

        def protected_attributes
          self.read_inheritable_attribute(:attr_protected)
        end

        def key(*args)
          key = super
          attr_protected key.name.to_sym if key.options[:protected]
          key
        end
      end

      module InstanceMethods
        def assign(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def update_attributes(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def update_attributes!(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def protected_attributes
          self.class.protected_attributes
        end

        protected
          def filter_protected_attrs(attrs)
            return attrs if protected_attributes.blank? || attrs.blank?
            attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
          end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_mapper_ign-0.7.8 lib/mongo_mapper/plugins/protected.rb