activerecord-collections 0.0.12 → 0.0.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e584a84c4e5c74033fe3c722e3ac2ac0637c0c6
4
- data.tar.gz: 9ab963d3b8e4a1243dcf98b980f8d03ed4b1360f
3
+ metadata.gz: 04112ca97ff8955d8c031f7f5a2f2a485d542d1e
4
+ data.tar.gz: 9a7cfa08962b824a6eeba91be93b5aff5a23a37e
5
5
  SHA512:
6
- metadata.gz: dc7da22fd53664c88a8f951752e602603f1f33b5458be045d47d5d3688ca32994493b6c22c23d0fa03ae31fdd4c662016d988b3aede109325469b5ee32986fc4
7
- data.tar.gz: 7e41289a5a3c5decb18fa639d91f0c52afc238129216ae480bc0b8ea6f3039ad985a9caa4dc969d340eda073c4dbdd1649086f4cc44261c174f5efdc840cbfb3
6
+ metadata.gz: af95c3591f0e8829550b4041282b713ef97532ccdfd79ee068467da0b8de6741ffe445792f4109c338fe43108810811db02a3105246bac65754d8e708ceb37fe
7
+ data.tar.gz: bc5472e5748c8eb9444b5558d756443984dffc3b966b4d03e63e69e499ecb7c9bfb7c7418084dfc53dc1729c5abe8b860e0212f8de7b67729aada847fc938b35
@@ -53,7 +53,9 @@ module ActiveRecord
53
53
  protected
54
54
 
55
55
  def initialize(*criteria)
56
- if criteria.first.present? && criteria.first.respond_to?(:ancestors) && criteria.first.ancestors.include?(ActiveRecord::Base)
56
+ criteria.compact!
57
+
58
+ if criteria.first.present? && criteria.first.respond_to?(:ancestors) && criteria.first < ActiveRecord::Base
57
59
  @collectable = criteria.slice!(0)
58
60
  end
59
61
 
@@ -6,6 +6,7 @@ module ActiveRecord
6
6
  @collection_class = klass unless klass.nil?
7
7
  @collection_class
8
8
  end
9
+ alias_method :collector, :collection_class
9
10
 
10
11
  def kollektion
11
12
  plural_klass = begin
@@ -32,18 +33,18 @@ module ActiveRecord
32
33
  def collection
33
34
  # do this with a hash so that we don't cause the relation query to execute
34
35
  kollektion.from_hash({
35
- klass: klass,
36
- select: select_values,
37
- distinct: distinct_value,
38
- joins: joins_values,
39
- references: references_values,
40
- includes: includes_values,
41
- where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
42
- group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
43
- order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
44
- bind: bind_values.map { |b| {name: b.first.name, value: b.last} },
45
- limit: limit_value,
46
- offset: offset_value
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
47
48
  })
48
49
  end
49
50
  alias_method :to_collection, :collection
@@ -12,7 +12,10 @@ module ActiveRecord
12
12
 
13
13
  def from_hash(hash)
14
14
  hash.symbolize_keys! unless hash.is_a?(HashWithIndifferentAccess)
15
- collection = hash.has_key?(:klass) ? new(hash[:klass]) : new
15
+
16
+ kollektion = kollektion_from_hash(hash)
17
+ kollektable = kollektable_from_hash(hash)
18
+ collection = kollektion.new(kollektable)
16
19
  collection.select!(*hash[:select]) unless hash[:select].empty?
17
20
  collection.distinct! if hash[:distinct] == true
18
21
  collection.joins!(*hash[:joins]) unless hash[:joins].empty?
@@ -25,6 +28,22 @@ module ActiveRecord
25
28
  collection.offset!(hash[:offset]) unless hash[:offset].nil?
26
29
  collection
27
30
  end
31
+
32
+ def kollektion_from_hash(hash)
33
+ kollektion = self
34
+ kollektion = hash[:collection] if hash.has_key?(:collection)
35
+ kollektion = kollektion.constantize unless kollektion.is_a?(Class)
36
+ raise "Invalid collection class: #{kollektion}" unless kollektion <= ActiveRecord::Collection
37
+ kollektion
38
+ end
39
+
40
+ def kollektable_from_hash(hash)
41
+ kollektable = nil
42
+ kollektable = hash[:collectable] if hash.has_key?(:collectable)
43
+ kollektable = kollektable.constantize unless kollektable.is_a?(Class)
44
+ raise "Invalid collectable model: #{kollektable}" unless kollektable < ActiveRecord::Base
45
+ kollektable
46
+ end
28
47
  end
29
48
 
30
49
  def to_sql
@@ -33,16 +52,18 @@ module ActiveRecord
33
52
 
34
53
  def to_hash(include_limit=false)
35
54
  h = {
36
- select: select_values,
37
- distinct: distinct_value,
38
- joins: joins_values,
39
- references: references_values,
40
- includes: includes_values,
41
- where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
42
- group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
43
- order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
44
- bind: bind_values.map { |b| {name: b.first.name, value: b.last} }
55
+ collectable: collectable,
56
+ select: select_values,
57
+ distinct: distinct_value,
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} }
45
65
  }
66
+ h[:collection] = self.class if self.class < ActiveRecord::Collection
46
67
  if include_limit || try(:is_batch?)
47
68
  h[:limit] = limit_value
48
69
  h[:offset] = offset_value
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Collections
3
- VERSION = '0.0.12'
3
+ VERSION = '0.0.13'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-collections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec