Sha256: f207fad2429b31bf6b586a1880d5479ec422a825bd962d3535d29dd2bd85cc4b

Contents?: true

Size: 1019 Bytes

Versions: 3

Compression:

Stored size: 1019 Bytes

Contents

module MongoMapper
  module Plugins
    module Accessible
      extend ActiveSupport::Concern

      included do
        class_attribute :accessible_attributes
      end

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

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo_mapper-0.10.0 lib/mongo_mapper/plugins/accessible.rb
mongo_mapper-0.9.2 lib/mongo_mapper/plugins/accessible.rb
mongo_mapper-0.9.1 lib/mongo_mapper/plugins/accessible.rb