Sha256: 0f0b0cd84d20f99cb495c3473dadf8c39e13b001051c4850fac117a7e2683da4

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

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

5 entries across 5 versions & 3 rubygems

Version Path
pwnash-mongo_mapper-0.7.5 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.7.6 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.7.5 lib/mongo_mapper/plugins/protected.rb
mongo_mapper_ign-0.7.4 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.7.4 lib/mongo_mapper/plugins/protected.rb