winton-acts_as_relationable 1.0.3 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -2
- data/init.rb +2 -1
- data/lib/acts_as_relationable.rb +87 -88
- metadata +2 -2
data/README.markdown
CHANGED
@@ -15,9 +15,14 @@ Features
|
|
15
15
|
Install
|
16
16
|
-------
|
17
17
|
|
18
|
+
gem install winton-acts_as_relationable
|
19
|
+
|
20
|
+
### Add to `environment.rb`
|
21
|
+
|
22
|
+
config.gem 'winton-acts_as_relationable', :lib => 'acts_as_relationable', :source => 'http://gems.github.com'
|
23
|
+
|
18
24
|
### From your application directory
|
19
25
|
|
20
|
-
gem install winton-acts_as_relationable
|
21
26
|
script/generate acts_as_relationable
|
22
27
|
rake db:migrate
|
23
28
|
|
@@ -28,4 +33,4 @@ Usage
|
|
28
33
|
[Check out the wiki](http://github.com/winton/acts_as_relationable/wikis) for usage examples.
|
29
34
|
|
30
35
|
|
31
|
-
##### Copyright © 2008 [Winton Welsh](mail@wintoni.us)
|
36
|
+
##### Copyright © 2008 [Winton Welsh](mailto:mail@wintoni.us)
|
data/init.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
require 'acts_as_relationable'
|
1
|
+
require 'acts_as_relationable'
|
2
|
+
ActiveRecord::Base.send :include, AppTower::ActsAsRelationable
|
data/lib/acts_as_relationable.rb
CHANGED
@@ -1,106 +1,105 @@
|
|
1
|
-
|
1
|
+
module AppTower
|
2
|
+
module ActsAsRelationable
|
3
|
+
|
4
|
+
MODELS = Dir[RAILS_ROOT + "/app/models/*.rb"].collect { |f| File.basename f, '.rb' }
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ActMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActMethods
|
11
|
+
def acts_as_relationable(*types)
|
12
|
+
if types.empty? # Relationship model
|
13
|
+
belongs_to :parent, :polymorphic => true
|
14
|
+
belongs_to :child, :polymorphic => true
|
2
15
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
16
|
+
MODELS.each do |m|
|
17
|
+
belongs_to "parent_#{m}".intern, :foreign_key => 'parent_id', :class_name => m.camelize
|
18
|
+
belongs_to "child_#{m}".intern, :foreign_key => 'child_id', :class_name => m.camelize
|
19
|
+
end
|
20
|
+
else
|
21
|
+
options = types.extract_options!
|
22
|
+
sql = options[:conditions]
|
23
|
+
table = options[:table]
|
24
|
+
fields = options[:fields] || []
|
25
|
+
fields = [ fields ] unless fields.respond_to?(:flatten)
|
9
26
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
belongs_to "parent_#{m}".intern, :foreign_key => 'parent_id', :class_name => m.camelize
|
18
|
-
belongs_to "child_#{m}".intern, :foreign_key => 'child_id', :class_name => m.camelize
|
19
|
-
end
|
20
|
-
else
|
21
|
-
options = types.extract_options!
|
22
|
-
sql = options[:conditions]
|
23
|
-
table = options[:table]
|
24
|
-
fields = options[:fields] || []
|
25
|
-
fields = [ fields ] unless fields.respond_to?(:flatten)
|
26
|
-
|
27
|
-
before_save :save_relationship_fields
|
27
|
+
has_many :parent_relationships, :class_name => 'Relationship', :as => :child
|
28
|
+
has_many :child_relationships, :class_name => 'Relationship', :as => :parent
|
29
|
+
|
30
|
+
types.each do |type|
|
31
|
+
type = type.to_s
|
32
|
+
table = table || type
|
33
|
+
select = "#{table}.*, relationships.id AS relationship_id#{fields.empty? ? '' : ', '}" + fields.collect { |f| "relationships.#{f}" }.join(', ')
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
has_many 'parent_' + type,
|
38
|
-
:select => select, :conditions => sql, :through => :parent_relationships,
|
39
|
-
:source => :parent, :class_name => type.classify, :source_type => table.classify do
|
40
|
-
fields.each do |field|
|
41
|
-
define_method field.to_s.pluralize do |*args|
|
42
|
-
value = args[0] || 1
|
43
|
-
find :all, :conditions => [ "relationships.#{field} = ?", value ]
|
35
|
+
has_many 'parent_' + type,
|
36
|
+
:select => select, :conditions => sql, :through => :parent_relationships,
|
37
|
+
:source => :parent, :class_name => type.classify, :source_type => table.classify do
|
38
|
+
fields.each do |field|
|
39
|
+
define_method field.to_s.pluralize do |*args|
|
40
|
+
value = args[0] || 1
|
41
|
+
find :all, :conditions => [ "relationships.#{field} = ?", value ]
|
42
|
+
end
|
44
43
|
end
|
45
44
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
45
|
+
|
46
|
+
has_many 'child_' + type,
|
47
|
+
:select => select, :conditions => sql, :through => :child_relationships,
|
48
|
+
:source => :child, :class_name => type.classify, :source_type => table.classify do
|
49
|
+
fields.each do |field|
|
50
|
+
define_method field.to_s.pluralize do |*args|
|
51
|
+
value = args[0] || 1
|
52
|
+
find :all, :conditions => [ "relationships.#{field} = ?", value ]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
self.class_eval do
|
58
|
+
# Records reader
|
59
|
+
define_method type do |*args|
|
60
|
+
if (read_attribute(:type) || self.class.to_s) < (args.empty? ? type.classify : args[0].to_s)
|
61
|
+
eval "self.child_#{type}"
|
62
|
+
else
|
63
|
+
eval "self.parent_#{type}"
|
55
64
|
end
|
56
65
|
end
|
57
66
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
67
|
+
|
68
|
+
fields.each do |field|
|
69
|
+
# Relationship field writer
|
70
|
+
self.class_eval do
|
71
|
+
define_method field.to_s + '=' do |value|
|
72
|
+
modified = read_attribute(:modified_relationship_fields) || []
|
73
|
+
modified << field
|
74
|
+
write_attribute :modified_relationship_fields, modified.uniq
|
75
|
+
write_attribute field, value
|
76
|
+
end
|
65
77
|
end
|
66
78
|
end
|
67
79
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
define_method field.to_s + '=' do |value|
|
73
|
-
modified = read_attribute(:modified_relationship_fields) || []
|
74
|
-
modified << field
|
75
|
-
write_attribute :modified_relationship_fields, modified.uniq
|
76
|
-
write_attribute field, value
|
77
|
-
end
|
78
|
-
define_method field.to_s do
|
79
|
-
read_attribute(field) || nil
|
80
|
-
end
|
80
|
+
unless included_modules.include? InstanceMethods
|
81
|
+
extend ClassMethods
|
82
|
+
include InstanceMethods
|
83
|
+
before_save :save_relationship_fields
|
81
84
|
end
|
82
85
|
end
|
83
86
|
end
|
84
|
-
|
85
|
-
include ActsAsRelationable::InstanceMethods
|
86
|
-
extend ActsAsRelationable::SingletonMethods
|
87
87
|
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
88
|
+
|
89
|
+
module ClassMethods
|
90
|
+
end
|
91
|
+
|
92
|
+
module InstanceMethods
|
93
|
+
# Before save
|
94
|
+
def save_relationship_fields
|
95
|
+
return unless read_attribute(:relationship_id) && read_attribute(:modified_relationship_fields)
|
96
|
+
r = Relationship.find self.relationship_id
|
97
|
+
read_attribute(:modified_relationship_fields).each do |field|
|
98
|
+
r[field] = self[field]
|
99
|
+
end
|
100
|
+
r.save
|
101
|
+
write_attribute :modified_relationship_fields, nil
|
99
102
|
end
|
100
|
-
r.save
|
101
|
-
write_attribute :modified_relationship_fields, nil
|
102
103
|
end
|
103
104
|
end
|
104
|
-
end
|
105
|
-
|
106
|
-
ActiveRecord::Base.send(:include, ActsAsRelationable)
|
105
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: winton-acts_as_relationable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: "1.1"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winton Welsh
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-11-26 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|