pluckers 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +51 -0
  3. data/Appraisals +22 -0
  4. data/CHANGELOG +7 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +56 -0
  8. data/LICENSE +674 -0
  9. data/README.md +40 -0
  10. data/Rakefile +10 -0
  11. data/circle.yml +15 -0
  12. data/doc/idea.md +49 -0
  13. data/doc/usage/basics.md +46 -0
  14. data/doc/usage/extending.md +109 -0
  15. data/doc/usage/globalize.md +54 -0
  16. data/doc/usage/relationships.md +216 -0
  17. data/doc/usage/renaming.md +26 -0
  18. data/lib/pluckers/base.rb +166 -0
  19. data/lib/pluckers/features/active_record_3_2/belongs_to_reflections.rb +16 -0
  20. data/lib/pluckers/features/active_record_3_2/globalize.rb +11 -0
  21. data/lib/pluckers/features/active_record_3_2/has_and_belongs_to_many_reflections.rb +40 -0
  22. data/lib/pluckers/features/active_record_3_2/has_many_reflections.rb +16 -0
  23. data/lib/pluckers/features/active_record_3_2/has_many_through_reflections.rb +17 -0
  24. data/lib/pluckers/features/active_record_3_2/has_one_reflections.rb +17 -0
  25. data/lib/pluckers/features/active_record_3_2/has_one_through_reflections.rb +17 -0
  26. data/lib/pluckers/features/active_record_3_2/pluck.rb +26 -0
  27. data/lib/pluckers/features/active_record_3_2/renaming.rb +11 -0
  28. data/lib/pluckers/features/active_record_3_2/simple_attributes.rb +11 -0
  29. data/lib/pluckers/features/active_record_3_2.rb +10 -0
  30. data/lib/pluckers/features/active_record_4_0/belongs_to_reflections.rb +16 -0
  31. data/lib/pluckers/features/active_record_4_0/globalize.rb +11 -0
  32. data/lib/pluckers/features/active_record_4_0/has_and_belongs_to_many_reflections.rb +40 -0
  33. data/lib/pluckers/features/active_record_4_0/has_many_reflections.rb +16 -0
  34. data/lib/pluckers/features/active_record_4_0/has_many_through_reflections.rb +17 -0
  35. data/lib/pluckers/features/active_record_4_0/has_one_reflections.rb +17 -0
  36. data/lib/pluckers/features/active_record_4_0/has_one_through_reflections.rb +17 -0
  37. data/lib/pluckers/features/active_record_4_0/pluck.rb +11 -0
  38. data/lib/pluckers/features/active_record_4_0/renaming.rb +11 -0
  39. data/lib/pluckers/features/active_record_4_0/simple_attributes.rb +11 -0
  40. data/lib/pluckers/features/active_record_4_0.rb +10 -0
  41. data/lib/pluckers/features/active_record_4_1/belongs_to_reflections.rb +16 -0
  42. data/lib/pluckers/features/active_record_4_1/globalize.rb +11 -0
  43. data/lib/pluckers/features/active_record_4_1/has_and_belongs_to_many_reflections.rb +40 -0
  44. data/lib/pluckers/features/active_record_4_1/has_many_reflections.rb +16 -0
  45. data/lib/pluckers/features/active_record_4_1/has_many_through_reflections.rb +17 -0
  46. data/lib/pluckers/features/active_record_4_1/has_one_reflections.rb +17 -0
  47. data/lib/pluckers/features/active_record_4_1/has_one_through_reflections.rb +17 -0
  48. data/lib/pluckers/features/active_record_4_1/pluck.rb +11 -0
  49. data/lib/pluckers/features/active_record_4_1/renaming.rb +11 -0
  50. data/lib/pluckers/features/active_record_4_1/simple_attributes.rb +11 -0
  51. data/lib/pluckers/features/active_record_4_1.rb +10 -0
  52. data/lib/pluckers/features/active_record_4_2/belongs_to_reflections.rb +15 -0
  53. data/lib/pluckers/features/active_record_4_2/globalize.rb +11 -0
  54. data/lib/pluckers/features/active_record_4_2/has_and_belongs_to_many_reflections.rb +39 -0
  55. data/lib/pluckers/features/active_record_4_2/has_many_reflections.rb +15 -0
  56. data/lib/pluckers/features/active_record_4_2/has_many_through_reflections.rb +17 -0
  57. data/lib/pluckers/features/active_record_4_2/has_one_reflections.rb +16 -0
  58. data/lib/pluckers/features/active_record_4_2/has_one_through_reflections.rb +17 -0
  59. data/lib/pluckers/features/active_record_4_2/pluck.rb +11 -0
  60. data/lib/pluckers/features/active_record_4_2/renaming.rb +11 -0
  61. data/lib/pluckers/features/active_record_4_2/simple_attributes.rb +11 -0
  62. data/lib/pluckers/features/active_record_4_2.rb +10 -0
  63. data/lib/pluckers/features/active_record_5_0/belongs_to_reflections.rb +15 -0
  64. data/lib/pluckers/features/active_record_5_0/globalize.rb +11 -0
  65. data/lib/pluckers/features/active_record_5_0/has_and_belongs_to_many_reflections.rb +39 -0
  66. data/lib/pluckers/features/active_record_5_0/has_many_reflections.rb +15 -0
  67. data/lib/pluckers/features/active_record_5_0/has_many_through_reflections.rb +17 -0
  68. data/lib/pluckers/features/active_record_5_0/has_one_reflections.rb +16 -0
  69. data/lib/pluckers/features/active_record_5_0/has_one_through_reflections.rb +17 -0
  70. data/lib/pluckers/features/active_record_5_0/pluck.rb +11 -0
  71. data/lib/pluckers/features/active_record_5_0/renaming.rb +11 -0
  72. data/lib/pluckers/features/active_record_5_0/simple_attributes.rb +11 -0
  73. data/lib/pluckers/features/active_record_5_0.rb +10 -0
  74. data/lib/pluckers/features/base/belongs_to_reflections.rb +131 -0
  75. data/lib/pluckers/features/base/globalize.rb +116 -0
  76. data/lib/pluckers/features/base/has_and_belongs_to_many_reflections.rb +190 -0
  77. data/lib/pluckers/features/base/has_many_reflections.rb +193 -0
  78. data/lib/pluckers/features/base/has_many_through_reflections.rb +131 -0
  79. data/lib/pluckers/features/base/has_one_reflections.rb +122 -0
  80. data/lib/pluckers/features/base/has_one_through_reflections.rb +129 -0
  81. data/lib/pluckers/features/base/pluck.rb +30 -0
  82. data/lib/pluckers/features/base/renaming.rb +55 -0
  83. data/lib/pluckers/features/base/simple_attributes.rb +64 -0
  84. data/lib/pluckers/version.rb +3 -0
  85. data/lib/pluckers.rb +7 -0
  86. data/pluckers.gemspec +38 -0
  87. metadata +236 -0
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_one_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneReflections
6
+
7
+ def active_record_has_one_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::AssociationReflection) &&
9
+ (reflection.macro == :has_one)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasOneReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_one_through_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneThroughReflections
6
+
7
+ def active_record_has_one_through_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
9
+ (reflection.macro == :has_one)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasOneThroughReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/pluck'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Pluck
6
+
7
+ include Pluckers::Features::Base::Pluck
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/renaming'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Renaming
6
+
7
+ include Pluckers::Features::Base::Renaming
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/simple_attributes'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module SimpleAttributes
6
+
7
+ include Pluckers::Features::Base::SimpleAttributes
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'active_record_4_1/simple_attributes'
2
+ require_relative 'active_record_4_1/belongs_to_reflections'
3
+ require_relative 'active_record_4_1/has_many_reflections'
4
+ require_relative 'active_record_4_1/has_many_through_reflections'
5
+ require_relative 'active_record_4_1/has_and_belongs_to_many_reflections'
6
+ require_relative 'active_record_4_1/has_one_reflections'
7
+ require_relative 'active_record_4_1/has_one_through_reflections'
8
+ require_relative 'active_record_4_1/renaming'
9
+ require_relative 'active_record_4_1/globalize'
10
+ require_relative 'active_record_4_1/pluck'
@@ -0,0 +1,15 @@
1
+ require_relative '../base/belongs_to_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module BelongsToReflections
6
+
7
+ def active_record_belongs_to_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
9
+ end
10
+
11
+ include Pluckers::Features::Base::BelongsToReflections
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/globalize'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Globalize
6
+
7
+ include Pluckers::Features::Base::Globalize
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../base/has_and_belongs_to_many_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasAndBelongsToManyReflections
6
+
7
+ def active_record_has_and_belongs_to_many_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasAndBelongsToManyReflection
9
+ end
10
+
11
+ def has_and_belongs_to_many_ids klass_reflection
12
+
13
+ # First, we get the the join table
14
+ join_table = Arel::Table.new(klass_reflection.join_table)
15
+
16
+ # And now, the foreign_keys.
17
+ # In our example with BlogPost and Category they would be:
18
+ # model_foreign_key = blog_post_id
19
+ # related_model_foreign_key = category_id
20
+ model_foreign_key = klass_reflection.foreign_key
21
+ related_model_foreign_key = klass_reflection.association_foreign_key
22
+
23
+ # Now we query the join table so we get the two ids
24
+ ids_query = join_table.where(
25
+ join_table[model_foreign_key].in(@results.map{|_, r| r[:id] })
26
+ ).project(
27
+ join_table[related_model_foreign_key],
28
+ join_table[model_foreign_key]
29
+ )
30
+
31
+ join_results = ActiveRecord::Base.connection.execute(ids_query.to_sql)
32
+
33
+ end
34
+
35
+ include Pluckers::Features::Base::HasAndBelongsToManyReflections
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../base/has_many_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasManyReflections
6
+
7
+ def active_record_has_many_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasManyReflection
9
+ end
10
+
11
+ include Pluckers::Features::Base::HasManyReflections
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_many_through_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasManyThroughReflections
6
+
7
+ def active_record_has_many_through_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
9
+ reflection.delegate_reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasManyThroughReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require_relative '../base/has_one_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneReflections
6
+
7
+ def active_record_has_one_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasOneReflection
9
+ end
10
+
11
+ include Pluckers::Features::Base::HasOneReflections
12
+
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_one_through_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneThroughReflections
6
+
7
+ def active_record_has_one_through_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
9
+ reflection.delegate_reflection.is_a?(ActiveRecord::Reflection::HasOneReflection)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasOneThroughReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/pluck'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Pluck
6
+
7
+ include Pluckers::Features::Base::Pluck
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/renaming'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Renaming
6
+
7
+ include Pluckers::Features::Base::Renaming
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/simple_attributes'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module SimpleAttributes
6
+
7
+ include Pluckers::Features::Base::SimpleAttributes
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'active_record_4_2/simple_attributes'
2
+ require_relative 'active_record_4_2/belongs_to_reflections'
3
+ require_relative 'active_record_4_2/has_many_reflections'
4
+ require_relative 'active_record_4_2/has_many_through_reflections'
5
+ require_relative 'active_record_4_2/has_and_belongs_to_many_reflections'
6
+ require_relative 'active_record_4_2/has_one_reflections'
7
+ require_relative 'active_record_4_2/has_one_through_reflections'
8
+ require_relative 'active_record_4_2/renaming'
9
+ require_relative 'active_record_4_2/globalize'
10
+ require_relative 'active_record_4_2/pluck'
@@ -0,0 +1,15 @@
1
+ require_relative '../base/belongs_to_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module BelongsToReflections
6
+
7
+ def active_record_belongs_to_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
9
+ end
10
+
11
+ include Pluckers::Features::Base::BelongsToReflections
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/globalize'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Globalize
6
+
7
+ include Pluckers::Features::Base::Globalize
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../base/has_and_belongs_to_many_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasAndBelongsToManyReflections
6
+
7
+ def active_record_has_and_belongs_to_many_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasAndBelongsToManyReflection
9
+ end
10
+
11
+ def has_and_belongs_to_many_ids klass_reflection
12
+
13
+ # First, we get the the join table
14
+ join_table = Arel::Table.new(klass_reflection.join_table)
15
+
16
+ # And now, the foreign_keys.
17
+ # In our example with BlogPost and Category they would be:
18
+ # model_foreign_key = blog_post_id
19
+ # related_model_foreign_key = category_id
20
+ model_foreign_key = klass_reflection.foreign_key
21
+ related_model_foreign_key = klass_reflection.association_foreign_key
22
+
23
+ # Now we query the join table so we get the two ids
24
+ ids_query = join_table.where(
25
+ join_table[model_foreign_key].in(@results.map{|_, r| Arel::Nodes::Quoted.new(r[:id]) })
26
+ ).project(
27
+ join_table[related_model_foreign_key],
28
+ join_table[model_foreign_key]
29
+ )
30
+
31
+ join_results = ActiveRecord::Base.connection.execute(ids_query.to_sql)
32
+
33
+ end
34
+
35
+ include Pluckers::Features::Base::HasAndBelongsToManyReflections
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../base/has_many_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasManyReflections
6
+
7
+ def active_record_has_many_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasManyReflection
9
+ end
10
+
11
+ include Pluckers::Features::Base::HasManyReflections
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_many_through_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasManyThroughReflections
6
+
7
+ def active_record_has_many_through_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
9
+ reflection.delegate_reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasManyThroughReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require_relative '../base/has_one_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneReflections
6
+
7
+ def active_record_has_one_reflection? reflection
8
+ reflection.is_a? ActiveRecord::Reflection::HasOneReflection
9
+ end
10
+
11
+ include Pluckers::Features::Base::HasOneReflections
12
+
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../base/has_one_through_reflections'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module HasOneThroughReflections
6
+
7
+ def active_record_has_one_through_reflection? reflection
8
+ reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
9
+ reflection.delegate_reflection.is_a?(ActiveRecord::Reflection::HasOneReflection)
10
+ end
11
+
12
+ include Pluckers::Features::Base::HasOneThroughReflections
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/pluck'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Pluck
6
+
7
+ include Pluckers::Features::Base::Pluck
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/renaming'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module Renaming
6
+
7
+ include Pluckers::Features::Base::Renaming
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../base/simple_attributes'
2
+
3
+ module Pluckers
4
+ module Features
5
+ module SimpleAttributes
6
+
7
+ include Pluckers::Features::Base::SimpleAttributes
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'active_record_5_0/simple_attributes'
2
+ require_relative 'active_record_5_0/belongs_to_reflections'
3
+ require_relative 'active_record_5_0/has_many_reflections'
4
+ require_relative 'active_record_5_0/has_many_through_reflections'
5
+ require_relative 'active_record_5_0/has_and_belongs_to_many_reflections'
6
+ require_relative 'active_record_5_0/has_one_reflections'
7
+ require_relative 'active_record_5_0/has_one_through_reflections'
8
+ require_relative 'active_record_5_0/renaming'
9
+ require_relative 'active_record_5_0/globalize'
10
+ require_relative 'active_record_5_0/pluck'
@@ -0,0 +1,131 @@
1
+ module Pluckers
2
+ ##
3
+ # This module groups diferent modules that will configure and build the
4
+ # results for a specific kind of information (attributes, translations,
5
+ # relations...)
6
+ #
7
+ # All this modules will have two methods, one for configuration (e.g, attributes
8
+ # to be included in the real pluck) and one for building the final results
9
+ module Features
10
+
11
+ module Base
12
+
13
+ ##
14
+ # This module implements plucking belongs_to relationships in a recursive
15
+ # way.
16
+ #
17
+ # The options used in this feature are:
18
+ #
19
+ # * reflections: A hash of the reflections we will pluck recursively. The
20
+ # key of this hash will be the name of the reflection and the value is
21
+ # another hash of options.
22
+ #
23
+ # - scope: You can limit the scope of the objects plucked. E.g, you
24
+ # could use Author.active instead of Author.all. Notice that .all is
25
+ # the default.
26
+ #
27
+ # - plucker: You can use a custom plucker instead of Pluckers::Base in
28
+ # case you want any specific logic. Pluckers::Base is the default one.
29
+ #
30
+ # - Any other option will be passed to the plucker, so you can send any
31
+ # other regular option such as attributes, custom ones or even more
32
+ # reflections. Recursivity FTW!!
33
+ #
34
+ module BelongsToReflections
35
+
36
+
37
+ ##
38
+ # Here we obtain the belongs_to reflections to include in the pluck
39
+ # operation and also include the relation foreign key in the attributes to
40
+ # pluck for this model.
41
+ def configure_query
42
+ super
43
+
44
+ pluck_reflections = @options[:reflections] || {}
45
+
46
+ return if pluck_reflections.blank?
47
+
48
+ @belongs_to_reflections = { }
49
+
50
+ # We iterate through the class reflections passed as options
51
+ @klass_reflections.slice(*pluck_reflections.keys).
52
+ # And select those that are BelongsTo
53
+ select{|_, r| active_record_belongs_to_reflection?(r) }.
54
+ # And store them in the belongs_to_reflection hash that will be used later
55
+ each do |name, reflection|
56
+ name = name.to_sym
57
+ @belongs_to_reflections[name] = pluck_reflections[name]
58
+ end
59
+
60
+ # First thing we do is to include the foreign key in the attributes to
61
+ # pluck array, so the base plucker plucks them
62
+ @belongs_to_reflections.each do |name, _|
63
+ foreign_key_name = @klass_reflections[name].foreign_key
64
+ @attributes_to_pluck << {
65
+ name: foreign_key_name.to_sym,
66
+ sql: foreign_key_name
67
+ }
68
+ end
69
+
70
+ end
71
+
72
+ ##
73
+ # In this method we get the reflections and for each one creates and
74
+ # executes a new plucker.
75
+ #
76
+ # This pluck gives the whole process a recursive character and options
77
+ # for that plucker may be passed in the options hash.
78
+ def build_results
79
+ super
80
+
81
+ return if @belongs_to_reflections.blank?
82
+ # For each reflection
83
+ @belongs_to_reflections.each do |name, reflection|
84
+ # As an example we will imagine that we are plucking BlogPosts and
85
+ # this relation is the Author
86
+
87
+ # We get the meta information about the reflection
88
+ klass_reflection = @klass_reflections[name]
89
+
90
+ # initialize some options such as the plucker or the scope of the pluck
91
+ scope = reflection[:scope] || klass_reflection.klass.send(all_method)
92
+ plucker = reflection[:plucker] || Pluckers::Base
93
+
94
+ reflection_primary_key = klass_reflection.active_record_primary_key.to_sym
95
+ reflection_foreign_key = klass_reflection.foreign_key.to_sym
96
+ # And now we create the plucker. Notice that we add a where to the
97
+ # scope, so we filter the records to pluck as we only get those with
98
+ # an id in the set of the foreign keys of the records already
99
+ # plucked by the base plucker
100
+ #
101
+ # In our Example we would be doing something like
102
+ # Author.all.where(id: author_ids)
103
+ reflection_plucker = plucker.new scope.where(
104
+ reflection_primary_key => @results.map{|_, r| r[reflection_foreign_key] }.compact
105
+ ),
106
+ reflection
107
+
108
+
109
+ # We initialize so we return a nil if there are no record related
110
+ @results.each do |_,result|
111
+ result[name] = nil
112
+ end
113
+
114
+ # And now pluck the related class and process the results
115
+ reflection_plucker.pluck.each do |r|
116
+ # For each related result (Author) we search those records
117
+ # (BlogPost) that are related (post.author_id == author.id) and
118
+ # insert them in the relationship attributes
119
+ @results.each do |_,result|
120
+ if result[reflection_foreign_key] == r[reflection_primary_key]
121
+ result[name] = r
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ end
129
+ end
130
+ end
131
+ end