carlosbrando-remarkable 0.0.99 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/History.txt +5 -5
  2. data/Manifest.txt +53 -35
  3. data/PostInstall.txt +1 -6
  4. data/README.rdoc +109 -15
  5. data/Rakefile +29 -29
  6. data/lib/remarkable/active_record/README.markdown +378 -0
  7. data/lib/remarkable/active_record/active_record.rb +12 -11
  8. data/lib/remarkable/active_record/helpers.rb +215 -5
  9. data/lib/remarkable/active_record/macros/associations/association_matcher.rb +242 -0
  10. data/lib/remarkable/active_record/macros/callbacks/callback_matcher.rb +46 -0
  11. data/lib/remarkable/active_record/macros/database/column_matcher.rb +122 -0
  12. data/lib/remarkable/active_record/macros/database/index_matcher.rb +103 -0
  13. data/lib/remarkable/active_record/macros/validations/allow_mass_assignment_of_matcher.rb +52 -0
  14. data/lib/remarkable/active_record/macros/validations/ensure_value_in_list_matcher.rb +83 -0
  15. data/lib/remarkable/active_record/macros/validations/ensure_value_in_range_matcher.rb +172 -0
  16. data/lib/remarkable/active_record/macros/validations/have_class_methods_matcher.rb +54 -0
  17. data/lib/remarkable/active_record/macros/validations/have_instance_methods_matcher.rb +54 -0
  18. data/lib/remarkable/active_record/macros/validations/have_named_scope_matcher.rb +94 -0
  19. data/lib/remarkable/active_record/macros/validations/have_readonly_attributes_matcher.rb +48 -0
  20. data/lib/remarkable/active_record/macros/validations/protect_attributes_matcher.rb +51 -0
  21. data/lib/remarkable/active_record/macros/validations/validate_acceptance_of_matcher.rb +79 -0
  22. data/lib/remarkable/active_record/macros/validations/validate_associated_matcher.rb +177 -0
  23. data/lib/remarkable/active_record/macros/validations/validate_confirmation_of_matcher.rb +74 -0
  24. data/lib/remarkable/active_record/macros/validations/validate_exclusion_of_matcher.rb +38 -0
  25. data/lib/remarkable/active_record/macros/validations/validate_format_of_matcher.rb +33 -0
  26. data/lib/remarkable/active_record/macros/validations/validate_inclusion_of_matcher.rb +45 -0
  27. data/lib/remarkable/active_record/macros/validations/validate_length_of_matcher.rb +248 -0
  28. data/lib/remarkable/active_record/macros/validations/validate_numericality_of_matcher.rb +206 -0
  29. data/lib/remarkable/active_record/macros/validations/validate_presence_of_matcher.rb +72 -0
  30. data/lib/remarkable/active_record/macros/validations/validate_uniqueness_of_matcher.rb +222 -0
  31. data/lib/remarkable/active_record/macros.rb +52 -0
  32. data/lib/remarkable/assertions.rb +29 -0
  33. data/lib/remarkable/controller/README.markdown +147 -0
  34. data/lib/remarkable/controller/controller.rb +11 -6
  35. data/lib/remarkable/controller/helpers.rb +4 -38
  36. data/lib/remarkable/controller/macros/assign_matcher.rb +85 -0
  37. data/lib/remarkable/controller/macros/filter_params_matcher.rb +63 -0
  38. data/lib/remarkable/controller/macros/metadata_matcher.rb +63 -0
  39. data/lib/remarkable/controller/macros/render_with_layout_matcher.rb +75 -0
  40. data/lib/remarkable/controller/macros/respond_with_content_type_matcher.rb +60 -0
  41. data/lib/remarkable/controller/macros/respond_with_matcher.rb +62 -0
  42. data/lib/remarkable/controller/macros/return_from_session_matcher.rb +58 -0
  43. data/lib/remarkable/controller/macros/route_matcher.rb +75 -0
  44. data/lib/remarkable/controller/macros/set_the_flash_to_matcher.rb +60 -0
  45. data/lib/remarkable/controller/macros.rb +78 -0
  46. data/lib/remarkable/dsl.rb +239 -0
  47. data/lib/remarkable/example/example_methods.rb +27 -7
  48. data/lib/remarkable/helpers.rb +28 -0
  49. data/lib/remarkable/matcher_base.rb +64 -0
  50. data/lib/remarkable/private_helpers.rb +10 -115
  51. data/lib/remarkable/rails.rb +27 -0
  52. data/lib/remarkable.rb +13 -5
  53. data/remarkable.gemspec +43 -0
  54. data/spec/controllers/posts_controller_spec.rb +58 -4
  55. data/spec/controllers/users_controller_spec.rb +1 -0
  56. data/spec/fixtures/fleas.yml +10 -0
  57. data/spec/fixtures/users.yml +7 -0
  58. data/spec/models/address_spec.rb +44 -0
  59. data/spec/models/dog_spec.rb +64 -3
  60. data/spec/models/flea_spec.rb +30 -0
  61. data/spec/models/post_spec.rb +36 -2
  62. data/spec/models/product_spec.rb +73 -8
  63. data/spec/models/tag_spec.rb +2 -2
  64. data/spec/models/tagging_spec.rb +24 -0
  65. data/spec/models/user_spec.rb +206 -21
  66. data/spec/other/custom_macros_spec.rb +27 -0
  67. data/spec/other/my_own_matcher_spec.rb +11 -0
  68. data/spec/other/private_helpers_spec.rb +31 -0
  69. data/spec/rails_root/app/controllers/posts_controller.rb +2 -0
  70. data/spec/rails_root/app/models/address.rb +2 -2
  71. data/spec/rails_root/app/models/flea.rb +4 -0
  72. data/spec/rails_root/app/models/pets/dog.rb +12 -0
  73. data/spec/rails_root/app/models/product.rb +7 -5
  74. data/spec/rails_root/app/models/tagging.rb +2 -0
  75. data/spec/rails_root/app/models/user.rb +20 -5
  76. data/spec/rails_root/app/views/layouts/posts.rhtml +8 -6
  77. data/spec/rails_root/config/database.yml +1 -2
  78. data/spec/rails_root/config/environment.rb +3 -1
  79. data/spec/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  80. data/spec/rails_root/config/locales/en.yml +8 -0
  81. data/spec/rails_root/db/migrate/001_create_users.rb +2 -2
  82. data/spec/rails_root/db/migrate/005_create_dogs.rb +1 -0
  83. data/spec/rails_root/db/migrate/011_add_fleas_color.rb +10 -0
  84. data/spec/rails_root/db/migrate/012_add_fleas_address.rb +10 -0
  85. data/spec/rails_root/spec/remarkable_macros/.keep +0 -0
  86. data/spec/rails_root/vendor/plugins/my_plugin/remarkable_macros/.keep +0 -0
  87. data/spec/spec_helper.rb +0 -2
  88. metadata +63 -43
  89. data/lib/remarkable/active_record/macros/associations/belong_to.rb +0 -81
  90. data/lib/remarkable/active_record/macros/associations/have_and_belong_to_many.rb +0 -77
  91. data/lib/remarkable/active_record/macros/associations/have_many.rb +0 -160
  92. data/lib/remarkable/active_record/macros/associations/have_one.rb +0 -133
  93. data/lib/remarkable/active_record/macros/database/have_db_column.rb +0 -81
  94. data/lib/remarkable/active_record/macros/database/have_db_columns.rb +0 -73
  95. data/lib/remarkable/active_record/macros/database/have_indices.rb +0 -75
  96. data/lib/remarkable/active_record/macros/validations/allow_values_for.rb +0 -103
  97. data/lib/remarkable/active_record/macros/validations/ensure_length_at_least.rb +0 -97
  98. data/lib/remarkable/active_record/macros/validations/ensure_length_in_range.rb +0 -134
  99. data/lib/remarkable/active_record/macros/validations/ensure_length_is.rb +0 -106
  100. data/lib/remarkable/active_record/macros/validations/ensure_value_in_range.rb +0 -117
  101. data/lib/remarkable/active_record/macros/validations/have_class_methods.rb +0 -74
  102. data/lib/remarkable/active_record/macros/validations/have_instance_methods.rb +0 -74
  103. data/lib/remarkable/active_record/macros/validations/have_named_scope.rb +0 -148
  104. data/lib/remarkable/active_record/macros/validations/have_readonly_attributes.rb +0 -81
  105. data/lib/remarkable/active_record/macros/validations/only_allow_numeric_values_for.rb +0 -89
  106. data/lib/remarkable/active_record/macros/validations/protect_attributes.rb +0 -89
  107. data/lib/remarkable/active_record/macros/validations/require_acceptance_of.rb +0 -94
  108. data/lib/remarkable/active_record/macros/validations/require_attributes.rb +0 -94
  109. data/lib/remarkable/active_record/macros/validations/require_unique_attributes.rb +0 -146
  110. data/lib/remarkable/controller/macros/assign_to.rb +0 -110
  111. data/lib/remarkable/controller/macros/filter_params.rb +0 -52
  112. data/lib/remarkable/controller/macros/redirect_to.rb +0 -24
  113. data/lib/remarkable/controller/macros/render_a_form.rb +0 -23
  114. data/lib/remarkable/controller/macros/render_template.rb +0 -18
  115. data/lib/remarkable/controller/macros/render_with_layout.rb +0 -61
  116. data/lib/remarkable/controller/macros/respond_with.rb +0 -86
  117. data/lib/remarkable/controller/macros/respond_with_content_type.rb +0 -45
  118. data/lib/remarkable/controller/macros/return_from_session.rb +0 -45
  119. data/lib/remarkable/controller/macros/route.rb +0 -91
  120. data/lib/remarkable/controller/macros/set_the_flash_to.rb +0 -58
  121. data/spec/rails_root/app/models/dog.rb +0 -5
