has_many_polymorphic 0.1.0 → 0.2.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/MIT-LICENSE.txt CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2007-2011 Collective Idea
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
1
+ Copyright (c) 2007-2011 Collective Idea
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
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,45 +1,53 @@
1
- = HasManyPolymorphic
2
-
3
- This mixin adds a has many polymorphic relationship to a model and creates all the relationships needed by rails to handle it.
4
-
5
- == Params
6
- - Name
7
- - name of the relationship, there is a convention that whatever name you choose, the polymorphic table columns on your through table should match.
8
-
9
- - Options
10
- - through - the model that handles the through relationship
11
- - models - models that should be included in this polymophic relationship
12
-
13
-
14
- == Added methods
15
-
16
- - {name param}
17
- - the name of your relationship is used for the method name of this method. it will return an array of the models that are related via the has_many relationships
18
-
19
- There is an after_save call back that will save the relationships when they are added or removed. If you want to remove a relationship the models need to be destroyed and this model reloaded.
20
-
21
- == Example Usage
22
-
23
- class PreferenceType < ActiveRecord::Base
24
- has_many_polymorphic :preferenced_records,
25
- :through => :valid_preference_types,
26
- :models => [:desktops, :organizers]
27
- end
28
-
29
- this gives you the following
30
-
31
- preferenceType = PreferenceType.first
32
- preferenceType.desktops
33
- preferenceType.organizers
34
-
35
- and
36
-
37
- preferenceType.preferenced_records
38
-
39
- which is a concatentated array of the models. You also get the following
40
-
41
- desktop = Desktop.first
42
- desktop.preference_types
43
-
44
- organizer = Organizer.first
1
+ = HasManyPolymorphic
2
+
3
+ This mixin adds a has many polymorphic relationship to a model and creates all the relationships needed by rails to handle it.
4
+
5
+ == Options
6
+ - Models
7
+ - the models you want to autoload so all the relationships are created on initialzation of Rails
8
+
9
+ == Example Usage
10
+
11
+ RussellEdge::HasManyPolymorphic.options[:models] = %w(PreferenceType)
12
+
13
+ == Params
14
+ - Name
15
+ - name of the relationship, there is a convention that whatever name you choose, the polymorphic table columns on your through table should match.
16
+
17
+ - Options
18
+ - through - the model that handles the through relationship
19
+ - models - models that should be included in this polymophic relationship
20
+
21
+
22
+ == Added methods
23
+
24
+ - {name param}
25
+ - the name of your relationship is used for the method name of this method. it will return an array of the models that are related via the has_many relationships
26
+
27
+ There is an after_save call back that will save the relationships when they are added or removed. If you want to remove a relationship the models need to be destroyed and this model reloaded.
28
+
29
+ == Example Usage
30
+
31
+ class PreferenceType < ActiveRecord::Base
32
+ has_many_polymorphic :preferenced_records,
33
+ :through => :valid_preference_types,
34
+ :models => [:desktops, :organizers]
35
+ end
36
+
37
+ this gives you the following
38
+
39
+ preferenceType = PreferenceType.first
40
+ preferenceType.desktops
41
+ preferenceType.organizers
42
+
43
+ and
44
+
45
+ preferenceType.preferenced_records
46
+
47
+ which is a concatentated array of the models. You also get the following
48
+
49
+ desktop = Desktop.first
50
+ desktop.preference_types
51
+
52
+ organizer = Organizer.first
45
53
  organizer.preference_types
@@ -1,37 +1,37 @@
1
- module RussellEdge
2
- module HasManyPolymorphic
3
-
4
- =begin rdoc
5
- Searches for models that use <tt>has_many_polymorphic</tt> and makes sure that they get loaded during app initialization.
6
- This ensures that helper methods are injected into the target classes.
7
- =end
8
-
9
- #define the models that use has_many_polymorphic. has_many_polymorphs combed the file system for models
10
- #that had the has_many_polymorphs method. This is not as robust but more efficent. It can be set via
11
- #
12
- #RussellEdge::HasManyPolymorphic::OPTIONS = { :models => %w(PreferenceType AnotherModel) }
13
- #
14
- DEFAULT_OPTIONS = {:models => %w()}
15
- mattr_accessor :options
16
- @@options = HashWithIndifferentAccess.new(DEFAULT_OPTIONS)
17
-
18
- # Dispatcher callback to load polymorphic relationships
19
- def self.autoload
20
- options[:models].each do |model|
21
- #try to load model if it exists.
22
- begin
23
- model.constantize
24
- rescue=>e
25
- end
26
- end
27
-
28
- end
29
- end
30
- end
31
-
32
- #create Railtie to plugin into rails initialization
33
- class RussellEdge::HasManyPolymorphic::RailTie < Rails::Railtie
34
- initializer "has_many_polymorphic.autoload_models", :after => :initialize do |app|
35
- RussellEdge::HasManyPolymorphic.autoload
36
- end
37
- end
1
+ module RussellEdge
2
+ module HasManyPolymorphic
3
+
4
+ =begin rdoc
5
+ Searches for models that use <tt>has_many_polymorphic</tt> and makes sure that they get loaded during app initialization.
6
+ This ensures that helper methods are injected into the target classes.
7
+ =end
8
+
9
+ #define the models that use has_many_polymorphic. has_many_polymorphs combed the file system for models
10
+ #that had the has_many_polymorphs method. This is not as robust but more efficent. It can be set via
11
+ #
12
+ #RussellEdge::HasManyPolymorphic::OPTIONS = { :models => %w(PreferenceType AnotherModel) }
13
+ #
14
+ DEFAULT_OPTIONS = {:models => %w()}
15
+ mattr_accessor :options
16
+ @@options = HashWithIndifferentAccess.new(DEFAULT_OPTIONS)
17
+
18
+ # Dispatcher callback to load polymorphic relationships
19
+ def self.autoload
20
+ options[:models].each do |model|
21
+ #try to load model if it exists.
22
+ begin
23
+ model.constantize
24
+ rescue=>e
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+ #create Railtie to plugin into rails initialization
33
+ class RussellEdge::HasManyPolymorphic::RailTie < Rails::Railtie
34
+ initializer "has_many_polymorphic.autoload_models", :after => :initialize do |app|
35
+ RussellEdge::HasManyPolymorphic.autoload
36
+ end
37
+ end
@@ -118,15 +118,11 @@ module RussellEdge #:nodoc:
118
118
  include RussellEdge::HasManyPolymorphic::InstanceMethods
