attribution 0.6.5 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/attribution.gemspec +1 -1
- data/lib/attribution/version.rb +1 -1
- data/lib/attribution.rb +7 -4
- data/test/attribution_model_test.rb +2 -2
- data/test/attribution_test.rb +51 -3
- data/test/test_helper.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12280ee9715f6fab2bb30351d1bb4db0fb7ff3b8
|
4
|
+
data.tar.gz: d90c899d05ae2475a49b4acd971041c2517b066f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d896f36c33b15cf12912f07453dfadf4894e80044ed1e975f94ef8965a3d72a3f6fc8e1b55fa4208a0eb2eae8b67c4eb6b8ca6e6fc000363dbb5d601e32b5081
|
7
|
+
data.tar.gz: 8ec63499d6ed0238ae23990fb5e3f5d89f1d3ee4ec48ee06839e4a72ab882c7b26aec3ccff48ec026bb860555b583d7c372a325625227469da020b41d5fe8fe3
|
data/attribution.gemspec
CHANGED
data/lib/attribution/version.rb
CHANGED
data/lib/attribution.rb
CHANGED
@@ -197,6 +197,10 @@ module Attribution
|
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
|
+
def autoload_associations?
|
201
|
+
true
|
202
|
+
end
|
203
|
+
|
200
204
|
# Association macros
|
201
205
|
|
202
206
|
# Defines an attribute that is a reference to another Attribution class.
|
@@ -236,14 +240,13 @@ module Attribution
|
|
236
240
|
instance_variable_get("@#{association_name}")
|
237
241
|
elsif id = instance_variable_get("@#{association_name}_id")
|
238
242
|
|
239
|
-
# TODO: Support a more generic version of lazy-loading
|
240
243
|
begin
|
241
244
|
association_class = Object.const_get(association_class_name)
|
242
245
|
rescue NameError => ex
|
243
246
|
raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist")
|
244
247
|
end
|
245
248
|
|
246
|
-
if association_class.respond_to?(:find)
|
249
|
+
if self.class.autoload_associations? && association_class.respond_to?(:find)
|
247
250
|
instance_variable_set("@#{association_name}", association_class.find(id))
|
248
251
|
end
|
249
252
|
else
|
@@ -290,11 +293,11 @@ module Attribution
|
|
290
293
|
ivar = "@#{association_name}"
|
291
294
|
if instance_variable_defined?(ivar)
|
292
295
|
instance_variable_get(ivar)
|
293
|
-
elsif association_class.respond_to?(:all)
|
296
|
+
elsif self.class.autoload_associations? && association_class.respond_to?(:all)
|
294
297
|
instance_variable_set(ivar, Array(association_class.all("#{self.class.name.underscore}_id" => id)))
|
295
298
|
end
|
296
299
|
else # Ex: Book.all(:name => "The..."), so we do not want to cache it
|
297
|
-
if association_class.respond_to?(:all)
|
300
|
+
if self.class.autoload_associations? && association_class.respond_to?(:all)
|
298
301
|
Array(association_class.all({"#{self.class.name.demodulize.underscore}_id" => id}.merge(query.first)))
|
299
302
|
end
|
300
303
|
end
|
@@ -4,12 +4,12 @@ require 'attribution/model'
|
|
4
4
|
class Article
|
5
5
|
include Attribution::Model
|
6
6
|
|
7
|
-
string :title, required
|
7
|
+
string :title, :required => true, :format => { :with => /\A\w/, :message => "must start with a letter" }, :length => 4..20
|
8
8
|
end
|
9
9
|
|
10
10
|
class AttributionModelTest < Test::Unit::TestCase
|
11
11
|
def test_model
|
12
|
-
article = Article.new(id
|
12
|
+
article = Article.new(:id => 1, :created_at => Time.now, :updated_at => Time.now)
|
13
13
|
assert !article.valid?
|
14
14
|
assert_equal ["can't be blank", "must start with a letter", "is too short (minimum is 4 characters)"], article.errors[:title]
|
15
15
|
end
|
data/test/attribution_test.rb
CHANGED
@@ -42,6 +42,9 @@ class Book
|
|
42
42
|
belongs_to :book
|
43
43
|
has_many :chapters
|
44
44
|
has_many :readers
|
45
|
+
|
46
|
+
def self.find(*args)
|
47
|
+
end
|
45
48
|
end
|
46
49
|
|
47
50
|
class Reader
|
@@ -65,6 +68,10 @@ class Chapter
|
|
65
68
|
|
66
69
|
belongs_to :book
|
67
70
|
has_many :pages
|
71
|
+
|
72
|
+
def self.all(*args)
|
73
|
+
[]
|
74
|
+
end
|
68
75
|
end
|
69
76
|
|
70
77
|
class Page
|
@@ -120,6 +127,22 @@ module Music
|
|
120
127
|
end
|
121
128
|
end
|
122
129
|
|
130
|
+
class StoreModel
|
131
|
+
include Attribution
|
132
|
+
|
133
|
+
def self.autoload_associations?
|
134
|
+
false
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Product < StoreModel
|
139
|
+
has_many :orders
|
140
|
+
end
|
141
|
+
|
142
|
+
class Order < StoreModel
|
143
|
+
belongs_to :product
|
144
|
+
end
|
145
|
+
|
123
146
|
class AttributionTest < Test::Unit::TestCase
|
124
147
|
|
125
148
|
def test_create
|
@@ -274,7 +297,7 @@ class AttributionTest < Test::Unit::TestCase
|
|
274
297
|
end
|
275
298
|
|
276
299
|
def test_to_json_should_only_include_attibutes
|
277
|
-
book = Book.new(id
|
300
|
+
book = Book.new(:id => 1, :foo => 'bar')
|
278
301
|
assert_equal nil, JSON.parse(book.to_json)['foo']
|
279
302
|
assert JSON.parse(book.to_json).key?('title'), 'to_json sohuld include attributes that have no value assigned to them'
|
280
303
|
end
|
@@ -289,12 +312,37 @@ class AttributionTest < Test::Unit::TestCase
|
|
289
312
|
end
|
290
313
|
|
291
314
|
def test_namespaced_belongs_to
|
292
|
-
track = Music::Track.new(number
|
315
|
+
track = Music::Track.new(:number => 1, :title => "Elevator Music", :album => { :artist => "Beck", :title => "The Information" })
|
293
316
|
assert_equal "Beck", track.album.artist
|
294
317
|
end
|
295
318
|
|
296
319
|
def test_namespaced_has_many
|
297
|
-
album = Music::Album.new(artist
|
320
|
+
album = Music::Album.new(:artist => "Beck", :title => "The Information", :tracks => [ { :number => 1, :title => "Elevator Music" } ])
|
298
321
|
assert_equal "Elevator Music", album.tracks.first.title
|
299
322
|
end
|
323
|
+
|
324
|
+
def test_autoload_belongs_to_associations
|
325
|
+
chapter = Chapter.new(:book_id => 42)
|
326
|
+
Book.expects(:find).with(42)
|
327
|
+
chapter.book
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_autoload_has_many_associations
|
331
|
+
book = Book.new(:id => 42)
|
332
|
+
Chapter.expects(:all).with("book_id" => 42)
|
333
|
+
book.chapters
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_disabled_autoload_belongs_to_associations
|
337
|
+
order = Order.new
|
338
|
+
Product.expects(:find).never
|
339
|
+
order.product
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_disabled_autoload_has_many_associations
|
343
|
+
product = Product.new
|
344
|
+
Order.expects(:all).never
|
345
|
+
product.orders
|
346
|
+
end
|
347
|
+
|
300
348
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribution
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Barry
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|