@@ -1,148 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveNamedScope
6
- def initialize(scope_call, *args)
7
- @scope_opts = args.extract_options!
8
- @scope_call = scope_call.to_s
9
- @args = args
10
- end
11
-
12
- def matches?(klass)
13
- @klass = klass
14
-
15
- begin
16
- scope = eval("#{klass}.#{@scope_call}")
17
-
18
- unless scope.class == ::ActiveRecord::NamedScope::Scope
19
- fail "#{@scope_call} didn't return a scope object"
20
- end
21
-
22
- unless @scope_opts.empty?
23
- unless scope.proxy_options == @scope_opts
24
- fail "#{klass.name} didn't scope itself to #{@scope_opts.inspect}"
25
- end
26
- end
27
-
28
- true
29
- rescue Exception => e
30
- false
31
- end
32
- end
33
-
34
- def description
35
- "have to scope itself to #{@scope_opts.inspect} when #{@scope_call} is called"
36
- end
37
-
38
- def failure_message
39
- @failure_message || "expected that #{klass.name} has a method named #{@scope_call} that returns a NamedScope object with the proxy options set to the options you supply, but it didn't"
40
- end
41
-
42
- def negative_failure_message
43
- "expected that #{klass.name} hasn't a method named #{@scope_call} that returns a NamedScope object with the proxy options set to the options you supply, but it did"
44
- end
45
- end
46
-
47
- # Ensures that the model has a method named scope_name that returns a NamedScope object with the
48
- # proxy options set to the options you supply. scope_name can be either a symbol, or a method
49
- # call which will be evaled against the model. The eval'd method call has access to all the same
50
- # instance variables that a should statement would.
51
- #
52
- # Options: Any of the options that the named scope would pass on to find.
53
- #
54
- # Example:
55
- #
56
- # it { User.should have_named_scope(:visible, :conditions => {:visible => true}) }
57
- #
58
- # Passes for
59
- #
60
- # named_scope :visible, :conditions => {:visible => true}
61
- #
62
- # Or for
63
- #
64
- # def self.visible
65
- # scoped(:conditions => {:visible => true})
66
- # end
67
- #
68
- # You can test lambdas or methods that return ActiveRecord#scoped calls:
69
- #
70
- # it { User.should have_named_scope('recent(5)', :limit => 5) }
71
- # it { User.should have_named_scope('recent(1)', :limit => 1) }
72
- #
73
- # Passes for
74
- # named_scope :recent, lambda {|c| {:limit => c}}
75
- #
76
- # Or for
77
- #
78
- # def self.recent(c)
79
- # scoped(:limit => c)
80
- # end
81
- #
82
- def have_named_scope(scope_call, *args)
83
- Remarkable::Syntax::RSpec::HaveNamedScope.new(scope_call, *args)
84
- end
85
- end
86
-
87
- module Shoulda
88
- # Ensures that the model has a method named scope_name that returns a NamedScope object with the
89
- # proxy options set to the options you supply. scope_name can be either a symbol, or a method
90
- # call which will be evaled against the model. The eval'd method call has access to all the same
91
- # instance variables that a should statement would.
92
- #
93
- # Options: Any of the options that the named scope would pass on to find.
94
- #
95
- # Example:
96
- #
97
- # should_have_named_scope :visible, :conditions => {:visible => true}
98
- #
99
- # Passes for
100
- #
101
- # named_scope :visible, :conditions => {:visible => true}
102
- #
103
- # Or for
104
- #
105
- # def self.visible
106
- # scoped(:conditions => {:visible => true})
107
- # end
108
- #
109
- # You can test lambdas or methods that return ActiveRecord#scoped calls:
110
- #
111
- # should_have_named_scope 'recent(5)', :limit => 5
112
- # should_have_named_scope 'recent(1)', :limit => 1
113
- #
114
- # Passes for
115
- # named_scope :recent, lambda {|c| {:limit => c}}
116
- #
117
- # Or for
118
- #
119
- # def self.recent(c)
120
- # scoped(:limit => c)
121
- # end
122
- #
123
- def should_have_named_scope(scope_call, *args)
124
- klass = model_class
125
- scope_opts = args.extract_options!
126
- scope_call = scope_call.to_s
127
-
128
- describe scope_call do
129
- before(:each) do
130
- @scope = eval("#{klass}.#{scope_call}")
131
- end
132
-
133
- it "should return a scope object" do
134
- @scope.class.should == ::ActiveRecord::NamedScope::Scope
135
- end
136
-
137
- unless scope_opts.empty?
138
- it "should scope itself to #{scope_opts.inspect}" do
139
- @scope.proxy_options.should == scope_opts
140
- end
141
- end
142
- end
143
- end
144
-
145
- end
146
-
147
- end
148
- end
@@ -1,81 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveReadonlyAttributes
6
- include Remarkable::Private
7
-
8
- def initialize(*attributes)
9
- get_options!(attributes)
10
- @attributes = attributes
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass
15
-
16
- begin
17
- @attributes.each do |attribute|
18
- attribute = attribute.to_sym
19
- readonly = klass.readonly_attributes || []
20
-
21
- unless readonly.include?(attribute.to_s)
22
- fail readonly.empty? ?
23
- "#{klass} attribute #{attribute} is not read-only" :
24
- "#{klass} is making #{readonly.to_a.to_sentence} read-only, but not #{attribute}"
25
- end
26
- end
27
-
28
- true
29
- rescue Exception => e
30
- false
31
- end
32
- end
33
-
34
- def description
35
- "make #{@attributes.to_sentence} read-only"
36
- end
37
-
38
- def failure_message
39
- @failure_message || "expected that #{@attributes.to_sentence} cannot be changed once the record has been created, but it did"
40
- end
41
-
42
- def negative_failure_message
43
- "expected that #{@attributes.to_sentence} cann be changed once the record has been created, but it didn't"
44
- end
45
- end
46
-
47
- # Ensures that the attribute cannot be changed once the record has been created.
48
- #
49
- # it { User.should have_readonly_attributes(:password, :admin_flag) }
50
- #
51
- def have_readonly_attributes(*attributes)
52
- Remarkable::Syntax::RSpec::HaveReadonlyAttributes.new(*attributes)
53
- end
54
- end
55
-
56
- module Shoulda
57
- # Ensures that the attribute cannot be changed once the record has been created.
58
- #
59
- # should_have_readonly_attributes :password, :admin_flag
60
- #
61
- def should_have_readonly_attributes(*attributes)
62
- get_options!(attributes)
63
- klass = model_class
64
-
65
- attributes.each do |attribute|
66
- attribute = attribute.to_sym
67
- it "should make #{attribute} read-only" do
68
- readonly = klass.readonly_attributes || []
69
-
70
- unless readonly.include?(attribute.to_s)
71
- fail_with(readonly.empty? ?
72
- "#{klass} attribute #{attribute} is not read-only" :
73
- "#{klass} is making #{readonly.to_a.to_sentence} read-only, but not #{attribute}.")
74
- end
75
- end
76
- end
77
- end
78
- end
79
-
80
- end
81
- end
@@ -1,89 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class OnlyAllowNumericValuesFor
6
- include Remarkable::Private
7
-
8
- def initialize(*attributes)
9
- @message = get_options!(attributes, :message)
10
- @message ||= default_error_message(:not_a_number)
11
-
12
- @attributes = attributes
13
- end
14
-
15
- def matches?(klass)
16
- @klass = klass
17
-
18
- begin
19
- @attributes.each do |attribute|
20
- attribute = attribute.to_sym
21
- return false unless assert_bad_value(klass, attribute, "abcd", @message)
22
- end
23
-
24
- true
25
- rescue Exception => e
26
- false
27
- end
28
- end
29
-
30
- def description
31
- "only allow numeric values for #{@attributes.to_sentence}"
32
- end
33
-
34
- def failure_message
35
- "expected only numeric values for #{@attributes.to_sentence}, but it didn't"
36
- end
37
-
38
- def negative_failure_message
39
- "expected not only numeric values for #{@attributes.to_sentence}, but it did"
40
- end
41
- end
42
-
43
- # Ensure that the attribute is numeric
44
- #
45
- # If an instance variable has been created in the setup named after the
46
- # model being tested, then this method will use that. Otherwise, it will
47
- # create a new instance to test against.
48
- #
49
- # Options:
50
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
51
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
52
- #
53
- # Example:
54
- # it { User.should only_allow_numeric_values_for(:age) }
55
- #
56
- def only_allow_numeric_values_for(*attributes)
57
- Remarkable::Syntax::RSpec::OnlyAllowNumericValuesFor.new(*attributes)
58
- end
59
- end
60
-
61
- module Shoulda
62
- # Ensure that the attribute is numeric
63
- #
64
- # If an instance variable has been created in the setup named after the
65
- # model being tested, then this method will use that. Otherwise, it will
66
- # create a new instance to test against.
67
- #
68
- # Options:
69
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
70
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
71
- #
72
- # Example:
73
- # should_only_allow_numeric_values_for :age
74
- #
75
- def should_only_allow_numeric_values_for(*attributes)
76
- message = get_options!(attributes, :message)
77
- message ||= default_error_message(:not_a_number)
78
- klass = model_class
79
- attributes.each do |attribute|
80
- attribute = attribute.to_sym
81
- it "should only allow numeric values for #{attribute}" do
82
- assert_bad_value(klass, attribute, "abcd", message).should be_true
83
- end
84
- end
85
- end
86
- end
87
-
88
- end
89
- end
@@ -1,89 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class ProtectAttributes
6
- include Remarkable::Private
7
-
8
- def initialize(*attributes)
9
- get_options!(attributes)
10
- @attributes = attributes
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass
15
-
16
- begin
17
- @attributes.each do |attribute|
18
- attribute = attribute.to_sym
19
- protected = klass.protected_attributes || []
20
- accessible = klass.accessible_attributes || []
21
-
22
- unless protected.include?(attribute.to_s) || (!accessible.empty? && !accessible.include?(attribute.to_s))
23
- message = if accessible.empty?
24
- "#{klass} is protecting #{protected.to_a.to_sentence}, but not #{attribute}."
25
- else
26
- "#{klass} has made #{attribute} accessible"
27
- end
28
- fail(message)
29
- end
30
- end
31
-
32
- true
33
- rescue Exception => e
34
- false
35
- end
36
- end
37
-
38
- def description
39
- "protect #{@attributes.to_sentence} from mass updates"
40
- end
41
-
42
- def failure_message
43
- "expected that #{@attributes.to_sentence} cannot be set on mass update, but it did"
44
- end
45
-
46
- def negative_failure_message
47
- "expected that #{@attributes.to_sentence} can be set on mass update, but it didn't"
48
- end
49
- end
50
-
51
- # Ensures that the attribute cannot be set on mass update.
52
- #
53
- # it { User.should protect_attributes(:password, :admin_flag) }
54
- #
55
- def protect_attributes(*attributes)
56
- Remarkable::Syntax::RSpec::ProtectAttributes.new(*attributes)
57
- end
58
- end
59
-
60
- module Shoulda
61
- # Ensures that the attribute cannot be set on mass update.
62
- #
63
- # should_protect_attributes :password, :admin_flag
64
- #
65
- def should_protect_attributes(*attributes)
66
- get_options!(attributes)
67
- klass = model_class
68
-
69
- attributes.each do |attribute|
70
- attribute = attribute.to_sym
71
- it "should protect #{attribute} from mass updates" do
72
- protected = klass.protected_attributes || []
73
- accessible = klass.accessible_attributes || []
74
-
75
- unless protected.include?(attribute.to_s) || (!accessible.empty? && !accessible.include?(attribute.to_s))
76
- message = if accessible.empty?
77
- "#{klass} is protecting #{protected.to_a.to_sentence}, but not #{attribute}."
78
- else
79
- "#{klass} has made #{attribute} accessible"
80
- end
81
- fail_with(message)
82
- end
83
- end
84
- end
85
- end
86
- end
87
-
88
- end
89
- end
@@ -1,94 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class RequireAcceptanceOf
6
- include Remarkable::Private
7
-
8
- def initialize(*attributes)
9
- @message = get_options!(attributes, :message)
10
- @message ||= default_error_message(:accepted)
11
-
12
- @attributes = attributes
13
- end
14
-
15
- def matches?(klass)
16
- @klass = klass
17
-
18
- begin
19
- @attributes.each do |attribute|
20
- return false unless assert_bad_value(klass, attribute, false, @message)
21
- end
22
-
23
- true
24
- rescue Exception => e
25
- false
26
- end
27
- end
28
-
29
- def description
30
- "require #{@attributes.to_sentence} to be accepted"
31
- end
32
-
33
- def failure_message
34
- message = "expected that the #{@klass.name} cannot be saved if #{@attributes.to_sentence} "
35
- message += @attributes.size > 1 ? "are" : "is"
36
- message += " not accepted, but it did"
37
- message
38
- end
39
-
40
- def negative_failure_message
41
- message = "expected that the #{@klass.name} can be saved if #{@attributes.to_sentence} "
42
- message += @attributes.size > 1 ? "are" : "is"
43
- message += " not accepted, but it didn't"
44
- message
45
- end
46
- end
47
-
48
- # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
49
- #
50
- # If an instance variable has been created in the setup named after the
51
- # model being tested, then this method will use that. Otherwise, it will
52
- # create a new instance to test against.
53
- #
54
- # Options:
55
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
56
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
57
- #
58
- # Example:
59
- # it { User.should require_acceptance_of(:eula) }
60
- #
61
- def require_acceptance_of(*attributes)
62
- Remarkable::Syntax::RSpec::RequireAcceptanceOf.new(*attributes)
63
- end
64
- end
65
-
66
- module Shoulda
67
- # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
68
- #
69
- # If an instance variable has been created in the setup named after the
70
- # model being tested, then this method will use that. Otherwise, it will
71
- # create a new instance to test against.
72
- #
73
- # Options:
74
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
75
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
76
- #
77
- # Example:
78
- # should_require_acceptance_of :eula
79
- #
80
- def should_require_acceptance_of(*attributes)
81
- message = get_options!(attributes, :message)
82
- message ||= default_error_message(:accepted)
83
- klass = model_class
84
-
85
- attributes.each do |attribute|
86
- it "should require #{attribute} to be accepted" do
87
- assert_bad_value(klass, attribute, false, message).should be_true
88
- end
89
- end
90
- end
91
- end
92
-
93
- end
94
- end
@@ -1,94 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class RequireAttributes
6
- include Remarkable::Private
7
-
8
- def initialize(*attributes)
9
- @message = get_options!(attributes, :message)
10
- @message ||= default_error_message(:blank)
11
-
12
- @attributes = attributes
13
- end
14
-
15
- def matches?(klass)
16
- @klass = klass
17
-
18
- begin
19
- @attributes.each do |attribute|
20
- return false unless assert_bad_value(klass, attribute, nil, @message)
21
- end
22
-
23
- true
24
- rescue Exception => e
25
- false
26
- end
27
- end
28
-
29
- def description
30
- "require #{@attributes.to_sentence} to be set"
31
- end
32
-
33
- def failure_message
34
- message = "expected that the #{@klass.name} cannot be saved if #{@attributes.to_sentence} "
35
- message += @attributes.size > 1 ? "are" : "is"
36
- message += " not present, but it did"
37
- message
38
- end
39
-
40
- def negative_failure_message
41
- message = "expected that the #{@klass.name} can be saved if #{@attributes.to_sentence} "
42
- message += @attributes.size > 1 ? "are" : "is"
43
- message += " not present, but it didn't"
44
- message
45
- end
46
- end
47
-
48
- # Ensures that the model cannot be saved if one of the attributes listed is not present.
49
- #
50
- # If an instance variable has been created in the setup named after the
51
- # model being tested, then this method will use that. Otherwise, it will
52
- # create a new instance to test against.
53
- #
54
- # Options:
55
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
56
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
57
- #
58
- # Example:
59
- # it { User.should require_attributes(:name, :phone_number) }
60
- #
61
- def require_attributes(*attributes)
62
- Remarkable::Syntax::RSpec::RequireAttributes.new(*attributes)
63
- end
64
- end
65
-
66
- module Shoulda
67
- # Ensures that the model cannot be saved if one of the attributes listed is not present.
68
- #
69
- # If an instance variable has been created in the setup named after the
70
- # model being tested, then this method will use that. Otherwise, it will
71
- # create a new instance to test against.
72
- #
73
- # Options:
74
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
75
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
76
- #
77
- # Example:
78
- # should_require_attributes :name, :phone_number
79
- #
80
- def should_require_attributes(*attributes)
81
- message = get_options!(attributes, :message)
82
- message ||= default_error_message(:blank)
83
- klass = model_class
84
-
85
- attributes.each do |attribute|
86
- it "should require #{attribute} to be set" do
87
- assert_bad_value(klass, attribute, nil, message).should be_true
88
- end
89
- end
90
- end
91
- end
92
-
93
- end
94
- end