active_collection 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/active_collection.gemspec +4 -2
- data/lib/active_collection.rb +2 -0
- data/lib/active_collection/base.rb +2 -68
- data/lib/active_collection/member_class.rb +33 -0
- data/lib/active_collection/pagination.rb +8 -2
- data/lib/active_collection/serialization.rb +34 -0
- data/spec/active_collection_spec.rb +7 -0
- data/spec/pagination_spec.rb +8 -0
- metadata +4 -2
data/VERSION.yml
CHANGED
data/active_collection.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{active_collection}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Emde"]
|
12
|
-
s.date = %q{2009-09-
|
12
|
+
s.date = %q{2009-09-16}
|
13
13
|
s.description = %q{A lazy-loading, Array-like collection proxy for ActiveRecord that understands conditions and paging.}
|
14
14
|
s.email = %q{martin.emde@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,9 +27,11 @@ Gem::Specification.new do |s|
|
|
27
27
|
"lib/active_collection.rb",
|
28
28
|
"lib/active_collection/base.rb",
|
29
29
|
"lib/active_collection/includes.rb",
|
30
|
+
"lib/active_collection/member_class.rb",
|
30
31
|
"lib/active_collection/order.rb",
|
31
32
|
"lib/active_collection/pagination.rb",
|
32
33
|
"lib/active_collection/scope.rb",
|
34
|
+
"lib/active_collection/serialization.rb",
|
33
35
|
"spec/active_collection_spec.rb",
|
34
36
|
"spec/pagination_spec.rb",
|
35
37
|
"spec/spec.opts",
|
data/lib/active_collection.rb
CHANGED
@@ -4,10 +4,12 @@ require 'active_support'
|
|
4
4
|
|
5
5
|
module ActiveCollection
|
6
6
|
autoload :Base, 'active_collection/base'
|
7
|
+
autoload :MemberClass, 'active_collection/member_class'
|
7
8
|
autoload :Scope, 'active_collection/scope'
|
8
9
|
autoload :Order, 'active_collection/order'
|
9
10
|
autoload :Includes, 'active_collection/includes'
|
10
11
|
autoload :Pagination, 'active_collection/pagination'
|
12
|
+
autoload :Serialization, 'active_collection/serialization'
|
11
13
|
|
12
14
|
Base
|
13
15
|
end
|
@@ -37,30 +37,6 @@ module ActiveCollection
|
|
37
37
|
proxy_respond_to?(*args) || collection.respond_to?(*args)
|
38
38
|
end
|
39
39
|
|
40
|
-
def self.model_class
|
41
|
-
@model_class ||= name.sub(/Collection$/,'').constantize
|
42
|
-
end
|
43
|
-
|
44
|
-
def model_class
|
45
|
-
self.class.model_class
|
46
|
-
end
|
47
|
-
|
48
|
-
def self.table_name
|
49
|
-
model_class.table_name
|
50
|
-
end
|
51
|
-
|
52
|
-
def table_name
|
53
|
-
self.class.table_name
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.human_name
|
57
|
-
table_name.gsub(/_/,' ')
|
58
|
-
end
|
59
|
-
|
60
|
-
def human_name
|
61
|
-
self.class.human_name
|
62
|
-
end
|
63
|
-
|
64
40
|
# Forwards <tt>===</tt> explicitly to the collection because the instance method
|
65
41
|
# removal above doesn't catch it. Loads the collection if needed.
|
66
42
|
def ===(other)
|
@@ -75,36 +51,6 @@ module ActiveCollection
|
|
75
51
|
end
|
76
52
|
end
|
77
53
|
|
78
|
-
# Turn the params into a hash suitable for passing the collection directly as an arg to a named path.
|
79
|
-
def to_param
|
80
|
-
params.empty?? nil : params.to_param
|
81
|
-
end
|
82
|
-
|
83
|
-
def as_data_hash
|
84
|
-
data_hash = { "collection" => collection.as_json }
|
85
|
-
data_hash["total_entries"] = total_entries
|
86
|
-
data_hash
|
87
|
-
end
|
88
|
-
|
89
|
-
def to_xml(options = {})
|
90
|
-
collect
|
91
|
-
options[:indent] ||= 2
|
92
|
-
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
|
93
|
-
xml.instruct! unless options[:skip_instruct]
|
94
|
-
xml.tag!(table_name) do
|
95
|
-
xml.total_entries(total_entries, :type => "integer")
|
96
|
-
xml.collection(:type => "array") do
|
97
|
-
collection.each do |item|
|
98
|
-
item.to_xml(:indent => options[:indent], :builder => xml, :skip_instruct => true)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def as_json(options = nil)
|
105
|
-
{table_name => as_data_hash}.as_json(options)
|
106
|
-
end
|
107
|
-
|
108
54
|
# Implements Enumerable
|
109
55
|
def each(&block)
|
110
56
|
collection.each(&block)
|
@@ -191,23 +137,11 @@ module ActiveCollection
|
|
191
137
|
def raise_if_loaded
|
192
138
|
raise AlreadyLoadedError, "Cannot modify a collection that has already been loaded." if loaded?
|
193
139
|
end
|
194
|
-
|
195
|
-
# Extracted from AR:B
|
196
|
-
# Object#to_a is deprecated, though it does have the desired behavior
|
197
|
-
def safe_to_array(o)
|
198
|
-
case o
|
199
|
-
when NilClass
|
200
|
-
[]
|
201
|
-
when Array
|
202
|
-
o
|
203
|
-
else
|
204
|
-
[o]
|
205
|
-
end
|
206
|
-
end
|
207
140
|
end
|
208
141
|
|
209
142
|
Base.class_eval do
|
143
|
+
include MemberClass
|
210
144
|
include Scope
|
211
|
-
include Includes, Order, Pagination
|
145
|
+
include Includes, Order, Pagination, Serialization
|
212
146
|
end
|
213
147
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActiveCollection
|
2
|
+
module MemberClass
|
3
|
+
def self.included(mod)
|
4
|
+
mod.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def model_class
|
9
|
+
@model_class ||= name.sub(/Collection$/,'').constantize
|
10
|
+
end
|
11
|
+
|
12
|
+
def table_name
|
13
|
+
model_class.table_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def human_name(*args)
|
17
|
+
model_class.human_name(*args).pluralize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def model_class
|
22
|
+
self.class.model_class
|
23
|
+
end
|
24
|
+
|
25
|
+
def table_name
|
26
|
+
self.class.table_name
|
27
|
+
end
|
28
|
+
|
29
|
+
def human_name(*args)
|
30
|
+
self.class.human_name(*args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -31,7 +31,13 @@ module ActiveCollection
|
|
31
31
|
# Loads total entries and calculates the size from that.
|
32
32
|
def size_with_pagination
|
33
33
|
if paginated?
|
34
|
-
|
34
|
+
if out_of_bounds?
|
35
|
+
0
|
36
|
+
elsif last_page?
|
37
|
+
size_without_pagination % per_page
|
38
|
+
else
|
39
|
+
per_page
|
40
|
+
end
|
35
41
|
else
|
36
42
|
size_without_pagination
|
37
43
|
end
|
@@ -92,7 +98,7 @@ module ActiveCollection
|
|
92
98
|
#
|
93
99
|
# may load total_entries if not already loaded.
|
94
100
|
def last_page?
|
95
|
-
next_page.nil?
|
101
|
+
!out_of_bounds? && next_page.nil?
|
96
102
|
end
|
97
103
|
|
98
104
|
# New Collection for current_page - 1 or nil.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveCollection
|
2
|
+
module Serialization
|
3
|
+
# Turn the params into a hash suitable such that passing the collection
|
4
|
+
# directly to a named path generates the path for the current collection.
|
5
|
+
def to_param
|
6
|
+
params.empty?? nil : params.to_param
|
7
|
+
end
|
8
|
+
|
9
|
+
def as_data_hash
|
10
|
+
data_hash = { table_name => collection.as_json }
|
11
|
+
data_hash["total_entries"] = total_entries
|
12
|
+
data_hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_xml(options = {})
|
16
|
+
collect
|
17
|
+
options[:indent] ||= 2
|
18
|
+
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
|
19
|
+
xml.instruct! unless options[:skip_instruct]
|
20
|
+
xml.collection do
|
21
|
+
xml.total_entries(total_entries, :type => "integer")
|
22
|
+
xml.tag!(table_name, :type => "array") do
|
23
|
+
collection.each do |item|
|
24
|
+
item.to_xml(:indent => options[:indent], :builder => xml, :skip_instruct => true)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def as_json(options = nil)
|
31
|
+
{"collection" => as_data_hash}.as_json(options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -4,11 +4,18 @@ class BeerCollection < ActiveCollection::Base
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Beer
|
7
|
+
def self.human_name(*args)
|
8
|
+
"Beer"
|
9
|
+
end
|
7
10
|
end
|
8
11
|
|
9
12
|
describe ActiveCollection do
|
10
13
|
subject { BeerCollection.new }
|
11
14
|
|
15
|
+
it "passes human_name to the member class and then pluralizes" do
|
16
|
+
subject.human_name(:locale => 'en-us').should == "Beers"
|
17
|
+
end
|
18
|
+
|
12
19
|
context "(empty)" do
|
13
20
|
describe "(count methods)" do
|
14
21
|
before do
|
data/spec/pagination_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Emde
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,9 +42,11 @@ files:
|
|
42
42
|
- lib/active_collection.rb
|
43
43
|
- lib/active_collection/base.rb
|
44
44
|
- lib/active_collection/includes.rb
|
45
|
+
- lib/active_collection/member_class.rb
|
45
46
|
- lib/active_collection/order.rb
|
46
47
|
- lib/active_collection/pagination.rb
|
47
48
|
- lib/active_collection/scope.rb
|
49
|
+
- lib/active_collection/serialization.rb
|
48
50
|
- spec/active_collection_spec.rb
|
49
51
|
- spec/pagination_spec.rb
|
50
52
|
- spec/spec.opts
|