119
119
 
120
120
  #add the relationship to the models.
121
- options[:models].each do |model|
121
+ options[:models].each do |model|
122
122
  model.to_s.classify.constantize.class_exec do
123
123
  #build the has many polymorphic relationship via finder_sql
124
- has_many target_class_name.underscore.pluralize.to_sym,
125
- :class_name => "#{target_class_name}",
126
- :finder_sql => 'SELECT DISTINCT target.* from '+target_class_name.tableize+' target join '+
127
- options[:through].to_s+' join_table on join_table.'+target_class_name.underscore.singularize+'_id = target.id '+
128
- 'and join_table.'+name.to_s.singularize+'_type = \'#{model_class_name}\' and '+
129
- 'join_table.'+name.to_s.singularize+'_id = #{id}'
124
+ has_many options[:through], :as => name.to_s.singularize
125
+ has_many target_class_name.tableize, :through => options[:through]
130
126
 
131
127
  #we want to save the relantionships when the model is saved
132
128
  before_save do |record|
@@ -1,3 +1,3 @@
1
- module HasManyPolymorphic
2
- VERSION = '0.1.0' unless defined?(::HasManyPolymorphic::VERSION)
1
+ module HasManyPolymorphic
2
+ VERSION = '0.2.0' unless defined?(::HasManyPolymorphic::VERSION)
3
3
  end
@@ -1,7 +1,7 @@
1
- require 'has_many_polymorphic/has_many_polymorphic'
2
- ActiveRecord::Base.send :include, RussellEdge::HasManyPolymorphic
3
-
4
- #used to tie into Rails initialization to load the models so the relationships are created
5
- require 'has_many_polymorphic/autoload'
6
-
7
-
1
+ require 'has_many_polymorphic/has_many_polymorphic'
2
+ ActiveRecord::Base.send :include, RussellEdge::HasManyPolymorphic
3
+
4
+ #used to tie into Rails initialization to load the models so the relationships are created
5
+ require 'has_many_polymorphic/autoload'
6
+
7
+
metadata CHANGED
@@ -1,33 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: has_many_polymorphic
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Russell Holmes
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-08-05 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2011-08-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: activerecord
16
- requirement: &2169155880 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 5
21
33
  version: 2.3.5
22
34
  type: :runtime
23
- prerelease: false
24
- version_requirements: *2169155880
35
+ version_requirements: *id001
25
36
  description: Simple replacement for has_many_polymorphs
26
37
  email: rholmes@tnsolutionsinc.com
27
38
  executables: []
39
+
28
40
  extensions: []
41
+
29
42
  extra_rdoc_files: []
30
- files:
43
+
44
+ files:
31
45
  - lib/has_many_polymorphic/autoload.rb
32
46
  - lib/has_many_polymorphic/has_many_polymorphic.rb
33
47
  - lib/has_many_polymorphic/version.rb
@@ -36,26 +50,36 @@ files:
36
50
  - README.rdoc
37
51
  homepage: https://github.com/russ1985/has_many_polymorphic
38
52
  licenses: []
53
+
39
54
  post_install_message:
40
55
  rdoc_options: []
41
- require_paths:
56
+
57
+ require_paths:
42
58
  - lib
43
- required_ruby_version: !ruby/object:Gem::Requirement
59
+ required_ruby_version: !ruby/object:Gem::Requirement
44
60
  none: false
45
- requirements:
46
- - - ! '>='
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
69
  none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
55
77
  requirements: []
78
+
56
79
  rubyforge_project:
57
80
  rubygems_version: 1.8.6
58
81
  signing_key:
59
82
  specification_version: 3
60
83
  summary: Simple replacement for has_many_polymorphs
61
84
  test_files: []
85
+