deep_pluck 0.1.2 → 0.1.3

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: 51c4e3a56a22aaf35ded1d2614e87d76f5b52283
4
- data.tar.gz: 5638b67af223f0c2c34009abdcfa7d7fe75c1186
3
+ metadata.gz: b2fc03be65ab872d226258b31d8ae589f3fbcd5a
4
+ data.tar.gz: e9509aff8cb651ac9f5cdc6589df4a392c1da44e
5
5
  SHA512:
6
- metadata.gz: 76a4685fbe45190c0da0c75175dcc0769da3a43007be9791bf78f06208cf701534f997f946b72f095916cda4e8971bf1583de2758166a6ded366849d2c1dd814
7
- data.tar.gz: 57c237f957442f8b04c915808f1f3e58491000e78ec0aae54a312dd8c2d9f051886269fcc6e323dfaa942c42b5cb8c12afe88a447405d4ba483f90e12fd99b2b
6
+ metadata.gz: 23ac49792df4c092022648f7a7ae8bc1ca90f27f6bb743b29ed5f7648c1048ff20d41f6697e1bda4c12f87f59e5b122a515c409c3bade3d233f3a32fa2c4e156
7
+ data.tar.gz: 4a470d307b03034676d5b16fdd2da2a13c52258e1403c6313e156ef58201f72e2f705679c35d8a98fe9ab711d9ad4273adf7c2e9b2a3767049460755fd6e28b5
data/README.md CHANGED
@@ -6,7 +6,9 @@
6
6
  [![Code Climate](https://codeclimate.com/github/khiav223577/deep_pluck/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/deep_pluck)
7
7
  [![Test Coverage](https://codeclimate.com/github/khiav223577/deep_pluck/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/deep_pluck/coverage)
8
8
 
9
- Use deep_pluck as a shortcut to select one or more attributes and include associated models without loading a bunch of records. And DRY up your code when using #as_json.
9
+ Allow you to pluck deeply into nested associations without loading a bunch of records.
10
+
11
+ And DRY up your code when using #as_json.
10
12
 
11
13
 
12
14
  ## Installation
@@ -36,12 +38,12 @@ User.deep_pluck(:id, :name)
36
38
 
37
39
  ### Pluck deep into associations
38
40
  ```rb
39
- User.deep_pluck(:name, 'posts' => :title)
41
+ User.deep_pluck(:name, :posts => :title)
40
42
  # SELECT `users`.`id`, `users`.`name` FROM `users`
41
43
  # SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
42
44
  # => [
43
- # {'name' => 'David' , 'posts' => [{'title' => 'post1'}, {'title' => 'post2'}]},
44
- # {'name' => 'Jeremy', 'posts' => [{'title' => 'post3'}]}
45
+ # {'name' => 'David' , :posts => [{'title' => 'post1'}, {'title' => 'post2'}]},
46
+ # {'name' => 'Jeremy', :posts => [{'title' => 'post3'}]}
45
47
  # ]
46
48
  ```
47
49
 
@@ -17,17 +17,23 @@ module DeepPluck
17
17
  @relation.klass.reflect_on_association(association_key.to_sym) || #add to_sym since rails 3 only support symbol
18
18
  raise(ActiveRecord::ConfigurationError, "ActiveRecord::ConfigurationError: Association named '#{association_key}' was not found on #{@relation.klass.name}; perhaps you misspelled it?")
19
19
  end
20
+ def get_join_table(reflect, bool_flag = false)
21
+ return reflect.options[:through] if reflect.options[:through]
22
+ return (reflect.options[:join_table] || reflect.send(:derive_join_table)) if reflect.macro == :has_and_belongs_to_many
23
+ return
24
+ end
25
+ def get_primary_key(reflect)
26
+ return (reflect.belongs_to? ? reflect.klass : reflect.active_record).primary_key
27
+ end
20
28
  def get_foreign_key(reflect, reverse: false, with_table_name: false)
21
- if reflect.options[:through] and reverse #reverse = parent
22
- chain_reflect = reflect.chain.last
23
- table_name = chain_reflect.table_name
24
- key = chain_reflect.foreign_key
29
+ if reverse and (table_name = get_join_table(reflect)) #reverse = parent
30
+ key = reflect.chain.last.foreign_key
25
31
  else
26
- return (reflect.belongs_to? ? reflect.active_record.primary_key : reflect.foreign_key) if reverse
32
+ return (reflect.belongs_to? ? get_primary_key(reflect) : reflect.foreign_key).to_s if reverse
27
33
  table_name = reflect.active_record.table_name
28
- key = (reflect.belongs_to? ? reflect.foreign_key : reflect.active_record.primary_key)
34
+ key = (reflect.belongs_to? ? reflect.foreign_key : get_primary_key(reflect))
29
35
  end
30
- return key if !with_table_name
36
+ return key.to_s if !with_table_name #key may be symbol if specify foreign_key in association options
31
37
  return "#{table_name}.#{key}"
32
38
  end
33
39
  #---------------------------------------
@@ -60,30 +66,31 @@ module DeepPluck
60
66
  #---------------------------------------
61
67
  private
62
68
  def do_query(parent, reflect, relation)
63
- relation = relation.joins(reflect.options[:through]) if reflect.options[:through]
64
69
  parent_key = get_foreign_key(reflect)
65
70
  relation_key = get_foreign_key(reflect, reverse: true, with_table_name: true)
66
71
  ids = parent.map{|s| s[parent_key]}
67
72
  ids.uniq!
68
73
  ids.compact!
69
- return relation.where(relation_key => ids)
74
+ return relation.joins(get_join_table(reflect)).where(relation_key => ids)
70
75
  end
71
76
  private
72
77
  def set_includes_data(parent, children_store_name, model)
73
78
  reflect = get_reflect(children_store_name)
79
+ primary_key = get_primary_key(reflect)
74
80
  if reflect.belongs_to? #Child.where(:id => parent.pluck(:child_id))
75
81
  children = model.load_data{|relation| do_query(parent, reflect, relation) }
76
- children_hash = children.map{|s| [s["id"], s]}.to_h
82
+ children_hash = children.map{|s| [s[primary_key], s]}.to_h
83
+ foreign_key = get_foreign_key(reflect)
77
84
  parent.each{|s|
78
- next if (id = s[reflect.foreign_key]) == nil
85
+ next if (id = s[foreign_key]) == nil
79
86
  s[children_store_name] = children_hash[id]
80
87
  }
81
88
  else #Child.where(:parent_id => parent.pluck(:id))
82
89
  parent_hash = {}
83
90
  parent.each do |model_hash|
84
- key = model_hash['id']
91
+ key = model_hash[primary_key]
85
92
  if reflect.collection?
86
- array = (parent_hash[key] ? parent_hash[key][children_store_name] : []) #hare the children if id is duplicated
93
+ array = (parent_hash[key] ? parent_hash[key][children_store_name] : []) #share the children if id is duplicated
87
94
  model_hash[children_store_name] = array
88
95
  end
89
96
  parent_hash[key] = model_hash
@@ -1,3 +1,3 @@
1
1
  module DeepPluck
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
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.1.2
4
+ version: 0.1.3
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-17 00:00:00.000000000 Z
11
+ date: 2017-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler