active_hash 0.9.9 → 0.9.10
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/lib/active_hash/version.rb +1 -1
- data/lib/associations/associations.rb +16 -0
- data/spec/associations/associations_spec.rb +36 -0
- metadata +10 -16
data/lib/active_hash/version.rb
CHANGED
|
@@ -50,6 +50,22 @@ module ActiveHash
|
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
+
def has_one(association_id, options = {})
|
|
54
|
+
define_method(association_id) do
|
|
55
|
+
options = {
|
|
56
|
+
:class_name => association_id.to_s.classify,
|
|
57
|
+
:foreign_key => self.class.to_s.foreign_key
|
|
58
|
+
}.merge(options)
|
|
59
|
+
|
|
60
|
+
scope = options[:class_name].constantize
|
|
61
|
+
|
|
62
|
+
if scope.respond_to?(:scoped) && options[:conditions]
|
|
63
|
+
scope = scope.scoped(:conditions => options[:conditions])
|
|
64
|
+
end
|
|
65
|
+
scope.send("find_by_#{options[:foreign_key]}", id)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
53
69
|
def belongs_to(association_id, options = {})
|
|
54
70
|
|
|
55
71
|
options = {
|
|
@@ -209,7 +209,43 @@ describe ActiveHash::Base, "associations" do
|
|
|
209
209
|
author.city_id.should == @city.id
|
|
210
210
|
end
|
|
211
211
|
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
describe "#has_one" do
|
|
215
|
+
context "with ActiveRecord children" do
|
|
216
|
+
before do
|
|
217
|
+
Author.has_one :book
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
it "find the correct records" do
|
|
221
|
+
book = Book.create! :author_id => 1, :published => true
|
|
222
|
+
author = Author.create :id => 1
|
|
223
|
+
author.book.should == book
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it "returns nil when there is no record" do
|
|
227
|
+
author = Author.create :id => 1
|
|
228
|
+
author.book.should be_nil
|
|
229
|
+
end
|
|
230
|
+
end
|
|
212
231
|
|
|
232
|
+
context "with ActiveHash children" do
|
|
233
|
+
before do
|
|
234
|
+
City.has_one :author
|
|
235
|
+
Author.field :city_id
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
it "find the correct records" do
|
|
239
|
+
city = City.create :id => 1
|
|
240
|
+
author = Author.create :city_id => 1
|
|
241
|
+
city.author.should == author
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
it "returns nil when there are no records" do
|
|
245
|
+
city = City.create :id => 1
|
|
246
|
+
city.author.should be_nil
|
|
247
|
+
end
|
|
248
|
+
end
|
|
213
249
|
end
|
|
214
250
|
|
|
215
251
|
describe "#marked_for_destruction?" do
|
metadata
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
5
|
-
prerelease:
|
|
4
|
+
prerelease: false
|
|
6
5
|
segments:
|
|
7
6
|
- 0
|
|
8
7
|
- 9
|
|
9
|
-
-
|
|
10
|
-
version: 0.9.
|
|
8
|
+
- 10
|
|
9
|
+
version: 0.9.10
|
|
11
10
|
platform: ruby
|
|
12
11
|
authors:
|
|
13
12
|
- Jeff Dean
|
|
@@ -31,7 +30,8 @@ autorequire:
|
|
|
31
30
|
bindir: bin
|
|
32
31
|
cert_chain: []
|
|
33
32
|
|
|
34
|
-
date: 2012-01-18 00:00:00
|
|
33
|
+
date: 2012-01-18 00:00:00 -05:00
|
|
34
|
+
default_executable:
|
|
35
35
|
dependencies:
|
|
36
36
|
- !ruby/object:Gem::Dependency
|
|
37
37
|
name: activesupport
|
|
@@ -41,7 +41,6 @@ dependencies:
|
|
|
41
41
|
requirements:
|
|
42
42
|
- - ">="
|
|
43
43
|
- !ruby/object:Gem::Version
|
|
44
|
-
hash: 3
|
|
45
44
|
segments:
|
|
46
45
|
- 2
|
|
47
46
|
- 2
|
|
@@ -57,7 +56,6 @@ dependencies:
|
|
|
57
56
|
requirements:
|
|
58
57
|
- - ~>
|
|
59
58
|
- !ruby/object:Gem::Version
|
|
60
|
-
hash: 7
|
|
61
59
|
segments:
|
|
62
60
|
- 2
|
|
63
61
|
- 2
|
|
@@ -73,7 +71,6 @@ dependencies:
|
|
|
73
71
|
requirements:
|
|
74
72
|
- - ">="
|
|
75
73
|
- !ruby/object:Gem::Version
|
|
76
|
-
hash: 3
|
|
77
74
|
segments:
|
|
78
75
|
- 0
|
|
79
76
|
version: "0"
|
|
@@ -87,7 +84,6 @@ dependencies:
|
|
|
87
84
|
requirements:
|
|
88
85
|
- - ">="
|
|
89
86
|
- !ruby/object:Gem::Version
|
|
90
|
-
hash: 3
|
|
91
87
|
segments:
|
|
92
88
|
- 2
|
|
93
89
|
- 2
|
|
@@ -103,7 +99,6 @@ dependencies:
|
|
|
103
99
|
requirements:
|
|
104
100
|
- - ">="
|
|
105
101
|
- !ruby/object:Gem::Version
|
|
106
|
-
hash: 3
|
|
107
102
|
segments:
|
|
108
103
|
- 0
|
|
109
104
|
version: "0"
|
|
@@ -124,12 +119,12 @@ files:
|
|
|
124
119
|
- README.md
|
|
125
120
|
- active_hash.gemspec
|
|
126
121
|
- lib/active_file/base.rb
|
|
127
|
-
- lib/active_hash/base.rb
|
|
128
|
-
- lib/active_hash/version.rb
|
|
129
|
-
- lib/active_hash.rb
|
|
130
122
|
- lib/active_yaml/base.rb
|
|
131
123
|
- lib/associations/associations.rb
|
|
124
|
+
- lib/active_hash.rb
|
|
132
125
|
- lib/enum/enum.rb
|
|
126
|
+
- lib/active_hash/version.rb
|
|
127
|
+
- lib/active_hash/base.rb
|
|
133
128
|
- Gemfile
|
|
134
129
|
- spec/active_file/base_spec.rb
|
|
135
130
|
- spec/active_hash/base_spec.rb
|
|
@@ -138,6 +133,7 @@ files:
|
|
|
138
133
|
- spec/enum/enum_spec.rb
|
|
139
134
|
- spec/lint_spec.rb
|
|
140
135
|
- spec/spec_helper.rb
|
|
136
|
+
has_rdoc: true
|
|
141
137
|
homepage: http://github.com/zilkey/active_hash
|
|
142
138
|
licenses: []
|
|
143
139
|
|
|
@@ -151,7 +147,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
151
147
|
requirements:
|
|
152
148
|
- - ">="
|
|
153
149
|
- !ruby/object:Gem::Version
|
|
154
|
-
hash: 3
|
|
155
150
|
segments:
|
|
156
151
|
- 0
|
|
157
152
|
version: "0"
|
|
@@ -160,14 +155,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
160
155
|
requirements:
|
|
161
156
|
- - ">="
|
|
162
157
|
- !ruby/object:Gem::Version
|
|
163
|
-
hash: 3
|
|
164
158
|
segments:
|
|
165
159
|
- 0
|
|
166
160
|
version: "0"
|
|
167
161
|
requirements: []
|
|
168
162
|
|
|
169
163
|
rubyforge_project:
|
|
170
|
-
rubygems_version: 1.
|
|
164
|
+
rubygems_version: 1.3.7
|
|
171
165
|
signing_key:
|
|
172
166
|
specification_version: 3
|
|
173
167
|
summary: An ActiveRecord-like model that uses a hash or file as a datasource
|