activerecord-collections 0.0.13 → 0.0.14
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.
- checksums.yaml +4 -4
- data/lib/active_record/collections/batching.rb +4 -4
- data/lib/active_record/collections/collectable.rb +7 -14
- data/lib/active_record/collections/serialization.rb +22 -22
- data/lib/active_record/collections/serializer.rb +141 -0
- data/lib/active_record/collections/version.rb +1 -1
- data/lib/activerecord-collections.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4234629f1bf42416c64069012d7957116937bab
|
4
|
+
data.tar.gz: 2087c0d1a4abe5e4820c112430e52fe7ec3c7438
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10b6c0db6816071a0aca2184fc442af40f62b4b796595b951d4c739b48e844c419f9b184fbd6670131a89ae39a6c7f21b1c90ee1514b63ab617ddf5d1d0378f1
|
7
|
+
data.tar.gz: 8fed417c0a2d7f4f889d254c0f7eba27c1c768e83ded7e5bd1e35a6d39838c719ccb90991a9b174a225d0bd9361442fdd99a2cb1cbae1c14b1c45b11bed8c85c
|
@@ -170,7 +170,7 @@ module ActiveRecord
|
|
170
170
|
end
|
171
171
|
|
172
172
|
def first_batch
|
173
|
-
dup.first_batch!
|
173
|
+
dup.first_batch!.is_batch!
|
174
174
|
end
|
175
175
|
|
176
176
|
def first_batch!
|
@@ -182,7 +182,7 @@ module ActiveRecord
|
|
182
182
|
end
|
183
183
|
|
184
184
|
def next_batch
|
185
|
-
dup.next_batch!
|
185
|
+
dup.next_batch!.is_batch!
|
186
186
|
end
|
187
187
|
|
188
188
|
def next_batch!
|
@@ -194,7 +194,7 @@ module ActiveRecord
|
|
194
194
|
end
|
195
195
|
|
196
196
|
def prev_batch
|
197
|
-
dup.prev_batch!
|
197
|
+
dup.prev_batch!.is_batch!
|
198
198
|
end
|
199
199
|
|
200
200
|
def prev_batch!
|
@@ -202,7 +202,7 @@ module ActiveRecord
|
|
202
202
|
end
|
203
203
|
|
204
204
|
def last_batch
|
205
|
-
dup.last_batch!
|
205
|
+
dup.last_batch!.is_batch!
|
206
206
|
end
|
207
207
|
|
208
208
|
def last_batch!
|
@@ -30,22 +30,15 @@ module ActiveRecord
|
|
30
30
|
klass.kollektion
|
31
31
|
end
|
32
32
|
|
33
|
+
def values_hash
|
34
|
+
ActiveRecord::Collections::Serializer.to_hash(values.merge({collectable: klass}))
|
35
|
+
end
|
36
|
+
alias_method :to_values_hash, :values_hash
|
37
|
+
alias_method :to_hash, :values_hash
|
38
|
+
|
33
39
|
def collection
|
34
40
|
# do this with a hash so that we don't cause the relation query to execute
|
35
|
-
kollektion.from_hash(
|
36
|
-
collectable: klass,
|
37
|
-
select: select_values,
|
38
|
-
distinct: distinct_value,
|
39
|
-
joins: joins_values,
|
40
|
-
references: references_values,
|
41
|
-
includes: includes_values,
|
42
|
-
where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
|
43
|
-
group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
|
44
|
-
order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
|
45
|
-
bind: bind_values.map { |b| {name: b.first.name, value: b.last} },
|
46
|
-
limit: limit_value,
|
47
|
-
offset: offset_value
|
48
|
-
})
|
41
|
+
kollektion.from_hash(values_hash)
|
49
42
|
end
|
50
43
|
alias_method :to_collection, :collection
|
51
44
|
end
|
@@ -18,20 +18,27 @@ module ActiveRecord
|
|
18
18
|
collection = kollektion.new(kollektable)
|
19
19
|
collection.select!(*hash[:select]) unless hash[:select].empty?
|
20
20
|
collection.distinct! if hash[:distinct] == true
|
21
|
+
|
21
22
|
collection.joins!(*hash[:joins]) unless hash[:joins].empty?
|
22
23
|
collection.references!(*hash[:references]) unless hash[:references].empty?
|
23
24
|
collection.includes!(*hash[:includes]) unless hash[:includes].empty?
|
24
|
-
|
25
|
+
|
26
|
+
wheres = hash[:where].partition { |w| w.is_a?(Hash) }
|
27
|
+
wheres.first.each { |wh| collection.where!(wh) }
|
28
|
+
collection.where!(*hash[:bind].map { |b| b[:value] }.unshift(wheres.last.join(" AND ").gsub(/\$\d/,'?'))) unless wheres.last.empty?
|
29
|
+
|
25
30
|
collection.group!(hash[:group]) unless hash[:group].empty?
|
26
31
|
collection.order!(hash[:order]) unless hash[:order].empty?
|
32
|
+
|
27
33
|
collection.limit!(hash[:limit]) unless hash[:limit].nil?
|
28
34
|
collection.offset!(hash[:offset]) unless hash[:offset].nil?
|
35
|
+
|
29
36
|
collection
|
30
37
|
end
|
31
38
|
|
32
39
|
def kollektion_from_hash(hash)
|
33
40
|
kollektion = self
|
34
|
-
kollektion = hash[:collection] if hash.has_key?(:collection)
|
41
|
+
kollektion = hash[:collection] if hash.has_key?(:collection) && !hash[:collection].nil?
|
35
42
|
kollektion = kollektion.constantize unless kollektion.is_a?(Class)
|
36
43
|
raise "Invalid collection class: #{kollektion}" unless kollektion <= ActiveRecord::Collection
|
37
44
|
kollektion
|
@@ -40,7 +47,7 @@ module ActiveRecord
|
|
40
47
|
def kollektable_from_hash(hash)
|
41
48
|
kollektable = nil
|
42
49
|
kollektable = hash[:collectable] if hash.has_key?(:collectable)
|
43
|
-
kollektable = kollektable.constantize unless kollektable.is_a?(Class)
|
50
|
+
kollektable = kollektable.constantize unless kollektable.is_a?(Class) || kollektable.nil?
|
44
51
|
raise "Invalid collectable model: #{kollektable}" unless kollektable < ActiveRecord::Base
|
45
52
|
kollektable
|
46
53
|
end
|
@@ -51,29 +58,22 @@ module ActiveRecord
|
|
51
58
|
end
|
52
59
|
|
53
60
|
def to_hash(include_limit=false)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
joins: joins_values,
|
59
|
-
references: references_values,
|
60
|
-
includes: includes_values,
|
61
|
-
where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
|
62
|
-
group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
|
63
|
-
order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
|
64
|
-
bind: bind_values.map { |b| {name: b.first.name, value: b.last} }
|
65
|
-
}
|
66
|
-
h[:collection] = self.class if self.class < ActiveRecord::Collection
|
67
|
-
if include_limit || try(:is_batch?)
|
68
|
-
h[:limit] = limit_value
|
69
|
-
h[:offset] = offset_value
|
70
|
-
end
|
71
|
-
h
|
61
|
+
values = relation.values.merge({collectable: collectable})
|
62
|
+
values.merge!({limit: nil, offset: nil}) if !include_limit && !try(:is_batch?)
|
63
|
+
values[:collection] = self.class if self.class < ActiveRecord::Collection
|
64
|
+
ActiveRecord::Collections::Serializer.to_hash(values)
|
72
65
|
end
|
73
66
|
alias_method :to_h, :to_hash
|
74
67
|
|
68
|
+
def as_json(options=nil)
|
69
|
+
h = to_hash
|
70
|
+
h[:collectable] = h[:collectable].try(:name)
|
71
|
+
h[:collection] = h[:collection].name if h.has_key?(:collection)
|
72
|
+
h.as_json(options)
|
73
|
+
end
|
74
|
+
|
75
75
|
def to_json(options=nil)
|
76
|
-
|
76
|
+
as_json.to_json(options)
|
77
77
|
end
|
78
78
|
|
79
79
|
def to_param
|
@@ -0,0 +1,141 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Collections
|
3
|
+
class Serializer
|
4
|
+
attr_reader :values
|
5
|
+
|
6
|
+
def self.to_hash(*args)
|
7
|
+
new(*args).to_hash
|
8
|
+
end
|
9
|
+
|
10
|
+
def collectable
|
11
|
+
values[:collectable]
|
12
|
+
end
|
13
|
+
|
14
|
+
def collection
|
15
|
+
values[:collection]
|
16
|
+
end
|
17
|
+
|
18
|
+
def select
|
19
|
+
values[:select]
|
20
|
+
end
|
21
|
+
|
22
|
+
def distinct
|
23
|
+
values[:distinct]
|
24
|
+
end
|
25
|
+
|
26
|
+
def joins
|
27
|
+
values[:joins]
|
28
|
+
end
|
29
|
+
|
30
|
+
def includes
|
31
|
+
values[:includes]
|
32
|
+
end
|
33
|
+
|
34
|
+
def references
|
35
|
+
values[:references]
|
36
|
+
end
|
37
|
+
|
38
|
+
def group
|
39
|
+
values[:group].map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql }
|
40
|
+
end
|
41
|
+
|
42
|
+
def order
|
43
|
+
values[:order].map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql }
|
44
|
+
end
|
45
|
+
|
46
|
+
def limit
|
47
|
+
values[:limit]
|
48
|
+
end
|
49
|
+
|
50
|
+
def offset
|
51
|
+
values[:offset]
|
52
|
+
end
|
53
|
+
|
54
|
+
def bind
|
55
|
+
@bind ||= values[:bind].map { |b| {name: b.first.name.to_s, value: b.last} }
|
56
|
+
end
|
57
|
+
|
58
|
+
def where
|
59
|
+
return @where unless @where.nil?
|
60
|
+
|
61
|
+
@where = values[:where].map { |node| serialize_node(node) }
|
62
|
+
hashed = {}
|
63
|
+
@where.select! do |w|
|
64
|
+
if w.is_a?(Hash)
|
65
|
+
if hashed.has_key?(w.keys.first)
|
66
|
+
hashed[w.keys.first].merge!(w.values.first)
|
67
|
+
else
|
68
|
+
hashed[w.keys.first] = w.values.first
|
69
|
+
end
|
70
|
+
false
|
71
|
+
else
|
72
|
+
true
|
73
|
+
end
|
74
|
+
end.unshift(hashed)
|
75
|
+
@where
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_hash
|
79
|
+
{
|
80
|
+
collectable: collectable,
|
81
|
+
collection: collection,
|
82
|
+
select: select,
|
83
|
+
distinct: distinct,
|
84
|
+
joins: joins,
|
85
|
+
includes: includes,
|
86
|
+
references: references,
|
87
|
+
where: where,
|
88
|
+
bind: bind,
|
89
|
+
group: group,
|
90
|
+
order: order,
|
91
|
+
limit: limit,
|
92
|
+
offset: offset
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
def initialize(*args)
|
99
|
+
@values = {
|
100
|
+
collectable: nil,
|
101
|
+
collection: nil,
|
102
|
+
where: [],
|
103
|
+
bind: [],
|
104
|
+
select: [],
|
105
|
+
distinct: nil,
|
106
|
+
group: [],
|
107
|
+
order: [],
|
108
|
+
joins: [],
|
109
|
+
includes: [],
|
110
|
+
references: [],
|
111
|
+
limit: nil,
|
112
|
+
offset: nil
|
113
|
+
}.merge(args.extract_options!)
|
114
|
+
end
|
115
|
+
|
116
|
+
def serialize_node(node)
|
117
|
+
if node.is_a?(String)
|
118
|
+
node
|
119
|
+
elsif node.class < Arel::Nodes::Node
|
120
|
+
case node
|
121
|
+
when Arel::Nodes::Grouping
|
122
|
+
serialize_node(node.expr)
|
123
|
+
when Arel::Nodes::And
|
124
|
+
node.children.map { |child| serialize_node(child) }
|
125
|
+
when Arel::Nodes::Or
|
126
|
+
{or: [serialize_node(node.left), serialize_node(node.right)]}
|
127
|
+
else
|
128
|
+
if node.right.is_a?(Arel::Nodes::BindParam)
|
129
|
+
bound = bind.delete_at(bind.find_index { |b| b[:name] == node.left.name.to_s })[:value]
|
130
|
+
elsif node.right.is_a?(Arel::Nodes::Casted)
|
131
|
+
bound = node.right.val
|
132
|
+
else
|
133
|
+
raise "ActiveRecord::Collection does not know how to serialize this attribute: #{node.left.name} / #{node.right.class.name}"
|
134
|
+
end
|
135
|
+
{node.left.relation.name.to_s => {node.left.name.to_s => bound}}
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -3,6 +3,7 @@ require 'active_record/collections/delegation'
|
|
3
3
|
require 'active_record/collections/pagination'
|
4
4
|
require 'active_record/collections/records'
|
5
5
|
require 'active_record/collections/relation'
|
6
|
+
require 'active_record/collections/serializer'
|
6
7
|
require 'active_record/collections/serialization'
|
7
8
|
require 'active_record/collection'
|
8
9
|
require 'active_record/collections/collectable'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-collections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/active_record/collections/records.rb
|
112
112
|
- lib/active_record/collections/relation.rb
|
113
113
|
- lib/active_record/collections/serialization.rb
|
114
|
+
- lib/active_record/collections/serializer.rb
|
114
115
|
- lib/active_record/collections/version.rb
|
115
116
|
- lib/activerecord-collections.rb
|
116
117
|
- spec/active_record/collection_spec.rb
|