carlosbrando / remarkable

Simplifying tests!

This URL has Read+Write access

remarkable / remarkable_activerecord / lib / remarkable_activerecord / matchers / allow_mass_assignment_of_matcher.rb
100644 35 lines (29 sloc) 1.025 kb
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
module Remarkable
  module ActiveRecord
    module Matchers
      class AllowMassAssignmentOfMatcher < Remarkable::ActiveRecord::Base #:nodoc:
        arguments :collection => :attributes, :as => :attribute
 
        collection_assertions :is_protected?, :is_accessible?
 
        protected
 
          def is_protected?
            protected = subject_class.protected_attributes || []
            protected.empty? || !protected.include?(@attribute.to_s)
          end
 
          def is_accessible?
            accessible = subject_class.accessible_attributes || []
            accessible.empty? || accessible.include?(@attribute.to_s)
          end
      end
 
      # Ensures that the attribute can be set on mass update.
      #
      # == Examples
      #
      # should_allow_mass_assignment_of :email, :name
      # it { should allow_mass_assignment_of(:email, :name) }
      #
      def allow_mass_assignment_of(*attributes)
        AllowMassAssignmentOfMatcher.new(*attributes).spec(self)
      end
    end
  end
end