acts-as-joinable 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/acts_as_joinable/core.rb +28 -39
- data/test/test_helper.rb +1 -0
- metadata +4 -4
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
|
|
5
5
|
spec = Gem::Specification.new do |s|
|
6
6
|
s.name = "acts-as-joinable"
|
7
7
|
s.authors = ["Lance Pollard"]
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
s.summary = "ActsAsJoinable: DRYing up Many-to-Many Relationships in ActiveRecord"
|
10
10
|
s.homepage = "http://github.com/viatropos/acts-as-joinable"
|
11
11
|
s.email = "lancejpollard@gmail.com"
|
@@ -40,11 +40,11 @@ module ActsAsJoinable
|
|
40
40
|
# acts_as_joinable_on :tenants, :as => :parent
|
41
41
|
# User
|
42
42
|
def initialize_acts_as_joinable_on_core
|
43
|
-
joins
|
44
|
-
block
|
45
|
-
options
|
43
|
+
joins = acts_as_joinable_config.dup
|
44
|
+
block = joins.pop
|
45
|
+
options = joins.extract_options!
|
46
46
|
if joins.empty?
|
47
|
-
joins
|
47
|
+
joins = ActsAsJoinable.models
|
48
48
|
relationships = [:child]
|
49
49
|
else
|
50
50
|
relationships = [options[:as] || :parent].flatten.map(&:to_sym)
|
@@ -54,7 +54,7 @@ module ActsAsJoinable
|
|
54
54
|
|
55
55
|
association_type = options[:limit] == 1 ? :has_one : :has_many
|
56
56
|
|
57
|
-
scope_name
|
57
|
+
scope_name = options[:named_scope]
|
58
58
|
|
59
59
|
joins.map!(&:to_sym)
|
60
60
|
|
@@ -125,16 +125,11 @@ module ActsAsJoinable
|
|
125
125
|
|
126
126
|
join_value = value
|
127
127
|
if join_context
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
condition_string << " AND (#{ActsAsJoinable::Relationship.table_name.to_s}.value = ?)" if join_value
|
134
|
-
join_contexts = [join_context, class_name.underscore]
|
135
|
-
conditions = [condition_string, join_contexts]
|
136
|
-
conditions << join_value.to_s if join_value
|
137
|
-
end
|
128
|
+
condition_string = "(#{ActsAsJoinable::Relationship.table_name.to_s}.context IN (?))"
|
129
|
+
condition_string << " AND (#{ActsAsJoinable::Relationship.table_name.to_s}.value = ?)" if join_value
|
130
|
+
join_contexts = [join_context, class_name.underscore]
|
131
|
+
conditions = [condition_string, join_contexts]
|
132
|
+
conditions << join_value.to_s if join_value
|
138
133
|
|
139
134
|
through_options = {
|
140
135
|
:class_name => "ActsAsJoinable::Relationship",
|
@@ -162,22 +157,7 @@ module ActsAsJoinable
|
|
162
157
|
#send(method_scope)
|
163
158
|
# has_many :child_users, :through => :child_relationships
|
164
159
|
add_association(relationship.to_s, plural_type, options, join_context, join_value, nestable, &block)
|
165
|
-
|
166
|
-
if association_type == :has_one
|
167
|
-
define_method singular_type do
|
168
|
-
send(plural_type).first
|
169
|
-
end
|
170
|
-
define_method "#{singular_type}=" do |value|
|
171
|
-
send(relationship_with_context).destroy_all
|
172
|
-
send(plural_type) << value
|
173
|
-
end
|
174
|
-
define_method "#{singular_type}_id" do
|
175
|
-
send(singular_type).id rescue nil
|
176
|
-
end
|
177
|
-
define_method "#{singular_type}_id=" do |id|
|
178
|
-
send("#{singular_type}=", class_name.constantize.find(id))
|
179
|
-
end
|
180
|
-
end
|
160
|
+
add_has_one(singular_type, plural_type, relationship_with_context, class_name) if association_type == :has_one
|
181
161
|
end
|
182
162
|
end
|
183
163
|
end
|
@@ -192,16 +172,25 @@ module ActsAsJoinable
|
|
192
172
|
role.to_s == "parent" ? "child" : "parent"
|
193
173
|
end
|
194
174
|
|
175
|
+
def add_has_one(singular_type, plural_type, relationship_with_context, class_name)
|
176
|
+
define_method singular_type do
|
177
|
+
send(plural_type).last
|
178
|
+
end
|
179
|
+
define_method "#{singular_type}=" do |value|
|
180
|
+
# send(relationship_with_context).delete_all
|
181
|
+
send(plural_type) << value
|
182
|
+
end
|
183
|
+
define_method "#{singular_type}_id" do
|
184
|
+
send(singular_type).id rescue nil
|
185
|
+
end
|
186
|
+
define_method "#{singular_type}_id=" do |id|
|
187
|
+
send("#{singular_type}=", class_name.constantize.find(id))
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
195
191
|
def add_association(relationship, plural_type, options, join_context, join_value, nestable, &block)
|
196
192
|
eval_options = {:context => join_context}
|
197
193
|
eval_options[:value] = join_value unless join_value.blank?
|
198
|
-
send(:has_many, "#{relationship}_#{plural_type}".to_sym, options) do
|
199
|
-
class_eval <<-EOF
|
200
|
-
def construct_join_attributes(associate)
|
201
|
-
super.merge(#{eval_options.inspect})
|
202
|
-
end
|
203
|
-
EOF
|
204
|
-
end
|
205
194
|
# has_many :users, :through => :child_relationships
|
206
195
|
|
207
196
|
unless self.reflect_on_all_associations.map(&:name).include?(plural_type.to_sym)
|
@@ -211,8 +200,8 @@ module ActsAsJoinable
|
|
211
200
|
super.merge(#{eval_options.inspect})
|
212
201
|
end
|
213
202
|
EOF
|
203
|
+
|
214
204
|
end
|
215
|
-
|
216
205
|
accepts_nested_attributes_for plural_type.to_sym if nestable
|
217
206
|
end
|
218
207
|
end
|
data/test/test_helper.rb
CHANGED
@@ -13,6 +13,7 @@ require 'logger'
|
|
13
13
|
this = File.expand_path(File.dirname(__FILE__))
|
14
14
|
|
15
15
|
#ActiveRecord::Base.logger = Logger.new(STDOUT)
|
16
|
+
# b.class.reflect_on_all_associations.detect {|i| i.name == :child_location_relationship}
|
16
17
|
|
17
18
|
require File.expand_path(File.join(this, '/../lib/acts-as-joinable'))
|
18
19
|
require File.join(this, "database")
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts-as-joinable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lance Pollard
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-21 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|