inherited_resources 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +6 -0
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +86 -0
  5. data/README.rdoc +91 -37
  6. data/Rakefile +0 -22
  7. data/inherited_resources.gemspec +24 -0
  8. data/lib/inherited_resources.rb +7 -2
  9. data/lib/inherited_resources/actions.rb +4 -4
  10. data/lib/inherited_resources/base.rb +11 -10
  11. data/lib/inherited_resources/base_helpers.rb +32 -3
  12. data/lib/inherited_resources/class_methods.rb +102 -10
  13. data/lib/inherited_resources/shallow_helpers.rb +87 -0
  14. data/lib/inherited_resources/url_helpers.rb +48 -2
  15. data/lib/inherited_resources/version.rb +1 -1
  16. data/test/aliases_test.rb +1 -1
  17. data/test/association_chain_test.rb +1 -1
  18. data/test/base_test.rb +16 -8
  19. data/test/belongs_to_test.rb +44 -2
  20. data/test/belongs_to_with_shallow_test.rb +93 -0
  21. data/test/class_methods_test.rb +6 -4
  22. data/test/customized_base_test.rb +1 -1
  23. data/test/customized_belongs_to_test.rb +2 -2
  24. data/test/customized_redirect_to_test.rb +32 -0
  25. data/test/defaults_test.rb +117 -11
  26. data/test/locales/en.yml +17 -0
  27. data/test/nested_belongs_to_test.rb +1 -1
  28. data/test/nested_belongs_to_with_shallow_test.rb +123 -0
  29. data/test/nested_model_with_shallow_test.rb +133 -0
  30. data/test/optional_belongs_to_test.rb +4 -4
  31. data/test/polymorphic_test.rb +5 -5
  32. data/test/redirect_to_test.rb +1 -1
  33. data/test/singleton_test.rb +5 -2
  34. data/test/test_helper.rb +8 -24
  35. data/test/url_helpers_test.rb +243 -2
  36. data/test/views/cars/edit.html.erb +1 -0
  37. data/test/views/cars/index.html.erb +1 -0
  38. data/test/views/cars/new.html.erb +1 -0
  39. data/test/views/cars/show.html.erb +1 -0
  40. data/test/views/cities/edit.html.erb +1 -0
  41. data/test/views/cities/index.html.erb +1 -0
  42. data/test/views/cities/new.html.erb +1 -0
  43. data/test/views/cities/show.html.erb +1 -0
  44. data/test/views/comments/edit.html.erb +1 -0
  45. data/test/views/comments/index.html.erb +1 -0
  46. data/test/views/comments/new.html.erb +1 -0
  47. data/test/views/comments/show.html.erb +1 -0
  48. data/test/views/educations/new.html.erb +0 -0
  49. data/test/views/employees/edit.html.erb +1 -0
  50. data/test/views/employees/index.html.erb +1 -0
  51. data/test/views/employees/new.html.erb +1 -0
  52. data/test/views/employees/show.html.erb +1 -0
  53. data/test/views/groups/edit.html.erb +0 -0
  54. data/test/views/managers/edit.html.erb +1 -0
  55. data/test/views/managers/new.html.erb +1 -0
  56. data/test/views/managers/show.html.erb +1 -0
  57. data/test/views/painters/edit.html.erb +1 -0
  58. data/test/views/painters/index.html.erb +1 -0
  59. data/test/views/painters/new.html.erb +1 -0
  60. data/test/views/painters/show.html.erb +1 -0
  61. data/test/views/pets/edit.html.erb +1 -0
  62. data/test/views/pets/index.html.erb +1 -0
  63. data/test/views/pets/new.html.erb +1 -0
  64. data/test/views/pets/show.html.erb +1 -0
  65. data/test/views/plates/edit.html.erb +1 -0
  66. data/test/views/plates/index.html.erb +1 -0
  67. data/test/views/plates/new.html.erb +1 -0
  68. data/test/views/plates/show.html.erb +1 -0
  69. data/test/views/products/edit.html.erb +1 -0
  70. data/test/views/products/index.html.erb +1 -0
  71. data/test/views/products/new.html.erb +1 -0
  72. data/test/views/products/show.html.erb +1 -0
  73. data/test/views/professors/edit.html.erb +1 -0
  74. data/test/views/professors/index.html.erb +1 -0
  75. data/test/views/professors/new.html.erb +1 -0
  76. data/test/views/professors/show.html.erb +1 -0
  77. data/test/views/projects/index.html.erb +1 -0
  78. data/test/views/projects/index.json.erb +1 -0
  79. data/test/views/projects/respond_to_skip_default_template.html.erb +1 -0
  80. data/test/views/projects/respond_with_resource.html.erb +1 -0
  81. data/test/views/students/edit.html.erb +1 -0
  82. data/test/views/students/new.html.erb +1 -0
  83. data/test/views/tags/edit.html.erb +1 -0
  84. data/test/views/tags/index.html.erb +1 -0
  85. data/test/views/tags/new.html.erb +1 -0
  86. data/test/views/tags/show.html.erb +1 -0
  87. data/test/views/trees/edit.html.erb +1 -0
  88. data/test/views/trees/index.html.erb +1 -0
  89. data/test/views/trees/new.html.erb +1 -0
  90. data/test/views/trees/show.html.erb +1 -0
  91. data/test/views/university/professors/edit.html.erb +1 -0
  92. data/test/views/university/professors/index.html.erb +1 -0
  93. data/test/views/university/professors/new.html.erb +1 -0
  94. data/test/views/university/professors/show.html.erb +1 -0
  95. data/test/views/users/create.js.erb +1 -0
  96. data/test/views/users/destroy.js.erb +1 -0
  97. data/test/views/users/edit.html.erb +1 -0
  98. data/test/views/users/index.html.erb +1 -0
  99. data/test/views/users/new.html.erb +1 -0
  100. data/test/views/users/show.html.erb +1 -0
  101. data/test/views/users/update.js.erb +1 -0
  102. metadata +180 -9
