polymorphic_identity 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,9 @@
1
1
  *SVN*
2
2
 
3
+ *0.0.4* (May 5th, 2008)
4
+
5
+ * Updated documentation
6
+
3
7
  *0.0.3* (September 26th, 2007)
4
8
 
5
9
  * Move test fixtures out of the test application root directory
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2006-2007 Aaron Pfeifer & Neil Abraham
1
+ Copyright (c) 2006-2008 Aaron Pfeifer
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
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.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README CHANGED
@@ -5,25 +5,21 @@ based on the class names of those associations.
5
5
 
6
6
  == Resources
7
7
 
8
- API
9
-
10
- * http://api.pluginaweek.org/polymorphic_identity
11
-
12
8
  Wiki
13
9
 
14
10
  * http://wiki.pluginaweek.org/Polymorphic_Identity
15
11
 
16
- Announcement
12
+ API
17
13
 
18
- * http://www.pluginaweek.org/2007/02/12/12-models-seek-damages-for-identity-theft
14
+ * http://api.pluginaweek.org/polymorphic_identity
19
15
 
20
- Source
16
+ Development
21
17
 
22
- * http://svn.pluginaweek.org/trunk/plugins/active_record/associations/polymorphic_identity
18
+ * http://dev.pluginaweek.org/browser/trunk/polymorphic_identity
23
19
 
24
- Development
20
+ Source
25
21
 
26
- * http://dev.pluginaweek.org/browser/trunk/plugins/active_record/associations/polymorphic_identity
22
+ * http://svn.pluginaweek.org/trunk/polymorphic_identity
27
23
 
28
24
  == Description
29
25
 
@@ -82,4 +78,4 @@ Before you can run any tests, the following gem must be installed:
82
78
 
83
79
  == Dependencies
84
80
 
85
- This plugin does not depend on the presence of any other plugin.
81
+ None.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/gempackagetask'
4
4
  require 'rake/contrib/sshpublisher'
5
5
 
6
6
  PKG_NAME = 'polymorphic_identity'
7
- PKG_VERSION = '0.0.3'
7
+ PKG_VERSION = '0.0.4'
8
8
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
9
9
  RUBY_FORGE_PROJECT = 'pluginaweek'
10
10
 
@@ -39,8 +39,8 @@ spec = Gem::Specification.new do |s|
39
39
  s.has_rdoc = true
40
40
  s.test_files = Dir['test/**/*_test.rb']
41
41
 
42
- s.author = 'Aaron Pfeifer, Neil Abraham'
43
- s.email = 'info@pluginaweek.org'
42
+ s.author = 'Aaron Pfeifer'
43
+ s.email = 'aaron@pluginaweek.org'
44
44
  s.homepage = 'http://www.pluginaweek.org'
45
45
  end
46
46
 
@@ -52,12 +52,12 @@ end
52
52
 
53
53
  desc 'Publish the beta gem'
54
54
  task :pgem => [:package] do
55
- Rake::SshFilePublisher.new('pluginaweek@pluginaweek.org', '/home/pluginaweek/gems.pluginaweek.org/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
55
+ Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
56
56
  end
57
57
 
58
58
  desc 'Publish the API documentation'
59
59
  task :pdoc => [:rdoc] do
60
- Rake::SshDirPublisher.new('pluginaweek@pluginaweek.org', "/home/pluginaweek/api.pluginaweek.org/#{PKG_NAME}", 'rdoc').upload
60
+ Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{PKG_NAME}", 'rdoc').upload
61
61
  end
62
62
 
63
63
  desc 'Publish the API docs and gem'
@@ -39,19 +39,19 @@ module PluginAWeek
39
39
  end
40
40
 
41
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] && read_attribute(reflection.options[:foreign_type]) == foreign_type_value
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
48
51
  end
49
-
50
- reflection ? reflection.name : nil
51
- end
52
52
  end
53
53
  end
54
54
 
55
55
  ActiveRecord::Base.class_eval do
56
56
  include PluginAWeek::PolymorphicIdentity
57
- end
57
+ end
@@ -1,4 +1,5 @@
1
1
  class Article < ActiveRecord::Base
2
- has_many :comments,
3
- :as => :commentable
4
- end
2
+ belongs_to :author
3
+ has_many :comments,
4
+ :as => :commentable
5
+ end
@@ -1,4 +1,6 @@
1
1
  class Author < ActiveRecord::Base
2
+ has_many :articles
3
+ has_many :pages
2
4
  has_many :comments,
3
5
  :as => :commenter
4
- end
6
+ end
@@ -1,4 +1,5 @@
1
1
  class Page < ActiveRecord::Base
