picky-client 4.11.3 → 4.12.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,9 @@ module Picky
|
|
36
36
|
|
37
37
|
# Populates the ids with (rendered) model instances.
|
38
38
|
#
|
39
|
-
#
|
39
|
+
# It does this by calling #find_by_id on the given model class.
|
40
|
+
#
|
41
|
+
# Give it eg. an ActiveRecord class and options for the find_by_id and it
|
40
42
|
# will yield each found result for you to render.
|
41
43
|
#
|
42
44
|
# If you don't pass it a block, it will just use the AR results.
|
@@ -49,12 +51,16 @@ module Picky
|
|
49
51
|
#
|
50
52
|
# === Options
|
51
53
|
# * up_to: Amount of results to populate. All of them by default.
|
52
|
-
# *
|
54
|
+
# * finder_method: Specify which AR finder method you want to load the model with. Default is #find_by_id.
|
55
|
+
# * The rest of the options are directly passed through to the ModelClass.find_by_id(ids, options) method. Default is {}.
|
53
56
|
#
|
54
57
|
def populate_with model_class, options = {}, &block
|
55
|
-
the_ids
|
56
|
-
|
57
|
-
|
58
|
+
the_ids = ids options.delete(:up_to)
|
59
|
+
finder_method = options.delete(:finder_method) || :find_by_id
|
60
|
+
|
61
|
+
# Call finder method.
|
62
|
+
#
|
63
|
+
objects = model_class.send finder_method, the_ids, options
|
58
64
|
|
59
65
|
# Put together a mapping.
|
60
66
|
#
|
@@ -66,9 +72,13 @@ module Picky
|
|
66
72
|
# Preserves the order
|
67
73
|
#
|
68
74
|
objects = the_ids.map { |id| mapped_entries[id] }
|
69
|
-
|
70
|
-
objects
|
71
|
-
|
75
|
+
|
76
|
+
# Replace objects with rendered versions if a block is given.
|
77
|
+
#
|
78
|
+
objects.collect! &block if block
|
79
|
+
|
80
|
+
# Enhance the allocations with the objects or rendered objects.
|
81
|
+
#
|
72
82
|
amend_ids_with objects
|
73
83
|
|
74
84
|
objects
|
@@ -112,4 +122,4 @@ module Picky
|
|
112
122
|
end
|
113
123
|
|
114
124
|
end
|
115
|
-
end
|
125
|
+
end
|
@@ -93,7 +93,7 @@ describe Picky::Convenience do
|
|
93
93
|
def initialize id
|
94
94
|
@id = id
|
95
95
|
end
|
96
|
-
def self.
|
96
|
+
def self.find_by_id ids, options = {}
|
97
97
|
ids.map { |id| new(id) }
|
98
98
|
end
|
99
99
|
def == other
|
@@ -113,6 +113,10 @@ describe Picky::Convenience do
|
|
113
113
|
@results.populate_with(ARClass) { |ar_instance| ar_instance.id.to_s }
|
114
114
|
@results.ids.should_not be_empty
|
115
115
|
end
|
116
|
+
it "should allow you to override the finder method you use" do
|
117
|
+
ARClass.should_receive(:find_by_id).and_return((1..20).map { |id| ARClass.new(id) })
|
118
|
+
@results.populate_with ARClass, :finder_method => :find_by_id
|
119
|
+
end
|
116
120
|
end
|
117
121
|
|
118
122
|
describe 'replace_ids_with' do
|
@@ -210,4 +214,4 @@ describe Picky::Convenience do
|
|
210
214
|
end
|
211
215
|
end
|
212
216
|
|
213
|
-
end
|
217
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: picky-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.12.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yajl-ruby
|