better_pluck 0.9.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf2465548537590cdc1ea74f2addd0b7cf1d5503ea1456cc5cd647a8b42f2cda
4
- data.tar.gz: 82f00ef52e2042cb6fa91de2af7334321702759247f2ac37d772190d67ae3cba
3
+ metadata.gz: 64a910349a333fa7833d93fc87015f91b11344588c3b6fc3c2635b89d9a85960
4
+ data.tar.gz: 3b88f50b8e75d588849954b9d27dd46f006cdb94ee70890e4f5ed87a69f14a5d
5
5
  SHA512:
6
- metadata.gz: adea8a324626ea010c04bac42e0b9783ddacbe57b133078a5b05cab94735e6693c591e4c54405242ac73f18c7cb188bb83e2953dcaf9ec64591208138d8cb3ea
7
- data.tar.gz: 1b14db90fc98cfbd4f35df2b96842b10c5665634aa54906521c050b87f82abf08fe7663a4e100db0e20fb4b91964da986096cbdfba05c2bdbf928210dfdd694c
6
+ metadata.gz: 49329adec0a9d95bac2c321fb4dbe30b68c8c194c7bc57d4102e8ce177216ca2c9482dda07698ab90f04c5172483e8588fc81fdb13bc8d08a5b680500e67a2bb
7
+ data.tar.gz: 725142f9eaab1335c370d8da493463df52b473e155ef25c1428314b034e3b3126e39338b274d23e558b529f1ec745413179a3e9e19daf5c7de0eeb67ab59b318
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
- # BetterPluck
1
+ # better_pluck
2
2
 
3
- `BetterPluck` adds `pluck_with_methods` and `pluck_with_display_name` to ActiveRecord. These methods allow you to pluck not only database columns but also virtual methods and association data, returning lightweight `Struct` objects instead of heavy ActiveRecord instances.
3
+
4
+ Main purpose is to reduce loading into memory expensive ActiveRecord objects, where it is not needed (for read-only operations).
5
+
6
+ `better_pluck` adds `pluck_with_methods`, `pluck_with_display_name` to ActiveRecord. These methods allow you to pluck not only database columns but also virtual methods and association data, returning lightweight `Struct` objects instead of heavy ActiveRecord instances.
4
7
 
5
8
  This approach can reduce memory usage by up to 3-20x compared to loading full ActiveRecord objects.
6
9
 
@@ -67,8 +70,20 @@ articles.first.author.display_name # => "Author Name <author@example.com>"
67
70
  4. **Struct Generation**: It creates a `Struct` class (cached for performance) and injects the source code of requested instance methods into it.
68
71
  5. **Instantiation**: It maps the raw data into these `Struct` objects.
69
72
 
70
- ## Requirements
71
73
 
72
- * ActiveRecord >= 6.0
73
- * method_source
74
- * concurrent-ruby
74
+ ### select_with_joins method
75
+
76
+ You can use the `select_with_joins` method which will create field aliases for associations instead of fully fetching activerecord objects for them. The main model can be selected with specific columns or fully fetched.
77
+
78
+ ```ruby
79
+ # Example with positional arguments for base model
80
+ users = User.select_with_joins(:id, :name, organization: :name)
81
+ # SELECT users.id, users.name, organizations.name AS organization_name FROM users LEFT JOIN organizations ...
82
+ users.first.name # => "John"
83
+ users.first.organization_name # => "Acme Corp"
84
+
85
+ # Example fetching all base model columns
86
+ articles = Article.select_with_joins(author: [:name])
87
+ # SELECT articles.*, authors.name AS author_name FROM articles LEFT JOIN authors ...
88
+ articles.first.author_name # => "John Doe"
89
+ ```
@@ -0,0 +1,50 @@
1
+ module BetterPluck::SelectWithJoins
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ # extended select, allowing to write custom select queries in short format
6
+ # Article.select_with_joins(:id, :name, author: :name).first.author_name
7
+ def select_with_joins(*args)
8
+ fields_data = args.extract_options!
9
+ base_columns = args
10
+
11
+ base_key = self.table_name.singularize.to_sym
12
+
13
+ # Default to base_table.* if no base columns specified anywhere
14
+ if base_columns.empty? && !fields_data.key?(base_key)
15
+ base_columns = ["*"]
16
+ end
17
+
18
+ select_clause = []
19
+
20
+ # Handle base columns (no alias)
21
+ base_columns.each do |col|
22
+ if col.to_s == "*"
23
+ select_clause << "#{self.table_name}.*"
24
+ else
25
+ select_clause << "#{self.table_name}.#{col}"
26
+ end
27
+ end
28
+
29
+ # Handle hash columns (with alias)
30
+ fields_data.each do |table, cols|
31
+ is_base_table = (table.to_s == self.table_name.singularize)
32
+ table_name = is_base_table ? self.table_name : table.to_s.pluralize
33
+
34
+ if cols.to_s == "*"
35
+ select_clause << "#{table_name}.*"
36
+ else
37
+ Array(cols).each do |col|
38
+ select_clause << "#{table_name}.#{col} AS #{table}_#{col}"
39
+ end
40
+ end
41
+ end
42
+
43
+ # 2. Identify tables that need joining
44
+ joins_to_make = fields_data.keys.reject { |k| k.to_sym == base_key }
45
+
46
+ # 3. Apply left_joins and select
47
+ left_joins(joins_to_make).select(select_clause)
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module BetterPluck
2
- VERSION = "0.9.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/better_pluck.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require "better_pluck/version"
2
2
  require "better_pluck/pluck_with_methods"
3
- require "better_pluck/select_only"
3
+ require "better_pluck/select_with_joins"
4
4
 
5
5
  module BetterPluck
6
6
  end
7
7
 
8
8
  ActiveSupport.on_load(:active_record) do
9
9
  include BetterPluck::PluckWithMethods
10
- include BetterPluck::SelectOnly
10
+ include BetterPluck::SelectWithJoins
11
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.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - NazarK
@@ -118,7 +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
+ - lib/better_pluck/select_with_joins.rb
122
122
  - lib/better_pluck/version.rb
123
123
  homepage: https://github.com/NazarK/better_pluck
124
124
  licenses:
@@ -1,30 +0,0 @@
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