2
- has_many :comments,
3
- :as => :commentable
4
- end
2
+ belongs_to :author
3
+ has_many :comments,
4
+ :as => :commentable
5
+ end
@@ -1,11 +1,11 @@
1
1
  class CreateAuthors < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :authors do |t|
4
- t.column :name, :string
4
+ t.string :name
5
5
  end
6
6
  end
7
7
 
8
8
  def self.down
9
9
  drop_table :authors
10
10
  end
11
- end
11
+ end
@@ -1,12 +1,12 @@
1
1
  class CreateArticles < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :articles do |t|
4
- t.column :author_id, :integer
5
- t.column :content, :text
4
+ t.integer :author_id
5
+ t.text :content
6
6
  end
7
7
  end
8
8
 
9
9
  def self.down
10
10
  drop_table :articles
11
11
  end
12
- end
12
+ end
@@ -1,12 +1,12 @@
1
1
  class CreatePages < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :pages do |t|
4
- t.column :author_id, :integer
5
- t.column :content, :text
4
+ t.integer :author_id
5
+ t.text :content
6
6
  end
7
7
  end
8
8
 
9
9
  def self.down
10
10
  drop_table :pages
11
11
  end
12
- end
12
+ end
@@ -1,11 +1,11 @@
1
1
  class CreateUsers < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :users do |t|
4
- t.column :name, :string
4
+ t.string :name
5
5
  end
6
6
  end
7
7
 
8
8
  def self.down
9
9
  drop_table :users
10
10
  end
11
- end
11
+ end
@@ -1,14 +1,14 @@
1
1
  class CreateComments < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :comments do |t|
4
- t.column :commentable_id, :integer
5
- t.column :commentable_type, :string
6
- t.column :commenter_id, :string
7
- t.column :commenter_type, :string
4
+ t.integer :commentable_id
5
+ t.string :commentable_type
6
+ t.string :commenter_id
7
+ t.string :commenter_type
8
8
  end
9
9
  end
10
10
 
11
11
  def self.down
12
12
  drop_table :comments
13
13
  end
14
- end
14
+ end
data/test/factory.rb ADDED
@@ -0,0 +1,56 @@
1
+ module Factory
2
+ # Build actions for the class
3
+ def self.build(klass, &block)
4
+ name = klass.to_s.underscore
5
+ define_method("#{name}_attributes", block)
6
+
7
+ module_eval <<-end_eval
8
+ def valid_#{name}_attributes(attributes = {})
9
+ #{name}_attributes(attributes)
10
+ attributes
11
+ end
12
+
13
+ def new_#{name}(attributes = {})
14
+ #{klass}.new(valid_#{name}_attributes(attributes))
15
+ end
16
+
17
+ def create_#{name}(*args)
18
+ record = new_#{name}(*args)
19
+ record.save!
20
+ record.reload
21
+ record
22
+ end
23
+ end_eval
24
+ end
25
+
26
+ build Article do |attributes|
27
+ attributes[:author] = create_author unless attributes.include?(:author)
28
+ attributes.reverse_merge!(
29
+ :content => 'Thanks for visiting my blog!'
30
+ )
31
+ end
32
+
33
+ build Author do |attributes|
34
+ attributes.reverse_merge!(
35
+ :name => 'John Smith'
36
+ )
37
+ end
38
+
39
+ build Comment do |attributes|
40
+ attributes[:commentable] = create_article unless attributes.include?(:commentable)
41
+ attributes[:commenter] = create_user unless attributes.include?(:commenter)
42
+ end
43
+
44
+ build Page do |attributes|
45
+ attributes[:author] = create_author unless attributes.include?(:author)
46
+ attributes.reverse_merge!(
47
+ :content => 'Some information about me...'
48
+ )
49
+ end
50
+
51
+ build User do |attributes|
52
+ attributes.reverse_merge!(
53
+ :name => 'Mr. Bean'
54
+ )
55
+ end
56
+ end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  # Load the plugin testing framework
2
- $:.unshift("#{File.dirname(__FILE__)}/../../../../test/plugin_test_helper/lib")
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
3
3
  require 'rubygems'
4
4
  require 'plugin_test_helper'
5
5
 
6
- ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
6
+ # Run the migrations
7
+ ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
8
+
9
+ # Mixin the factory helper
10
+ require File.expand_path("#{File.dirname(__FILE__)}/factory")
11
+ class Test::Unit::TestCase #:nodoc:
12
+ include Factory
13
+ end
@@ -1,62 +1,63 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  class PolymorphicIdentityTest < Test::Unit::TestCase
4
- fixtures :authors, :articles, :pages, :users, :comments
4
+ def setup
5
+ @comment = Comment.new
6
+ @comment.commenter_type = 'Article'
7
+ end
5
8
 
