forked from remarkable-rb/remarkable
/
allow_mass_assignment_of_matcher.rb
55 lines (46 loc) · 1.6 KB
/
allow_mass_assignment_of_matcher.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
46
47
48
49
50
51
52
53
54
55
module Remarkable # :nodoc:
module ActiveRecord # :nodoc:
module Matchers # :nodoc:
class AllowMassAssignmentOfMatcher < Remarkable::Matcher::Base
def initialize(*attributes)
attributes.extract_options!
@attributes = attributes
end
def matches?(subject)
@subject = subject
assert_matcher_for(@attributes) do |attribute|
@attribute = attribute
allowed_to_mass_update?
end
end
def description
"allow mass assignment of #{@attributes.to_sentence}"
end
private
def allowed_to_mass_update?
attribute = @attribute.to_sym
protected = subject_class.protected_attributes || []
accessible = subject_class.accessible_attributes || []
if protected.empty?
return true if accessible.empty? || accessible.include?(attribute.to_s)
else
return true unless protected.include?(attribute.to_s)
end
@missing = accessible.empty? ? "#{subject_class} is protecting #{protected.to_a.to_sentence}" :
"#{subject_class} has not made #{attribute} accessible"
return false
end
def expectation
"to allow mass assignment of #{@attribute}"
end
end
# Ensures that the attribute can be set on mass update.
#
# it { should allow_mass_assignment_of(:email, :name) }
#
def allow_mass_assignment_of(*attributes)
AllowMassAssignmentOfMatcher.new(*attributes)
end
end
end
end