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,160 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveMany
6
- include Remarkable::Private
7
-
8
- def initialize(*associations)
9
- @through, @dependent = get_options!(associations, :through, :dependent)
10
- @associations = associations
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass
15
-
16
- begin
17
- @associations.each do |association|
18
- reflection = klass.reflect_on_association(association)
19
- fail("#{klass.name} does not have any relationship to #{association}") unless reflection && reflection.macro == :has_many
20
-
21
- if @through
22
- through_reflection = klass.reflect_on_association(@through)
23
- fail("#{klass.name} does not have any relationship to #{@through}") unless through_reflection && @through == reflection.options[:through]
24
- end
25
-
26
- if @dependent
27
- fail("#{association} should have #{@dependent} dependency") unless @dependent.to_s == reflection.options[:dependent].to_s
28
- end
29
-
30
- # Check for the existence of the foreign key on the other table
31
- unless reflection.options[:through]
32
- if reflection.options[:foreign_key]
33
- fk = reflection.options[:foreign_key]
34
- elsif reflection.options[:as]
35
- fk = reflection.options[:as].to_s.foreign_key
36
- else
37
- fk = reflection.primary_key_name
38
- end
39
-
40
- associated_klass_name = (reflection.options[:class_name] || association.to_s.classify)
41
- associated_klass = associated_klass_name.constantize
42
-
43
- fail("#{associated_klass.name} does not have a #{fk} foreign key.") unless associated_klass.column_names.include?(fk.to_s)
44
- end
45
- end
46
-
47
- true
48
- rescue Exception => e
49
- false
50
- end
51
- end
52
-
53
- def description
54
- message = "have many #{@associations.to_sentence}"
55
- message += " through #{@through}" if @through
56
- message += " dependent => #{@dependent}" if @dependent
57
- message
58
- end
59
-
60
- def failure_message
61
- message = "expected #{@klass.name} to have many #{@associations.to_sentence}"
62
- message += " through #{@through}" if @through
63
- message += " dependent => #{@dependent}" if @dependent
64
- message += ", but it didn't"
65
- @failure_message || message
66
- end
67
-
68
- def negative_failure_message
69
- message = "expected #{@klass.name} not to have many #{@associations.to_sentence}"
70
- message += " through #{@through}" if @through
71
- message += " dependent => #{@dependent}" if @dependent
72
- message += ", but it did"
73
- message
74
- end
75
- end
76
-
77
- # Ensures that the has_many relationship exists. Will also test that the
78
- # associated table has the required columns. Works with polymorphic
79
- # associations.
80
- #
81
- # Options:
82
- # * <tt>:through</tt> - association name for <tt>has_many :through</tt>
83
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
84
- #
85
- # Example:
86
- # it { User.should have_many(:friends) }
87
- # it { User.should have_many(:enemies, :through => :friends) }
88
- # it { User.should have_many(:friends, :dependent => :destroy) }
89
- #
90
- def have_many(*associations)
91
- Remarkable::Syntax::RSpec::HaveMany.new(*associations)
92
- end
93
- end
94
-
95
- module Shoulda
96
- # Ensures that the has_many relationship exists. Will also test that the
97
- # associated table has the required columns. Works with polymorphic
98
- # associations.
99
- #
100
- # Options:
101
- # * <tt>:through</tt> - association name for <tt>has_many :through</tt>
102
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
103
- #
104
- # Example:
105
- # should_have_many :friends
106
- # should_have_many :enemies, :through => :friends
107
- # should_have_many :enemies, :dependent => :destroy
108
- #
109
- def should_have_many(*associations)
110
- through, dependent = get_options!(associations, :through, :dependent)
111
- klass = model_class
112
- associations.each do |association|
113
- name = "should have many #{association}"
114
- name += " through #{through}" if through
115
- name += " dependent => #{dependent}" if dependent
116
- it name do
117
- reflection = klass.reflect_on_association(association)
118
- unless reflection
119
- fail_with("#{klass.name} does not have any relationship to #{association}")
120
- end
121
- reflection.macro.should == :has_many
122
-
123
- if through
124
- through_reflection = klass.reflect_on_association(through)
125
- unless through_reflection
126
- fail_with("#{klass.name} does not have any relationship to #{through}")
127
- end
128
- reflection.options[:through].should == through
129
- end
130
-
131
- if dependent
132
- unless reflection.options[:dependent].to_s == dependent.to_s
133
- fail_with("#{association} should have #{dependent} dependency")
134
- end
135
- end
136
-
137
- # Check for the existence of the foreign key on the other table
138
- unless reflection.options[:through]
139
- if reflection.options[:foreign_key]
140
- fk = reflection.options[:foreign_key]
141
- elsif reflection.options[:as]
142
- fk = reflection.options[:as].to_s.foreign_key
143
- else
144
- fk = reflection.primary_key_name
145
- end
146
-
147
- associated_klass_name = (reflection.options[:class_name] || association.to_s.classify)
148
- associated_klass = associated_klass_name.constantize
149
-
150
- unless associated_klass.column_names.include?(fk.to_s)
151
- fail_with("#{associated_klass.name} does not have a #{fk} foreign key.")
152
- end
153
- end
154
- end
155
- end
156
- end
157
- end
158
-
159
- end
160
- end
@@ -1,133 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveOne
6
- include Remarkable::Private
7
-
8
- def initialize(*associations)
9
- @dependent = get_options!(associations, :dependent)
10
- @associations = associations
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass
15
-
16
- begin
17
- @associations.each do |association|
18
- reflection = klass.reflect_on_association(association)
19
- fail("#{klass.name} does not have any relationship to #{association}") unless reflection && reflection.macro == :has_one
20
-
21
- associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
22
-
23
- if reflection.options[:foreign_key]
24
- fk = reflection.options[:foreign_key]
25
- elsif reflection.options[:as]
26
- fk = reflection.options[:as].to_s.foreign_key
27
- fk_type = fk.gsub(/_id$/, '_type')
28
-
29
- fail("#{associated_klass.name} does not have a #{fk_type} column.") unless associated_klass.column_names.include?(fk_type)
30
- else
31
- fk = klass.name.foreign_key
32
- end
33
-
34
- fail("#{associated_klass.name} does not have a #{fk} foreign key.") unless associated_klass.column_names.include?(fk.to_s)
35
-
36
- if @dependent
37
- fail("#{association} should have #{@dependent} dependency") unless @dependent.to_s == reflection.options[:dependent].to_s
38
- end
39
- end
40
-
41
- true
42
- rescue Exception => e
43
- false
44
- end
45
- end
46
-
47
- def description
48
- message = "have one #{@associations.to_sentence}"
49
- message += " dependent => #{@dependent}" if @dependent
50
- message
51
- end
52
-
53
- def failure_message
54
- message = "expected #{@klass.name} to have one #{@associations.to_sentence}"
55
- message += " dependent => #{@dependent}" if @dependent
56
- message += ", but it didn't"
57
- @failure_message || message
58
- end
59
-
60
- def negative_failure_message
61
- message = "expected #{@klass.name} not to have one #{@associations.to_sentence}"
62
- message += " dependent => #{@dependent}" if @dependent
63
- message += ", but it did"
64
- message
65
- end
66
- end
67
-
68
- # Ensure that the has_one relationship exists. Will also test that the
69
- # associated table has the required columns. Works with polymorphic
70
- # associations.
71
- #
72
- # Options:
73
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
74
- #
75
- # Example:
76
- # it { User.should have_one(:address) }
77
- #
78
- def have_one(*associations)
79
- Remarkable::Syntax::RSpec::HaveOne.new(*associations)
80
- end
81
- end
82
-
83
- module Shoulda
84
- # Ensure that the has_one relationship exists. Will also test that the
85
- # associated table has the required columns. Works with polymorphic
86
- # associations.
87
- #
88
- # Options:
89
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
90
- #
91
- # Example:
92
- # should_have_one :god # unless hindu
93
- #
94
- def should_have_one(*associations)
95
- dependent = get_options!(associations, :dependent)
96
- klass = model_class
97
- associations.each do |association|
98
- name = "should have one #{association}"
99
- name += " dependent => #{dependent}" if dependent
100
- it name do
101
- reflection = klass.reflect_on_association(association)
102
- fail_with("#{klass.name} does not have any relationship to #{association}") unless reflection
103
- reflection.macro.should == :has_one
104
-
105
- associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
106
-
107
- if reflection.options[:foreign_key]
108
- fk = reflection.options[:foreign_key]
109
- elsif reflection.options[:as]
110
- fk = reflection.options[:as].to_s.foreign_key
111
- fk_type = fk.gsub(/_id$/, '_type')
112
- unless associated_klass.column_names.include?(fk_type)
113
- fail_with "#{associated_klass.name} does not have a #{fk_type} column."
114
- end
115
- else
116
- fk = klass.name.foreign_key
117
- end
118
- unless associated_klass.column_names.include?(fk.to_s)
119
- fail_with "#{associated_klass.name} does not have a #{fk} foreign key."
120
- end
121
-
122
- if dependent
123
- unless reflection.options[:dependent].to_s == dependent.to_s
124
- fail_with "#{association} should have #{dependent} dependency"
125
- end
126
- end
127
- end
128
- end
129
- end
130
- end
131
-
132
- end
133
- end
@@ -1,81 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveDbColumn
6
- def initialize(name, opts)
7
- @name = name
8
- @opts = opts
9
- end
10
-
11
- def matches?(klass)
12
- @klass = klass
13
-
14
- column = klass.columns.detect {|c| c.name == @name.to_s }
15
- fail("#{klass.name} does not have column #{@name}") unless column
16
-
17
- @opts.each do |k, v|
18
- fail(":#{@name} column on table for #{klass} does not match option :#{k}") unless column.instance_variable_get("@#{k}").to_s == v.to_s
19
- end
20
- end
21
-
22
- def description
23
- message = "have column named :#{@name}"
24
- message += " with options " + @opts.inspect unless @opts.empty?
25
- message
26
- end
27
-
28
- def failure_message
29
- message = "expected #{@klass.name} to have column named :#{@name}"
30
- message += " with options " + @opts.inspect unless @opts.empty?
31
- message += ", but it didn't"
32
-
33
- @failure_message || message
34
- end
35
-
36
- def negative_failure_message
37
- message = "expected #{@klass.name} not to have column named :#{@name}"
38
- message += " with options " + @opts.inspect unless @opts.empty?
39
- message += ", but it did"
40
- message
41
- end
42
- end
43
-
44
- # Ensure that the given column is defined on the models backing SQL table. The options are the same as
45
- # the instance variables defined on the column definition: :precision, :limit, :default, :null,
46
- # :primary, :type, :scale, and :sql_type.
47
- #
48
- # it { User.should have_db_column(:email, :type => "string", :default => nil, :precision => nil, :limit => 255,
49
- # :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)') }
50
- #
51
- def have_db_column(name, opts = {})
52
- Remarkable::Syntax::RSpec::HaveDbColumn.new(name, opts)
53
- end
54
- end
55
-
56
- module Shoulda
57
- # Ensure that the given column is defined on the models backing SQL table. The options are the same as
58
- # the instance variables defined on the column definition: :precision, :limit, :default, :null,
59
- # :primary, :type, :scale, and :sql_type.
60
- #
61
- # should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
62
- # :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
63
- #
64
- def should_have_db_column(name, opts = {})
65
- klass = model_class
66
- test_name = "should have column named :#{name}"
67
- test_name += " with options " + opts.inspect unless opts.empty?
68
- it test_name do
69
- column = klass.columns.detect {|c| c.name == name.to_s }
70
- fail_with("#{klass.name} does not have column #{name}") unless column
71
- opts.each do |k, v|
72
- unless v.to_s == column.instance_variable_get("@#{k}").to_s
73
- fail_with(":#{name} column on table for #{klass} does not match option :#{k}")
74
- end
75
- end
76
- end
77
- end
78
- end
79
-
80
- end
81
- end
@@ -1,73 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveDbColumns
6
- include Remarkable::Private
7
-
8
- def initialize(*columns)
9
- @column_type = get_options!(columns, :type)
10
- @columns = columns
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass
15
-
16
- @columns.each do |name|
17
- column = klass.columns.detect {|c| c.name == name.to_s }
18
- fail("#{klass.name} does not have column #{name}") unless column
19
- end
20
- end
21
-
22
- def description
23
- message = "have columns #{@columns.to_sentence}"
24
- message += " of type #{@column_type}" if @column_type
25
- message
26
- end
27
-
28
- def failure_message
29
- message = "expected to have columns #{@columns.to_sentence}"
30
- message += " of type #{@column_type}" if @column_type
31
- message += ", but it didn't"
32
-
33
- @failure_message || message
34
- end
35
-
36
- def negative_failure_message
37
- message = "expected not to have columns #{@columns.to_sentence}"
38
- message += " of type #{@column_type}" if @column_type
39
- message += ", but it did"
40
- message
41
- end
42
- end
43
-
44
- # Ensure that the given columns are defined on the models backing SQL table.
45
- #
46
- # it { User.should have_db_columns(:id, :email, :name, :created_at) }
47
- #
48
- def have_db_columns(*columns)
49
- Remarkable::Syntax::RSpec::HaveDbColumns.new(*columns)
50
- end
51
- end
52
-
53
- module Shoulda
54
- # Ensure that the given columns are defined on the models backing SQL table.
55
- #
56
- # should_have_db_columns :id, :email, :name, :created_at
57
- #
58
- def should_have_db_columns(*columns)
59
- column_type = get_options!(columns, :type)
60
- klass = model_class
61
- columns.each do |name|
62
- test_name = "should have column #{name}"
63
- test_name += " of type #{column_type}" if column_type
64
- it test_name do
65
- column = klass.columns.detect {|c| c.name == name.to_s }
66
- fail_with("#{klass.name} does not have column #{name}") unless column
67
- end
68
- end
69
- end
70
- end
71
-
72
- end
73
- end
@@ -1,75 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class HaveIndices
6
- def initialize(*columns)
7
- @columns = columns
8
- end
9
-
10
- def matches?(klass)
11
- @table = klass.table_name
12
- indices = ::ActiveRecord::Base.connection.indexes(@table).map(&:columns)
13
-
14
- @columns.each do |column|
15
- columns = [column].flatten.map(&:to_s)
16
- return false unless indices.include?(columns)
17
- end
18
- end
19
-
20
- def description
21
- "have index on #{@table} for #{pretty_columns}"
22
- end
23
-
24
- def failure_message
25
- "expected to have index on #{@table} for #{pretty_columns}, but it didn't"
26
- end
27
-
28
- def negative_failure_message
29
- "expected not to have index on #{@table} for #{pretty_columns}, but it did"
30
- end
31
-
32
- private
33
-
34
- def pretty_columns
35
- @columns.collect { |col| col.is_a?(Array) ? "[#{col.join(', ')}]" : col }.to_sentence
36
- end
37
- end
38
-
39
- # Ensures that there are DB indices on the given columns or tuples of columns.
40
- # Also aliased to should_have_index for readability
41
- #
42
- # it { User.should have_indices(:email, :name, [:commentable_type, :commentable_id]) }
43
- # it { User.should have_index(:age) }
44
- #
45
- def have_indices(*columns)
46
- Remarkable::Syntax::RSpec::HaveIndices.new(*columns)
47
- end
48
-
49
- alias :have_index :have_indices
50
- end
51
-
52
- module Shoulda
53
- # Ensures that there are DB indices on the given columns or tuples of columns.
54
- # Also aliased to should_have_index for readability
55
- #
56
- # should_have_indices :email, :name, [:commentable_type, :commentable_id]
57
- # should_have_index :age
58
- #
59
- def should_have_indices(*columns)
60
- table = model_class.table_name
61
- indices = ::ActiveRecord::Base.connection.indexes(table).map(&:columns)
62
-
63
- columns.each do |column|
64
- it "should have index on #{table} for #{column.inspect}" do
65
- columns = [column].flatten.map(&:to_s)
66
- assert_contains(indices, columns).should be_true
67
- end
68
- end
69
- end
70
-
71
- alias_method :should_have_index, :should_have_indices
72
- end
73
-
74
- end
75
- end
@@ -1,103 +0,0 @@
1
- module Remarkable
2
- module Syntax
3
-
4
- module RSpec
5
- class AllowValuesFor
6
- include Remarkable::Private
7
-
8
- def initialize(attribute, *good_values)
9
- get_options!(good_values)
10
-
11
- @attribute = attribute
12
- @good_values = good_values
13
- end
14
-
15
- def matches?(klass)
16
- @klass = klass
17
-
18
- begin
19
- @good_values.each do |v|
20
- return false unless assert_good_value(klass, @attribute, v)
21
- end
22
-
23
- true
24
- rescue Exception => e
25
- false
26
- end
27
- end
28
-
29
- def description
30
- "allow #{@attribute} to be set to #{@good_values.to_sentence}"
31
- end
32
-
33
- def failure_message
34
- "expected allow #{@attribute} to be set to #{@good_values.to_sentence}, but it didn't"
35
- end
36
-
37
- def negative_failure_message
38
- "expected allow #{@attribute} not to be set to #{@good_values.to_sentence}, but it did"
39
- end
40
- end
41
-
42
- # Ensures that the attribute can be set to the given values.
43
- #
44
- # If an instance variable has been created in the setup named after the
45
- # model being tested, then this method will use that. Otherwise, it will
46
- # create a new instance to test against.
47
- #
48
- # Example:
49
- # it { Book.should allow_values_for(:isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0") }
50
- # it { Book.should_not allow_values_for(:isbn, "bad 1", "bad 2") }
51
- #
52
- def allow_values_for(attribute, *good_values)
53
- Remarkable::Syntax::RSpec::AllowValuesFor.new(attribute, *good_values)
54
- end
55
- end
56
-
57
- module Shoulda
58
- # Ensures that the attribute can be set to the given values.
59
- #
60
- # If an instance variable has been created in the setup named after the
61
- # model being tested, then this method will use that. Otherwise, it will
62
- # create a new instance to test against.
63
- #
64
- # Example:
65
- # should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
66
- #
67
- def should_allow_values_for(attribute, *good_values)
68
- get_options!(good_values)
69
- klass = model_class
70
- good_values.each do |v|
71
- it "should allow #{attribute} to be set to #{v.inspect}" do
72
- assert_good_value(klass, attribute, v).should be_true
73
- end
74
- end
75
- end
76
-
77
- # Ensures that the attribute cannot be set to the given values
78
- #
79
- # If an instance variable has been created in the setup named after the
80
- # model being tested, then this method will use that. Otherwise, it will
81
- # create a new instance to test against.
82
- #
83
- # Options:
84
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
85
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
86
- #
87
- # Example:
88
- # should_not_allow_values_for :isbn, "bad 1", "bad 2"
89
- #
90
- def should_not_allow_values_for(attribute, *bad_values)
91
- message = get_options!(bad_values, :message)
92
- message ||= default_error_message(:invalid)
93
- klass = model_class
94
- bad_values.each do |v|
95
- it "should not allow #{attribute} to be set to #{v.inspect}" do
96
- assert_bad_value(klass, attribute, v, message).should be_true
97
- end
98
- end
99
- end
100
- end
101
-
102
- end
103
- end