6
9
  def test_should_find_polymorphic_association_name_for_valid_symbolized_association
7
- c = Comment.new
8
- c.commenter_type = 'Article'
9
- assert_equal :commenter, c.send(:find_polymorphic_association_name, :article)
10
+ assert_equal :commenter, @comment.send(:find_polymorphic_association_name, :article)
10
11
  end
11
12
 
12
13
  def test_should_find_polymorphic_association_name_for_valid_stringified_association
13
- c = Comment.new
14
- c.commenter_type = 'Article'
15
- assert_equal :commenter, c.send(:find_polymorphic_association_name, 'article')
14
+ assert_equal :commenter, @comment.send(:find_polymorphic_association_name, 'article')
16
15
  end
17
16
 
18
17
  def test_should_not_find_polymorphic_association_name_for_invalid_association
19
- c = Comment.new
20
- c.commenter_type = 'Article'
21
- assert_equal nil, c.send(:find_polymorphic_association_name, 'page')
18
+ assert_equal nil, @comment.send(:find_polymorphic_association_name, 'page')
19
+ end
20
+ end
21
+
22
+ class PolymorphicAssociationWithNoValueTest < Test::Unit::TestCase
23
+ def setup
24
+ @comment = create_comment(:commentable => nil, :commenter => nil)
22
25
  end
23
26
 
24
- def test_should_not_respond_to_polymorhic_association_name_if_association_is_nil
25
- ([Comment.new] + Comment.find(:all)).each do |c|
26
- c.commentable = nil if c.commentable
27
- c.commenter = nil if c.commenter
28
-
29
- assert !c.respond_to?(:article)
30
- assert !c.respond_to?(:page)
31
- assert !c.respond_to?(:author)
32
- assert !c.respond_to?(:user)
33
- end
27
+ def test_should_not_respond_to_polymorphic_association_name
28
+ assert !@comment.respond_to?(:article)
29
+ assert !@comment.respond_to?(:page)
30
+ assert !@comment.respond_to?(:author)
31
+ assert !@comment.respond_to?(:user)
32
+ end
33
+ end
34
+
35
+ class PolymorphicAssociationsWithValueTest < Test::Unit::TestCase
36
+ def setup
37
+ @author = create_author
38
+ @article = create_article(:author => @author)
39
+ @comment = create_comment(:commentable => @article, :commenter => @author)
34
40
  end
35
41
 
36
42
  def test_should_respond_to_polymorphic_association_name_if_association_exists
37
- c = comments(:article_test_author_john_doe)
38
- assert c.respond_to?(:article)
39
- assert c.respond_to?(:author)
40
- assert_equal articles(:test), c.article
41
- assert_equal authors(:john_doe), c.author
42
-
43
- c = comments(:page_about_user_anonymous)
44
- assert c.respond_to?(:page)
45
- assert c.respond_to?(:user)
46
- assert_equal pages(:about), c.page
47
- assert_equal users(:anonymous), c.user
43
+ assert @comment.respond_to?(:article)
44
+ assert @comment.respond_to?(:author)
45
+ assert_equal @article, @comment.article
46
+ assert_equal @author, @comment.author
48
47
  end
49
48
 
50
49
  def test_should_update_response_when_changing_polymorphic_association
51
- c = comments(:article_test_author_john_doe)
52
- c.commentable = pages(:about)
53
- c.commenter = users(:anonymous)
50
+ page = create_page
51
+ user = create_user
52
+
53
+ @comment.commentable = page
54
+ @comment.commenter = user
54
55
 
55
- assert !c.respond_to?(:article)
56
- assert !c.respond_to?(:author)
57
- assert c.respond_to?(:page)
58
- assert c.respond_to?(:user)
59
- assert_equal pages(:about), c.page
60
- assert_equal users(:anonymous), c.user
56
+ assert !@comment.respond_to?(:article)
57
+ assert !@comment.respond_to?(:author)
58
+ assert @comment.respond_to?(:page)
59
+ assert @comment.respond_to?(:user)
60
+ assert_equal page, @comment.page
61
+ assert_equal user, @comment.user
61
62
  end
62
63
  end
metadata CHANGED
@@ -1,75 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: polymorphic_identity
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2007-09-26 00:00:00 -04:00
8
- summary: Dynamically generates aliases for polymorphic associations based on their class names
9
- require_paths:
10
- - lib
11
- email: info@pluginaweek.org
12
- homepage: http://www.pluginaweek.org
13
- rubyforge_project:
14
- description:
15
- autorequire: polymorphic_identity
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.0.4
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
- - Aaron Pfeifer, Neil Abraham
7
+ - Aaron Pfeifer
8
+ autorequire: polymorphic_identity
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-05 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: aaron@pluginaweek.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
25
  - lib/polymorphic_identity.rb
