Sha256: 1c4b27187aa983cbd3bdbb7684f16157b71f42f33ad4972a3fa754d089aa81d5

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 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 & 3 rubygems

Version Path
mongo_mapper-0.13.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.13.0 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.13.0.beta2 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.13.0.beta1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.12.0 lib/mongo_mapper/plugins/protected.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.11.2 lib/mongo_mapper/plugins/protected.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.11.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.11.0 lib/mongo_mapper/plugins/protected.rb