Sha256: e41167c8944bd59071cdd1c672b914f4d88e92e9a578be73275faa7a3da5fe62

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

# encoding: UTF-8
require 'set'

module MongoMapper
  module Plugins
    module Protected
      extend ActiveSupport::Concern

      included do
        class_attribute :protected_attributes
      end

      module ClassMethods
        def attr_protected(*attrs)
          raise AccessibleOrProtected.new(name) if try(:accessible_attributes?)
          self.protected_attributes = Set.new(attrs) + (protected_attributes || [])
        end

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

      def attributes=(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

    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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/protected.rb