@@ -0,0 +1 @@
1
+ .bundle
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ # Version 1.2
2
+
3
+ * Improved lookup for namespaces (by github.com/Sirupsen)
4
+ * Support to custom actions (by github.com/lda)
5
+ * Rails 3.1 compatibility (by github.com/etehtsea)
6
+
1
7
  # Version 1.1
2
8
 
3
9
  * Rails 3 compatible
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.3"
4
+ gem "responders", "~> 0.6.0"
5
+ gem "has_scope", "~> 0.5.0"
6
+ gem "mocha"
7
+
8
+ if RUBY_VERSION < "1.9"
9
+ gem "ruby-debug"
10
+ else
11
+ gem "test-unit"
12
+ end
@@ -0,0 +1,86 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ arel (2.0.6)
32
+ builder (2.1.2)
33
+ columnize (0.3.1)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ has_scope (0.5.0)
37
+ i18n (0.5.0)
38
+ linecache (0.43)
39
+ mail (2.2.14)
40
+ activesupport (>= 2.3.6)
41
+ i18n (>= 0.4.0)
42
+ mime-types (~> 1.16)
43
+ treetop (~> 1.4.8)
44
+ mime-types (1.16)
45
+ mocha (0.9.8)
46
+ rake
47
+ polyglot (0.3.1)
48
+ rack (1.2.1)
49
+ rack-mount (0.6.13)
50
+ rack (>= 1.0.0)
51
+ rack-test (0.5.7)
52
+ rack (>= 1.0)
53
+ rails (3.0.3)
54
+ actionmailer (= 3.0.3)
55
+ actionpack (= 3.0.3)
56
+ activerecord (= 3.0.3)
57
+ activeresource (= 3.0.3)
58
+ activesupport (= 3.0.3)
59
+ bundler (~> 1.0)
60
+ railties (= 3.0.3)
61
+ railties (3.0.3)
62
+ actionpack (= 3.0.3)
63
+ activesupport (= 3.0.3)
64
+ rake (>= 0.8.7)
65
+ thor (~> 0.14.4)
66
+ rake (0.8.7)
67
+ responders (0.6.2)
68
+ ruby-debug (0.10.3)
69
+ columnize (>= 0.1)
70
+ ruby-debug-base (~> 0.10.3.0)
71
+ ruby-debug-base (0.10.3)
72
+ linecache (>= 0.3)
73
+ thor (0.14.6)
74
+ treetop (1.4.9)
75
+ polyglot (>= 0.3.1)
76
+ tzinfo (0.3.24)
77
+
78
+ PLATFORMS
79
+ ruby
80
+
81
+ DEPENDENCIES
82
+ has_scope (~> 0.5.0)
83
+ mocha
84
+ rails (= 3.0.3)
85
+ responders (~> 0.6.0)
86
+ ruby-debug
@@ -5,18 +5,31 @@ all restful actions so you just have to focus on what is important. It makes
5
5
  your controllers more powerful and cleaner at the same time.
