better_pluck 0.8.3 → 0.9.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 +4 -4
- data/lib/better_pluck/pluck_with_methods.rb +2 -0
- data/lib/better_pluck/select_only.rb +30 -0
- data/lib/better_pluck/version.rb +1 -1
- data/lib/better_pluck.rb +2 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bf2465548537590cdc1ea74f2addd0b7cf1d5503ea1456cc5cd647a8b42f2cda
|
|
4
|
+
data.tar.gz: 82f00ef52e2042cb6fa91de2af7334321702759247f2ac37d772190d67ae3cba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: adea8a324626ea010c04bac42e0b9783ddacbe57b133078a5b05cab94735e6693c591e4c54405242ac73f18c7cb188bb83e2953dcaf9ec64591208138d8cb3ea
|
|
7
|
+
data.tar.gz: 1b14db90fc98cfbd4f35df2b96842b10c5665634aa54906521c050b87f82abf08fe7663a4e100db0e20fb4b91964da986096cbdfba05c2bdbf928210dfdd694c
|
|
@@ -23,6 +23,8 @@ module BetterPluck::PluckWithMethods
|
|
|
23
23
|
|
|
24
24
|
class_methods do
|
|
25
25
|
|
|
26
|
+
|
|
27
|
+
|
|
26
28
|
# usage: zones = Zone.pluck_with_methods(:id, :name, :display_name, partner: [:имя, :email, :display_name], location: [:name, :display_name])
|
|
27
29
|
def pluck_with_methods(*fields_and_methods)
|
|
28
30
|
require "method_source" unless defined?(MethodSource)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module BetterPluck::SelectOnly
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
class_methods do
|
|
5
|
+
#extended select, allowing to write custom select queries in short format
|
|
6
|
+
#Article.select_only(author: [:id, :name]).first.author_name
|
|
7
|
+
def select_only(fields_data = {})
|
|
8
|
+
# Ensure the base model is included in fields_data if missing
|
|
9
|
+
base_key = self.table_name.singularize.to_sym
|
|
10
|
+
fields_data[base_key] = "*" unless fields_data.key?(base_key)
|
|
11
|
+
|
|
12
|
+
# 1. Build the select clause
|
|
13
|
+
select_clause = fields_data.map do |table, cols|
|
|
14
|
+
table_name = (table.to_s == self.table_name.singularize) ? self.table_name : table.to_s.pluralize
|
|
15
|
+
|
|
16
|
+
if cols == "*"
|
|
17
|
+
"#{table_name}.*"
|
|
18
|
+
else
|
|
19
|
+
Array(cols).map { |col| "#{table_name}.#{col} AS #{table}_#{col}" }
|
|
20
|
+
end
|
|
21
|
+
end.flatten
|
|
22
|
+
|
|
23
|
+
# 2. Identify tables that need joining
|
|
24
|
+
joins_to_make = fields_data.keys.reject { |k| k.to_sym == base_key }
|
|
25
|
+
|
|
26
|
+
# 3. Apply left_joins and select
|
|
27
|
+
left_joins(joins_to_make).select(select_clause)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/better_pluck/version.rb
CHANGED
data/lib/better_pluck.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
require "better_pluck/version"
|
|
2
2
|
require "better_pluck/pluck_with_methods"
|
|
3
|
+
require "better_pluck/select_only"
|
|
3
4
|
|
|
4
5
|
module BetterPluck
|
|
5
6
|
end
|
|
6
7
|
|
|
7
8
|
ActiveSupport.on_load(:active_record) do
|
|
8
9
|
include BetterPluck::PluckWithMethods
|
|
10
|
+
include BetterPluck::SelectOnly
|
|
9
11
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: better_pluck
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- NazarK
|
|
@@ -118,6 +118,7 @@ files:
|
|
|
118
118
|
- better_pluck.gemspec
|
|
119
119
|
- lib/better_pluck.rb
|
|
120
120
|
- lib/better_pluck/pluck_with_methods.rb
|
|
121
|
+
- lib/better_pluck/select_only.rb
|
|
121
122
|
- lib/better_pluck/version.rb
|
|
122
123
|
homepage: https://github.com/NazarK/better_pluck
|
|
123
124
|
licenses:
|