pluckers 1.0.0
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 +7 -0
- data/.gitignore +51 -0
- data/Appraisals +22 -0
- data/CHANGELOG +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +56 -0
- data/LICENSE +674 -0
- data/README.md +40 -0
- data/Rakefile +10 -0
- data/circle.yml +15 -0
- data/doc/idea.md +49 -0
- data/doc/usage/basics.md +46 -0
- data/doc/usage/extending.md +109 -0
- data/doc/usage/globalize.md +54 -0
- data/doc/usage/relationships.md +216 -0
- data/doc/usage/renaming.md +26 -0
- data/lib/pluckers/base.rb +166 -0
- data/lib/pluckers/features/active_record_3_2/belongs_to_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_3_2/globalize.rb +11 -0
- data/lib/pluckers/features/active_record_3_2/has_and_belongs_to_many_reflections.rb +40 -0
- data/lib/pluckers/features/active_record_3_2/has_many_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_3_2/has_many_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_3_2/has_one_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_3_2/has_one_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_3_2/pluck.rb +26 -0
- data/lib/pluckers/features/active_record_3_2/renaming.rb +11 -0
- data/lib/pluckers/features/active_record_3_2/simple_attributes.rb +11 -0
- data/lib/pluckers/features/active_record_3_2.rb +10 -0
- data/lib/pluckers/features/active_record_4_0/belongs_to_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_4_0/globalize.rb +11 -0
- data/lib/pluckers/features/active_record_4_0/has_and_belongs_to_many_reflections.rb +40 -0
- data/lib/pluckers/features/active_record_4_0/has_many_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_4_0/has_many_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_0/has_one_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_0/has_one_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_0/pluck.rb +11 -0
- data/lib/pluckers/features/active_record_4_0/renaming.rb +11 -0
- data/lib/pluckers/features/active_record_4_0/simple_attributes.rb +11 -0
- data/lib/pluckers/features/active_record_4_0.rb +10 -0
- data/lib/pluckers/features/active_record_4_1/belongs_to_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_4_1/globalize.rb +11 -0
- data/lib/pluckers/features/active_record_4_1/has_and_belongs_to_many_reflections.rb +40 -0
- data/lib/pluckers/features/active_record_4_1/has_many_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_4_1/has_many_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_1/has_one_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_1/has_one_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_1/pluck.rb +11 -0
- data/lib/pluckers/features/active_record_4_1/renaming.rb +11 -0
- data/lib/pluckers/features/active_record_4_1/simple_attributes.rb +11 -0
- data/lib/pluckers/features/active_record_4_1.rb +10 -0
- data/lib/pluckers/features/active_record_4_2/belongs_to_reflections.rb +15 -0
- data/lib/pluckers/features/active_record_4_2/globalize.rb +11 -0
- data/lib/pluckers/features/active_record_4_2/has_and_belongs_to_many_reflections.rb +39 -0
- data/lib/pluckers/features/active_record_4_2/has_many_reflections.rb +15 -0
- data/lib/pluckers/features/active_record_4_2/has_many_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_2/has_one_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_4_2/has_one_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_4_2/pluck.rb +11 -0
- data/lib/pluckers/features/active_record_4_2/renaming.rb +11 -0
- data/lib/pluckers/features/active_record_4_2/simple_attributes.rb +11 -0
- data/lib/pluckers/features/active_record_4_2.rb +10 -0
- data/lib/pluckers/features/active_record_5_0/belongs_to_reflections.rb +15 -0
- data/lib/pluckers/features/active_record_5_0/globalize.rb +11 -0
- data/lib/pluckers/features/active_record_5_0/has_and_belongs_to_many_reflections.rb +39 -0
- data/lib/pluckers/features/active_record_5_0/has_many_reflections.rb +15 -0
- data/lib/pluckers/features/active_record_5_0/has_many_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_5_0/has_one_reflections.rb +16 -0
- data/lib/pluckers/features/active_record_5_0/has_one_through_reflections.rb +17 -0
- data/lib/pluckers/features/active_record_5_0/pluck.rb +11 -0
- data/lib/pluckers/features/active_record_5_0/renaming.rb +11 -0
- data/lib/pluckers/features/active_record_5_0/simple_attributes.rb +11 -0
- data/lib/pluckers/features/active_record_5_0.rb +10 -0
- data/lib/pluckers/features/base/belongs_to_reflections.rb +131 -0
- data/lib/pluckers/features/base/globalize.rb +116 -0
- data/lib/pluckers/features/base/has_and_belongs_to_many_reflections.rb +190 -0
- data/lib/pluckers/features/base/has_many_reflections.rb +193 -0
- data/lib/pluckers/features/base/has_many_through_reflections.rb +131 -0
- data/lib/pluckers/features/base/has_one_reflections.rb +122 -0
- data/lib/pluckers/features/base/has_one_through_reflections.rb +129 -0
- data/lib/pluckers/features/base/pluck.rb +30 -0
- data/lib/pluckers/features/base/renaming.rb +55 -0
- data/lib/pluckers/features/base/simple_attributes.rb +64 -0
- data/lib/pluckers/version.rb +3 -0
- data/lib/pluckers.rb +7 -0
- data/pluckers.gemspec +38 -0
- 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,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,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,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,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,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
|