6
6
 
7
7
  Plus, making your controllers follow a pattern, it helps you to write better
8
- code by following fat models and skinny controllers convention. There is
9
- a screencast made by Fabio Akita about its features:
8
+ code by following fat models and skinny controllers convention. There are
9
+ two screencasts available besides this README:
10
10
 
11
+ http://railscasts.com/episodes/230-inherited-resources
11
12
  http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources
12
13
 
13
- == Rails 3
14
+ == Installation
15
+
16
+ === Rails 3
14
17
 
15
18
  Inherited Resources master branch is now supports Rails 3 and is NOT backward compatible.
16
19
 
17
- You can install it as:
20
+ You can let bundler to install Inherited Resources by adding this line to your application's Gemfile:
21
+
22
+ gem 'inherited_resources', '~> 1.2'
23
+
24
+ And then execute:
25
+
26
+ bundle install
27
+
28
+ Or install it yourself as:
18
29
 
19
- sudo gem install inherited_resources --version=1.1.1
30
+ gem install inherited_resources --version=1.2
31
+
32
+ === Rails 2.3.x
20
33
 
21
34
  If you want to use the Rails 2.3.x version, you should install:
22
35
 
@@ -38,7 +51,7 @@ has_scope gem is available at:
38
51
 
39
52
  And can be installed as:
40
53
 
41
- sudo gem install has_scope
54
+ gem install has_scope
42
55
 
43
56
  == Responders
44
57
 
@@ -51,24 +64,13 @@ how it will change your application:
51
64
 
52
65
  And it can be installed as:
53
66
 
54
- sudo gem install responders
67
+ gem install responders
55
68
 
56
69
  Using responders will set the flash message to :notice and :alert. You can change
57
70
  that through the following configuration value:
58
71
 
59
72
  InheritedResources.flash_keys = [ :success, :failure ]
60
73
 
61
- == Rspec known bug
62
-
63
- Rspec monkey patches a couple of controller methods in a way that Controller specs
64
- (with integrate views true or false) and Inherited Resources are not compatible.
65
-
66
- However, since your controllers inherit from InheritedResources::Base, they are
67
- already unit-tested in the plugin, so there is no need to test them again in Rspec.
68
-
69
- You should test things like url redirection and associations in your integration
70
- specs.
71
-
72
74
  == Basic Usage
73
75
 
74
76
  To use Inherited Resources you just have to inherit (duh) it:
@@ -279,6 +281,25 @@ give a block that expects two arguments, the first will be executed only in
279
281
  success scenarios and the second in failure scenarios. You keep everything
280
282
  clean and organized inside the same action.
281
283
 
284
+ == Smart redirects
285
+
286
+ Although the syntax above is a nice shortcut, you won't need to do it frequently
287
+ because (since version 1.2) Inherited Resources has smart redirects. Redirects
288
+ in actions calculates depending on the existent controller methods.
289
+
290
+ Redirects in create and update actions calculates in following order resource_url,
291
+ collection_url, parent_url (which we are going to see later), root_url. Redirect
292
+ in destroy action calculate in following order collection_url, parent_url, root_url.
293
+
294
+ Example:
295
+
296
+ class ButtonsConntroller < InheritedResources::Base
297
+ belong_to :window
298
+ actions :all, :except => [:show, :index]
299
+ end
300
+
301
+ This controller redirect to parent window after all CUD actions.
302
+
282
303
  == Success and failure scenarios on destroy
283
304
 
284
305
  The destroy action can also fail, this usually happens when you have a
@@ -294,24 +315,6 @@ be something like this:
294
315
  end
295
316
  end
296
317
 
297
- == Some DSL
298
-
299
- For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
300
- your success/failure blocks straight from your class binding. For it, you just
301
- need to add a DSL module to your application controller:
302
-
303
- class ApplicationController < ActionController::Base
304
- include InheritedResources::DSL
305
- end
306
-
307
- And then you can rewrite the last example as:
308
-
309
- class ProjectsController < InheritedResources::Base
310
- update! do |success, failure|
311
- failure.html { redirect_to project_url(@project) }
312
- end
313
- end
314
-
315
318
  == Belongs to
