matsimitsu-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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ rdoc
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,37 @@
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 = "matsimitsu-risosu-san"
30
+ s.homepage = "http://github.com/Matsimitsu/risosu-san"
31
+ s.email = "robert@matsimitsu.nl"
32
+ s.authors = ["Eloy Duran", "Robert Beekman"]
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
+ end
36
+
37
+ Jeweler::GemcutterTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/risosu_san.rb ADDED
@@ -0,0 +1,72 @@
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, :field => 'slug'
11
+ # end
12
+ def find_parent_resource(options = {})
13
+ if options[:field] then
14
+ field = options[:field]
15
+ options.delete(:field)
16
+ end
17
+ before_filter options do
18
+ find_parent_resource(field)
19
+ end
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ # Returns whether or not the request for the current resource is nested under
26
+ # a parent resource, by reflecting on the params.
27
+ #
28
+ # params # => { :id => 42 }
29
+ # nested? # => false
30
+ #
31
+ # params # => { :member_id => 24, :id => 42 }
32
+ # nested? # => true
33
+ def nested?
34
+ !parent_resource_params.empty?
35
+ end
36
+
37
+ # Returns a hash of params for the parent resource, if available.
38
+ #
39
+ # params # => { :member_id => 24, :id => 42 }
40
+ # parent_resource_params # => { :param => :member_id, :id => 24, :name => 'member', :class_name => 'Member', :class => Member }
41
+ def parent_resource_params
42
+ @parent_resource_params ||=
43
+ if key = params.keys.find { |k| k =~ /^(\w+)_id$/ }
44
+ { :param => key, :id => params[key], :name => $1, :class_name => $1.classify, :class => $1.classify.constantize }
45
+ else
46
+ {}
47
+ end
48
+ end
49
+
50
+ # Finds the parent resource, if available, and assigns it to
51
+ # <tt>@parent_resource</tt> and an instance variable with the name of the
52
+ # resource.
53
+ #
54
+ # params # => { :member_id => 24, :id => 42 }
55
+ # find_parent_resource
56
+ # @parent_resource # => #<Member id: 24>
57
+ # @member # => #<Member id: 24>
58
+ #
59
+ # OR:
60
+ #
61
+ # params # => { :member_id => 24, :id => 'this-is-a-unique-slug }
62
+ # find_parent_resource, :field => 'slug'
63
+ # @parent_resource # => #<Member id: 24>
64
+ # @member # => #<Member id: 24>
65
+ def find_parent_resource(field=nil)
66
+ finder_sender = field ? "find_by_#{field}" : 'find'
67
+ if @parent_resource.nil? && nested? && @parent_resource = parent_resource_params[:class].send(finder_sender, parent_resource_params[:id])
68
+ instance_variable_set("@#{parent_resource_params[:name]}", @parent_resource)
69
+ end
70
+ @parent_resource
71
+ end
72
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'risosu_san'
2
+ ActionController::Base.send :include, RisosuSan
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{risosu-san}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Eloy Duran"]
12
+ s.date = %q{2009-10-26}
13
+ s.description = %q{RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.}
14
+ s.email = %q{eloy.de.enige@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "lib/risosu_san.rb",
24
+ "rails/init.rb",
25
+ "test/risosu_san_test.rb",
26
+ "test/test_helper.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/Fingertips/risosu-san}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.5}
32
+ s.summary = %q{RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.}
33
+ s.test_files = [
34
+ "test/risosu_san_test.rb",
35
+ "test/test_helper.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
48
+
@@ -0,0 +1,104 @@
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 FieldTestController < ActionController::Base
13
+ public :nested?, :parent_resource_params, :find_parent_resource
14
+
15
+ attr_reader :params
16
+ def params=(params)
17
+ @params = params.with_indifferent_access
18
+ end
19
+ end
20
+
21
+
22
+ class CamelCaseTest
23
+ end
24
+
25
+ describe "RisosuSan, at the class level" do
26
+ it "should define a before_filter which finds the parent resource" do
27
+ TestController.find_parent_resource
28
+ end
29
+
30
+ it "should forward options to the before_filter" do
31
+ TestController.find_parent_resource :only => :index
32
+ end
33
+ end
34
+
35
+ describe "RisosuSan" do
36
+ attr_accessor :controller
37
+
38
+ before do
39
+ RisosuSanTest::Initializer.setup_database
40
+
41
+ @controller = TestController.new
42
+ @member = Member.create(:name => 'Eloy')
43
+ end
44
+
45
+ after do
46
+ RisosuSanTest::Initializer.teardown_database
47
+ end
48
+
49
+ it "should know if it's not a nested request" do
50
+ controller.params = {}
51
+ controller.should.not.be.nested
52
+ end
53
+
54
+ it "should know if this is a nested request" do
55
+ controller.params = { :member_id => 12 }
56
+ controller.should.be.nested
57
+ end
58
+
59
+ it "should know the parent resource params" do
60
+ controller.params = { :member_id => 12, :id => 34 }
61
+ controller.parent_resource_params.should == { :name => 'member', :class => Member, :param => 'member_id', :class_name => 'Member', :id => 12 }
62
+ end
63
+
64
+ it "should know the parent resource params for camelcased classes" do
65
+ controller.params = { :camel_case_test_id => 12, :id => 34 }
66
+ controller.parent_resource_params.should == { :name => 'camel_case_test', :class => CamelCaseTest, :param => 'camel_case_test_id', :class_name => 'CamelCaseTest', :id => 12 }
67
+ end
68
+
69
+ it "should have cached the parent_resource_params" do
70
+ controller.params = { :member_id => 12, :id => 34 }
71
+ params = controller.parent_resource_params
72
+ controller.parent_resource_params.should.be params
73
+ end
74
+
75
+ it "should find the nested resource" do
76
+ controller.params = { :member_id => @member.to_param }
77
+ controller.find_parent_resource
78
+ assigns(:parent_resource).should == @member
79
+ end
80
+
81
+ it "should also set an instance variable named after the parent resource" do
82
+ controller.params = { :member_id => @member.to_param }
83
+ controller.find_parent_resource.should == @member
84
+ assigns(:member).should == @member
85
+ end
86
+
87
+ it "should also find by the correct field" do
88
+ controller.params = { :member_id => @member.name }
89
+ controller.find_parent_resource('name').should == @member
90
+ assigns(:member).should == @member
91
+ end
92
+
93
+ it "should return nil if the resource isn't nested" do
94
+ controller.params = {}
95
+ controller.find_parent_resource.should.be nil
96
+ assigns(:parent_resource).should.be nil
97
+ end
98
+
99
+ private
100
+
101
+ def assigns(name)
102
+ controller.instance_variable_get("@#{name}")
103
+ end
104
+ end
@@ -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", :database => ":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,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matsimitsu-risosu-san
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eloy Duran
8
+ - Robert Beekman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-01-14 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.
18
+ email: robert@matsimitsu.nl
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - LICENSE
25
+ - README.rdoc
26
+ files:
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/risosu_san.rb
33
+ - rails/init.rb
34
+ - risosu-san.gemspec
35
+ - test/risosu_san_test.rb
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/Matsimitsu/risosu-san
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: RisosuSan is a Rails plugin that assists in situations where a resource controller is nested under another resource.
65
+ test_files:
66
+ - test/risosu_san_test.rb
67
+ - test/test_helper.rb