nested-resources 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -10,11 +10,11 @@ In Items controller.
10
10
  You can write controller like this.
11
11
 
12
12
  class ItemsController < ApplicationController
13
- nested_controller :user # nested resource
13
+ nested_resources :user # nested resource
14
14
 
15
15
  def index
16
- @user = nested(User) # find nested resource instance
17
- @domains = @user.domains
16
+ @user = nested.instance(User) # find nested resource instance
17
+ @items = @user.items
18
18
 
19
19
  respond_to do |format|
20
20
  format.html # index.html.erb
@@ -32,7 +32,7 @@ In views.
32
32
 
33
33
  In form view.
34
34
 
35
- <%= form_for(nested(@item)) do |f| %> => [@user, @item]
35
+ <%= form_for(nested.resource(@item)) do |f| %> => [@user, @item]
36
36
  ...
37
37
  <% end %>
38
38
 
@@ -50,12 +50,20 @@ Run bundle install
50
50
 
51
51
  Has nested path?
52
52
 
53
- nested? User
53
+ nested?(:user)
54
54
  => true or false
55
55
 
56
56
  Deep nested resources like /users/1/items/2/comments
57
57
 
58
58
  nested_resources [:user, :item]
59
59
 
60
+ Recursive resources
61
+
62
+ resources :users, :as => "parents" do
63
+ resources :users
64
+ end
65
+
66
+ nested_resources :user => :parent
67
+
60
68
 
61
69
  This project rocks and uses MIT-LICENSE.
data/Rakefile CHANGED
@@ -7,19 +7,21 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  require 'rake'
10
- require 'rake/rdoctask'
11
10
 
12
- require 'rake/testtask'
11
+ require 'rspec/core'
12
+ require 'rspec/core/rake_task'
13
+ RSpec::Core::RakeTask.new(:spec) do |spec|
14
+ spec.pattern = FileList['spec/**/*_spec.rb']
15
+ end
13
16
 
14
- Rake::TestTask.new(:test) do |t|
15
- t.libs << 'lib'
16
- t.libs << 'test'
17
- t.pattern = 'test/**/*_test.rb'
18
- t.verbose = false
17
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
18
+ spec.pattern = 'spec/**/*_spec.rb'
19
+ spec.rcov = true
19
20
  end
20
21
 
21
- task :default => :test
22
+ task :default => :spec
22
23
 
24
+ require 'rake/rdoctask'
23
25
  Rake::RDocTask.new(:rdoc) do |rdoc|
24
26
  rdoc.rdoc_dir = 'rdoc'
25
27
  rdoc.title = 'Nested-resources'
@@ -6,29 +6,35 @@ 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
12
  @given_id = {}
13
13
  resources = [] if resources.blank?
14
- resources = [resources] unless resources.is_a?(Array)
15
- @resources = resources.map{|v|
16
- v = v.to_s.underscore if v.is_a?(Class)
17
- v.to_sym
18
- }
14
+ parse(resources)
19
15
  params.each { |k,v|
20
- @resources.each{|resource|
21
- key = (resource.to_s+"_id").to_sym
22
- @given_id[resource]= v if key == k.to_sym
16
+ @resources.each{|resource, resource_id|
17
+ @given_id[resource]= v if resource_id == k.to_sym
23
18
  }
24
19
  }
25
20
  end
26
21
 
22
+ def parse(resource)
23
+ @resources ||= {}
24
+ if resource.is_a?(Array)
25
+ resource.each{|v| parse(v) }
26
+ elsif resource.is_a?(Hash)
27
+ resource.each{|k, v| @resources[k.to_sym] = (v.to_s.underscore+"_id").to_sym}
28
+ else
29
+ @resources[resource.to_sym] = (resource.to_s.underscore+"_id").to_sym
30
+ end
31
+ end
32
+
27
33
  def path(original_path)
28
34
  original_path.split('/').map { |path|
29
35
  if path==controller
30
36
  respath=""
31
- @resources.each { |resource|
37
+ @resources.each { |resource, resource_id|
32
38
  respath += resource.to_s.pluralize+"/"+@given_id[resource].to_s+"/" unless @given_id[resource].blank?
33
39
  }
34
40
  respath+path
@@ -43,7 +49,7 @@ module NestedResources
43
49
  result = []
44
50
  res.each_index { |i|
45
51
  if i==res.length-1
46
- @resources.each{ |resource|
52
+ @resources.each{ |resource, resource_id|
47
53
  result.push instance(resource) unless @given_id[resource].blank?
48
54
  }
49
55
  end
@@ -56,12 +62,12 @@ module NestedResources
56
62
  alias_method :resources, :object
57
63
 
58
64
  def instance(name)
59
- name = name.to_s.underscore if name.is_a?(Class)
60
- @given[name.to_sym] ||= eval(name.to_s.camelize).find(@given_id[name.to_sym])
65
+ name = name.to_s.underscore
66
+ @given[name.to_sym] ||= name.to_s.camelize.constantize.find(@given_id[name.to_sym])
61
67
  end
62
68
 
63
69
  def exists?(name)
64
- name = name.to_s.underscore if name.is_a?(Class)
70
+ name = name.to_s.underscore
65
71
  !!@given_id[name.to_sym]
66
72
  end
67
73
 
@@ -6,16 +6,18 @@ module NestedResources
6
6
  module NestedResourcesHelper
7
7
  def nested(obj = nil)
8
8
  if obj.is_a?(Class)
9
- @nested.instance(obj)
9
+ @nested_resources.instance(obj)
10
+ elsif obj.is_a?(String)
11
+ @nested_resources.path(obj)
10
12
  elsif obj
11
- @nested.path(obj)
13
+ @nested_resources.resources(obj)
12
14
  else
13
- @nested
15
+ @nested_resources
14
16
  end
15
17
  end
16
18
 
17
19
  def nested?(obj)
18
- @nested.exists?(obj)
20
+ @nested_resources.exists?(obj)
19
21
  end
20
22
  end
21
23
  end
@@ -35,7 +37,7 @@ class ActionController::Base
35
37
  def nested_resources_filter
36
38
  resources = self.class.read_inheritable_attribute(:nested_resources)
37
39
  return if resources.blank?
38
- @nested = NestedResources::NestedResources.new(params, resources)
40
+ @nested_resources = NestedResources::NestedResources.new(params, resources)
39
41
  end
40
42
 
41
43
  end
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.2
5
+ version: 0.0.3
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-13 00:00:00 Z
13
+ date: 2011-05-17 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Resolve Rails3 nested resources.