/
protected.rb
45 lines (37 loc) · 1.09 KB
/
protected.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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