activerecord-collections 0.0.25 → 0.0.26

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90413291270816c9e336b6bd84f6186cf4cbf4d8
4
- data.tar.gz: 14211182826f221348fe208ab53309d14dcba7b4
3
+ metadata.gz: 402158ead8c85144c6a63d76b088b59751600cf3
4
+ data.tar.gz: c44bda53b2d6b4d1958d9cb25cce62ef852e7964
5
5
  SHA512:
6
- metadata.gz: accb3a04fb6d89895211cb10e9e8cf98e4d622091b2ec5e080dbee1356f710f467472ca39d2ccdfcaf9a98af7ee8c5fd1e23d3bcdca86b3b90c5f63d367c4d3d
7
- data.tar.gz: f80335585daaccd9d94b74b128d4de923f9b9af9466ea1aa6497fba397064107c0fa5dd9e8ef3e067377d3aab389e90bba08d937afb53be8ae02694720eb2689
6
+ metadata.gz: b1942ebc7a10b5d8b7534070f52fc72e9bc453531955de1e0c8e292595475408627ba34a6b54f670f279dd7932ce5466bad11fe00e591a613771fbd44607ac67
7
+ data.tar.gz: d9aacdb70cdc964d569cd968d862c6b3a669f5c258df054e361e5697180ba5261eb2a27f5e9c4dc0f8dccc85565a654408ab286d16ca22d6dc036eaa1c72dc5d
@@ -14,47 +14,51 @@ module ActiveRecord
14
14
  class << self
15
15
  def inherited(subclass)
16
16
  ActiveRecord::Collection::COLLECTIONS << subclass.name unless ActiveRecord::Collection::COLLECTIONS.include?(subclass.name)
17
- # if parent class is not Collection, register the collectable on the class as the closest parent's collectable
17
+ subclass.collectable rescue nil #noop
18
18
  end
19
19
 
20
20
  def collections
21
21
  ActiveRecord::Collection::COLLECTIONS.map(&:constantize)
22
22
  end
23
23
 
24
- def collectable(klass=nil)
25
- unless klass.nil?
26
- raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless klass.ancestors.include?(ActiveRecord::Base)
27
- ActiveRecord::Collection::COLLECTABLES[name] ||= klass.name
28
- end
24
+ def kollektable
25
+ ActiveRecord::Collection::COLLECTABLES[name]
26
+ end
29
27
 
30
- if ActiveRecord::Collection::COLLECTABLES[name].nil?
31
- klass = infer_collectable
32
- ActiveRecord::Collection::COLLECTABLES[name] = klass.name if !klass.nil? && klass.ancestors.include?(ActiveRecord::Base)
33
- end
28
+ def collectable=(klass)
29
+ raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless !klass.nil? && klass < ActiveRecord::Base
30
+ ActiveRecord::Collection::COLLECTABLES[name] = klass.name
31
+ end
34
32
 
35
- raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if ActiveRecord::Collection::COLLECTABLES[name].nil? # TODO implement real exceptions
33
+ def collectable(klass=nil)
34
+ if klass.nil?
35
+ self.collectable = infer_collectable if kollektable.nil?
36
+ raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if kollektable.nil?
37
+ else
38
+ self.collectable = klass
39
+ end
36
40
 
37
- ActiveRecord::Collection::COLLECTABLES[name].constantize
41
+ kollektable.constantize
38
42
  end
39
43
  alias_method :model, :collectable
40
44
 
41
- def infer_collectable(klass=self)
42
- singular = klass.name.demodulize.singularize
43
- raise "Cannot infer collectable from singular collection" if singular == klass.name.demodulize
45
+ def infer_collectable
46
+ singular = name.demodulize.singularize
47
+ raise "Cannot infer collectable name from collection name" if singular == name.demodulize
44
48
  singular.constantize
45
49
  rescue
46
- parent = klass.ancestors[1]
47
- return nil if parent.name == 'ActiveRecord::Collection'
48
- if ActiveRecord::Collection::COLLECTABLES.has_key?(parent.name)
49
- ActiveRecord::Collection::COLLECTABLES[parent.name].constantize
50
- else
51
- infer_collectable(parent)
50
+ parent = ancestors[1]
51
+ return nil unless parent < ActiveRecord::Collection
52
+ begin
53
+ parent.collectable
54
+ rescue
55
+ nil
52
56
  end
53
57
  end
54
58
  end
55
59
 
56
60
  def collectable
57
- @collectable ||= self.class.collectable
61
+ @collectable# ||= self.class.collectable
58
62
  end
59
63
  alias_method :model, :collectable
60
64
 
@@ -75,6 +79,8 @@ module ActiveRecord
75
79
 
76
80
  if criteria.first.present? && criteria.first.respond_to?(:ancestors) && criteria.first < ActiveRecord::Base
77
81
  @collectable = criteria.slice!(0)
82
+ else
83
+ @collectable = self.class.collectable
78
84
  end
79
85
 
80
86
  plural_name = collectable.name.demodulize.pluralize.underscore
@@ -102,7 +108,7 @@ module ActiveRecord
102
108
  end
103
109
 
104
110
  def initialize_copy(old)
105
- @collectable = old.collectable
111
+ @collectable = old.collectable.name.constantize
106
112
  @options = old.options.dup
107
113
  @records = @relation = old.relation.dup
108
114
  @total_count = old.instance_variable_get(:@total_count)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Collections
3
- VERSION = '0.0.25'
3
+ VERSION = '0.0.26'
4
4
  end
5
5
  end
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.25
4
+ version: 0.0.26
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-04-27 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord