acts_as_polymorphic_controller 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +3 -0
- data/.rspec +1 -0
- data/ChangeLog.rdoc +4 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +55 -0
- data/Rakefile +21 -0
- data/acts_as_polymorphic_controller.gemspec +10 -0
- data/gemspec.yml +11 -0
- data/lib/acts_as_polymorphic_controller.rb +2 -0
- data/lib/acts_as_polymorphic_controller/controller.rb +47 -0
- data/lib/acts_as_polymorphic_controller/version.rb +4 -0
- data/spec/acts_as_polymorphic_controller_spec.rb +8 -0
- data/spec/spec_helper.rb +5 -0
- metadata +112 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/ChangeLog.rdoc
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 rob
|
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,55 @@
|
|
1
|
+
= acts_as_polymorphic_controller
|
2
|
+
|
3
|
+
* {Homepage}[http://rubygems.org/gems/acts_as_polymorphic_controller]
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
acts_as_polymorphic_controller is a bit of code written to assist me with a Rails3 project. I became increasingly tired of handling polymorphism by using if/case statements:
|
8
|
+
|
9
|
+
if params[:user_id]
|
10
|
+
@obj = User.find(params[:user_id])
|
11
|
+
elsif params[:group_id]
|
12
|
+
@obj = Group.find(params[:group_id])
|
13
|
+
elsif params[:whatever_id]
|
14
|
+
@obj = Whatever.find(params[:whatever_id])
|
15
|
+
end
|
16
|
+
|
17
|
+
This gem aims to alleviate that annoyance.
|
18
|
+
|
19
|
+
== Features
|
20
|
+
|
21
|
+
== Examples
|
22
|
+
|
23
|
+
In ApplicationController.rb
|
24
|
+
include ActsAsPolymorphicController
|
25
|
+
|
26
|
+
In some controller file, such as: BlogPostsController.rb
|
27
|
+
class BlogPostsController < ApplicationController
|
28
|
+
acts_as_polymorphic_controller
|
29
|
+
|
30
|
+
# These are the models that have_many BlogPosts, :as
|
31
|
+
aapc_resources :user, :group, :event
|
32
|
+
|
33
|
+
def index
|
34
|
+
@object = aapc_parent_object
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
In this example, each of the following URLs would return the appropriate
|
39
|
+
instace into the @object variable:
|
40
|
+
|
41
|
+
/users/5/blog_posts (User :id => 5)
|
42
|
+
/groups/3/blog_posts (Group :id => 3)
|
43
|
+
/events/12/blog_posts (Event :id => 12)
|
44
|
+
|
45
|
+
== Requirements
|
46
|
+
|
47
|
+
== Install
|
48
|
+
|
49
|
+
$ gem install acts_as_polymorphic_controller
|
50
|
+
|
51
|
+
== Copyright
|
52
|
+
|
53
|
+
Copyright (c) 2010 rob
|
54
|
+
|
55
|
+
See LICENSE.txt for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
gem 'rspec', '~> 2.1.0'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new
|
9
|
+
rescue LoadError => e
|
10
|
+
task :spec do
|
11
|
+
abort "Please run `gem install rspec` to install RSpec."
|
12
|
+
end
|
13
|
+
end
|
14
|
+
task :default => :spec
|
15
|
+
|
16
|
+
require 'rake/rdoctask'
|
17
|
+
Rake::RDocTask.new do |rdoc|
|
18
|
+
rdoc.title = "acts_as_polymorphic_controller"
|
19
|
+
rdoc.rdoc_files.include("README.rdoc")
|
20
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
21
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
begin
|
4
|
+
Ore::Specification.new do |gemspec|
|
5
|
+
# custom logic here
|
6
|
+
end
|
7
|
+
rescue NameError
|
8
|
+
STDERR.puts "The 'acts_as_polymorphic_controller.gemspec' file requires Ore."
|
9
|
+
STDERR.puts "Run `gem install ore-core` to install Ore."
|
10
|
+
end
|
data/gemspec.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
name: acts_as_polymorphic_controller
|
2
|
+
summary: "A ruby gem for making polymorphism in controllers easier."
|
3
|
+
description: "A ruby gem for making polymorphism in controllers easier to handle. This alleviates the need for if/case statements to handle parent classes."
|
4
|
+
license: MIT
|
5
|
+
authors: Rob Flynn
|
6
|
+
homepage: https://github.com/robflynn/acts_as_polymorphic_controller
|
7
|
+
email: rob@thingerly.com
|
8
|
+
|
9
|
+
development_dependencies:
|
10
|
+
ore-core: ~> 0.1.0
|
11
|
+
rspec: ~> 2.1.0
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ActsAsPolymorphicController
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def acts_as_polymorphic_controller
|
8
|
+
# Set up the class methods
|
9
|
+
(class << self; self; end).instance_eval do
|
10
|
+
define_method :aapc_resources do |*resources|
|
11
|
+
define_method(:initialize) do
|
12
|
+
super
|
13
|
+
|
14
|
+
@aapc_parent_objects = resources
|
15
|
+
end #define initialize
|
16
|
+
end # define aapc_resources
|
17
|
+
end # instance eval
|
18
|
+
|
19
|
+
# Set up instance methods
|
20
|
+
include ActsAsPolymorphicController::InstanceMethods
|
21
|
+
|
22
|
+
end # aapc
|
23
|
+
end # ClassMethods
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def aapc_parent_id(parent)
|
27
|
+
params["#{parent}_id"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def aapc_parent_type
|
31
|
+
@aapc_parent_objects.detect { |parent| aapc_parent_id(parent) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def aapc_parent_class
|
35
|
+
aapc_parent_type && aapc_parent_type.to_s.classify.constantize
|
36
|
+
end
|
37
|
+
|
38
|
+
def aapc_parent_object
|
39
|
+
aapc_parent_class && aapc_parent_class.find_by_id(aapc_parent_id(aapc_parent_type))
|
40
|
+
end
|
41
|
+
|
42
|
+
def aapc_parent_controller
|
43
|
+
aapc_parent_type.to_s.pluralize
|
44
|
+
end
|
45
|
+
end # InstanceMethods
|
46
|
+
end # PC
|
47
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_polymorphic_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Flynn
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-05 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ore-core
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 0.1.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 11
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 1
|
49
|
+
- 0
|
50
|
+
version: 2.1.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: A ruby gem for making polymorphism in controllers easier to handle. This alleviates the need for if/case statements to handle parent classes.
|
54
|
+
email: rob@thingerly.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.rdoc
|
61
|
+
- LICENSE.txt
|
62
|
+
- ChangeLog.rdoc
|
63
|
+
files:
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
- gemspec.yml
|
66
|
+
- ChangeLog.rdoc
|
67
|
+
- .rspec
|
68
|
+
- lib/acts_as_polymorphic_controller.rb
|
69
|
+
- spec/acts_as_polymorphic_controller_spec.rb
|
70
|
+
- lib/acts_as_polymorphic_controller/version.rb
|
71
|
+
- Rakefile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.rdoc
|
74
|
+
- acts_as_polymorphic_controller.gemspec
|
75
|
+
- .document
|
76
|
+
- lib/acts_as_polymorphic_controller/controller.rb
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: https://github.com/robflynn/acts_as_polymorphic_controller
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project: acts_as_polymorphic_controller
|
107
|
+
rubygems_version: 1.3.7
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: A ruby gem for making polymorphism in controllers easier.
|
111
|
+
test_files:
|
112
|
+
- spec/acts_as_polymorphic_controller_spec.rb
|