eagerbeaver 0.1.0 → 0.1.1

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: c558246984c62a7470ea996b956053150d58ec50
4
- data.tar.gz: 5d8a81bb64fed4f171dc327604b9c06253bd5323
3
+ metadata.gz: e1cae32a36d43b90bcf7fd44a1761c735124877d
4
+ data.tar.gz: 7d1af0d4a273f42c87b751d02442bcf48e412265
5
5
  SHA512:
6
- metadata.gz: 646386f43d5f2c9510121878dd95fc7c5b94221ee3337a0ef168d93d5e9e9150eae592bf01d61737a716da45adb622b4905ff4b20ff756bb9668b5ad78fcc4d6
7
- data.tar.gz: ad9f291388b5b039c0f25d94993e2f178b87a537cbbfea74e5633efdfc851bfb830b2d4abd5673217e65d84cd138cd9550cc5f8c9e09d0e7fadf6d4a174235bb
6
+ metadata.gz: 30314ca7cd502769c85e9faa79912c2c691393e201aaf9a51ad4d4c80699f288594b28d1b49fabec34d42408c6337cec2096567d7c4e380858a37b23c9e6f391
7
+ data.tar.gz: 6928cff69c9e2aa8f0d4cf6c8a7ff47e958e731c50d94aab56e2134a77e261a5a98db584e3f97f45e0e43d4ddbaf14c504fd11c698b37d827550ffe7469faefa
data/.gitignore CHANGED
@@ -10,3 +10,6 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ tags
15
+ *.gem
data/lib/eagerbeaver.rb CHANGED
@@ -3,7 +3,7 @@ require "eagerbeaver/version"
3
3
  class EagerBeaver
4
4
  def initialize(model, preloads)
5
5
  @model = model
6
- @preloads = Array(preloads)
6
+ @preloads = preloads
7
7
  end
8
8
 
9
9
  def errors
@@ -26,10 +26,31 @@ class EagerBeaver
26
26
  end
27
27
 
28
28
  def association_name(model_string, association_name_or_alias)
29
- association = model_string.constantize.reflect_on_all_associations.find { |a| a.name == association_name_or_alias }
30
- association.try(:source_reflection_name) ||
31
- association.try(:options).try(:[], :class_name) ||
32
- association_name_or_alias
29
+ association = match_association(model_string.to_s, association_name_or_alias)
30
+ return association_name_or_alias unless association
31
+
32
+ options = association.options
33
+
34
+ if options[:through] && options[:source]
35
+ source = options[:source_type] || options[:source]
36
+ through = association_name(model_string, options[:through]) ||
37
+ association_name(options[:through], source)
38
+ association_name(through, source)
39
+ else
40
+ options[:class_name] || association_name_or_alias
41
+ end
42
+ end
43
+
44
+ def match_association(model_string, association)
45
+ constantize(model_string).reflect_on_all_associations.find { |a| a.name == association }
46
+ end
47
+
48
+ def constantize(model_string)
49
+ if model_string.first == model_string.first.upcase
50
+ model_string.singularize.constantize
51
+ else
52
+ model_string.split('_').map(&:capitalize).join.singularize.constantize
53
+ end
33
54
  end
34
55
 
35
56
  attr_reader :model, :preloads
@@ -1,3 +1,3 @@
1
1
  module Eagerbeaver
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eagerbeaver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Park
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-01 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler