foreign_model 0.2.0 → 0.2.1
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/VERSION +1 -1
- data/foreign_model.gemspec +2 -2
- data/lib/foreign_model.rb +37 -33
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/foreign_model.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{foreign_model}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mike Nicholaides"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-07-29}
|
13
13
|
s.description = %q{Works for ActiveRecord and Mongoid}
|
14
14
|
s.email = %q{mike@ablegray.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/foreign_model.rb
CHANGED
@@ -12,9 +12,9 @@ module ForeignModel
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
def belongs_to_foreign_model(name, options={})
|
15
|
-
options[:class_name] ||= ForeignModel.camelize(name
|
15
|
+
options[:class_name] ||= ForeignModel.camelize(name)
|
16
16
|
|
17
|
-
ForeignModel::SCOPE_PROCS[self][name
|
17
|
+
ForeignModel::SCOPE_PROCS[self][name] = begin
|
18
18
|
if options[:scope]
|
19
19
|
options[:scope]
|
20
20
|
elsif options[:polymorphic]
|
@@ -27,43 +27,47 @@ module ForeignModel
|
|
27
27
|
proc{ eval(options[:class_name]) }
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
30
|
+
|
31
|
+
define_method :_foreign_models do
|
32
|
+
@_foreign_models ||= {}
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method :_parent_procs do
|
36
|
+
@_parent_procs ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method :parent_proc_for do |foreign_model_name|
|
40
|
+
_parent_procs[foreign_model_name] ||= ForeignModel::SCOPE_PROCS[self.class][foreign_model_name].call(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
define_method name do
|
44
|
+
if parent_proc_for(name) && send("#{name}_id") && send("#{name}_id") != ""
|
45
|
+
_foreign_models[name] ||= parent_proc_for(name).find(send("#{name}_id"))
|
40
46
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method "#{name}=" do |foreign_model|
|
50
|
+
_foreign_models[name] = foreign_model
|
51
|
+
if foreign_model
|
52
|
+
send("#{name}_id=", foreign_model.id)
|
53
|
+
send("#{name}_type=", foreign_model.class.name) if respond_to? "#{name}_type="
|
48
54
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method "#{name}_id=" do |foreign_model_id|
|
58
|
+
if send("#{name}_id") != foreign_model_id
|
59
|
+
write_raw_attribute("#{name}_id", foreign_model_id)
|
60
|
+
_foreign_models[name] = nil
|
55
61
|
end
|
56
|
-
|
62
|
+
end
|
57
63
|
|
58
64
|
if options[:polymorphic]
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
@#{name} = nil
|
64
|
-
end
|
65
|
+
define_method "#{name}_type=" do |foreign_model_type|
|
66
|
+
if send("#{name}_type") != foreign_model_type
|
67
|
+
write_raw_attribute("#{name}_type", foreign_model_type)
|
68
|
+
_foreign_models[name] = nil
|
65
69
|
end
|
66
|
-
|
70
|
+
end
|
67
71
|
end
|
68
72
|
end
|
69
73
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Mike Nicholaides
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-07-29 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|