nested-resources 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "rails", "3.0.5"
4
+ gem "rspec"
5
+ gem "rspec-rails"
4
6
 
5
7
  # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
6
8
  # gem 'ruby-debug'
data/README.rdoc CHANGED
@@ -2,20 +2,19 @@
2
2
 
3
3
  Your app has routes like this.
4
4
 
5
- namespace :admin do
6
- resources :users do
7
- resources :items
8
- end
5
+ resources :users do
6
+ resources :items
9
7
  end
10
8
 
11
9
  In Items controller.
12
10
  You can write controller like this.
13
11
 
14
- class Admin::ItemsController < ApplicationController
12
+ class ItemsController < ApplicationController
15
13
  nested_controller :user # nested resource
16
14
 
17
15
  def index
18
- @domains = nested.instance(User).domains # find nested resource instance
16
+ @user = nested(User) # find nested resource instance
17
+ @domains = @user.domains
19
18
 
20
19
  respond_to do |format|
21
20
  format.html # index.html.erb
@@ -29,16 +28,34 @@ You can write controller like this.
29
28
 
30
29
  In views.
31
30
 
32
- <%= nested.path(admin_item_path(@item)) %> # => "/admin/users/1/item"
31
+ <%= nested(item_path(@item)) %> # => "/users/1/item"
33
32
 
34
33
  In form view.
35
34
 
36
- <%= form_for(nested.object([:admin,@item])) do |f| %> => [:admin, @user, @item]
35
+ <%= form_for(nested(@item)) do |f| %> => [@user, @item]
37
36
  ...
38
37
  <% end %>
39
38
 
40
39
  = Install
41
40
 
41
+ Add to Gemfile
42
+
43
+ gem 'nested-resources'
44
+
45
+ Run bundle install
46
+
47
+ bundle install
48
+
42
49
  = TIPS
43
50
 
51
+ Has nested path?
52
+
53
+ nested? User
54
+ => true or false
55
+
56
+ Deep nested resources like /users/1/items/2/comments
57
+
58
+ nested_resources [:user, :item]
59
+
60
+
44
61
  This project rocks and uses MIT-LICENSE.
@@ -6,11 +6,12 @@ module NestedResources
6
6
  attr_reader :controller
7
7
  attr_reader :resources
8
8
 
9
- def initialize(params, *resources)
9
+ def initialize(params, resources)
10
10
  @controller = params[:controller].split('/').last
11
11
  @given = {}
12
+ @given_id = {}
12
13
  resources = [] if resources.blank?
13
- resources = [resources] if resources.class != Array
14
+ resources = [resources] unless resources.is_a?(Array)
14
15
  @resources = resources.map{|v|
15
16
  v = v.to_s.underscore if v.is_a?(Class)
16
17
  v.to_sym
@@ -18,7 +19,7 @@ module NestedResources
18
19
  params.each { |k,v|
19
20
  @resources.each{|resource|
20
21
  key = (resource.to_s+"_id").to_sym
21
- @given[resource]= eval(resource.to_s.camelize).find(v) if key == k.to_sym
22
+ @given_id[resource]= v if key == k.to_sym
22
23
  }
23
24
  }
24
25
  end
@@ -28,7 +29,7 @@ module NestedResources
28
29
  if path==controller
29
30
  respath=""
30
31
  @resources.each { |resource|
31
- respath += resource.to_s.pluralize+"/"+@given[resource].id.to_s+"/" unless @given[resource].blank?
32
+ respath += resource.to_s.pluralize+"/"+@given_id[resource].to_s+"/" unless @given_id[resource].blank?
32
33
  }
33
34
  respath+path
34
35
  else
@@ -43,7 +44,7 @@ module NestedResources
43
44
  res.each_index { |i|
44
45
  if i==res.length-1
45
46
  @resources.each{ |resource|
46
- result.push @given[resource] unless @given[resource].blank?
47
+ result.push instance(resource) unless @given_id[resource].blank?
47
48
  }
48
49
  end
49
50
  result.push res[i]
@@ -56,12 +57,12 @@ module NestedResources
56
57
 
57
58
  def instance(name)
58
59
  name = name.to_s.underscore if name.is_a?(Class)
59
- @given[name.to_sym]
60
+ @given[name.to_sym] ||= eval(name.to_s.camelize).find(@given_id[name.to_sym])
60
61
  end
61
62
 
62
63
  def exists?(name)
63
64
  name = name.to_s.underscore if name.is_a?(Class)
64
- !!@given[name.to_sym]
65
+ !!@given_id[name.to_sym]
65
66
  end
66
67
 
67
68
  end
@@ -4,8 +4,18 @@ require 'action_view'
4
4
  module NestedResources
5
5
  require 'nested-resources/nested-resources'
6
6
  module NestedResourcesHelper
7
- def nested
8
- @nested
7
+ def nested(obj = nil)
8
+ if obj.is_a?(Class)
9
+ @nested.instance(obj)
10
+ elsif obj
11
+ @nested.path(obj)
12
+ else
13
+ @nested
14
+ end
15
+ end
16
+
17
+ def nested?(obj)
18
+ @nested.exists?(obj)
9
19
  end
10
20
  end
11
21
  end
@@ -15,7 +25,7 @@ module ActionView::Helpers
15
25
  end
16
26
 
17
27
  class ActionController::Base
18
- attr_reader :nested
28
+ include NestedResources::NestedResourcesHelper
19
29
  before_filter :nested_resources_filter
20
30
 
21
31
  def self.nested_resources(*resources)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: nested-resources
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors: []
8
8
 
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-12 00:00:00 Z
13
+ date: 2011-05-13 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Resolve Rails3 nested resources.