nested-resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.5"
4
+
5
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
6
+ # gem 'ruby-debug'
7
+ # gem 'ruby-debug19'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 mewlist
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = nested-resources
2
+
3
+ Your app has routes like this.
4
+
5
+ namespace :admin do
6
+ resources :users do
7
+ resources :items
8
+ end
9
+ end
10
+
11
+ In Items controller.
12
+ You can write controller like this.
13
+
14
+ class Admin::ItemsController < ApplicationController
15
+ nested_controller :user # nested resource
16
+
17
+ def index
18
+ @domains = nested.instance(User).domains # find nested resource instance
19
+
20
+ respond_to do |format|
21
+ format.html # index.html.erb
22
+ format.xml { render :xml => @domains }
23
+ end
24
+ end
25
+
26
+ ...
27
+
28
+ end
29
+
30
+ In views.
31
+
32
+ <%= nested.path(admin_item_path(@item)) %> # => "/admin/users/1/item"
33
+
34
+ In form view.
35
+
36
+ <%= form_for(nested.object([:admin,@item])) do |f| %> => [:admin, @user, @item]
37
+ ...
38
+ <% end %>
39
+
40
+ = Install
41
+
42
+ = TIPS
43
+
44
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
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
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'Nested-resources'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,69 @@
1
+
2
+
3
+ module NestedResources
4
+
5
+ class NestedResources
6
+ attr_reader :controller
7
+ attr_reader :resources
8
+
9
+ def initialize(params, *resources)
10
+ @controller = params[:controller].split('/').last
11
+ @given = {}
12
+ resources = [] if resources.blank?
13
+ resources = [resources] if resources.class != Array
14
+ @resources = resources.map{|v|
15
+ v = v.to_s.underscore if v.is_a?(Class)
16
+ v.to_sym
17
+ }
18
+ params.each { |k,v|
19
+ @resources.each{|resource|
20
+ key = (resource.to_s+"_id").to_sym
21
+ @given[resource]= eval(resource.to_s.camelize).find(v) if key == k.to_sym
22
+ }
23
+ }
24
+ end
25
+
26
+ def path(original_path)
27
+ original_path.split('/').map { |path|
28
+ if path==controller
29
+ respath=""
30
+ @resources.each { |resource|
31
+ respath += resource.to_s.pluralize+"/"+@given[resource].id.to_s+"/" unless @given[resource].blank?
32
+ }
33
+ respath+path
34
+ else
35
+ path
36
+ end
37
+ }.join("/")
38
+ end
39
+
40
+ def object(res)
41
+ res = [res] if res.class != Array
42
+ result = []
43
+ res.each_index { |i|
44
+ if i==res.length-1
45
+ @resources.each{ |resource|
46
+ result.push @given[resource] unless @given[resource].blank?
47
+ }
48
+ end
49
+ result.push res[i]
50
+ }
51
+ result
52
+ end
53
+
54
+ alias_method :resource, :object
55
+ alias_method :resources, :object
56
+
57
+ def instance(name)
58
+ name = name.to_s.underscore if name.is_a?(Class)
59
+ @given[name.to_sym]
60
+ end
61
+
62
+ def exists?(name)
63
+ name = name.to_s.underscore if name.is_a?(Class)
64
+ !!@given[name.to_sym]
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,32 @@
1
+ require 'action_controller'
2
+ require 'action_view'
3
+
4
+ module NestedResources
5
+ require 'nested-resources/nested-resources'
6
+ module NestedResourcesHelper
7
+ def nested
8
+ @nested
9
+ end
10
+ end
11
+ end
12
+
13
+ module ActionView::Helpers
14
+ include NestedResources::NestedResourcesHelper
15
+ end
16
+
17
+ class ActionController::Base
18
+ attr_reader :nested
19
+ before_filter :nested_resources_filter
20
+
21
+ def self.nested_resources(*resources)
22
+ write_inheritable_attribute(:nested_resources, *resources)
23
+ end
24
+
25
+ def nested_resources_filter
26
+ resources = self.class.read_inheritable_attribute(:nested_resources)
27
+ return if resources.blank?
28
+ @nested = NestedResources::NestedResources.new(params, resources)
29
+ end
30
+
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested-resources
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-12 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Resolve Rails3 nested resources.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/nested-resources/nested-resources.rb
26
+ - lib/nested-resources.rb
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - Gemfile
30
+ - README.rdoc
31
+ homepage:
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.7.2
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Resolve Rails3 nested resources.
58
+ test_files: []
59
+