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
@@ -0,0 +1,91 @@
1
+ module Remarkable
2
+ module Syntax
3
+
4
+ module RSpec
5
+ # Macro that creates a routing test. It tries to use the given HTTP
6
+ # +method+ on the given +path+, and asserts that it routes to the
7
+ # given +options+.
8
+ #
9
+ # +to_param+ is called on the +options+ given.
10
+ #
11
+ # Examples:
12
+ #
13
+ # it { should route(:get, "/posts", :controller => :posts, :action => :index) }
14
+ # it { should route(:get, "/posts/new", :controller => :posts, :action => :new) }
15
+ # it { should route(:post, "/posts", :controller => :posts, :action => :create) }
16
+ # it { should route(:get, "/posts/1", :controller => :posts, :action => :show, :id => 1) }
17
+ # it { should route(:edit, "/posts/1", :controller => :posts, :action => :show, :id => 1) }
18
+ # it { should route(:put, "/posts/1", :controller => :posts, :action => :update, :id => 1) }
19
+ # it { should route(:delete, "/posts/1", :controller => :posts, :action => :destroy, :id => 1) }
20
+ # it do
21
+ # should route :get, "/users/1/posts/1", :controller => :posts, :action => :show, :id => 1, :user_id => 1
22
+ # end
23
+ #
24
+ def route(method, path, options)
25
+ simple_matcher "route #{method.to_s.upcase} #{path} to/from #{options.inspect}" do |controller|
26
+ unless options[:controller]
27
+ options[:controller] = controller.name.gsub(/Controller$/, '').tableize
28
+ end
29
+ options[:controller] = options[:controller].to_s
30
+ options[:action] = options[:action].to_s
31
+
32
+ populated_path = path.dup
33
+ options.each do |key, value|
34
+ options[key] = value.to_param if value.respond_to? :to_param
35
+ populated_path.gsub!(key.inspect, value.to_s)
36
+ end
37
+
38
+ route_for(options).should == populated_path && params_from(method.to_sym, populated_path).should == options
39
+ end
40
+ end
41
+ end
42
+
43
+ module Shoulda
44
+ # Macro that creates a routing test. It tries to use the given HTTP
45
+ # +method+ on the given +path+, and asserts that it routes to the
46
+ # given +options+.
47
+ #
48
+ # +to_param+ is called on the +options+ given.
49
+ #
50
+ # Examples:
51
+ #
52
+ # should_route :get, "/posts", :controller => :posts, :action => :index
53
+ # should_route :get, "/posts/new", :controller => :posts, :action => :new
54
+ # should_route :post, "/posts", :controller => :posts, :action => :create
55
+ # should_route :get, "/posts/1", :controller => :posts, :action => :show, :id => 1
56
+ # should_route :edit, "/posts/1", :controller => :posts, :action => :show, :id => 1
57
+ # should_route :put, "/posts/1", :controller => :posts, :action => :update, :id => 1
58
+ # should_route :delete, "/posts/1", :controller => :posts, :action => :destroy, :id => 1
59
+ # should_route :get, "/users/1/posts/1",
60
+ # :controller => :posts, :action => :show, :id => 1, :user_id => 1
61
+ #
62
+ def should_route(method, path, options)
63
+ populated_path = path.dup
64
+
65
+ unless options[:controller]
66
+ it "should explicitly specify :controller" do
67
+ options[:controller].should_not be_nil
68
+ end
69
+ return
70
+ end
71
+
72
+ options[:controller] = options[:controller].to_s
73
+ options[:action] = options[:action].to_s
74
+
75
+ options.each do |key, value|
76
+ options[key] = value.to_param if value.respond_to? :to_param
77
+ populated_path.gsub!(key.inspect, value.to_s)
78
+ end
79
+
80
+ it "should map #{options.inspect} to #{path.inspect}" do
81
+ route_for(options).should == populated_path
82
+ end
83
+
84
+ it "should generate params #{options.inspect} from #{method.to_s.upcase} to #{path.inspect}" do
85
+ params_from(method.to_sym, populated_path).should == options
86
+ end
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,58 @@
1
+ module Remarkable
2
+ module Syntax
3
+
4
+ module RSpec
5
+ # Macro that creates a test asserting that the flash contains the given value.
6
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
7
+ #
8
+ # Example:
9
+ #
10
+ # should_set_the_flash_to "Thank you for placing this order."
11
+ # should_set_the_flash_to /created/i
12
+ # should_set_the_flash_to nil
13
+ #
14
+ def set_the_flash_to(val = nil)
15
+ if val
16
+ simple_matcher "should have #{val.inspect} in the flash" do
17
+ assert_contains(flash.values, val)
18
+ end
19
+ else
20
+ simple_matcher "should not set the flash" do
21
+ assert_equal({}, flash)
22
+ end
23
+ end
24
+ end
25
+ alias_method :set_the_flash, :set_the_flash_to
26
+ end
27
+
28
+ module Shoulda
29
+ # Macro that creates a test asserting that the flash contains the given value.
30
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
31
+ #
32
+ # Example:
33
+ #
34
+ # should_set_the_flash_to "Thank you for placing this order."
35
+ # should_set_the_flash_to /created/i
36
+ # should_set_the_flash_to nil
37
+ #
38
+ def should_set_the_flash_to(val)
39
+ if val
40
+ it "should have #{val.inspect} in the flash" do
41
+ assert_contains(flash.values, val)
42
+ end
43
+ else
44
+ it "should not set the flash" do
45
+ assert_equal({}, flash)
46
+ end
47
+ end
48
+ end
49
+
50
+ # Macro that creates a test asserting that the flash is empty. Same as
51
+ # @should_set_the_flash_to nil@
52
+ def should_not_set_the_flash
53
+ should_set_the_flash_to nil
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,32 @@
1
+ module Spec
2
+ module Example
3
+ module ExampleMethods
4
+ def should(matcher)
5
+ case matcher.class.name
6
+ when "Spec::Rails::Matchers::RenderTemplate", "Spec::Rails::Matchers::RedirectTo"
7
+ remarkable_response.should matcher
8
+ else
9
+ remarkable_subject.should matcher
10
+ end
11
+ end
12
+
13
+ def should_not(matcher)
14
+ case matcher.class.name
15
+ when "Spec::Rails::Matchers::RenderTemplate", "Spec::Rails::Matchers::RedirectTo"
16
+ remarkable_response.should_not matcher
17
+ else
18
+ remarkable_subject.should_not matcher
19
+ end
20
+ end
21
+
22
+ def remarkable_subject
23
+ @remarkable_subject = subject if self.respond_to?(:subject)
24
+ @remarkable_subject ||= self.class.described_type
25
+ end
26
+
27
+ def remarkable_response
28
+ @remarkable_response ||= self.response if self.respond_to?(:response)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,123 @@
1
+ module Remarkable # :nodoc:
2
+ module Private # :nodoc:
3
+ def fail(message)
4
+ @failure_message = message
5
+ raise Exception.new(message)
6
+ end
7
+
8
+ # Returns the values for the entries in the args hash who's keys are listed in the wanted array.
9
+ # Will raise if there are keys in the args hash that aren't listed.
10
+ def get_options!(args, *wanted)
11
+ ret = []
12
+ opts = (args.last.is_a?(Hash) ? args.pop : {})
13
+ wanted.each {|w| ret << opts.delete(w)}
14
+ raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
15
+ return *ret
16
+ end
17
+
18
+ # Helper method that determines the default error message used by Active
19
+ # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
20
+ # introduced I18n module used for localization.
21
+ #
22
+ # default_error_message(:blank)
23
+ # default_error_message(:too_short, :count => 5)
24
+ # default_error_message(:too_long, :count => 60)
25
+ def default_error_message(key, values = {})
26
+ if Object.const_defined?(:I18n) # Rails >= 2.2
27
+ I18n.translate("activerecord.errors.messages.#{key}", values)
28
+ else # Rails <= 2.1.x
29
+ ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
30
+ end
31
+ end
32
+
33
+ # Asserts that an Active Record model validates with the passed
34
+ # <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
35
+ # contained within the list of errors for that attribute.
36
+ #
37
+ # assert_good_value(User.new, :email, "user@example.com")
38
+ # assert_good_value(User.new, :ssn, "123456789", /length/)
39
+ #
40
+ # If a class is passed as the first argument, a new object will be
41
+ # instantiated before the assertion. If an instance variable exists with
42
+ # the same name as the class (underscored), that object will be used
43
+ # instead.
44
+ #
45
+ # assert_good_value(User, :email, "user@example.com")
46
+ #
47
+ # @product = Product.new(:tangible => false)
48
+ # assert_good_value(Product, :price, "0")
49
+ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
50
+ object = get_instance_of(object_or_klass)
51
+ object.send("#{attribute}=", value)
52
+ return true if object.valid?
53
+ assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid)
54
+ end
55
+
56
+ # Asserts that an Active Record model invalidates the passed
57
+ # <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
58
+ # contained within the list of errors for that attribute.
59
+ #
60
+ # assert_bad_value(User.new, :email, "invalid")
61
+ # assert_bad_value(User.new, :ssn, "123", /length/)
62
+ #
63
+ # If a class is passed as the first argument, a new object will be
64
+ # instantiated before the assertion. If an instance variable exists with
65
+ # the same name as the class (underscored), that object will be used
66
+ # instead.
67
+ #
68
+ # assert_bad_value(User, :email, "invalid")
69
+ #
70
+ # @product = Product.new(:tangible => true)
71
+ # assert_bad_value(Product, :price, "0")
72
+ def assert_bad_value(object_or_klass, attribute, value,
73
+ error_message_to_expect = self.class.default_error_message(:invalid))
74
+ object = get_instance_of(object_or_klass)
75
+ object.send("#{attribute}=", value)
76
+
77
+ return false if object.valid?
78
+ return false unless object.errors.on(attribute)
79
+ assert_contains(object.errors.on(attribute), error_message_to_expect)
80
+ end
81
+
82
+ # Asserts that the given collection contains item x. If x is a regular expression, ensure that
83
+ # at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
84
+ #
85
+ # assert_contains(['a', '1'], /\d/) => passes
86
+ # assert_contains(['a', '1'], 'a') => passes
87
+ # assert_contains(['a', '1'], /not there/) => fails
88
+ def assert_contains(collection, x)
89
+ collection = [collection] unless collection.is_a?(Array)
90
+ case x
91
+ when Regexp
92
+ return false unless collection.detect { |e| e =~ x }
93
+ else
94
+ return false unless collection.include?(x)
95
+ end
96
+ true
97
+ end
98
+
99
+ # Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
100
+ # none of the elements from the collection match x.
101
+ def assert_does_not_contain(collection, x)
102
+ collection = [collection] unless collection.is_a?(Array)
103
+ case x
104
+ when Regexp
105
+ return false if collection.detect { |e| e =~ x }
106
+ else
107
+ return false if collection.include?(x)
108
+ end
109
+ true
110
+ end
111
+
112
+ private
113
+
114
+ def get_instance_of(object_or_klass)
115
+ if object_or_klass.is_a?(Class)
116
+ klass = object_or_klass
117
+ instance_variable_get("@#{klass.to_s.underscore}") || klass.new
118
+ else
119
+ object_or_klass
120
+ end
121
+ end
122
+ end
123
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "remarkable.rb"
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/remarkable.rb'}"
9
+ puts "Loading remarkable gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,166 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe PostsController do
4
+ fixtures :all
5
+
6
+ # autodetects the :controller
7
+ should_route :get, '/posts', :controller => :posts, :action => :index
8
+ # explicitly specify :controller
9
+ should_route :post, '/posts', :controller => :posts, :action => :create
10
+ # non-string parameter
11
+ should_route :get, '/posts/1', :controller => :posts, :action => :show, :id => 1
12
+ # string-parameter
13
+ should_route :put, '/posts/1', :controller => :posts, :action => :update, :id => "1"
14
+ should_route :delete, '/posts/1', :controller => :posts, :action => :destroy, :id => 1
15
+ should_route :get, '/posts/new', :controller => :posts, :action => :new
16
+
17
+ # Test the nested routes
18
+ should_route :get, '/users/5/posts', :controller => :posts, :action => :index, :user_id => 5
19
+ should_route :post, '/users/5/posts', :controller => :posts, :action => :create, :user_id => 5
20
+ should_route :get, '/users/5/posts/1', :controller => :posts, :action => :show, :id => 1, :user_id => 5
21
+ should_route :delete, '/users/5/posts/1', :controller => :posts, :action => :destroy, :id => 1, :user_id => 5
22
+ should_route :get, '/users/5/posts/new', :controller => :posts, :action => :new, :user_id => 5
23
+ should_route :put, '/users/5/posts/1', :controller => :posts, :action => :update, :id => 1, :user_id => 5
24
+
25
+ describe "Logged in" do
26
+ before do
27
+ request.session[:logged_in] = true
28
+ end
29
+
30
+ describe "viewing posts for a user" do
31
+ before do
32
+ get :index, :user_id => users(:first)
33
+ end
34
+ should_respond_with 200
35
+ should_assign_to :user, :class => User, :equals => 'users(:first)'
36
+ it { lambda { should_assign_to(:user, :class => Post) }.should raise_error }
37
+ it { lambda { should_assign_to :user, :equals => 'posts(:first)' }.should raise_error }
38
+ should_assign_to :posts
39
+ should_not_assign_to :foo, :bar
40
+ end
41
+
42
+ describe "on POST to :create" do
43
+ before do
44
+ post :create, :post => { :title => "Title", :body => "Body" }, :user_id => users(:first)
45
+ @post = Post.last
46
+ end
47
+ should_respond_with :redirect
48
+ should_redirect_to "user_post_url(@post.user, @post)"
49
+ should_set_the_flash_to /created/i
50
+ end
51
+
52
+ describe "viewing posts for a user with rss format" do
53
+ before do
54
+ get :index, :user_id => users(:first), :format => 'rss'
55
+ @user = users(:first)
56
+ end
57
+ should_respond_with :success
58
+ should_respond_with_content_type 'application/rss+xml'
59
+ should_respond_with_content_type :rss
60
+ should_respond_with_content_type /rss/
61
+ should_return_from_session :special, "'$2 off your next purchase'"
62
+ should_return_from_session :special_user_id, '@user.id'
63
+ should_assign_to :user, :posts
64
+ should_not_assign_to :foo, :bar
65
+ end
66
+
67
+ describe "viewing a post on GET to #show" do
68
+ before { get :show, :user_id => users(:first), :id => posts(:first) }
69
+ should_render_with_layout 'wide'
70
+ should_render_with_layout :wide
71
+ should_render_template :show
72
+ end
73
+
74
+ describe "on GET to #new" do
75
+ before { get :new, :user_id => users(:first) }
76
+ should_render_without_layout
77
+ should_not_set_the_flash
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ describe PostsController do
84
+ fixtures :all
85
+
86
+ # autodetects the :controller
87
+ it { should route(:get, '/posts', :action => :index) }
88
+ # explicitly specify :controller
89
+ it { should route(:post, '/posts', :controller => :posts, :action => :create) }
90
+ # # non-string parameter
91
+ it { should route(:get, '/posts/1', :action => :show, :id => 1) }
92
+ # # string-parameter
93
+ it { should route(:put, '/posts/1', :action => :update, :id => "1") }
94
+ it { should route(:delete, '/posts/1', :action => :destroy, :id => 1) }
95
+ it { should route(:get, '/posts/new', :action => :new) }
96
+
97
+ # # Test the nested routes
98
+ it { should route(:get, '/users/5/posts', :action => :index, :user_id => 5) }
99
+ it { should route(:post, '/users/5/posts', :action => :create, :user_id => 5) }
100
+ it { should route(:get, '/users/5/posts/1', :action => :show, :id => 1, :user_id => 5) }
101
+ it { should route(:delete, '/users/5/posts/1', :action => :destroy, :id => 1, :user_id => 5) }
102
+ it { should route(:get, '/users/5/posts/new', :action => :new, :user_id => 5) }
103
+ it { should route(:put, '/users/5/posts/1', :action => :update, :id => 1, :user_id => 5) }
104
+
105
+ describe "Logged in" do
106
+ before do
107
+ request.session[:logged_in] = true
108
+ end
109
+
110
+ describe "viewing posts for a user" do
111
+ before do
112
+ get :index, :user_id => users(:first)
113
+ end
114
+ it { should respond_with(200) }
115
+ it { should assign_to(:user, :class => User, :equals => 'users(:first)') }
116
+ it { should_not assign_to(:user, :class => Post) }
117
+ it { should_not assign_to(:user, :equals => 'posts(:first)') }
118
+ it { should assign_to(:posts) }
119
+ it { should_not assign_to(:foo, :bar) }
120
+ end
121
+
122
+ describe "on POST to :create" do
123
+ before(:each) do
124
+ post :create, :post => { :title => "Title", :body => "Body" }, :user_id => users(:first)
125
+ @post = Post.last
126
+ end
127
+ it { should respond_with(:redirect) }
128
+ it { should_not respond_with(:success) }
129
+ it { should redirect_to(user_post_url(@post.user, @post)) }
130
+ it { should set_the_flash_to(/created/i) }
131
+ it { should_not set_the_flash_to(/foo/i) }
132
+ end
133
+
134
+ describe "viewing posts for a user with rss format" do
135
+ before do
136
+ get :index, :user_id => users(:first), :format => 'rss'
137
+ @user = users(:first)
138
+ end
139
+ it { should respond_with(:success) }
140
+ it { should respond_with_content_type('application/rss+xml') }
141
+ it { should respond_with_content_type(:rss) }
142
+ it { should respond_with_content_type(/rss/) }
143
+ it { should return_from_session(:special, "'$2 off your next purchase'") }
144
+ it { should return_from_session(:special_user_id, '@user.id') }
145
+ it { should_not return_from_session(:monkey, "'fat'") }
146
+ it { should assign_to(:user, :posts) }
147
+ it { should_not assign_to(:foo, :bar) }
148
+ end
149
+
150
+ describe "viewing a post on GET to #show" do
151
+ before { get :show, :user_id => users(:first), :id => posts(:first) }
152
+ it { should render_with_layout('wide') }
153
+ it { should render_with_layout(:wide) }
154
+ it { should_not respond_with_content_type(:rss) }
155
+ it { should render_template(:show) }
156
+ it { should_not render_template(:new) }
157
+ end
158
+
159
+ describe "on GET to #new" do
160
+ before { get :new, :user_id => users(:first) }
161
+ it { should render_without_layout }
162
+ it { should_not set_the_flash }
163
+ end
164
+ end
165
+
166
+ end