316
319
 
317
320
  Finally, our Projects are going to get some Tasks. Then you create a
@@ -399,7 +402,7 @@ to have two polymorphic associations nested.
399
402
  Later you decide to create a view to show all comments, independent if they belong
400
403
  to a task, file or message. You can reuse your polymorphic controller just doing:
401
404
 
402
- class ProjectsController < InheritedResources::Base
405
+ class CommentsController < InheritedResources::Base
403
406
  belongs_to :task, :file, :message, :optional => true
404
407
  # optional_belongs_to :task, :file, :message
405
408
  end
@@ -435,6 +438,20 @@ option.
435
438
 
436
439
  It will deal with everything again and hide the action :index from you.
437
440
 
441
+ == Namespaced Controllers
442
+
443
+ Namespaced controllers works out the box.
444
+
445
+ class Forum::PostsController < InheritedResources::Base
446
+ end
447
+
448
+ Inherited Resources prioritizes the default resource class for the namespaced controller in
449
+ this order:
450
+
451
+ Forum::Post
452
+ ForumPost
453
+ Post
454
+
438
455
  == URL Helpers
439
456
 
440
457
  When you use InheritedResources it creates some URL helpers.
@@ -478,6 +495,43 @@ Another nice thing is that those urls are not guessed during runtime. They are
478
495
  all created when your application is loaded (except for polymorphic
479
496
  associations, that relies on Rails polymorphic_url).
480
497
 
498
+ == Custom actions
499
+
500
+ Since version 1.2, Inherited Resources allows you to define custom actions in controller:
501
+
502
+ class ButtonsController < InheritedResources::Base
503
+ custom_actions :resource => :delete, :collection => :search
504
+ end
505
+
506
+ This code creates delete and search actions in controller (they behaves like show and
507
+ index actions accordingly). Also, it will produce delete_resource_{path,url} and
508
+ search_resources_{path,url} url helpers.
509
+
510
+ == What about views?
511
+
512
+ Sometimes just DRY the controllers is not enough, if you need to DRY up your views,
513
+ check this Wiki page:
514
+
515
+ https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance
516
+
517
+ == Some DSL
518
+
519
+ For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
520
+ your success/failure blocks straight from your class binding. For it, you just
521
+ need to add a DSL module to your application controller:
522
+
523
+ class ApplicationController < ActionController::Base
524
+ include InheritedResources::DSL
525
+ end
526
+
527
+ And then you can rewrite the last example as:
528
+
529
+ class ProjectsController < InheritedResources::Base
530
+ update! do |success, failure|
531
+ failure.html { redirect_to project_url(@project) }
532
+ end
533
+ end
534
+
481
535
  == Bugs and Feedback
482
536
 
483
537
  If you discover any bugs or want to drop a line, join us in the mailing list:
data/Rakefile CHANGED
@@ -3,28 +3,6 @@
3
3
  require 'rake'
4
4
  require 'rake/testtask'
5
5
  require 'rake/rdoctask'
6
- require File.join(File.dirname(__FILE__), 'lib', 'inherited_resources', 'version')
7
-
8
- begin
9
- require 'jeweler'
10
- Jeweler::Tasks.new do |s|
11
- s.name = "inherited_resources"
12
- s.version = InheritedResources::VERSION.dup
13
- s.rubyforge_project = "inherited_resources"
14
- s.summary = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
15
- s.email = "jose.valim@gmail.com"
16
- s.homepage = "http://github.com/josevalim/inherited_resources"
17
- s.description = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
18
- s.authors = ['José Valim']
19
- s.files = FileList["[A-Z]*", "init.rb", "{lib}/**/*"]
20
- s.add_dependency("responders", "~> 0.6.0")
21
- s.add_dependency("has_scope", "~> 0.5.0")
22
- end
23
-
24
- Jeweler::GemcutterTasks.new
25
- rescue LoadError
26
- puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
- end
28
6
 
29
7
  desc 'Run tests for InheritedResources.'
