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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8917c7d3302302254ac42ec1c446b15fabb23607e4b5fd4990d178c83dcec6c
4
- data.tar.gz: 6ce82126054ac4e23c4081766d9858b0667c5cc7ad0fcf038bde152e6f2c141f
3
+ metadata.gz: bf2465548537590cdc1ea74f2addd0b7cf1d5503ea1456cc5cd647a8b42f2cda
4
+ data.tar.gz: 82f00ef52e2042cb6fa91de2af7334321702759247f2ac37d772190d67ae3cba
5
5
  SHA512:
6
- metadata.gz: cf18f8667bdf3edc65fb3db70e9677cb14190c2da0788caddf8398a5e2ca4f86ca32c99ee78e9868d24900014cb0474da11df7ba942a48210a95481deb69bd7f
7
- data.tar.gz: 3a9959d18d9c0210b9b12e97e914223158caeb1cf9451c666384eb329d9ecdc4ed508b8fbb613cf1661a6478112ab0bf98f5a2e103412e9659f39847e2356873
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
@@ -1,3 +1,3 @@
1
1
  module BetterPluck
2
- VERSION = "0.8.3"
2
+ VERSION = "0.9.0"
3
3
  end
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.8.3
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: