kronn-has_many_polymorphs 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +86 -0
- data/LICENSE +184 -0
- data/README +214 -0
- data/lib/has_many_polymorphs.rb +19 -0
- data/lib/has_many_polymorphs/association.rb +165 -0
- data/lib/has_many_polymorphs/autoload.rb +58 -0
- data/lib/has_many_polymorphs/base.rb +71 -0
- data/lib/has_many_polymorphs/class_methods.rb +598 -0
- data/lib/has_many_polymorphs/debugging_tools.rb +104 -0
- data/lib/has_many_polymorphs/railtie.rb +8 -0
- data/lib/has_many_polymorphs/rake_task_redefine_task.rb +35 -0
- data/lib/has_many_polymorphs/reflection.rb +58 -0
- data/lib/has_many_polymorphs/support_methods.rb +88 -0
- metadata +96 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
v2.2. Various fixes.
|
2
|
+
|
3
|
+
v2.13. Merge various fixes for Rails 2.2.2.
|
4
|
+
|
5
|
+
v2.12. Improvements to the test suite; bugfixes for STI children (rsl). Remove fancy dependency system in favor of using Dispatcher::to_prepare every time.
|
6
|
+
|
7
|
+
v2.11. Rails 1.2.6 tagging generator compatibility; change test suite to use included integration app.
|
8
|
+
|
9
|
+
v2.10. Add :parent_conditions option; bugfix for nullified conditions; bugfix for self-referential tagging generator; allow setting of has_many_polymorphs_options hash in Configuration's after_initialize if you need to adjust the autoload behavior; clear error message on missing or improperly namespaced models; fix .build on double-sided relationships; add :namespace key for easier set up of Camping apps or other unusual class structures.
|
10
|
+
|
11
|
+
v2.9. Gem version renumbering; my apologies if this messes anyone up.
|
12
|
+
|
13
|
+
v2.8. RDoc documentation; repository relocation; Rakefile cleanup; remove deprecated plugin-specific class caching.
|
14
|
+
|
15
|
+
v2.7.5. Various bugfixes; Postgres problems may remain on edge.
|
16
|
+
|
17
|
+
v2.7.3. Use new :source and :source_type options in 1.2.3 (David Lemstra); fix pluralization bug; add some tests; experimental tagging generator.
|
18
|
+
|
19
|
+
v2.7.2. Deprecate has_many_polymorphs_cache_classes= option because it doesn't really work. Use config.cache_classes= instead to cache all reloadable items.
|
20
|
+
|
21
|
+
v2.7.1. Dispatcher.to_prepare didn't fire in the console; now using a config.after_initialize wrapper instead.
|
22
|
+
|
23
|
+
v2.7. Dependency injection framework elimates having to care about load order.
|
24
|
+
|
25
|
+
v2.6. Make the logger act sane for the gem version.
|
26
|
+
|
27
|
+
v2.5.2. Allow :skip_duplicates on double relationships.
|
28
|
+
|
29
|
+
v2.5.1. Renamed :ignore_duplicates to :skip_duplicates to better express its non-passive behavior; made sure not to load target set on push unless necessary.
|
30
|
+
|
31
|
+
v2.5. Activerecord compatibility branch becomes trunk: extra options now supported for double polymorphism; conditions nulled-out and propogated to child relationships; more tests; new :ignore_duplicates option on macro can be set to false if you want << to push duplicate associations.
|
32
|
+
|
33
|
+
v2.4.1. Code split into multiple files; tests added for pluralization check; Rails 1.1.6 no longer supported.
|
34
|
+
|
35
|
+
v2.4. Unlimited mixed class association extensions for both single and double targets and joins.
|
36
|
+
|
37
|
+
v2.3. Gem version
|
38
|
+
|
39
|
+
v2.2. API change; prefix on methods is now singular when using :rename_individual_collections.
|
40
|
+
|
41
|
+
v2.1. Add configuration option to cache polymorphic classes in development mode.
|
42
|
+
|
43
|
+
v2.0. Collection methods (push, delete, clear) now on individual collections.
|
44
|
+
|
45
|
+
v1.9.2. Disjoint collection sides bugfix, don't raise on new records.
|
46
|
+
|
47
|
+
v1.9.1. Double classify bugfix.
|
48
|
+
|
49
|
+
v1.9. Large changes to properly support double polymorphism.
|
50
|
+
|
51
|
+
v1.8.2. Bugfix to make sure the type gets checked on doubly polymorphic parents.
|
52
|
+
|
53
|
+
v1.8.1. Bugfix for sqlite3 child attribute retrieval.
|
54
|
+
|
55
|
+
v1.8. Bugfix for instantiating attributes of namespaced models.
|
56
|
+
|
57
|
+
v1.7.1. Bugfix for double polymorphic relationships.
|
58
|
+
|
59
|
+
v1.7. Double polymorphic relationships (includes new API method).
|
60
|
+
|
61
|
+
v1.6. Namespaced model support.
|
62
|
+
|
63
|
+
v1.5. Bugfix for Postgres and Mysql under 1.1.6; refactored tests (hildofur); properly handles legacy table names set with set_table_name().
|
64
|
+
|
65
|
+
v1.4. STI support added (use the child class names, not the base class).
|
66
|
+
|
67
|
+
v1.3. Bug regarding table names with underscores in SQL query fixed.
|
68
|
+
|
69
|
+
v1.2. License change, again.
|
70
|
+
|
71
|
+
v1.1. File_column bug fixed.
|
72
|
+
|
73
|
+
v1.0. Tests written; after_find and after_initialize now correctly called.
|
74
|
+
|
75
|
+
v0.5. SQL performance enhancements added.
|
76
|
+
|
77
|
+
v0.4. Rewrote singletons as full-fledged proxy class so that marshalling works (e.g. in the session).
|
78
|
+
|
79
|
+
v0.3. Caching added.
|
80
|
+
|
81
|
+
v0.2. Fixed dependency reloading problem in development mode.
|
82
|
+
|
83
|
+
v0.1. License change.
|
84
|
+
|
85
|
+
v0. Added :dependent support on the join table; no changelog before this version.
|
86
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
Academic Free License (AFL) v. 3.0
|
2
|
+
|
3
|
+
This Academic Free License (the "License") applies to any original work
|
4
|
+
of authorship (the "Original Work") whose owner (the "Licensor") has
|
5
|
+
placed the following licensing notice adjacent to the copyright notice
|
6
|
+
for the Original Work:
|
7
|
+
|
8
|
+
Licensed under the Academic Free License version 3.0
|
9
|
+
|
10
|
+
1) Grant of Copyright License. Licensor grants You a worldwide,
|
11
|
+
royalty-free, non-exclusive, sublicensable license, for the duration of
|
12
|
+
the copyright, to do the following:
|
13
|
+
|
14
|
+
a) to reproduce the Original Work in copies, either alone or as part of
|
15
|
+
a collective work;
|
16
|
+
|
17
|
+
b) to translate, adapt, alter, transform, modify, or arrange the
|
18
|
+
Original Work, thereby creating derivative works ("Derivative Works")
|
19
|
+
based upon the Original Work;
|
20
|
+
|
21
|
+
c) to distribute or communicate copies of the Original Work and
|
22
|
+
Derivative Works to the public, under any license of your choice that
|
23
|
+
does not contradict the terms and conditions, including Licensor's
|
24
|
+
reserved rights and remedies, in this Academic Free License;
|
25
|
+
|
26
|
+
d) to perform the Original Work publicly; and
|
27
|
+
|
28
|
+
e) to display the Original Work publicly.
|
29
|
+
|
30
|
+
2) Grant of Patent License. Licensor grants You a worldwide,
|
31
|
+
royalty-free, non-exclusive, sublicensable license, under patent claims
|
32
|
+
owned or controlled by the Licensor that are embodied in the Original
|
33
|
+
Work as furnished by the Licensor, for the duration of the patents, to
|
34
|
+
make, use, sell, offer for sale, have made, and import the Original Work
|
35
|
+
and Derivative Works.
|
36
|
+
|
37
|
+
3) Grant of Source Code License. The term "Source Code" means the
|
38
|
+
preferred form of the Original Work for making modifications to it and
|
39
|
+
all available documentation describing how to modify the Original Work.
|
40
|
+
Licensor agrees to provide a machine-readable copy of the Source Code of
|
41
|
+
the Original Work along with each copy of the Original Work that
|
42
|
+
Licensor distributes. Licensor reserves the right to satisfy this
|
43
|
+
obligation by placing a machine-readable copy of the Source Code in an
|
44
|
+
information repository reasonably calculated to permit inexpensive and
|
45
|
+
convenient access by You for as long as Licensor continues to distribute
|
46
|
+
the Original Work.
|
47
|
+
|
48
|
+
4) Exclusions From License Grant. Neither the names of Licensor, nor the
|
49
|
+
names of any contributors to the Original Work, nor any of their
|
50
|
+
trademarks or service marks, may be used to endorse or promote products
|
51
|
+
derived from this Original Work without express prior permission of the
|
52
|
+
Licensor. Except as expressly stated herein, nothing in this License
|
53
|
+
grants any license to Licensor's trademarks, copyrights, patents, trade
|
54
|
+
secrets or any other intellectual property. No patent license is granted
|
55
|
+
to make, use, sell, offer for sale, have made, or import embodiments of
|
56
|
+
any patent claims other than the licensed claims defined in Section 2.
|
57
|
+
No license is granted to the trademarks of Licensor even if such marks
|
58
|
+
are included in the Original Work. Nothing in this License shall be
|
59
|
+
interpreted to prohibit Licensor from licensing under terms different
|
60
|
+
from this License any Original Work that Licensor otherwise would have a
|
61
|
+
right to license.
|
62
|
+
|
63
|
+
5) External Deployment. The term "External Deployment" means the use,
|
64
|
+
distribution, or communication of the Original Work or Derivative Works
|
65
|
+
in any way such that the Original Work or Derivative Works may be used
|
66
|
+
by anyone other than You, whether those works are distributed or
|
67
|
+
communicated to those persons or made available as an application
|
68
|
+
intended for use over a network. As an express condition for the grants
|
69
|
+
of license hereunder, You must treat any External Deployment by You of
|
70
|
+
the Original Work or a Derivative Work as a distribution under section
|
71
|
+
1(c).
|
72
|
+
|
73
|
+
6) Attribution Rights. You must retain, in the Source Code of any
|
74
|
+
Derivative Works that You create, all copyright, patent, or trademark
|
75
|
+
notices from the Source Code of the Original Work, as well as any
|
76
|
+
notices of licensing and any descriptive text identified therein as an
|
77
|
+
"Attribution Notice." You must cause the Source Code for any Derivative
|
78
|
+
Works that You create to carry a prominent Attribution Notice reasonably
|
79
|
+
calculated to inform recipients that You have modified the Original
|
80
|
+
Work.
|
81
|
+
|
82
|
+
7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants
|
83
|
+
that the copyright in and to the Original Work and the patent rights
|
84
|
+
granted herein by Licensor are owned by the Licensor or are sublicensed
|
85
|
+
to You under the terms of this License with the permission of the
|
86
|
+
contributor(s) of those copyrights and patent rights. Except as
|
87
|
+
expressly stated in the immediately preceding sentence, the Original
|
88
|
+
Work is provided under this License on an "AS IS" BASIS and WITHOUT
|
89
|
+
WARRANTY, either express or implied, including, without limitation, the
|
90
|
+
warranties of non-infringement, merchantability or fitness for a
|
91
|
+
particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL
|
92
|
+
WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential
|
93
|
+
part of this License. No license to the Original Work is granted by this
|
94
|
+
License except under this disclaimer.
|
95
|
+
|
96
|
+
8) Limitation of Liability. Under no circumstances and under no legal
|
97
|
+
theory, whether in tort (including negligence), contract, or otherwise,
|
98
|
+
shall the Licensor be liable to anyone for any indirect, special,
|
99
|
+
incidental, or consequential damages of any character arising as a
|
100
|
+
result of this License or the use of the Original Work including,
|
101
|
+
without limitation, damages for loss of goodwill, work stoppage,
|
102
|
+
computer failure or malfunction, or any and all other commercial damages
|
103
|
+
or losses. This limitation of liability shall not apply to the extent
|
104
|
+
applicable law prohibits such limitation.
|
105
|
+
|
106
|
+
9) Acceptance and Termination. If, at any time, You expressly assented
|
107
|
+
to this License, that assent indicates your clear and irrevocable
|
108
|
+
acceptance of this License and all of its terms and conditions. If You
|
109
|
+
distribute or communicate copies of the Original Work or a Derivative
|
110
|
+
Work, You must make a reasonable effort under the circumstances to
|
111
|
+
obtain the express assent of recipients to the terms of this License.
|
112
|
+
This License conditions your rights to undertake the activities listed
|
113
|
+
in Section 1, including your right to create Derivative Works based upon
|
114
|
+
the Original Work, and doing so without honoring these terms and
|
115
|
+
conditions is prohibited by copyright law and international treaty.
|
116
|
+
Nothing in this License is intended to affect copyright exceptions and
|
117
|
+
limitations (including "fair use" or "fair dealing"). This License shall
|
118
|
+
terminate immediately and You may no longer exercise any of the rights
|
119
|
+
granted to You by this License upon your failure to honor the conditions
|
120
|
+
in Section 1(c).
|
121
|
+
|
122
|
+
10) Termination for Patent Action. This License shall terminate
|
123
|
+
automatically and You may no longer exercise any of the rights granted
|
124
|
+
to You by this License as of the date You commence an action, including
|
125
|
+
a cross-claim or counterclaim, against Licensor or any licensee alleging
|
126
|
+
that the Original Work infringes a patent. This termination provision
|
127
|
+
shall not apply for an action alleging patent infringement by
|
128
|
+
combinations of the Original Work with other software or hardware.
|
129
|
+
|
130
|
+
11) Jurisdiction, Venue and Governing Law. Any action or suit relating
|
131
|
+
to this License may be brought only in the courts of a jurisdiction
|
132
|
+
wherein the Licensor resides or in which Licensor conducts its primary
|
133
|
+
business, and under the laws of that jurisdiction excluding its
|
134
|
+
conflict-of-law provisions. The application of the United Nations
|
135
|
+
Convention on Contracts for the International Sale of Goods is expressly
|
136
|
+
excluded. Any use of the Original Work outside the scope of this License
|
137
|
+
or after its termination shall be subject to the requirements and
|
138
|
+
penalties of copyright or patent law in the appropriate jurisdiction.
|
139
|
+
This section shall survive the termination of this License.
|
140
|
+
|
141
|
+
12) Attorneys' Fees. In any action to enforce the terms of this License
|
142
|
+
or seeking damages relating thereto, the prevailing party shall be
|
143
|
+
entitled to recover its costs and expenses, including, without
|
144
|
+
limitation, reasonable attorneys' fees and costs incurred in connection
|
145
|
+
with such action, including any appeal of such action. This section
|
146
|
+
shall survive the termination of this License.
|
147
|
+
|
148
|
+
13) Miscellaneous. If any provision of this License is held to be
|
149
|
+
unenforceable, such provision shall be reformed only to the extent
|
150
|
+
necessary to make it enforceable.
|
151
|
+
|
152
|
+
14) Definition of "You" in This License. "You" throughout this License,
|
153
|
+
whether in upper or lower case, means an individual or a legal entity
|
154
|
+
exercising rights under, and complying with all of the terms of, this
|
155
|
+
License. For legal entities, "You" includes any entity that controls, is
|
156
|
+
controlled by, or is under common control with you. For purposes of this
|
157
|
+
definition, "control" means (i) the power, direct or indirect, to cause
|
158
|
+
the direction or management of such entity, whether by contract or
|
159
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
160
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
161
|
+
|
162
|
+
15) Right to Use. You may use the Original Work in all ways not
|
163
|
+
otherwise restricted or conditioned by this License or by law, and
|
164
|
+
Licensor promises not to interfere with or be responsible for such uses
|
165
|
+
by You.
|
166
|
+
|
167
|
+
16) Modification of This License. This License is Copyright (c) 2005
|
168
|
+
Lawrence Rosen. Permission is granted to copy, distribute, or
|
169
|
+
communicate this License without modification. Nothing in this License
|
170
|
+
permits You to modify this License as applied to the Original Work or to
|
171
|
+
Derivative Works. However, You may modify the text of this License and
|
172
|
+
copy, distribute or communicate your modified version (the "Modified
|
173
|
+
License") and apply it to other original works of authorship subject to
|
174
|
+
the following conditions: (i) You may not indicate in any way that your
|
175
|
+
Modified License is the "Academic Free License" or "AFL" and you may not
|
176
|
+
use those names in the name of your Modified License; (ii) You must
|
177
|
+
replace the notice specified in the first paragraph above with the
|
178
|
+
notice "Licensed under <insert your license name here>" or with a notice
|
179
|
+
of your own that is not confusingly similar to the notice in this
|
180
|
+
License; and (iii) You may not claim that your original works are open
|
181
|
+
source software unless your Modified License has been approved by Open
|
182
|
+
Source Initiative (OSI) and You comply with its license review and
|
183
|
+
certification process.
|
184
|
+
|
data/README
ADDED
@@ -0,0 +1,214 @@
|
|
1
|
+
Has_many_polymorphs
|
2
|
+
|
3
|
+
This is a hacky attempt to port has_many_polymorphs to work under Rails 3. It is intended solely
|
4
|
+
for my use (and use as a gem) but is offered here in case it helps anyone else develop it into
|
5
|
+
a more complete port.
|
6
|
+
|
7
|
+
James Stewart - 3rd May 2010
|
8
|
+
|
9
|
+
|
10
|
+
== Original README
|
11
|
+
|
12
|
+
An ActiveRecord plugin for self-referential and double-sided polymorphic associations.
|
13
|
+
|
14
|
+
== License
|
15
|
+
|
16
|
+
Copyright 2006-2008 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file.
|
17
|
+
|
18
|
+
The public certificate for the gem is here[http://blog.evanweaver.com/files/evan_weaver-original-public_cert.pem].
|
19
|
+
|
20
|
+
If you use this software, please {make a donation}[http://blog.evanweaver.com/donate/], or {recommend Evan}[http://www.workingwithrails.com/person/7739-evan-weaver] at Working with Rails.
|
21
|
+
|
22
|
+
== Description
|
23
|
+
|
24
|
+
This plugin lets you define self-referential and double-sided polymorphic associations in your models. It is an extension of <tt>has_many :through</tt>.
|
25
|
+
|
26
|
+
“Polymorphic” means an association can freely point to any of several unrelated model classes, instead of being tied to one particular class.
|
27
|
+
|
28
|
+
== Features
|
29
|
+
|
30
|
+
* self-references
|
31
|
+
* double-sided polymorphism
|
32
|
+
* efficient database usage
|
33
|
+
* STI support
|
34
|
+
* namespace support
|
35
|
+
* automatic individual and reverse associations
|
36
|
+
|
37
|
+
The plugin also includes a generator for a tagging system, a common use case (see below).
|
38
|
+
|
39
|
+
== Requirements
|
40
|
+
|
41
|
+
* Rails 2.2.2 or greater
|
42
|
+
|
43
|
+
= Usage
|
44
|
+
|
45
|
+
== Installation
|
46
|
+
|
47
|
+
To install the Rails plugin, run:
|
48
|
+
script/plugin install git://github.com/fauna/has_many_polymorphs.git
|
49
|
+
|
50
|
+
There's also a gem version. To install it instead, run:
|
51
|
+
sudo gem install has_many_polymorphs
|
52
|
+
|
53
|
+
If you are using the gem, make sure to add <tt>require 'has_many_polymorphs'</tt> to <tt>environment.rb</tt>, before Rails::Initializer block.
|
54
|
+
|
55
|
+
== Configuration
|
56
|
+
|
57
|
+
Setup the parent model as so:
|
58
|
+
|
59
|
+
class Kennel < ActiveRecord::Base
|
60
|
+
has_many_polymorphs :guests, :from => [:dogs, :cats, :birds]
|
61
|
+
end
|
62
|
+
|
63
|
+
The join model:
|
64
|
+
|
65
|
+
class GuestsKennel < ActiveRecord::Base
|
66
|
+
belongs_to :kennel
|
67
|
+
belongs_to :guest, :polymorphic => true
|
68
|
+
end
|
69
|
+
|
70
|
+
One of the child models:
|
71
|
+
|
72
|
+
class Dog < ActiveRecord::Base
|
73
|
+
# nothing
|
74
|
+
end
|
75
|
+
|
76
|
+
For your parent and child models, you don't need any special fields in your migration. For the join model (GuestsKennel), use a migration like so:
|
77
|
+
|
78
|
+
class CreateGuestsKennels < ActiveRecord::Migration
|
79
|
+
def self.up
|
80
|
+
create_table :guests_kennels do |t|
|
81
|
+
t.references :guest, :polymorphic => true
|
82
|
+
t.references :kennel
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.down
|
87
|
+
drop_table :guests_kennels
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
See ActiveRecord::Associations::PolymorphicClassMethods for more configuration options.
|
92
|
+
|
93
|
+
== Helper methods example
|
94
|
+
|
95
|
+
>> k = Kennel.find(1)
|
96
|
+
#<Kennel id: 1, name: "Happy Paws">
|
97
|
+
>> k.guests.map(&:class)
|
98
|
+
[Dog, Cat, Cat, Bird]
|
99
|
+
|
100
|
+
>> k.guests.push(Cat.create); k.cats.size
|
101
|
+
3
|
102
|
+
>> k.guests << Cat.create; k.cats.size
|
103
|
+
4
|
104
|
+
>> k.guests.size
|
105
|
+
6
|
106
|
+
|
107
|
+
>> d = k.dogs.first
|
108
|
+
#<Dog id: 3, name: "Rover">
|
109
|
+
>> d.kennels
|
110
|
+
[#<Kennel id: 1, name: "Happy Paws">]
|
111
|
+
|
112
|
+
>> k.guests.delete(d); k.dogs.size
|
113
|
+
0
|
114
|
+
>> k.guests.size
|
115
|
+
5
|
116
|
+
|
117
|
+
Note that the parent method is always plural, even if there is only one parent (<tt>Dog#kennels</tt>, not <tt>Dog#kennel</tt>).
|
118
|
+
|
119
|
+
See ActiveRecord::Associations::PolymorphicAssociation for more helper method details.
|
120
|
+
|
121
|
+
= Extras
|
122
|
+
|
123
|
+
== Double-sided polymorphism
|
124
|
+
|
125
|
+
Double-sided relationships are defined on the join model:
|
126
|
+
|
127
|
+
class Devouring < ActiveRecord::Base
|
128
|
+
belongs_to :guest, :polymorphic => true
|
129
|
+
belongs_to :eaten, :polymorphic => true
|
130
|
+
|
131
|
+
acts_as_double_polymorphic_join(
|
132
|
+
:guests =>[:dogs, :cats],
|
133
|
+
:eatens => [:cats, :birds]
|
134
|
+
)
|
135
|
+
end
|
136
|
+
|
137
|
+
Now, dogs and cats can eat birds and cats. Birds can't eat anything (they aren't <tt>guests</tt>) and dogs can't be eaten by anything (since they aren't <tt>eatens</tt>). The keys stand for what the models are, not what they do.
|
138
|
+
|
139
|
+
In this case, each guest/eaten relationship is called a Devouring.
|
140
|
+
|
141
|
+
In your migration, you need to declare both sides as polymorphic:
|
142
|
+
|
143
|
+
class CreateDevourings < ActiveRecord::Migration
|
144
|
+
def self.up
|
145
|
+
create_table :devourings do |t|
|
146
|
+
t.references :guest, :polymorphic => true
|
147
|
+
t.references :eaten, :polymorphic => true
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def self.down
|
152
|
+
drop_table :devourings
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
See ActiveRecord::Associations::PolymorphicClassMethods for more.
|
157
|
+
|
158
|
+
== Tagging generator
|
159
|
+
|
160
|
+
Has_many_polymorphs includes a tagging system generator. Run:
|
161
|
+
script/generate tagging Dog Cat [...MoreModels...]
|
162
|
+
|
163
|
+
This adds a migration and new Tag and Tagging models in <tt>app/models</tt>. It configures Tag with an appropriate <tt>has_many_polymorphs</tt> call against the models you list at the command line. It also adds the file <tt>lib/tagging_extensions.rb</tt> and <tt>requires</tt> it in <tt>environment.rb</tt>.
|
164
|
+
|
165
|
+
Tests will also be generated.
|
166
|
+
|
167
|
+
Once you've run the generator, you can tag records as follows:
|
168
|
+
|
169
|
+
>> d = Dog.create(:name => "Rover")
|
170
|
+
#<Dog id: 3, name: "Rover">
|
171
|
+
>> d.tag_list
|
172
|
+
""
|
173
|
+
>> d.tag_with "fierce loud"
|
174
|
+
#<Dog id: 3, name: "Rover">
|
175
|
+
>> d.tag_list
|
176
|
+
"fierce loud"
|
177
|
+
>> c = Cat.create(:name => "Chloe")
|
178
|
+
#<Cat id: 1, name: "Chloe">
|
179
|
+
>> c.tag_with "fierce cute"
|
180
|
+
#<Cat id: 1, name: "Chloe">
|
181
|
+
>> c.tag_list
|
182
|
+
"cute fierce"
|
183
|
+
>> Tag.find_by_name("fierce").taggables
|
184
|
+
[#<Cat id: 1, name: "Chloe">, #<Dog id: 3, name: "Rover">]
|
185
|
+
|
186
|
+
The generator accepts the optional flag <tt>--skip-migration</tt> to skip generating a migration (for example, if you are converting from <tt>acts_as_taggable</tt>). It also accepts the flag <tt>--self-referential</tt> if you want to be able to tag tags.
|
187
|
+
|
188
|
+
See ActiveRecord::Base::TaggingExtensions, Tag, and Tagging for more.
|
189
|
+
|
190
|
+
== Troubleshooting
|
191
|
+
|
192
|
+
Some debugging tools are available in <tt>lib/has_many_polymorphs/debugging_tools.rb</tt>.
|
193
|
+
|
194
|
+
If you are having trouble, think very carefully about how your model classes, key columns, and table names relate. You may have to explicitly specify options on your join model such as <tt>:class_name</tt>, <tt>:foreign_key</tt>, or <tt>:as</tt>. The included tests are a good place to look for examples.
|
195
|
+
|
196
|
+
Note that because of the way Rails reloads model classes, the plugin can sometimes bog down your development server. Set <tt>config.cache_classes = true</tt> in <tt>config/environments/development.rb</tt> to avoid this.
|
197
|
+
|
198
|
+
== Reporting problems
|
199
|
+
|
200
|
+
The support forum is here[http://github.com/fauna/has_many_polymorphs/issues].
|
201
|
+
|
202
|
+
Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.
|
203
|
+
|
204
|
+
== Further resources
|
205
|
+
|
206
|
+
* http://blog.evanweaver.com/articles/2007/08/15/polymorphs-tutorial
|
207
|
+
* http://blog.evanweaver.com/articles/2007/02/22/polymorphs-25-total-insanity-branch
|
208
|
+
* http://blog.evanweaver.com/articles/2007/02/09/how-to-find-the-most-popular-tags
|
209
|
+
* http://blog.evanweaver.com/articles/2007/01/13/growing-up-your-acts_as_taggable
|
210
|
+
* http://blog.evanweaver.com/articles/2006/12/02/polymorphs-19
|
211
|
+
* http://blog.evanweaver.com/articles/2006/11/05/directed-double-polymorphic-associations
|
212
|
+
* http://blog.evanweaver.com/articles/2006/11/04/namespaced-model-support-in-has_many_polymorphs
|
213
|
+
* http://blog.evanweaver.com/articles/2006/09/26/sti-support-in-has_many_polymorphs
|
214
|
+
* http://blog.evanweaver.com/articles/2006/09/11/make-polymorphic-children-belong-to-only-one-parent
|