carlosbrando-remarkable 0.0.99

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. data/History.txt +6 -0
  2. data/Manifest.txt +124 -0
  3. data/PostInstall.txt +7 -0
  4. data/README.rdoc +48 -0
  5. data/Rakefile +29 -0
  6. data/init.rb +1 -0
  7. data/lib/remarkable.rb +13 -0
  8. data/lib/remarkable/active_record/active_record.rb +21 -0
  9. data/lib/remarkable/active_record/helpers.rb +16 -0
  10. data/lib/remarkable/active_record/macros/associations/belong_to.rb +81 -0
  11. data/lib/remarkable/active_record/macros/associations/have_and_belong_to_many.rb +77 -0
  12. data/lib/remarkable/active_record/macros/associations/have_many.rb +160 -0
  13. data/lib/remarkable/active_record/macros/associations/have_one.rb +133 -0
  14. data/lib/remarkable/active_record/macros/database/have_db_column.rb +81 -0
  15. data/lib/remarkable/active_record/macros/database/have_db_columns.rb +73 -0
  16. data/lib/remarkable/active_record/macros/database/have_indices.rb +75 -0
  17. data/lib/remarkable/active_record/macros/validations/allow_values_for.rb +103 -0
  18. data/lib/remarkable/active_record/macros/validations/ensure_length_at_least.rb +97 -0
  19. data/lib/remarkable/active_record/macros/validations/ensure_length_in_range.rb +134 -0
  20. data/lib/remarkable/active_record/macros/validations/ensure_length_is.rb +106 -0
  21. data/lib/remarkable/active_record/macros/validations/ensure_value_in_range.rb +117 -0
  22. data/lib/remarkable/active_record/macros/validations/have_class_methods.rb +74 -0
  23. data/lib/remarkable/active_record/macros/validations/have_instance_methods.rb +74 -0
  24. data/lib/remarkable/active_record/macros/validations/have_named_scope.rb +148 -0
  25. data/lib/remarkable/active_record/macros/validations/have_readonly_attributes.rb +81 -0
  26. data/lib/remarkable/active_record/macros/validations/only_allow_numeric_values_for.rb +89 -0
  27. data/lib/remarkable/active_record/macros/validations/protect_attributes.rb +89 -0
  28. data/lib/remarkable/active_record/macros/validations/require_acceptance_of.rb +94 -0
  29. data/lib/remarkable/active_record/macros/validations/require_attributes.rb +94 -0
  30. data/lib/remarkable/active_record/macros/validations/require_unique_attributes.rb +146 -0
  31. data/lib/remarkable/controller/controller.rb +15 -0
  32. data/lib/remarkable/controller/helpers.rb +48 -0
  33. data/lib/remarkable/controller/macros/assign_to.rb +110 -0
  34. data/lib/remarkable/controller/macros/filter_params.rb +52 -0
  35. data/lib/remarkable/controller/macros/redirect_to.rb +24 -0
  36. data/lib/remarkable/controller/macros/render_a_form.rb +23 -0
  37. data/lib/remarkable/controller/macros/render_template.rb +18 -0
  38. data/lib/remarkable/controller/macros/render_with_layout.rb +61 -0
  39. data/lib/remarkable/controller/macros/respond_with.rb +86 -0
  40. data/lib/remarkable/controller/macros/respond_with_content_type.rb +45 -0
  41. data/lib/remarkable/controller/macros/return_from_session.rb +45 -0
  42. data/lib/remarkable/controller/macros/route.rb +91 -0
  43. data/lib/remarkable/controller/macros/set_the_flash_to.rb +58 -0
  44. data/lib/remarkable/example/example_methods.rb +32 -0
  45. data/lib/remarkable/private_helpers.rb +123 -0
  46. data/rails/init.rb +1 -0
  47. data/script/console +10 -0
  48. data/script/destroy +14 -0
  49. data/script/generate +14 -0
  50. data/spec/controllers/posts_controller_spec.rb +166 -0
  51. data/spec/controllers/users_controller_spec.rb +14 -0
  52. data/spec/fixtures/addresses.yml +3 -0
  53. data/spec/fixtures/friendships.yml +0 -0
  54. data/spec/fixtures/posts.yml +5 -0
  55. data/spec/fixtures/products.yml +0 -0
  56. data/spec/fixtures/taggings.yml +0 -0
  57. data/spec/fixtures/tags.yml +9 -0
  58. data/spec/fixtures/users.yml +6 -0
  59. data/spec/models/address_spec.rb +21 -0
  60. data/spec/models/dog_spec.rb +17 -0
  61. data/spec/models/flea_spec.rb +9 -0
  62. data/spec/models/friendship_spec.rb +13 -0
  63. data/spec/models/post_spec.rb +35 -0
  64. data/spec/models/product_spec.rb +57 -0
  65. data/spec/models/tag_spec.rb +21 -0
  66. data/spec/models/tagging_spec.rb +13 -0
  67. data/spec/models/user_spec.rb +107 -0
  68. data/spec/rails_root/app/controllers/application.rb +25 -0
  69. data/spec/rails_root/app/controllers/posts_controller.rb +85 -0
  70. data/spec/rails_root/app/controllers/users_controller.rb +84 -0
  71. data/spec/rails_root/app/helpers/application_helper.rb +3 -0
  72. data/spec/rails_root/app/helpers/posts_helper.rb +2 -0
  73. data/spec/rails_root/app/helpers/users_helper.rb +2 -0
  74. data/spec/rails_root/app/models/address.rb +7 -0
  75. data/spec/rails_root/app/models/dog.rb +5 -0
  76. data/spec/rails_root/app/models/flea.rb +3 -0
  77. data/spec/rails_root/app/models/friendship.rb +4 -0
  78. data/spec/rails_root/app/models/post.rb +12 -0
  79. data/spec/rails_root/app/models/product.rb +12 -0
  80. data/spec/rails_root/app/models/tag.rb +8 -0
  81. data/spec/rails_root/app/models/tagging.rb +4 -0
  82. data/spec/rails_root/app/models/user.rb +28 -0
  83. data/spec/rails_root/app/views/layouts/posts.rhtml +17 -0
  84. data/spec/rails_root/app/views/layouts/users.rhtml +17 -0
  85. data/spec/rails_root/app/views/layouts/wide.html.erb +1 -0
  86. data/spec/rails_root/app/views/posts/edit.rhtml +27 -0
  87. data/spec/rails_root/app/views/posts/index.rhtml +25 -0
  88. data/spec/rails_root/app/views/posts/new.rhtml +26 -0
  89. data/spec/rails_root/app/views/posts/show.rhtml +18 -0
  90. data/spec/rails_root/app/views/users/edit.rhtml +22 -0
  91. data/spec/rails_root/app/views/users/index.rhtml +22 -0
  92. data/spec/rails_root/app/views/users/new.rhtml +21 -0
  93. data/spec/rails_root/app/views/users/show.rhtml +13 -0
  94. data/spec/rails_root/config/boot.rb +109 -0
  95. data/spec/rails_root/config/database.yml +4 -0
  96. data/spec/rails_root/config/environment.rb +14 -0
  97. data/spec/rails_root/config/environments/sqlite3.rb +0 -0
  98. data/spec/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  99. data/spec/rails_root/config/initializers/shoulda.rb +8 -0
  100. data/spec/rails_root/config/routes.rb +6 -0
  101. data/spec/rails_root/db/migrate/001_create_users.rb +18 -0
  102. data/spec/rails_root/db/migrate/002_create_posts.rb +13 -0
  103. data/spec/rails_root/db/migrate/003_create_taggings.rb +12 -0
  104. data/spec/rails_root/db/migrate/004_create_tags.rb +11 -0
  105. data/spec/rails_root/db/migrate/005_create_dogs.rb +12 -0
  106. data/spec/rails_root/db/migrate/006_create_addresses.rb +14 -0
  107. data/spec/rails_root/db/migrate/007_create_fleas.rb +11 -0
  108. data/spec/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  109. data/spec/rails_root/db/migrate/009_create_products.rb +17 -0
  110. data/spec/rails_root/db/migrate/010_create_friendships.rb +14 -0
  111. data/spec/rails_root/db/schema.rb +0 -0
  112. data/spec/rails_root/log/.keep +0 -0
  113. data/spec/rails_root/public/.htaccess +40 -0
  114. data/spec/rails_root/public/404.html +30 -0
  115. data/spec/rails_root/public/422.html +30 -0
  116. data/spec/rails_root/public/500.html +30 -0
  117. data/spec/rails_root/script/console +3 -0
  118. data/spec/rails_root/script/generate +3 -0
  119. data/spec/rails_root/vendor/plugins/.keep +0 -0
  120. data/spec/rcov.opts +2 -0
  121. data/spec/spec.opts +4 -0
  122. data/spec/spec_helper.rb +58 -0
  123. data/tasks/rspec.rake +21 -0
  124. metadata +216 -0
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ == 1.0.0 2008-11-12
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+ * All macros of Shoulda have been ported to RSpec.
6
+ * Two syntaxes of macros have been added: Shoulda and RSpec.
data/Manifest.txt ADDED
@@ -0,0 +1,124 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ init.rb
7
+ lib/remarkable.rb
8
+ lib/remarkable/active_record/active_record.rb
9
+ lib/remarkable/active_record/helpers.rb
10
+ lib/remarkable/active_record/macros/associations/belong_to.rb
11
+ lib/remarkable/active_record/macros/associations/have_and_belong_to_many.rb
12
+ lib/remarkable/active_record/macros/associations/have_many.rb
13
+ lib/remarkable/active_record/macros/associations/have_one.rb
14
+ lib/remarkable/active_record/macros/database/have_db_column.rb
15
+ lib/remarkable/active_record/macros/database/have_db_columns.rb
16
+ lib/remarkable/active_record/macros/database/have_indices.rb
17
+ lib/remarkable/active_record/macros/validations/allow_values_for.rb
18
+ lib/remarkable/active_record/macros/validations/ensure_length_at_least.rb
19
+ lib/remarkable/active_record/macros/validations/ensure_length_in_range.rb
20
+ lib/remarkable/active_record/macros/validations/ensure_length_is.rb
21
+ lib/remarkable/active_record/macros/validations/ensure_value_in_range.rb
22
+ lib/remarkable/active_record/macros/validations/have_class_methods.rb
23
+ lib/remarkable/active_record/macros/validations/have_instance_methods.rb
24
+ lib/remarkable/active_record/macros/validations/have_named_scope.rb
25
+ lib/remarkable/active_record/macros/validations/have_readonly_attributes.rb
26
+ lib/remarkable/active_record/macros/validations/only_allow_numeric_values_for.rb
27
+ lib/remarkable/active_record/macros/validations/protect_attributes.rb
28
+ lib/remarkable/active_record/macros/validations/require_acceptance_of.rb
29
+ lib/remarkable/active_record/macros/validations/require_attributes.rb
30
+ lib/remarkable/active_record/macros/validations/require_unique_attributes.rb
31
+ lib/remarkable/controller/controller.rb
32
+ lib/remarkable/controller/helpers.rb
33
+ lib/remarkable/controller/macros/assign_to.rb
34
+ lib/remarkable/controller/macros/filter_params.rb
35
+ lib/remarkable/controller/macros/redirect_to.rb
36
+ lib/remarkable/controller/macros/render_a_form.rb
37
+ lib/remarkable/controller/macros/render_template.rb
38
+ lib/remarkable/controller/macros/render_with_layout.rb
39
+ lib/remarkable/controller/macros/respond_with.rb
40
+ lib/remarkable/controller/macros/respond_with_content_type.rb
41
+ lib/remarkable/controller/macros/return_from_session.rb
42
+ lib/remarkable/controller/macros/route.rb
43
+ lib/remarkable/controller/macros/set_the_flash_to.rb
44
+ lib/remarkable/example/example_methods.rb
45
+ lib/remarkable/private_helpers.rb
46
+ rails/init.rb
47
+ script/console
48
+ script/destroy
49
+ script/generate
50
+ spec/controllers/posts_controller_spec.rb
51
+ spec/controllers/users_controller_spec.rb
52
+ spec/fixtures/addresses.yml
53
+ spec/fixtures/friendships.yml
54
+ spec/fixtures/posts.yml
55
+ spec/fixtures/products.yml
56
+ spec/fixtures/taggings.yml
57
+ spec/fixtures/tags.yml
58
+ spec/fixtures/users.yml
59
+ spec/models/address_spec.rb
60
+ spec/models/dog_spec.rb
61
+ spec/models/flea_spec.rb
62
+ spec/models/friendship_spec.rb
63
+ spec/models/post_spec.rb
64
+ spec/models/product_spec.rb
65
+ spec/models/tag_spec.rb
66
+ spec/models/tagging_spec.rb
67
+ spec/models/user_spec.rb
68
+ spec/rails_root/app/controllers/application.rb
69
+ spec/rails_root/app/controllers/posts_controller.rb
70
+ spec/rails_root/app/controllers/users_controller.rb
71
+ spec/rails_root/app/helpers/application_helper.rb
72
+ spec/rails_root/app/helpers/posts_helper.rb
73
+ spec/rails_root/app/helpers/users_helper.rb
74
+ spec/rails_root/app/models/address.rb
75
+ spec/rails_root/app/models/dog.rb
76
+ spec/rails_root/app/models/flea.rb
77
+ spec/rails_root/app/models/friendship.rb
78
+ spec/rails_root/app/models/post.rb
79
+ spec/rails_root/app/models/product.rb
80
+ spec/rails_root/app/models/tag.rb
81
+ spec/rails_root/app/models/tagging.rb
82
+ spec/rails_root/app/models/user.rb
83
+ spec/rails_root/app/views/layouts/posts.rhtml
84
+ spec/rails_root/app/views/layouts/users.rhtml
85
+ spec/rails_root/app/views/layouts/wide.html.erb
86
+ spec/rails_root/app/views/posts/edit.rhtml
87
+ spec/rails_root/app/views/posts/index.rhtml
88
+ spec/rails_root/app/views/posts/new.rhtml
89
+ spec/rails_root/app/views/posts/show.rhtml
90
+ spec/rails_root/app/views/users/edit.rhtml
91
+ spec/rails_root/app/views/users/index.rhtml
92
+ spec/rails_root/app/views/users/new.rhtml
93
+ spec/rails_root/app/views/users/show.rhtml
94
+ spec/rails_root/config/boot.rb
95
+ spec/rails_root/config/database.yml
96
+ spec/rails_root/config/environment.rb
97
+ spec/rails_root/config/environments/sqlite3.rb
98
+ spec/rails_root/config/initializers/new_rails_defaults.rb
99
+ spec/rails_root/config/initializers/shoulda.rb
100
+ spec/rails_root/config/routes.rb
101
+ spec/rails_root/db/migrate/001_create_users.rb
102
+ spec/rails_root/db/migrate/002_create_posts.rb
103
+ spec/rails_root/db/migrate/003_create_taggings.rb
104
+ spec/rails_root/db/migrate/004_create_tags.rb
105
+ spec/rails_root/db/migrate/005_create_dogs.rb
106
+ spec/rails_root/db/migrate/006_create_addresses.rb
107
+ spec/rails_root/db/migrate/007_create_fleas.rb
108
+ spec/rails_root/db/migrate/008_create_dogs_fleas.rb
109
+ spec/rails_root/db/migrate/009_create_products.rb
110
+ spec/rails_root/db/migrate/010_create_friendships.rb
111
+ spec/rails_root/db/schema.rb
112
+ spec/rails_root/log/.keep
113
+ spec/rails_root/log/sqlite3.log
114
+ spec/rails_root/public/.htaccess
115
+ spec/rails_root/public/404.html
116
+ spec/rails_root/public/422.html
117
+ spec/rails_root/public/500.html
118
+ spec/rails_root/script/console
119
+ spec/rails_root/script/generate
120
+ spec/rails_root/vendor/plugins/.keep
121
+ spec/rcov.opts
122
+ spec/spec.opts
123
+ spec/spec_helper.rb
124
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on remarkable, see http://remarkable.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = remarkable
2
+
3
+ * http://www.nomedojogo.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/remarkable'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('remarkable', Remarkable::VERSION) do |p|
7
+ p.developer('Carlos Brando', 'eduardobrando@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ p.extra_deps = [
12
+ ['rspec','>= 1.1.11'],
13
+ ['rspec-rails','>= 1.1.11']
14
+ ]
15
+ p.extra_dev_deps = [
16
+ ['newgem', ">= #{::Newgem::VERSION}"]
17
+ ]
18
+
19
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
20
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
21
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
22
+ p.rsync_args = '-av --delete --ignore-errors'
23
+ end
24
+
25
+ require 'newgem/tasks' # load /tasks/*.rake
26
+ Dir['tasks/**/*.rake'].each { |t| load t }
27
+
28
+ # TODO - want other tests/tasks run by default? Add them to the list
29
+ task :default => [:spec, :features]
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
data/lib/remarkable.rb ADDED
@@ -0,0 +1,13 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Remarkable
5
+ VERSION = '0.0.99'
6
+ end
7
+
8
+ require "spec"
9
+
10
+ require 'remarkable/example/example_methods'
11
+ require 'remarkable/private_helpers'
12
+ require 'remarkable/active_record/active_record'
13
+ require 'remarkable/controller/controller'
@@ -0,0 +1,21 @@
1
+ require 'remarkable/active_record/helpers'
2
+ %w( database associations validations ).each do |folder|
3
+ Dir[File.join(File.dirname(__FILE__), "macros", folder, '*')].each do |file|
4
+ require file
5
+ end
6
+ end
7
+
8
+ module Spec
9
+ module Rails
10
+ module Matchers
11
+ include Remarkable::Private
12
+ include Remarkable::ActiveRecord::Helpers
13
+ end
14
+ end
15
+ end
16
+
17
+ Spec::Rails::Matchers.send(:include, Remarkable::Syntax::RSpec)
18
+
19
+ Spec::Example::ExampleGroupMethods.send(:include, Remarkable::Private)
20
+ Spec::Example::ExampleGroupMethods.send(:include, Remarkable::ActiveRecord::Helpers)
21
+ Spec::Example::ExampleGroupMethods.send(:include, Remarkable::Syntax::Shoulda)
@@ -0,0 +1,16 @@
1
+ module Remarkable # :nodoc:
2
+ module ActiveRecord # :nodoc:
3
+ module Helpers # :nodoc:
4
+ private # :enddoc:
5
+
6
+ def model_class
7
+ self.described_type
8
+ end
9
+
10
+ def fail_with(message)
11
+ Spec::Expectations.fail_with(message)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,81 @@
1
+ module Remarkable
2
+ module Syntax
3
+
4
+ module RSpec
5
+ class BelongTo
6
+ include Remarkable::Private
7
+
8
+ def initialize(*associations)
9
+ get_options!(associations)
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 == :belongs_to
20
+
21
+ unless reflection.options[:polymorphic]
22
+ associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
23
+ fk = reflection.options[:foreign_key] || reflection.primary_key_name
24
+ fail("#{klass.name} does not have a #{fk} foreign key.") unless klass.column_names.include?(fk.to_s)
25
+ end
26
+ end
27
+
28
+ true
29
+ rescue Exception => e
30
+ false
31
+ end
32
+ end
33
+
34
+ def description
35
+ "belong to #{@associations.to_sentence}"
36
+ end
37
+
38
+ def failure_message
39
+ @failure_message || "expected #{@klass.name} to belong to #{@associations.to_sentence}, but it didn't"
40
+ end
41
+
42
+ def negative_failure_message
43
+ "expected #{@klass.name} not to belong to #{@associations.to_sentence}, but it did"
44
+ end
45
+ end
46
+
47
+ # Ensure that the belongs_to relationship exists.
48
+ #
49
+ # it { Post.should belong_to(:user) }
50
+ #
51
+ def belong_to(*associations)
52
+ Remarkable::Syntax::RSpec::BelongTo.new(*associations)
53
+ end
54
+ end
55
+
56
+ module Shoulda
57
+ # Ensure that the belongs_to relationship exists.
58
+ #
59
+ # should_belong_to :parent
60
+ #
61
+ def should_belong_to(*associations)
62
+ get_options!(associations)
63
+ klass = model_class
64
+ associations.each do |association|
65
+ it "should belong to #{association}" do
66
+ reflection = klass.reflect_on_association(association)
67
+ fail_with("#{klass.name} does not have any relationship to #{association}") unless reflection
68
+ reflection.macro.should == :belongs_to
69
+
70
+ unless reflection.options[:polymorphic]
71
+ associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
72
+ fk = reflection.options[:foreign_key] || reflection.primary_key_name
73
+ fail_with("#{klass.name} does not have a #{fk} foreign key.") unless klass.column_names.include?(fk.to_s)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,77 @@
1
+ module Remarkable
2
+ module Syntax
3
+
4
+ module RSpec
5
+ class HaveAndBelongToMany
6
+ include Remarkable::Private
7
+
8
+ def initialize(*associations)
9
+ get_options!(associations)
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_and_belongs_to_many
20
+
21
+ table = reflection.options[:join_table]
22
+ fail("table #{table} doesn't exist") unless ::ActiveRecord::Base.connection.tables.include?(table.to_s)
23
+ end
24
+
25
+ true
26
+ rescue Exception => e
27
+ false
28
+ end
29
+ end
30
+
31
+ def description
32
+ "should have and belong to many #{@associations.to_sentence}"
33
+ end
34
+
35
+ def failure_message
36
+ @failure_message || "expected #{@klass.name} to have and belong to many #{@associations.to_sentence}, but it didn't"
37
+ end
38
+
39
+ def negative_failure_message
40
+ "expected #{@klass.name} not to have and belong to many #{@associations.to_sentence}, but it did"
41
+ end
42
+ end
43
+
44
+ # Ensures that the has_and_belongs_to_many relationship exists, and that the join
45
+ # table is in place.
46
+ #
47
+ # it { User.should have_and_belong_to_many(:posts, :cars) }
48
+ #
49
+ def have_and_belong_to_many(*associations)
50
+ Remarkable::Syntax::RSpec::HaveAndBelongToMany.new(*associations)
51
+ end
52
+ end
53
+
54
+ module Shoulda
55
+ # Ensures that the has_and_belongs_to_many relationship exists, and that the join
56
+ # table is in place.
57
+ #
58
+ # should_have_and_belong_to_many :posts, :cars
59
+ #
60
+ def should_have_and_belong_to_many(*associations)
61
+ get_options!(associations)
62
+ klass = model_class
63
+
64
+ associations.each do |association|
65
+ it "should have and belong to many #{association}" do
66
+ reflection = klass.reflect_on_association(association)
67
+ fail_with("#{klass.name} does not have any relationship to #{association}") unless reflection
68
+ reflection.macro.should == :has_and_belongs_to_many
69
+ table = reflection.options[:join_table]
70
+ fail_with("table #{table} doesn't exist") unless ::ActiveRecord::Base.connection.tables.include?(table.to_s)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,160 @@
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