activerecord-collections 0.0.2 → 0.0.3
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/collection.rb +43 -11
- data/lib/active_record/collections/batching.rb +9 -4
- data/lib/active_record/collections/model_collection.rb +18 -0
- data/lib/active_record/collections/records.rb +15 -13
- data/lib/active_record/collections/{query_chain.rb → relation.rb} +1 -1
- data/lib/active_record/collections/version.rb +1 -1
- data/lib/activerecord-collections.rb +2 -1
- data/spec/support/models.rb +0 -6
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48403614a6f714267ffc6a749d2318a94a246987
|
4
|
+
data.tar.gz: 6658db0230f63496689196fe2da3d98dc58d733e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2a5163e7d978ec2e4814b6d1e253436f30882487774370501a4f0ea43a9ccebcd6481e7fa5ceccd196d92a85e2e44605b50fecdbfb767d6a2df2a7790878f02
|
7
|
+
data.tar.gz: ba491d965bfd9e90dd454b76bd7d20bebf95e953fba635110111d73f58ca7866f9f20a69126b8b2c4be90f70a08c9032492631f5f0f40acde3e61144603b1d97
|
@@ -1,11 +1,38 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
class Collection
|
3
|
-
include ActiveRecord::Collections::
|
3
|
+
include ActiveRecord::Collections::Relation
|
4
4
|
include ActiveRecord::Collections::Records
|
5
5
|
include ActiveRecord::Collections::Batching
|
6
6
|
include ActiveRecord::Collections::Delegation
|
7
7
|
include ActiveRecord::Collections::Serialization
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :relation, :options
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def collectable(klass=nil)
|
12
|
+
unless klass.nil?
|
13
|
+
raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless klass.ancestors.include?(ActiveRecord::Base)
|
14
|
+
@collectable = klass
|
15
|
+
end
|
16
|
+
|
17
|
+
if @collectable.nil?
|
18
|
+
begin
|
19
|
+
klass = self.name.demodulize.singularize.constantize
|
20
|
+
@collectable = klass if !klass.nil? && klass.ancestors.include?(ActiveRecord::Base)
|
21
|
+
rescue
|
22
|
+
# singularized class doesn't exist
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if @collectable.nil? # TODO implement real exceptions
|
27
|
+
@collectable
|
28
|
+
end
|
29
|
+
alias_method :model, :collectable
|
30
|
+
end
|
31
|
+
|
32
|
+
def collectable
|
33
|
+
@collectable ||= self.class.collectable
|
34
|
+
end
|
35
|
+
alias_method :model, :collectable
|
9
36
|
|
10
37
|
# dup relation and call none so that we don't end up inspecting it
|
11
38
|
# and loading it before we want it
|
@@ -19,15 +46,20 @@ module ActiveRecord
|
|
19
46
|
|
20
47
|
protected
|
21
48
|
|
22
|
-
def initialize(
|
23
|
-
|
49
|
+
def initialize(*criteria)
|
50
|
+
if criteria.first.present? && criteria.first.ancestors.include?(ActiveRecord::Base)
|
51
|
+
@collectable = criteria.slice!(0)
|
52
|
+
end
|
53
|
+
|
54
|
+
plural_name = collectable.name.demodulize.pluralize.underscore
|
55
|
+
singular_name = collectable.name.demodulize.singularize.underscore
|
56
|
+
|
24
57
|
self.class.instance_eval do
|
25
|
-
|
26
|
-
|
27
|
-
alias_method
|
28
|
-
alias_method "#{model_singular}_ids".to_sym, :record_ids
|
29
|
-
alias_method "on_#{model_plural}".to_sym, :on_records
|
58
|
+
alias_method plural_name.to_sym, :records
|
59
|
+
alias_method "#{singular_name}_ids".to_sym, :record_ids
|
60
|
+
alias_method "on_#{plural_name}".to_sym, :on_records
|
30
61
|
end
|
62
|
+
|
31
63
|
@options = {} # defaults, not implemented yet
|
32
64
|
@options.merge!(criteria.extract_options!) if criteria.length > 1
|
33
65
|
|
@@ -36,10 +68,10 @@ module ActiveRecord
|
|
36
68
|
if criteria.is_a?(ActiveRecord::Relation)
|
37
69
|
@relation = criteria
|
38
70
|
elsif criteria.is_a?(Hash) || criteria.is_a?(String) || criteria.is_a?(Array)
|
39
|
-
@relation =
|
71
|
+
@relation = collectable.where(criteria).dup
|
40
72
|
end
|
41
73
|
else
|
42
|
-
@relation =
|
74
|
+
@relation = collectable.where(criteria).dup
|
43
75
|
end
|
44
76
|
end
|
45
77
|
|
@@ -44,15 +44,15 @@ module ActiveRecord
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def batch_by_default?
|
47
|
-
self.class.batch_by_default?
|
47
|
+
self.class.batch_by_default? ||
|
48
|
+
( batching_threshold > 0 &&
|
49
|
+
total_records >= batching_threshold )
|
48
50
|
end
|
49
51
|
|
50
52
|
def should_batch?(check_if_batched=true)
|
51
53
|
return false if is_batch?
|
52
54
|
return false if check_if_batched && batched?
|
53
|
-
batch_by_default?
|
54
|
-
( batching_threshold > 0 &&
|
55
|
-
total_records >= batching_threshold )
|
55
|
+
batch_by_default?
|
56
56
|
end
|
57
57
|
|
58
58
|
def is_batch!
|
@@ -97,6 +97,7 @@ module ActiveRecord
|
|
97
97
|
end
|
98
98
|
alias_method :in_batches, :as_batches
|
99
99
|
|
100
|
+
# TODO Mark need to either depend on kaminari or check for it before using page/per
|
100
101
|
def page(*num)
|
101
102
|
dup.page!(*num)
|
102
103
|
end
|
@@ -153,6 +154,8 @@ module ActiveRecord
|
|
153
154
|
alias_method :total_batches, :total_pages
|
154
155
|
|
155
156
|
def each_page(&block)
|
157
|
+
batch! if should_batch?
|
158
|
+
|
156
159
|
if total_pages <= 1
|
157
160
|
yield to_a if block_given?
|
158
161
|
return [to_a]
|
@@ -171,6 +174,8 @@ module ActiveRecord
|
|
171
174
|
alias_method :each_batch, :each_page
|
172
175
|
|
173
176
|
def page_map(&block)
|
177
|
+
batch! if should_batch?
|
178
|
+
|
174
179
|
if total_pages <= 1
|
175
180
|
return (block_given? ? yield(to_a) : to_a)
|
176
181
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Collections
|
3
|
+
module ModelCollection
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def collection(*criteria)
|
10
|
+
ActiveRecord::Collection.new(self, *criteria)
|
11
|
+
end
|
12
|
+
alias_method :collect, :collection
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Base.send :include, ActiveRecord::Collections::ModelCollection
|
@@ -23,7 +23,6 @@ module ActiveRecord
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def total_count
|
26
|
-
#batch! if try(:should_batch?)
|
27
26
|
total_records
|
28
27
|
end
|
29
28
|
alias_method :total, :total_count
|
@@ -38,28 +37,31 @@ module ActiveRecord
|
|
38
37
|
end
|
39
38
|
|
40
39
|
def each(&block)
|
41
|
-
|
40
|
+
records.each { |record| block_given? ? yield(record) : record }
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
records.each { |record| block_given? ? yield(record) : record }
|
47
|
-
end
|
43
|
+
def each_in_batches(batch_size=nil, &block)
|
44
|
+
batch_size!(batch_size)
|
45
|
+
flat_batch_map.each { |record| block_given? ? yield(record) : record }
|
48
46
|
end
|
49
47
|
|
50
48
|
def map(&block)
|
51
|
-
|
49
|
+
each.map { |record| block_given? ? yield(record) : record }
|
50
|
+
end
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
each.map { |record| block_given? ? yield(record) : record }
|
57
|
-
end
|
52
|
+
def map_in_batches(batch_size=nil, &block)
|
53
|
+
batch_size!(batch_size)
|
54
|
+
flat_batch_map.map { |record| block_given? ? yield(record) : record }
|
58
55
|
end
|
59
56
|
|
60
57
|
def flat_map(&block)
|
61
58
|
map(&block).flatten
|
62
59
|
end
|
60
|
+
|
61
|
+
def flat_map_in_batches(batch_size=nil, &block)
|
62
|
+
batch_size!(batch_size)
|
63
|
+
flat_batch_map.map { |record| block_given? ? yield(record) : record }
|
64
|
+
end
|
63
65
|
end
|
64
66
|
end
|
65
67
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_record/collections/batching'
|
2
2
|
require 'active_record/collections/delegation'
|
3
|
-
require 'active_record/collections/
|
3
|
+
require 'active_record/collections/relation'
|
4
4
|
require 'active_record/collections/records'
|
5
5
|
require 'active_record/collections/serialization'
|
6
6
|
require 'active_record/collection'
|
7
|
+
require 'active_record/collections/model_collection'
|
data/spec/support/models.rb
CHANGED
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.3
|
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-01-
|
11
|
+
date: 2015-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -106,8 +106,9 @@ files:
|
|
106
106
|
- lib/active_record/collection.rb
|
107
107
|
- lib/active_record/collections/batching.rb
|
108
108
|
- lib/active_record/collections/delegation.rb
|
109
|
-
- lib/active_record/collections/
|
109
|
+
- lib/active_record/collections/model_collection.rb
|
110
110
|
- lib/active_record/collections/records.rb
|
111
|
+
- lib/active_record/collections/relation.rb
|
111
112
|
- lib/active_record/collections/serialization.rb
|
112
113
|
- lib/active_record/collections/version.rb
|
113
114
|
- lib/activerecord-collections.rb
|