33
- - test/test_helper.rb
34
- - test/fixtures
26
+ - test/factory.rb
35
27
  - test/app_root
36
- - test/unit
37
- - test/fixtures/users.yml
38
- - test/fixtures/authors.yml
39
- - test/fixtures/articles.yml
40
- - test/fixtures/comments.yml
41
- - test/fixtures/pages.yml
42
- - test/app_root/app
43
28
  - test/app_root/db
44
- - test/app_root/app/models
45
- - test/app_root/app/models/author.rb
46
- - test/app_root/app/models/article.rb
47
- - test/app_root/app/models/user.rb
48
- - test/app_root/app/models/page.rb
49
- - test/app_root/app/models/comment.rb
50
29
  - test/app_root/db/migrate
51
- - test/app_root/db/migrate/004_create_users.rb
52
- - test/app_root/db/migrate/005_create_comments.rb
53
30
  - test/app_root/db/migrate/002_create_articles.rb
54
31
  - test/app_root/db/migrate/003_create_pages.rb
32
+ - test/app_root/db/migrate/004_create_users.rb
55
33
  - test/app_root/db/migrate/001_create_authors.rb
34
+ - test/app_root/db/migrate/005_create_comments.rb
35
+ - test/app_root/app
36
+ - test/app_root/app/models
37
+ - test/app_root/app/models/user.rb
38
+ - test/app_root/app/models/page.rb
39
+ - test/app_root/app/models/article.rb
40
+ - test/app_root/app/models/author.rb
41
+ - test/app_root/app/models/comment.rb
42
+ - test/test_helper.rb
43
+ - test/unit
56
44
  - test/unit/polymorphic_identity_test.rb
57
45
  - CHANGELOG
58
46
  - init.rb
59
47
  - MIT-LICENSE
60
48
  - Rakefile
61
49
  - README
62
- test_files:
63
- - test/unit/polymorphic_identity_test.rb
50
+ has_rdoc: true
51
+ homepage: http://www.pluginaweek.org
52
+ post_install_message:
64
53
  rdoc_options: []
65
54
 
66
- extra_rdoc_files: []
67
-
68
- executables: []
69
-
70
- extensions: []
71
-
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
72
69
  requirements: []
73
70
 
74
- dependencies: []
75
-
71
+ rubyforge_project:
72
+ rubygems_version: 1.1.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Dynamically generates aliases for polymorphic associations based on their class names
76
+ test_files:
77
+ - test/unit/polymorphic_identity_test.rb
@@ -1,9 +0,0 @@
1
- test:
2
- id: 1
3
- author_id: 1
4
- content: This is a test of the Emergency Broadcast System. If this had been an actual emergency...
5
-
6
- welcome:
7
- id: 2
8
- author_id: 2
9
- content: Welcome!
@@ -1,7 +0,0 @@
1
- john_doe:
2
- id: 1
3
- name: John Doe
4
-
5
- jane_doe:
6
- id: 2
7
- name: Jane Doe
@@ -1,25 +0,0 @@
1
- <%
2
- id = 0
3
- [
4
- [:article, :test, 1],
5
- [:article, :welcome, 2],
6
- [:page, :about, 1],
7
- [:page, :projects, 2]
8
- ].each do |commentable_type, commentable_name, commentable_id|
9
- [
10
- [:author, :john_doe, 1],
11
- [:author, :jane_doe, 2],
12
- [:user, :anonymous, 1],
13
- [:user, :registered, 2]
14
- ].each do |commenter_type, commenter_name, commenter_id|
15
- %>
16
- <%= commentable_type %>_<%= commentable_name %>_<%= commenter_type %>_<%= commenter_name %>:
17
- id: <%= id += 1 %>
18
- commentable_id: <%= commentable_id %>
19
- commentable_type: <%= commentable_type.to_s.camelize %>
20
- commenter_id: <%= commenter_id %>
21
- commenter_type: <%= commenter_type.to_s.camelize %>
22
- <%
23
- end
24
- end
25
- %>
@@ -1,9 +0,0 @@
1
- about:
2
- id: 1
3
- author_id: 1
4
- content: A little section about us
5
-
6
- projects:
7
- id: 2
8
- author_id: 2
9
- content: A little section about the projects we do
@@ -1,7 +0,0 @@
1
- anonymous:
2
- id: 1
3
- name: Anonymous
4
-
5
- registered:
6
- id: 2
7
- name: Registered