polymorphic_identity 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGELOG → CHANGELOG.rdoc} +10 -10
- data/{MIT-LICENSE → LICENSE} +0 -0
- data/{README → README.rdoc} +15 -20
- data/Rakefile +44 -36
- data/lib/polymorphic_identity.rb +45 -50
- data/test/factory.rb +31 -21
- data/test/test_helper.rb +1 -1
- metadata +22 -23
@@ -1,29 +1,29 @@
|
|
1
|
-
|
1
|
+
== master
|
2
2
|
|
3
|
-
|
3
|
+
== 0.1.0 / 2008-12-14
|
4
|
+
|
5
|
+
* Remove the PluginAWeek namespace
|
6
|
+
|
7
|
+
== 0.0.5 / 2008-06-22
|
4
8
|
|
5
9
|
* Remove log files from gems
|
6
10
|
|
7
|
-
|
11
|
+
== 0.0.4 / 2008-05-05
|
8
12
|
|
9
13
|
* Updated documentation
|
10
14
|
|
11
|
-
|
15
|
+
== 0.0.3 / 2007-09-26
|
12
16
|
|
13
17
|
* Move test fixtures out of the test application root directory
|
14
|
-
|
15
18
|
* Remove gem dependency on activerecord
|
16
19
|
|
17
|
-
|
20
|
+
== 0.0.2 / 2007-08-05
|
18
21
|
|
19
22
|
* Add api documentation
|
20
|
-
|
21
23
|
* Fix Rakefile syntax errors
|
22
|
-
|
23
24
|
* Refactor tests
|
24
|
-
|
25
25
|
* Use the plugin_test_helper plugin for helping create a Rails environment
|
26
26
|
|
27
|
-
|
27
|
+
== 0.0.1 / 2007-02-12
|
28
28
|
|
29
29
|
* Initial release
|
data/{MIT-LICENSE → LICENSE}
RENAMED
File without changes
|
data/{README → README.rdoc}
RENAMED
@@ -5,21 +5,21 @@ based on the class names of those associations.
|
|
5
5
|
|
6
6
|
== Resources
|
7
7
|
|
8
|
-
Wiki
|
9
|
-
|
10
|
-
* http://wiki.pluginaweek.org/Polymorphic_Identity
|
11
|
-
|
12
8
|
API
|
13
9
|
|
14
10
|
* http://api.pluginaweek.org/polymorphic_identity
|
15
11
|
|
12
|
+
Bugs
|
13
|
+
|
14
|
+
* http://pluginaweek.lighthouseapp.com/projects/13285-polymorphic_identity
|
15
|
+
|
16
16
|
Development
|
17
17
|
|
18
|
-
* http://
|
18
|
+
* http://github.com/pluginaweek/polymorphic_identity
|
19
19
|
|
20
20
|
Source
|
21
21
|
|
22
|
-
*
|
22
|
+
* git://github.com/pluginaweek/polymorphic_identity.git
|
23
23
|
|
24
24
|
== Description
|
25
25
|
|
@@ -32,10 +32,10 @@ polymorphic assocation looks like the following:
|
|
32
32
|
:polymorphic => true
|
33
33
|
end
|
34
34
|
|
35
|
-
When getting the taggable record, you would normally have to
|
36
|
-
|
35
|
+
When getting the taggable record, you would normally have to call
|
36
|
+
<tt>tag.taggable</tt>. However, if you know that the taggable record is just an
|
37
37
|
instance of the Article model, then it would feel more comfortable if you could
|
38
|
-
just
|
38
|
+
just call <tt>tag.article</tt>. polymoprhic_identity makes this possible by
|
39
39
|
dynamically checking the name of the polymorphic record's class and creating
|
40
40
|
methods that allow you to access the polymorphic association based on that
|
41
41
|
class name.
|
@@ -60,21 +60,16 @@ class name.
|
|
60
60
|
:as => :commenter
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
|
-
=> #<
|
65
|
-
|
66
|
-
=> #<
|
67
|
-
|
68
|
-
=> #<Article:0xb779d5a8 @attributes={"id"=>"1"}>
|
69
|
-
>> c.commenter
|
70
|
-
=> #<User:0xb775d764 @attributes={"id"=>"1"}>
|
71
|
-
>> c.user
|
72
|
-
=> #<User:0xb775d764 @attributes={"id"=>"1"}>
|
63
|
+
c = Comment.find(1) # => #<Tag id: 1, commentable_id: 1, commentable_type: "Article", commenter_id: 1, commenter_type: "User"}>
|
64
|
+
c.commentable # => #<Article id: 1>
|
65
|
+
c.article # => #<Article id: 1>
|
66
|
+
c.commenter # => #<User id: 1>
|
67
|
+
c.user # => #<User id: 1>
|
73
68
|
|
74
69
|
== Testing
|
75
70
|
|
76
71
|
Before you can run any tests, the following gem must be installed:
|
77
|
-
* plugin_test_helper[http://
|
72
|
+
* plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]
|
78
73
|
|
79
74
|
To run against a specific version of Rails:
|
80
75
|
|
data/Rakefile
CHANGED
@@ -3,46 +3,54 @@ require 'rake/rdoctask'
|
|
3
3
|
require 'rake/gempackagetask'
|
4
4
|
require 'rake/contrib/sshpublisher'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
spec = Gem::Specification.new do |s|
|
7
|
+
s.name = 'polymorphic_identity'
|
8
|
+
s.version = '0.1.0'
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.summary = 'Dynamically generates aliases for polymorphic associations based on their class names'
|
11
|
+
|
12
|
+
s.files = FileList['{lib,test}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc) - FileList['test/app_root/{log,log/*,script,script/*}']
|
13
|
+
s.require_path = 'lib'
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.test_files = Dir['test/**/*_test.rb']
|
16
|
+
|
17
|
+
s.author = 'Aaron Pfeifer'
|
18
|
+
s.email = 'aaron@pluginaweek.org'
|
19
|
+
s.homepage = 'http://www.pluginaweek.org'
|
20
|
+
s.rubyforge_project = 'pluginaweek'
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Default: run all tests.'
|
12
24
|
task :default => :test
|
13
25
|
|
14
|
-
desc
|
26
|
+
desc "Test the #{spec.name} plugin."
|
15
27
|
Rake::TestTask.new(:test) do |t|
|
16
28
|
t.libs << 'lib'
|
17
|
-
t.
|
29
|
+
t.test_files = spec.test_files
|
18
30
|
t.verbose = true
|
19
31
|
end
|
20
32
|
|
21
|
-
|
33
|
+
begin
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
namespace :test do
|
36
|
+
desc "Test the #{spec.name} plugin with Rcov."
|
37
|
+
Rcov::RcovTask.new(:rcov) do |t|
|
38
|
+
t.libs << 'lib'
|
39
|
+
t.test_files = spec.test_files
|
40
|
+
t.rcov_opts << '--exclude="^(?!lib/)"'
|
41
|
+
t.verbose = true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue LoadError
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Generate documentation for the #{spec.name} plugin."
|
22
48
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
49
|
rdoc.rdoc_dir = 'rdoc'
|
24
|
-
rdoc.title =
|
50
|
+
rdoc.title = spec.name
|
25
51
|
rdoc.template = '../rdoc_template.rb'
|
26
52
|
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
-
rdoc.rdoc_files.include('README')
|
28
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
-
end
|
30
|
-
|
31
|
-
spec = Gem::Specification.new do |s|
|
32
|
-
s.name = PKG_NAME
|
33
|
-
s.version = PKG_VERSION
|
34
|
-
s.platform = Gem::Platform::RUBY
|
35
|
-
s.summary = 'Dynamically generates aliases for polymorphic associations based on their class names'
|
36
|
-
|
37
|
-
s.files = FileList['{lib,test}/**/*'].to_a - FileList['test/app_root/log/*'].to_a + %w(CHANGELOG init.rb MIT-LICENSE Rakefile README)
|
38
|
-
s.require_path = 'lib'
|
39
|
-
s.autorequire = 'polymorphic_identity'
|
40
|
-
s.has_rdoc = true
|
41
|
-
s.test_files = Dir['test/**/*_test.rb']
|
42
|
-
|
43
|
-
s.author = 'Aaron Pfeifer'
|
44
|
-
s.email = 'aaron@pluginaweek.org'
|
45
|
-
s.homepage = 'http://www.pluginaweek.org'
|
53
|
+
rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG.rdoc', 'LICENSE', 'lib/**/*.rb')
|
46
54
|
end
|
47
55
|
|
48
56
|
Rake::GemPackageTask.new(spec) do |p|
|
@@ -51,14 +59,14 @@ Rake::GemPackageTask.new(spec) do |p|
|
|
51
59
|
p.need_zip = true
|
52
60
|
end
|
53
61
|
|
54
|
-
desc 'Publish the beta gem'
|
62
|
+
desc 'Publish the beta gem.'
|
55
63
|
task :pgem => [:package] do
|
56
|
-
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{
|
64
|
+
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
|
57
65
|
end
|
58
66
|
|
59
|
-
desc 'Publish the API documentation'
|
67
|
+
desc 'Publish the API documentation.'
|
60
68
|
task :pdoc => [:rdoc] do
|
61
|
-
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{
|
69
|
+
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
|
62
70
|
end
|
63
71
|
|
64
72
|
desc 'Publish the API docs and gem'
|
@@ -71,10 +79,10 @@ task :release => [:gem, :package] do
|
|
71
79
|
ruby_forge = RubyForge.new.configure
|
72
80
|
ruby_forge.login
|
73
81
|
|
74
|
-
%w(
|
75
|
-
file = "pkg/#{
|
82
|
+
%w(gem tgz zip).each do |ext|
|
83
|
+
file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
|
76
84
|
puts "Releasing #{File.basename(file)}..."
|
77
85
|
|
78
|
-
ruby_forge.add_release(
|
86
|
+
ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
|
79
87
|
end
|
80
88
|
end
|
data/lib/polymorphic_identity.rb
CHANGED
@@ -1,57 +1,52 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def self.included(base) #:nodoc:
|
21
|
-
base.class_eval do
|
22
|
-
alias_method_chain :method_missing, :polymorphic_identity
|
23
|
-
alias_method_chain :respond_to?, :polymorphic_identity
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def method_missing_with_polymorphic_identity(method_id, *args, &block) #:nodoc:
|
28
|
-
if association_name = find_polymorphic_association_name(method_id)
|
29
|
-
send(association_name, *args, &block)
|
30
|
-
else
|
31
|
-
method_missing_without_polymorphic_identity(method_id, *args, &block)
|
32
|
-
end
|
1
|
+
# Adds dynamic attributes for polymorphic associations based on the name of
|
2
|
+
# the class for the polymorphic record. For example,
|
3
|
+
#
|
4
|
+
# class Tag < ActiveRecord::Base
|
5
|
+
# belongs_to :taggable, :polymorphic => true
|
6
|
+
# end
|
7
|
+
#
|
8
|
+
# class Article < ActiveRecord::Base
|
9
|
+
# has_many :tags, :as => :taggable
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# t = Tag.find(1) # => #<Tag id: 1, taggable_id: 1, taggable_type: "Article">
|
13
|
+
# t.taggable # => #<Article id: 1>
|
14
|
+
# t.article # => #<Article id: 1>
|
15
|
+
module PolymorphicIdentity
|
16
|
+
def self.included(base) #:nodoc:
|
17
|
+
base.class_eval do
|
18
|
+
alias_method_chain :method_missing, :polymorphic_identity
|
19
|
+
alias_method_chain :respond_to?, :polymorphic_identity
|
33
20
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing_with_polymorphic_identity(method_id, *args, &block) #:nodoc:
|
24
|
+
if association_name = find_polymorphic_association_name(method_id)
|
25
|
+
send(association_name, *args, &block)
|
26
|
+
else
|
27
|
+
method_missing_without_polymorphic_identity(method_id, *args, &block)
|
39
28
|
end
|
40
|
-
|
41
|
-
private
|
42
|
-
# Finds the name of the polymorphic association whose foreign type is set to
|
43
|
-
# the value specified.
|
44
|
-
def find_polymorphic_association_name(foreign_type_value)
|
45
|
-
foreign_type_value = foreign_type_value.to_s.camelize
|
46
|
-
reflection = self.class.reflections.values.find do |reflection|
|
47
|
-
reflection.options[:polymorphic] && self[reflection.options[:foreign_type]] == foreign_type_value
|
48
|
-
end
|
49
|
-
|
50
|
-
reflection ? reflection.name : nil
|
51
|
-
end
|
52
29
|
end
|
30
|
+
|
31
|
+
# True if a polymorphic association can be found whose foreign type is set
|
32
|
+
# to the name of the method
|
33
|
+
def respond_to_with_polymorphic_identity?(method, include_priv = false) #:nodoc:
|
34
|
+
respond_to_without_polymorphic_identity?(method, include_priv) || !find_polymorphic_association_name(method).nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
# Finds the name of the polymorphic association whose foreign type is set to
|
39
|
+
# the value specified.
|
40
|
+
def find_polymorphic_association_name(foreign_type_value)
|
41
|
+
foreign_type_value = foreign_type_value.to_s.camelize
|
42
|
+
reflection = self.class.reflections.values.find do |reflection|
|
43
|
+
reflection.options[:polymorphic] && self[reflection.options[:foreign_type]] == foreign_type_value
|
44
|
+
end
|
45
|
+
|
46
|
+
reflection ? reflection.name : nil
|
47
|
+
end
|
53
48
|
end
|
54
49
|
|
55
50
|
ActiveRecord::Base.class_eval do
|
56
|
-
include
|
51
|
+
include PolymorphicIdentity
|
57
52
|
end
|
data/test/factory.rb
CHANGED
@@ -1,26 +1,36 @@
|
|
1
1
|
module Factory
|
2
|
-
# Build actions for the
|
3
|
-
def self.build(
|
4
|
-
name =
|
5
|
-
define_method("#{name}_attributes", block)
|
2
|
+
# Build actions for the model
|
3
|
+
def self.build(model, &block)
|
4
|
+
name = model.to_s.underscore
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
6
|
+
define_method("#{name}_attributes", block)
|
7
|
+
define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
|
8
|
+
define_method("new_#{name}") {|*args| new_record(model, *args)}
|
9
|
+
define_method("create_#{name}") {|*args| create_record(model, *args)}
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get valid attributes for the model
|
13
|
+
def valid_attributes_for(model, attributes = {})
|
14
|
+
name = model.to_s.underscore
|
15
|
+
send("#{name}_attributes", attributes)
|
16
|
+
attributes.stringify_keys!
|
17
|
+
attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
# Build an unsaved record
|
21
|
+
def new_record(model, *args)
|
22
|
+
attributes = valid_attributes_for(model, *args)
|
23
|
+
record = model.new(attributes)
|
24
|
+
attributes.each {|attr, value| record.send("#{attr}=", value) if model.accessible_attributes && !model.accessible_attributes.include?(attr) || model.protected_attributes && model.protected_attributes.include?(attr)}
|
25
|
+
record
|
26
|
+
end
|
27
|
+
|
28
|
+
# Build and save/reload a record
|
29
|
+
def create_record(model, *args)
|
30
|
+
record = new_record(model, *args)
|
31
|
+
record.save!
|
32
|
+
record.reload
|
33
|
+
record
|
24
34
|
end
|
25
35
|
|
26
36
|
build Article do |attributes|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_identity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,31 +23,30 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- lib/polymorphic_identity.rb
|
26
|
+
- test/factory.rb
|
27
|
+
- test/test_helper.rb
|
28
|
+
- test/unit
|
29
|
+
- test/unit/polymorphic_identity_test.rb
|
26
30
|
- test/app_root
|
27
|
-
- test/app_root/app
|
28
|
-
- test/app_root/app/models
|
29
|
-
- test/app_root/app/models/page.rb
|
30
|
-
- test/app_root/app/models/author.rb
|
31
|
-
- test/app_root/app/models/article.rb
|
32
|
-
- test/app_root/app/models/comment.rb
|
33
|
-
- test/app_root/app/models/user.rb
|
34
31
|
- test/app_root/db
|
35
32
|
- test/app_root/db/migrate
|
36
|
-
- test/app_root/db/migrate/005_create_comments.rb
|
37
|
-
- test/app_root/db/migrate/002_create_articles.rb
|
38
|
-
- test/app_root/db/migrate/003_create_pages.rb
|
39
33
|
- test/app_root/db/migrate/004_create_users.rb
|
34
|
+
- test/app_root/db/migrate/005_create_comments.rb
|
40
35
|
- test/app_root/db/migrate/001_create_authors.rb
|
41
|
-
- test/app_root/
|
42
|
-
- test/
|
43
|
-
- test/
|
44
|
-
- test/
|
45
|
-
- test/
|
46
|
-
-
|
36
|
+
- test/app_root/db/migrate/003_create_pages.rb
|
37
|
+
- test/app_root/db/migrate/002_create_articles.rb
|
38
|
+
- test/app_root/app
|
39
|
+
- test/app_root/app/models
|
40
|
+
- test/app_root/app/models/article.rb
|
41
|
+
- test/app_root/app/models/author.rb
|
42
|
+
- test/app_root/app/models/user.rb
|
43
|
+
- test/app_root/app/models/comment.rb
|
44
|
+
- test/app_root/app/models/page.rb
|
45
|
+
- CHANGELOG.rdoc
|
47
46
|
- init.rb
|
48
|
-
-
|
47
|
+
- LICENSE
|
49
48
|
- Rakefile
|
50
|
-
- README
|
49
|
+
- README.rdoc
|
51
50
|
has_rdoc: true
|
52
51
|
homepage: http://www.pluginaweek.org
|
53
52
|
post_install_message:
|
@@ -69,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
68
|
version:
|
70
69
|
requirements: []
|
71
70
|
|
72
|
-
rubyforge_project:
|
73
|
-
rubygems_version: 1.
|
71
|
+
rubyforge_project: pluginaweek
|
72
|
+
rubygems_version: 1.2.0
|
74
73
|
signing_key:
|
75
74
|
specification_version: 2
|
76
75
|
summary: Dynamically generates aliases for polymorphic associations based on their class names
|