risosu-san 0.1.0
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/LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +44 -0
- data/lib/risosu_san.rb +58 -0
- data/rails/init.rb +2 -0
- data/test/risosu_san_test.rb +90 -0
- data/test/test_helper.rb +75 -0
- metadata +63 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2009 Fingertips, Eloy Duran <eloy.de.enige@gmail.com>
|
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,19 @@
|
|
1
|
+
= Risosu-san
|
2
|
+
|
3
|
+
A lean mixin for <tt>ActionController::Base</tt> that assists in situations
|
4
|
+
where a resource controller is nested under another resource. Eg:
|
5
|
+
|
6
|
+
/members/24/passwords/new
|
7
|
+
|
8
|
+
In this example, retrieving the parent resource is as simple as:
|
9
|
+
|
10
|
+
class PasswordsController < ActionController::Base
|
11
|
+
find_parent_resource :only => :new
|
12
|
+
|
13
|
+
def new
|
14
|
+
@parent_resource # => #<Member id: 24>
|
15
|
+
@member # => #<Member id: 24>
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
See RisosuSan for a bit more in depth documentation.
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the risosu-san plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the risosu-san plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Risosu-san'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '--charset=utf8'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('LICENSE')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'jeweler'
|
28
|
+
Jeweler::Tasks.new do |s|
|
29
|
+
s.name = "risosu-san"
|
30
|
+
s.homepage = "http://github.com/Fingertips/risosu-san"
|
31
|
+
s.email = "eloy.de.enige@gmail.com"
|
32
|
+
s.authors = ["Eloy Duran"]
|
33
|
+
s.summary = s.description = "RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource."
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
end
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'jewelry_portfolio/tasks'
|
40
|
+
JewelryPortfolio::Tasks.new do |p|
|
41
|
+
p.account = 'Fingertips'
|
42
|
+
end
|
43
|
+
rescue LoadError
|
44
|
+
end
|
data/lib/risosu_san.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module RisosuSan
|
2
|
+
def self.included(klass) #:nodoc:
|
3
|
+
klass.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Adds a before filter which will take care of finding the parent resource.
|
8
|
+
#
|
9
|
+
# class PasswordsController < ActionController::Base
|
10
|
+
# find_parent_resource :only => :new
|
11
|
+
# end
|
12
|
+
def find_parent_resource(options = {})
|
13
|
+
before_filter :find_parent_resource, options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
# Returns whether or not the request for the current resource is nested under
|
20
|
+
# a parent resource, by reflecting on the params.
|
21
|
+
#
|
22
|
+
# params # => { :id => 42 }
|
23
|
+
# nested? # => false
|
24
|
+
#
|
25
|
+
# params # => { :member_id => 24, :id => 42 }
|
26
|
+
# nested? # => true
|
27
|
+
def nested?
|
28
|
+
!parent_resource_params.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a hash of params for the parent resource, if available.
|
32
|
+
#
|
33
|
+
# params # => { :member_id => 24, :id => 42 }
|
34
|
+
# parent_resource_params # => { :param => :member_id, :id => 24, :name => 'member', :class_name => 'Member', :class => Member }
|
35
|
+
def parent_resource_params
|
36
|
+
@parent_resource_params ||=
|
37
|
+
if key = params.keys.find { |k| k =~ /^(\w+)_id$/ }
|
38
|
+
{ :param => key, :id => params[key], :name => $1, :class_name => $1.classify, :class => $1.classify.constantize }
|
39
|
+
else
|
40
|
+
{}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Finds the parent resource, if available, and assigns it to
|
45
|
+
# <tt>@parent_resource</tt> and an instance variable with the name of the
|
46
|
+
# resource.
|
47
|
+
#
|
48
|
+
# params # => { :member_id => 24, :id => 42 }
|
49
|
+
# find_parent_resource
|
50
|
+
# @parent_resource # => #<Member id: 24>
|
51
|
+
# @member # => #<Member id: 24>
|
52
|
+
def find_parent_resource
|
53
|
+
if @parent_resource.nil? && nested? && @parent_resource = parent_resource_params[:class].find(parent_resource_params[:id])
|
54
|
+
instance_variable_set("@#{parent_resource_params[:name]}", @parent_resource)
|
55
|
+
end
|
56
|
+
@parent_resource
|
57
|
+
end
|
58
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class TestController < ActionController::Base
|
4
|
+
public :nested?, :parent_resource_params, :find_parent_resource
|
5
|
+
|
6
|
+
attr_reader :params
|
7
|
+
def params=(params)
|
8
|
+
@params = params.with_indifferent_access
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class CamelCaseTest
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "RisosuSan, at the class level" do
|
16
|
+
it "should define a before_filter which finds the parent resource" do
|
17
|
+
TestController.expects(:before_filter).with(:find_parent_resource, {})
|
18
|
+
TestController.find_parent_resource
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should forward options to the before_filter" do
|
22
|
+
TestController.expects(:before_filter).with(:find_parent_resource, :only => :index)
|
23
|
+
TestController.find_parent_resource :only => :index
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "RisosuSan" do
|
28
|
+
attr_accessor :controller
|
29
|
+
|
30
|
+
before do
|
31
|
+
RisosuSanTest::Initializer.setup_database
|
32
|
+
|
33
|
+
@controller = TestController.new
|
34
|
+
@member = Member.create(:name => 'Eloy')
|
35
|
+
end
|
36
|
+
|
37
|
+
after do
|
38
|
+
RisosuSanTest::Initializer.teardown_database
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should know if it's not a nested request" do
|
42
|
+
controller.params = {}
|
43
|
+
controller.should.not.be.nested
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should know if this is a nested request" do
|
47
|
+
controller.params = { :member_id => 12 }
|
48
|
+
controller.should.be.nested
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should know the parent resource params" do
|
52
|
+
controller.params = { :member_id => 12, :id => 34 }
|
53
|
+
controller.parent_resource_params.should == { :name => 'member', :class => Member, :param => 'member_id', :class_name => 'Member', :id => 12 }
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should know the parent resource params for camelcased classes" do
|
57
|
+
controller.params = { :camel_case_test_id => 12, :id => 34 }
|
58
|
+
controller.parent_resource_params.should == { :name => 'camel_case_test', :class => CamelCaseTest, :param => 'camel_case_test_id', :class_name => 'CamelCaseTest', :id => 12 }
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should have cached the parent_resource_params" do
|
62
|
+
controller.params = { :member_id => 12, :id => 34 }
|
63
|
+
params = controller.parent_resource_params
|
64
|
+
controller.parent_resource_params.should.be params
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should find the nested resource" do
|
68
|
+
controller.params = { :member_id => @member.to_param }
|
69
|
+
controller.find_parent_resource
|
70
|
+
assigns(:parent_resource).should == @member
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should also set an instance variable named after the parent resource" do
|
74
|
+
controller.params = { :member_id => @member.to_param }
|
75
|
+
controller.find_parent_resource.should == @member
|
76
|
+
assigns(:member).should == @member
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return nil if the resource isn't nested" do
|
80
|
+
controller.params = {}
|
81
|
+
controller.find_parent_resource.should.be nil
|
82
|
+
assigns(:parent_resource).should.be nil
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def assigns(name)
|
88
|
+
controller.instance_variable_get("@#{name}")
|
89
|
+
end
|
90
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
module RisosuSanTest
|
2
|
+
module Initializer
|
3
|
+
VENDOR_RAILS = File.expand_path('../../../../rails', __FILE__)
|
4
|
+
OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
|
5
|
+
PLUGIN_ROOT = File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
def self.rails_directory
|
8
|
+
if File.exist?(File.join(VENDOR_RAILS, 'railties'))
|
9
|
+
VENDOR_RAILS
|
10
|
+
elsif File.exist?(File.join(OTHER_RAILS, 'railties'))
|
11
|
+
OTHER_RAILS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load_dependencies
|
16
|
+
if rails_directory
|
17
|
+
$:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
|
18
|
+
$:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
|
19
|
+
$:.unshift(File.join(rails_directory, 'actionpack', 'lib'))
|
20
|
+
else
|
21
|
+
require 'rubygems' rescue LoadError
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'active_support'
|
25
|
+
require 'active_record'
|
26
|
+
require 'action_controller'
|
27
|
+
|
28
|
+
require 'rubygems' rescue LoadError
|
29
|
+
|
30
|
+
require 'test/spec'
|
31
|
+
require 'mocha'
|
32
|
+
|
33
|
+
$:.unshift(File.join(PLUGIN_ROOT, 'lib'))
|
34
|
+
require File.join(PLUGIN_ROOT, 'rails', 'init')
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.configure_database
|
38
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
39
|
+
ActiveRecord::Migration.verbose = false
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.setup_database
|
43
|
+
ActiveRecord::Schema.define(:version => 1) do
|
44
|
+
create_table :members do |t|
|
45
|
+
t.column :name, :string
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.teardown_database
|
51
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
52
|
+
ActiveRecord::Base.connection.drop_table(table)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.start
|
57
|
+
load_dependencies
|
58
|
+
configure_database
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
RisosuSanTest::Initializer.start
|
64
|
+
|
65
|
+
# class RisosuSan::PageScope
|
66
|
+
# instance_methods.each { |method| alias_method method.sub(/^has_/, 'have_'), method if method =~ /^has_/ }
|
67
|
+
#
|
68
|
+
# # The delegation of all methods in NamedScope breaks #should.
|
69
|
+
# def should
|
70
|
+
# Test::Spec::Should.new(self)
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
|
74
|
+
class Member < ActiveRecord::Base
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: risosu-san
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eloy Duran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-26 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.
|
17
|
+
email: eloy.de.enige@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- lib/risosu_san.rb
|
30
|
+
- rails/init.rb
|
31
|
+
- test/risosu_san_test.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/Fingertips/risosu-san
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.
|
61
|
+
test_files:
|
62
|
+
- test/risosu_san_test.rb
|
63
|
+
- test/test_helper.rb
|