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,122 @@
|
|
|
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 has_one 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 HasOneReflections
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
##
|
|
38
|
+
# Here we obtain the has_one 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
|
+
@has_one_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_has_one_reflection?(r)}.
|
|
54
|
+
# And store them in the has_one_reflection hash that will be used later
|
|
55
|
+
each do |name, reflection|
|
|
56
|
+
name = name.to_sym
|
|
57
|
+
@has_one_reflections[name] = pluck_reflections[name]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
##
|
|
63
|
+
# In this method we get the reflections and for each one creates and
|
|
64
|
+
# executes a new plucker.
|
|
65
|
+
#
|
|
66
|
+
# This pluck gives the whole process a recursive character and options
|
|
67
|
+
# for that plucker may be passed in the options hash.
|
|
68
|
+
def build_results
|
|
69
|
+
super
|
|
70
|
+
|
|
71
|
+
return if @has_one_reflections.blank?
|
|
72
|
+
# For each reflection
|
|
73
|
+
@has_one_reflections.each do |name, reflection|
|
|
74
|
+
# As an example we will imagine that we are plucking BlogPosts and
|
|
75
|
+
# this relation is the Author
|
|
76
|
+
|
|
77
|
+
# We get the meta information about the reflection
|
|
78
|
+
klass_reflection = @klass_reflections[name]
|
|
79
|
+
|
|
80
|
+
# initialize some options such as the plucker or the scope of the pluck
|
|
81
|
+
scope = reflection[:scope] || klass_reflection.klass.send(all_method)
|
|
82
|
+
plucker = reflection[:plucker] || Pluckers::Base
|
|
83
|
+
|
|
84
|
+
# If there are attributes configured to be plucked we add the foreign
|
|
85
|
+
# key as we will need it to relate the records
|
|
86
|
+
reflection[:attributes] |= [klass_reflection.foreign_key.to_sym] if reflection[:attributes]
|
|
87
|
+
|
|
88
|
+
# And now we create the plucker. Notice that we add a where to the
|
|
89
|
+
# scope, so we filter the records to pluck as we only get those with
|
|
90
|
+
# an id in the set of the foreign keys of the records already
|
|
91
|
+
# plucked by the base plucker
|
|
92
|
+
#
|
|
93
|
+
# In our Example we would be doing something like
|
|
94
|
+
# Author.all.where(id: author_ids)
|
|
95
|
+
reflection_plucker = plucker.new scope.where(
|
|
96
|
+
klass_reflection.foreign_key => @results.map{|_, r| r[klass_reflection.active_record_primary_key.to_sym] }
|
|
97
|
+
),
|
|
98
|
+
reflection
|
|
99
|
+
|
|
100
|
+
# We initialize so we return a nil if there are no record related
|
|
101
|
+
@results.each do |_,result|
|
|
102
|
+
result[name] = nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# And now pluck the related class and process the results
|
|
106
|
+
reflection_plucker.pluck.each do |r|
|
|
107
|
+
# For each related result (Author) we search those records
|
|
108
|
+
# (BlogPost) that are related (post.author_id == author.id) and
|
|
109
|
+
# insert them in the relationship attributes
|
|
110
|
+
@results.each do |_,result|
|
|
111
|
+
if result[klass_reflection.active_record_primary_key.to_sym] == r[klass_reflection.foreign_key.to_sym]
|
|
112
|
+
result[name] = r
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
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 has_many :through relationships in a
|
|
15
|
+
# recursive 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 HasOneThroughReflections
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
##
|
|
38
|
+
# Here we obtain the has_many :through 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
|
+
@has_one_through_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 Through and which delegate reflection is a HasMany
|
|
53
|
+
select{|_, r| active_record_has_one_through_reflection?(r)}.
|
|
54
|
+
# And store them in the has_many_reflection hash that will be used later
|
|
55
|
+
each do |name, reflection|
|
|
56
|
+
name = name.to_sym
|
|
57
|
+
@has_one_through_reflections[name] = pluck_reflections[name]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
##
|
|
62
|
+
# In this method we get the reflections and for each one creates and
|
|
63
|
+
# executes a new plucker.
|
|
64
|
+
#
|
|
65
|
+
# This pluck gives the whole process a recursive character and options
|
|
66
|
+
# for that plucker may be passed in the options hash.
|
|
67
|
+
def build_results
|
|
68
|
+
super
|
|
69
|
+
|
|
70
|
+
return if @has_one_through_reflections.blank?
|
|
71
|
+
|
|
72
|
+
@has_one_through_reflections.each do |name, reflection|
|
|
73
|
+
# As an example we will imagine that we are plucking BlogPost and
|
|
74
|
+
# this relation is the :user, that is a relationship through
|
|
75
|
+
# Author
|
|
76
|
+
|
|
77
|
+
# We get the meta information about the :through reflection (:user)
|
|
78
|
+
klass_reflection = @klass_reflections[name]
|
|
79
|
+
|
|
80
|
+
# And also the has_many reflection (:posts) as we will need it to fetch information
|
|
81
|
+
reflection_to_pluck = klass_reflection.chain.reverse.first
|
|
82
|
+
|
|
83
|
+
# initialize some options such as the plucker or the scope of the pluck
|
|
84
|
+
scope = reflection_to_pluck.klass.send(all_method)
|
|
85
|
+
|
|
86
|
+
# Essentially we are going to pluck the has_one relationship and
|
|
87
|
+
# add the reflections option so it recursively plucks the has_one
|
|
88
|
+
# :user reflection from BlogPost.
|
|
89
|
+
plucker = reflection[:plucker] || Pluckers::Base
|
|
90
|
+
plucker_options = {
|
|
91
|
+
attributes: [reflection_to_pluck.active_record_primary_key.to_sym],
|
|
92
|
+
reflections: { klass_reflection.source_reflection.name => reflection }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
# In order to create this intermediary plucker we add a where to the
|
|
96
|
+
# scope, so we filter the records to pluck as we only get those with
|
|
97
|
+
# an id in the set of the foreign keys of the records already
|
|
98
|
+
# plucked by the base plucker
|
|
99
|
+
#
|
|
100
|
+
# In our Example we would be doing something like
|
|
101
|
+
# Author.all.where(id: author_ids)
|
|
102
|
+
reflection_plucker = plucker.new scope.where(
|
|
103
|
+
reflection_to_pluck.active_record_primary_key => @results.map{|_, r| r[reflection_to_pluck.foreign_key.to_sym] }
|
|
104
|
+
),
|
|
105
|
+
plucker_options
|
|
106
|
+
|
|
107
|
+
# We initialize so we return an empty array if there are no record
|
|
108
|
+
# related
|
|
109
|
+
@results.each do |_, result|
|
|
110
|
+
result[name] ||= nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
reflection_plucker.pluck.each do |r|
|
|
114
|
+
@results.each do |_,result|
|
|
115
|
+
# For each related result (Author) we search those records
|
|
116
|
+
# (BlogPost) that are related (author.id == post.author_id) and
|
|
117
|
+
# insert not the record itself but the desired reflection in the
|
|
118
|
+
# result
|
|
119
|
+
if result[reflection_to_pluck.foreign_key.to_sym] == r[reflection_to_pluck.active_record_primary_key.to_sym]
|
|
120
|
+
result[name] = r[name]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
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 the actual pluck. In ActiveRecord 4 and greater
|
|
15
|
+
# it just uses the standard `pluck' method but in other AR versions it
|
|
16
|
+
# could need some customizations.
|
|
17
|
+
module Pluck
|
|
18
|
+
|
|
19
|
+
def pluck_records fields_to_pluck
|
|
20
|
+
@records.pluck(*fields_to_pluck)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def all_method
|
|
24
|
+
:all
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
# * renames: A hash of the attributes/reflections/whatever that will be
|
|
20
|
+
# renamed. The key is the old name and the value is the new name.
|
|
21
|
+
#
|
|
22
|
+
module Renaming
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# Here we obtain the renames enabled for this plucker
|
|
27
|
+
def configure_query
|
|
28
|
+
super
|
|
29
|
+
|
|
30
|
+
@renames = @options.delete(:renames)
|
|
31
|
+
@renames ||= {}
|
|
32
|
+
@renames = @renames.with_indifferent_access
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
##
|
|
37
|
+
# In this method we get the renames and check result by result which
|
|
38
|
+
# ones must be applied
|
|
39
|
+
def build_results
|
|
40
|
+
super
|
|
41
|
+
|
|
42
|
+
@renames.each do |old_name, new_name|
|
|
43
|
+
@results.each do |_,result|
|
|
44
|
+
if result.keys.include? old_name.to_sym
|
|
45
|
+
result[new_name.to_sym] = result.delete(old_name.to_sym)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
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, fields
|
|
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 fetching simple attributes (columns) from the
|
|
15
|
+
# database through the AR's pluck method.
|
|
16
|
+
#
|
|
17
|
+
# The options used in this feature are:
|
|
18
|
+
#
|
|
19
|
+
# * attributes: Names of attributes of the objects to be plucked. This
|
|
20
|
+
# attributes should be the names of the columns in the database.
|
|
21
|
+
module SimpleAttributes
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
##
|
|
25
|
+
# Here we initialize the simple attributes to be retrieved and checks
|
|
26
|
+
# that those attributes exists in the current scope.
|
|
27
|
+
def configure_query
|
|
28
|
+
super
|
|
29
|
+
|
|
30
|
+
attributes = @options[:attributes]
|
|
31
|
+
attributes ||= default_attributes
|
|
32
|
+
|
|
33
|
+
plucker_attributes = attributes.map(&:to_sym)
|
|
34
|
+
|
|
35
|
+
klass_attributes = @records.attribute_names.map(&:to_sym)
|
|
36
|
+
|
|
37
|
+
# Validate that all attributes exists in the model
|
|
38
|
+
if (missing_attributes = plucker_attributes - klass_attributes).any?
|
|
39
|
+
raise ArgumentError.new("Plucker attributes '#{missing_attributes.to_sentence}', are missing in #{@records.klass}")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
simple_attributes = plucker_attributes & klass_attributes
|
|
43
|
+
|
|
44
|
+
@attributes_to_pluck += simple_attributes.map {|f| { name: f, sql: "\"#{@records.table_name}\".#{f}" }}
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
##
|
|
49
|
+
# We don't need to perform any extra operation as the pluck is executed
|
|
50
|
+
# in the Pluckers::Base class. We could omit this definition, but leave
|
|
51
|
+
# it for example purposes.
|
|
52
|
+
def build_results
|
|
53
|
+
super
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
##
|
|
57
|
+
# This private method returns the default attributes that must be retrieved
|
|
58
|
+
private def default_attributes records = @records
|
|
59
|
+
records.attribute_names
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/pluckers.rb
ADDED
data/pluckers.gemspec
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'pluckers/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "pluckers"
|
|
8
|
+
spec.version = Pluckers::VERSION
|
|
9
|
+
spec.authors = ["David J. Brenes"]
|
|
10
|
+
spec.email = ["gems@simplelogica.net"]
|
|
11
|
+
spec.license = 'GPL-3.0'
|
|
12
|
+
|
|
13
|
+
spec.summary = %q{Gem extending the idea behind AR's pluck method so we can fetch data from multiple tables}
|
|
14
|
+
spec.description = %q{Gem extending the idea behind AR's pluck method so we can fetch data from multiple tables}
|
|
15
|
+
spec.homepage = "https://github.com/simplelogica/pluckers"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
|
21
|
+
else
|
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(bin|gemfiles|test)/}) }
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_dependency "activerecord", "> 3.2", "< 5.1"
|
|
31
|
+
|
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
33
|
+
spec.add_development_dependency "minitest"
|
|
34
|
+
spec.add_development_dependency "sqlite3"
|
|
35
|
+
spec.add_development_dependency "globalize"
|
|
36
|
+
spec.add_development_dependency "byebug"
|
|
37
|
+
spec.add_development_dependency "appraisal", "~> 2.1.0"
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pluckers
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David J. Brenes
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-03-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.2'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '5.1'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '3.2'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '5.1'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: rake
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '10.0'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '10.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: minitest
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: sqlite3
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: globalize
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
type: :development
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: byebug
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
type: :development
|
|
97
|
+
prerelease: false
|
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
- !ruby/object:Gem::Dependency
|
|
104
|
+
name: appraisal
|
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - "~>"
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: 2.1.0
|
|
110
|
+
type: :development
|
|
111
|
+
prerelease: false
|
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - "~>"
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 2.1.0
|
|
117
|
+
description: Gem extending the idea behind AR's pluck method so we can fetch data
|
|
118
|
+
from multiple tables
|
|
119
|
+
email:
|
|
120
|
+
- gems@simplelogica.net
|
|
121
|
+
executables: []
|
|
122
|
+
extensions: []
|
|
123
|
+
extra_rdoc_files: []
|
|
124
|
+
files:
|
|
125
|
+
- ".gitignore"
|
|
126
|
+
- Appraisals
|
|
127
|
+
- CHANGELOG
|
|
128
|
+
- CODE_OF_CONDUCT.md
|
|
129
|
+
- Gemfile
|
|
130
|
+
- Gemfile.lock
|
|
131
|
+
- LICENSE
|
|
132
|
+
- README.md
|
|
133
|
+
- Rakefile
|
|
134
|
+
- circle.yml
|
|
135
|
+
- doc/idea.md
|
|
136
|
+
- doc/usage/basics.md
|
|
137
|
+
- doc/usage/extending.md
|
|
138
|
+
- doc/usage/globalize.md
|
|
139
|
+
- doc/usage/relationships.md
|
|
140
|
+
- doc/usage/renaming.md
|
|
141
|
+
- lib/pluckers.rb
|
|
142
|
+
- lib/pluckers/base.rb
|
|
143
|
+
- lib/pluckers/features/active_record_3_2.rb
|
|
144
|
+
- lib/pluckers/features/active_record_3_2/belongs_to_reflections.rb
|
|
145
|
+
- lib/pluckers/features/active_record_3_2/globalize.rb
|
|
146
|
+
- lib/pluckers/features/active_record_3_2/has_and_belongs_to_many_reflections.rb
|
|
147
|
+
- lib/pluckers/features/active_record_3_2/has_many_reflections.rb
|
|
148
|
+
- lib/pluckers/features/active_record_3_2/has_many_through_reflections.rb
|
|
149
|
+
- lib/pluckers/features/active_record_3_2/has_one_reflections.rb
|
|
150
|
+
- lib/pluckers/features/active_record_3_2/has_one_through_reflections.rb
|
|
151
|
+
- lib/pluckers/features/active_record_3_2/pluck.rb
|
|
152
|
+
- lib/pluckers/features/active_record_3_2/renaming.rb
|
|
153
|
+
- lib/pluckers/features/active_record_3_2/simple_attributes.rb
|
|
154
|
+
- lib/pluckers/features/active_record_4_0.rb
|
|
155
|
+
- lib/pluckers/features/active_record_4_0/belongs_to_reflections.rb
|
|
156
|
+
- lib/pluckers/features/active_record_4_0/globalize.rb
|
|
157
|
+
- lib/pluckers/features/active_record_4_0/has_and_belongs_to_many_reflections.rb
|
|
158
|
+
- lib/pluckers/features/active_record_4_0/has_many_reflections.rb
|
|
159
|
+
- lib/pluckers/features/active_record_4_0/has_many_through_reflections.rb
|
|
160
|
+
- lib/pluckers/features/active_record_4_0/has_one_reflections.rb
|
|
161
|
+
- lib/pluckers/features/active_record_4_0/has_one_through_reflections.rb
|
|
162
|
+
- lib/pluckers/features/active_record_4_0/pluck.rb
|
|
163
|
+
- lib/pluckers/features/active_record_4_0/renaming.rb
|
|
164
|
+
- lib/pluckers/features/active_record_4_0/simple_attributes.rb
|
|
165
|
+
- lib/pluckers/features/active_record_4_1.rb
|
|
166
|
+
- lib/pluckers/features/active_record_4_1/belongs_to_reflections.rb
|
|
167
|
+
- lib/pluckers/features/active_record_4_1/globalize.rb
|
|
168
|
+
- lib/pluckers/features/active_record_4_1/has_and_belongs_to_many_reflections.rb
|
|
169
|
+
- lib/pluckers/features/active_record_4_1/has_many_reflections.rb
|
|
170
|
+
- lib/pluckers/features/active_record_4_1/has_many_through_reflections.rb
|
|
171
|
+
- lib/pluckers/features/active_record_4_1/has_one_reflections.rb
|
|
172
|
+
- lib/pluckers/features/active_record_4_1/has_one_through_reflections.rb
|
|
173
|
+
- lib/pluckers/features/active_record_4_1/pluck.rb
|
|
174
|
+
- lib/pluckers/features/active_record_4_1/renaming.rb
|
|
175
|
+
- lib/pluckers/features/active_record_4_1/simple_attributes.rb
|
|
176
|
+
- lib/pluckers/features/active_record_4_2.rb
|
|
177
|
+
- lib/pluckers/features/active_record_4_2/belongs_to_reflections.rb
|
|
178
|
+
- lib/pluckers/features/active_record_4_2/globalize.rb
|
|
179
|
+
- lib/pluckers/features/active_record_4_2/has_and_belongs_to_many_reflections.rb
|
|
180
|
+
- lib/pluckers/features/active_record_4_2/has_many_reflections.rb
|
|
181
|
+
- lib/pluckers/features/active_record_4_2/has_many_through_reflections.rb
|
|
182
|
+
- lib/pluckers/features/active_record_4_2/has_one_reflections.rb
|
|
183
|
+
- lib/pluckers/features/active_record_4_2/has_one_through_reflections.rb
|
|
184
|
+
- lib/pluckers/features/active_record_4_2/pluck.rb
|
|
185
|
+
- lib/pluckers/features/active_record_4_2/renaming.rb
|
|
186
|
+
- lib/pluckers/features/active_record_4_2/simple_attributes.rb
|
|
187
|
+
- lib/pluckers/features/active_record_5_0.rb
|
|
188
|
+
- lib/pluckers/features/active_record_5_0/belongs_to_reflections.rb
|
|
189
|
+
- lib/pluckers/features/active_record_5_0/globalize.rb
|
|
190
|
+
- lib/pluckers/features/active_record_5_0/has_and_belongs_to_many_reflections.rb
|
|
191
|
+
- lib/pluckers/features/active_record_5_0/has_many_reflections.rb
|
|
192
|
+
- lib/pluckers/features/active_record_5_0/has_many_through_reflections.rb
|
|
193
|
+
- lib/pluckers/features/active_record_5_0/has_one_reflections.rb
|
|
194
|
+
- lib/pluckers/features/active_record_5_0/has_one_through_reflections.rb
|
|
195
|
+
- lib/pluckers/features/active_record_5_0/pluck.rb
|
|
196
|
+
- lib/pluckers/features/active_record_5_0/renaming.rb
|
|
197
|
+
- lib/pluckers/features/active_record_5_0/simple_attributes.rb
|
|
198
|
+
- lib/pluckers/features/base/belongs_to_reflections.rb
|
|
199
|
+
- lib/pluckers/features/base/globalize.rb
|
|
200
|
+
- lib/pluckers/features/base/has_and_belongs_to_many_reflections.rb
|
|
201
|
+
- lib/pluckers/features/base/has_many_reflections.rb
|
|
202
|
+
- lib/pluckers/features/base/has_many_through_reflections.rb
|
|
203
|
+
- lib/pluckers/features/base/has_one_reflections.rb
|
|
204
|
+
- lib/pluckers/features/base/has_one_through_reflections.rb
|
|
205
|
+
- lib/pluckers/features/base/pluck.rb
|
|
206
|
+
- lib/pluckers/features/base/renaming.rb
|
|
207
|
+
- lib/pluckers/features/base/simple_attributes.rb
|
|
208
|
+
- lib/pluckers/version.rb
|
|
209
|
+
- pluckers.gemspec
|
|
210
|
+
homepage: https://github.com/simplelogica/pluckers
|
|
211
|
+
licenses:
|
|
212
|
+
- GPL-3.0
|
|
213
|
+
metadata:
|
|
214
|
+
allowed_push_host: https://rubygems.org
|
|
215
|
+
post_install_message:
|
|
216
|
+
rdoc_options: []
|
|
217
|
+
require_paths:
|
|
218
|
+
- lib
|
|
219
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
220
|
+
requirements:
|
|
221
|
+
- - ">="
|
|
222
|
+
- !ruby/object:Gem::Version
|
|
223
|
+
version: '0'
|
|
224
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
225
|
+
requirements:
|
|
226
|
+
- - ">="
|
|
227
|
+
- !ruby/object:Gem::Version
|
|
228
|
+
version: '0'
|
|
229
|
+
requirements: []
|
|
230
|
+
rubyforge_project:
|
|
231
|
+
rubygems_version: 2.5.1
|
|
232
|
+
signing_key:
|
|
233
|
+
specification_version: 4
|
|
234
|
+
summary: Gem extending the idea behind AR's pluck method so we can fetch data from
|
|
235
|
+
multiple tables
|
|
236
|
+
test_files: []
|