30
8
  Rake::TestTask.new(:test) do |t|
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "inherited_resources/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "inherited_resources"
7
+ s.version = InheritedResources::VERSION.dup
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
10
+ s.email = "developers@plataformatec.com.br"
11
+ s.homepage = "http://github.com/josevalim/inherited_resources"
12
+ s.description = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
13
+ s.authors = ['José Valim']
14
+
15
+ s.rubyforge_project = "inherited_resources"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency("responders", "~> 0.6.0")
23
+ s.add_dependency("has_scope", "~> 0.5.0")
24
+ end
@@ -6,6 +6,7 @@ module InheritedResources
6
6
  autoload :Actions, 'inherited_resources/actions'
7
7
  autoload :Base, 'inherited_resources/base'
8
8
  autoload :BaseHelpers, 'inherited_resources/base_helpers'
9
+ autoload :ShallowHelpers, 'inherited_resources/shallow_helpers'
9
10
  autoload :BelongsToHelpers, 'inherited_resources/belongs_to_helpers'
10
11
  autoload :ClassMethods, 'inherited_resources/class_methods'
11
12
  autoload :DSL, 'inherited_resources/dsl'
@@ -21,7 +22,11 @@ module InheritedResources
21
22
 
22
23
  class Railtie < ::Rails::Railtie
23
24
  config.inherited_resources = InheritedResources
24
- config.generators.scaffold_controller = :inherited_resources_controller
25
+ if config.respond_to?(:app_generators)
26
+ config.app_generators.scaffold_controller = :inherited_resources_controller
27
+ else
28
+ config.generators.scaffold_controller = :inherited_resources_controller
29
+ end
25
30
  end
26
31
  end
27
32
 
@@ -34,4 +39,4 @@ class ActionController::Base
34
39
  initialize_resources_class_accessors!
35
40
  create_resources_url_helpers!
36
41
  end
37
- end
42
+ end
@@ -31,7 +31,7 @@ module InheritedResources
31
31
  object = build_resource
32
32
 
33
33
  if create_resource(object)
34
- options[:location] ||= resource_url rescue nil
34
+ options[:location] ||= smart_resource_url
35
35
  end
36
36
 
37
37
  respond_with_dual_blocks(object, options, &block)
@@ -42,8 +42,8 @@ module InheritedResources
42
42
  def update(options={}, &block)
43
43
  object = resource
44
44
 
45
- if update_resource(object, params[resource_instance_name])
46
- options[:location] ||= resource_url rescue nil
45
+ if update_resource(object, params[resource_request_name])
46
+ options[:location] ||= smart_resource_url
47
47
  end
48
48
 
49
49
  respond_with_dual_blocks(object, options, &block)
@@ -53,7 +53,7 @@ module InheritedResources
53
53
  # DELETE /resources/1
54
54
  def destroy(options={}, &block)
55
55
  object = resource
56
- options[:location] ||= collection_url rescue nil
56
+ options[:location] ||= smart_collection_url
57
57
 
58
58
  destroy_resource(object)
59
59
  respond_with_dual_blocks(object, options, &block)
@@ -24,18 +24,19 @@ module InheritedResources
24
24
  respond_to :html
25
25
  self.responder = InheritedResources::Responder
26
26
 
27
- helper_method :collection_url, :collection_path, :resource_url, :resource_path,
28
- :new_resource_url, :new_resource_path, :edit_resource_url, :edit_resource_path,
29
- :parent_url, :parent_path, :resource, :collection, :resource_class, :association_chain,
30
- :resource_instance_name, :resource_collection_name
27
+ helper_method :resource, :collection, :resource_class, :association_chain,
28
+ :resource_instance_name, :resource_collection_name,
29
+ :resource_url, :resource_path,
30
+ :collection_url, :collection_path,
31
+ :new_resource_url, :new_resource_path,
32
+ :edit_resource_url, :edit_resource_path,
33
+ :parent_url, :parent_path,
34
+ :smart_resource_url, :smart_collection_url
31
35
 
32
- base.with_options :instance_writer => false do |c|
33
- c.class_inheritable_accessor :resource_class
34
- c.class_inheritable_array :parents_symbols
35
- c.class_inheritable_hash :resources_configuration
36
- end
36
+ self.class_attribute :resource_class, :parents_symbols, :resources_configuration, :instance_writer => false
37
37
 
38
- protected :resource_class, :parents_symbols, :resources_configuration
38
+ protected :resource_class, :parents_symbols, :resources_configuration,
39
+ :resource_class?, :parents_symbols?, :resources_configuration?
39
40
  end
40
41
  end
41
42