resourceful_parenting 0.1
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/Gemfile +6 -0
- data/MIT-LICENSE +20 -0
- data/README +66 -0
- data/Rakefile +25 -0
- data/init.rb +3 -0
- data/lib/resourceful_parenting.rb +110 -0
- data/resourceful_parenting.gemspec +28 -0
- data/test/resourceful_parenting_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +119 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mina Naguib
|
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
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
ResourcefulParenting
|
2
|
+
====================
|
3
|
+
|
4
|
+
A rails plugin that makes working with nested resources easier.
|
5
|
+
|
6
|
+
It provides the following methods to your RESTful controllers:
|
7
|
+
|
8
|
+
* parent : Returns the most immediate parent resource
|
9
|
+
* parent(-1) : Same as parent
|
10
|
+
* parent(-2) : Returns the parent resource's parent
|
11
|
+
* parent(N) : Returns the Nth parent resource
|
12
|
+
* parent(0) : Returns the top level parent
|
13
|
+
* parents : Returns an array of all the parent resources
|
14
|
+
* polymorphic_p_url : Same as calling polymorphic_url but adding parents to the list
|
15
|
+
* polymorphic_p_path : Same as calling polymorphic_path but adding parents to the list
|
16
|
+
|
17
|
+
polymotphic_p_url and polymorphic_p_path are automatically exposed as helper methods for usage in the views. Feel free to expose the others via helper_method in your ApplicationController
|
18
|
+
|
19
|
+
|
20
|
+
Installation
|
21
|
+
============
|
22
|
+
|
23
|
+
Add to your Gemfile:
|
24
|
+
|
25
|
+
gem 'resourceful_parenting'
|
26
|
+
|
27
|
+
|
28
|
+
Example
|
29
|
+
=======
|
30
|
+
|
31
|
+
Assuming the classical problem of a comments resource nested under several different parent resources:
|
32
|
+
|
33
|
+
class CommentsController < ApplicationController
|
34
|
+
|
35
|
+
def create
|
36
|
+
# regular create work, then:
|
37
|
+
|
38
|
+
redirect_to parent # Back to the parent's show page
|
39
|
+
# OR
|
40
|
+
redirect_to polymorphic_p_url(@comment) # Back to /parents/ID/comments/ID
|
41
|
+
# OR
|
42
|
+
redirect_to polymorphic_p_url(:comments) # Back to /parents/ID/comments
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
Todo
|
50
|
+
====
|
51
|
+
|
52
|
+
A respectable test suite. This plugin was just a bunch of ApplicationController methods I decided to extract.
|
53
|
+
|
54
|
+
|
55
|
+
Notes
|
56
|
+
=====
|
57
|
+
|
58
|
+
There is similar functionality in other plugins, however they often do lots more which I didn't need.
|
59
|
+
|
60
|
+
This plugin was inspired after watching Ryan Bates hand-roll a custom method for doing this for the Nth time.
|
61
|
+
|
62
|
+
|
63
|
+
Copyright
|
64
|
+
=========
|
65
|
+
|
66
|
+
Copyright (c) 2009 Mina Naguib, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
#require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
# Run the test with 'rake' or 'rake test'
|
8
|
+
desc 'Default: run resourceful_parenting unit tests.'
|
9
|
+
task :default => :test
|
10
|
+
|
11
|
+
desc 'Test the resourceful_parenting plugin.'
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << 'lib' << 'test'
|
14
|
+
t.pattern = 'test/**/test_*.rb'
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
# Run the rdoc task to generate rdocs for this gem
|
19
|
+
require 'rdoc/task'
|
20
|
+
RDoc::Task.new do |rdoc|
|
21
|
+
rdoc.rdoc_dir = 'rdoc'
|
22
|
+
rdoc.title = "resourceful_parenting"
|
23
|
+
rdoc.rdoc_files.include('README*')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
module ResourcefulParenting; module ActionController; module Base;
|
2
|
+
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
include InstanceMethods
|
6
|
+
helper_method :polymorphic_p_url, :polymorphic_p_path
|
7
|
+
end
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
|
13
|
+
#
|
14
|
+
# Analogous to Rails' polymorphic_url, but add all parents to call
|
15
|
+
#
|
16
|
+
def polymorphic_p_url(record_or_hash_or_array, options = {})
|
17
|
+
record_or_hash_or_array = [record_or_hash_or_array] unless record_or_hash_or_array.is_a?(Array)
|
18
|
+
record_or_hash_or_array = parents + record_or_hash_or_array
|
19
|
+
polymorphic_url(record_or_hash_or_array, options)
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Analogous to Rails' polymorphic_path, but add all parents to call
|
24
|
+
#
|
25
|
+
def polymorphic_p_path(record_or_hash_or_array, options = {})
|
26
|
+
polymorphic_p_url(record_or_hash_or_array, options.merge(:routing_type => :path))
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Returns an array of parent objects
|
31
|
+
#
|
32
|
+
def parents
|
33
|
+
parse_parents.map do |info|
|
34
|
+
instance_from_parent_info(info)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Returns a parent object extracted from a nested URL
|
40
|
+
# Takes an array index of depth (how far up parents to get) - defaults to -1 (most immediate)
|
41
|
+
# Returns nil if no such parent exists
|
42
|
+
#
|
43
|
+
def parent(level = -1)
|
44
|
+
info = parse_parents[level]
|
45
|
+
instance_from_parent_info(info)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
#
|
51
|
+
# Returns an array of hashes
|
52
|
+
# Each hash is an info block (opaque as far as you're concerned) about a single parent in the lineage
|
53
|
+
#
|
54
|
+
def parse_parents
|
55
|
+
|
56
|
+
if !@_parents_info
|
57
|
+
|
58
|
+
#
|
59
|
+
# Populate @_parents_info
|
60
|
+
#
|
61
|
+
@_parents_info = []
|
62
|
+
request.path_parameters.each do |key, value|
|
63
|
+
next unless key.to_s.match(/^(.+)_id$/)
|
64
|
+
singular = $1.singularize
|
65
|
+
@_parents_info << {
|
66
|
+
:order => nil,
|
67
|
+
:singular => singular,
|
68
|
+
:class => singular.classify.constantize,
|
69
|
+
:id => value,
|
70
|
+
:instance => nil
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Properly sort it
|
76
|
+
#
|
77
|
+
path_parts = request.path.split("/")
|
78
|
+
path_parts.each_with_index do |part, i|
|
79
|
+
next unless part =~ /^[a-z_]+$/
|
80
|
+
singular = part.singularize or next
|
81
|
+
info = @_parents_info.detect{|p| p[:singular] == singular}
|
82
|
+
info[:order] = i if info
|
83
|
+
end
|
84
|
+
@_parents_info = @_parents_info.sort_by{|i| i[:order]}
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
return @_parents_info
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
def instance_from_parent_info(info)
|
93
|
+
return nil unless info
|
94
|
+
if !info[:instance]
|
95
|
+
if info[:class].respond_to?(:from_param)
|
96
|
+
info[:instance] = info[:class].from_param(info[:id])
|
97
|
+
else
|
98
|
+
info[:instance] = info[:class].find(info[:id])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
info[:instance]
|
102
|
+
end
|
103
|
+
|
104
|
+
end # module InstanceMethods
|
105
|
+
|
106
|
+
end;end;end
|
107
|
+
|
108
|
+
ActionController::Base.class_eval do
|
109
|
+
include ResourcefulParenting::ActionController::Base unless include?(ResourcefulParenting::ActionController::Base)
|
110
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
|
5
|
+
# Description Meta...
|
6
|
+
s.name = 'resourceful_parenting'
|
7
|
+
s.version = 0.1
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Mina Naguib']
|
10
|
+
s.email = ['mina.gems@naguib.ca']
|
11
|
+
s.homepage = 'http://github.com/minaguib/resourceful_parenting'
|
12
|
+
s.summary = %q{A rails plugin that makes working with nested resources easier.}
|
13
|
+
s.description = %q{A rails plugin that makes working with nested resources easier.}
|
14
|
+
s.rubyforge_project = 'resourceful_parenting'
|
15
|
+
|
16
|
+
|
17
|
+
# Load Paths...
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
|
23
|
+
|
24
|
+
# Dependencies (installed via 'bundle install')...
|
25
|
+
s.add_development_dependency("bundler", [">= 1.0.0"])
|
26
|
+
s.add_development_dependency("actionpack", [">= 3.0.0"])
|
27
|
+
s.add_development_dependency("rdoc")
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resourceful_parenting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Mina Naguib
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-05-25 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: bundler
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 23
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 1.0.0
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: actionpack
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 7
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 3.0.0
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rdoc
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: A rails plugin that makes working with nested resources easier.
|
66
|
+
email:
|
67
|
+
- mina.gems@naguib.ca
|
68
|
+
executables: []
|
69
|
+
|
70
|
+
extensions: []
|
71
|
+
|
72
|
+
extra_rdoc_files: []
|
73
|
+
|
74
|
+
files:
|
75
|
+
- Gemfile
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README
|
78
|
+
- Rakefile
|
79
|
+
- init.rb
|
80
|
+
- lib/resourceful_parenting.rb
|
81
|
+
- resourceful_parenting.gemspec
|
82
|
+
- test/resourceful_parenting_test.rb
|
83
|
+
- test/test_helper.rb
|
84
|
+
homepage: http://github.com/minaguib/resourceful_parenting
|
85
|
+
licenses: []
|
86
|
+
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
requirements: []
|
111
|
+
|
112
|
+
rubyforge_project: resourceful_parenting
|
113
|
+
rubygems_version: 1.8.24
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: A rails plugin that makes working with nested resources easier.
|
117
|
+
test_files:
|
118
|
+
- test/resourceful_parenting_test.rb
|
119
|
+
- test/test_helper.rb
|