deep_pluck 0.0.4 → 0.1.0

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: f04ba29f0ba05add69bb0765e11b4c72541cb7a5
4
- data.tar.gz: f5fe6993d0004478facd381dcbb851aeebe92d9d
3
+ metadata.gz: b20668dbd7b6878ed071b71534eb5c73facbd2a5
4
+ data.tar.gz: d0717078533f829893fd2e22f3331d00b22003c2
5
5
  SHA512:
6
- metadata.gz: 801eb82d2d1340ab93f60006a6be0d44588afd00c4354ec309af5c3714bc3aba9cca4074e2d6072d4e9a508a0bd6bd264d66e5a01a12700589a42f177d247a89
7
- data.tar.gz: 8e416508a72b631d70112e3f6ef527a23930dd1f0b9db332aec12f431e2e1bb26e690bc0cf8aaf094f7d734bcf60aa525656d39bf52030aa29cf12cbbb68d36d
6
+ metadata.gz: 72273fc908114709b56b034faf6278684063d5787a84559cc7fe5353ce913fca112c6f4efc315156154a99a43518e301b87f40bea294f99aa87d5b1116d64d3d
7
+ data.tar.gz: b2c45401285b2f407194506dd26be1aa9327ace23bf2e16b7acef734391d34533177e897fd599eb2e5a93115e32ebff15b6f2bd67e0774206073a7700c9cc4fc
@@ -13,13 +13,15 @@ module DeepPluck
13
13
  #---------------------------------------
14
14
  # Reader
15
15
  #---------------------------------------
16
- def reflect_on_association(association_key)
16
+ def get_reflect(association_key)
17
17
  @relation.klass.reflect_on_association(association_key.to_sym) #add to_sym since rails 3 only support symbol
18
18
  end
19
- def get_foreign_key(association_key, reverse = false)
20
- reflect = reflect_on_association(association_key)
21
- return (reflect.belongs_to? ? @relation.klass.primary_key : reflect.foreign_key) if reverse
22
- return (reflect.belongs_to? ? reflect.foreign_key : @relation.klass.primary_key)
19
+ def get_foreign_key(reflect, reverse = false)
20
+ if reflect.options[:through] and reverse #reverse = parent
21
+ return "#{reflect.options[:through]}.#{reflect.chain.last.foreign_key}"
22
+ end
23
+ return (reflect.belongs_to? ? reflect.active_record.primary_key : reflect.foreign_key) if reverse
24
+ return (reflect.belongs_to? ? reflect.foreign_key : reflect.active_record.primary_key)
23
25
  end
24
26
  #---------------------------------------
25
27
  # Contruction OPs
@@ -30,7 +32,7 @@ module DeepPluck
30
32
  end
31
33
  def add_association(hash)
32
34
  hash.each do |key, value|
33
- model = (@associations[key] ||= Model.new(reflect_on_association(key).klass.where(''), key, self))
35
+ model = (@associations[key] ||= Model.new(get_reflect(key).klass.where(''), key, self))
34
36
  model.add(value)
35
37
  end
36
38
  end
@@ -50,10 +52,20 @@ module DeepPluck
50
52
  # Load
51
53
  #---------------------------------------
52
54
  private
53
- def set_includes_data(parent, children_store_name, model, order_by = nil)
54
- reflect = reflect_on_association(children_store_name)
55
+ def do_query(parent, reflect, relation)
56
+ relation = relation.joins(reflect.options[:through]) if reflect.options[:through]
57
+ parent_key = get_foreign_key(reflect, false)
58
+ relation_key = get_foreign_key(reflect, true)
59
+ ids = parent.map{|s| s[parent_key]}
60
+ ids.uniq!
61
+ ids.compact!
62
+ return relation.where(relation_key => ids)
63
+ end
64
+ private
65
+ def set_includes_data(parent, children_store_name, model)
66
+ reflect = get_reflect(children_store_name)
55
67
  if reflect.belongs_to? #Child.where(:id => parent.pluck(:child_id))
56
- children = model.load_data{|relaction| relaction.where(:id => parent.map{|s| s[reflect.foreign_key]}.uniq.compact).order(order_by) }
68
+ children = model.load_data{|relation| do_query(parent, reflect, relation) }
57
69
  children_hash = Hash[children.map{|s| [s["id"], s]}]
58
70
  parent.each{|s|
59
71
  next if (id = s[reflect.foreign_key]) == nil
@@ -62,9 +74,10 @@ module DeepPluck
62
74
  else #Child.where(:parent_id => parent.pluck(:id))
63
75
  parent.each{|s| s[children_store_name] = [] } if reflect.collection?
64
76
  parent_hash = Hash[parent.map{|s| [s["id"], s]}]
65
- children = model.load_data{|relaction| relaction.where(reflect.foreign_key => parent.map{|s| s["id"]}.uniq.compact).order(order_by) }
77
+ children = model.load_data{|relation| do_query(parent, reflect, relation) }
78
+ foreign_key = get_foreign_key(reflect, true).sub(/\w+\./, '') #user_achievements.user_id => user_id
66
79
  children.each{|s|
67
- next if (id = s[reflect.foreign_key]) == nil
80
+ next if (id = s[foreign_key]) == nil
68
81
  if reflect.collection?
69
82
  parent_hash[id][children_store_name] << s
70
83
  else
@@ -76,13 +89,14 @@ module DeepPluck
76
89
  end
77
90
  public
78
91
  def load_data
79
- prev_need_columns = @parent_model.get_foreign_key(@parent_association_key, true) if @parent_model
80
- next_need_columns = @associations.map{|key, _| get_foreign_key(key) }.uniq
92
+ prev_need_columns = @parent_model.get_foreign_key(@parent_model.get_reflect(@parent_association_key), true) if @parent_model
93
+ next_need_columns = @associations.map{|key, _| get_foreign_key(get_reflect(key)) }.uniq
81
94
  all_need_columns = [*prev_need_columns, *next_need_columns, *@need_columns].uniq
82
95
  @relation = yield(@relation) if block_given?
83
96
  @data = @relation.pluck_all(*all_need_columns)
84
97
  if @data.size != 0
85
98
  @extra_columns = all_need_columns - @need_columns #for delete_extra_column_data!
99
+ @extra_columns.map!{|s| s.sub(/\w+\./, '')} #user_achievements.user_id => user_id
86
100
  @associations.each do |key, model|
87
101
  set_includes_data(@data, key, model)
88
102
  end
@@ -1,3 +1,3 @@
1
1
  module DeepPluck
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_pluck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-14 00:00:00.000000000 Z
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler