activerecord_lazy_columns 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3001f101fd41aa75d33964605b7d0c3b2f9e7376390c298da0a078ec31400d3
4
+ data.tar.gz: 11d35e89fbef2f2667e4ad14e24dabdedab8c5bcea576627d38a505a4a5c551e
5
+ SHA512:
6
+ metadata.gz: 8dc46a9789ff339e72480b738abb34e8b510517a7b5210d9e9a9d44bc3b5e2b8f9e8b89d877b33e414d560787f89c29ae3f76d964232158a1ad553ee8a0bd6c0
7
+ data.tar.gz: c9e2f9cf53ffee1be178750965d49b819d4df5804328b49078b6d9c66050323523e8ab071fcda0da2256880086d1fad4ad5e343ef923a521a4223ddfa9d9868b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## master (unreleased)
2
+
3
+ ## 0.1.0 (2025-09-16)
4
+
5
+ - First release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025- fatkodima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # ActiverecordLazyColumns
2
+
3
+ `activerecord_lazy_columns` is a gem that lets you specify columns to be loaded lazily in your Active Record models.
4
+
5
+ By default, Active Record loads all the columns in each model instance. This gem lets you specify columns
6
+ to be excluded by default. This is useful to reduce the memory taken by Active Record when loading records
7
+ that have large columns if those particular columns are actually not required most of the time.
8
+ In this situation it can also greatly reduce the database query time because loading large BLOB/TEXT columns
9
+ generally means seeking other database pages since they are not stored wholly in the record's page itself.
10
+
11
+ Notice that a better approach can be moving those columns to new models, since Active Record loads related models
12
+ lazily by default. This gem is an easy workaround.
13
+
14
+ ## Requirements
15
+
16
+ - ruby 3.2+
17
+ - activerecord 7.2+
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem "activerecord_lazy_columns"
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Use `lazy_columns` in your Active Record models to define which columns should be loaded lazily:
30
+
31
+ ```ruby
32
+ class Action < ApplicationRecord
33
+ lazy_columns :comments
34
+ end
35
+ ```
36
+
37
+ Now, when you fetch some action, the comments are not loaded:
38
+
39
+ ```ruby
40
+ Action.create!(title: "Some action", comments: "Some comments") # => <Action id: 1...>
41
+ action = Action.find(1) # => <Action id: 1, title: "Some action">
42
+ ```
43
+
44
+ And if you try to read the `comments` attribute, it will be loaded into the model:
45
+
46
+ ```ruby
47
+ action.comments # => "Some comments"
48
+ action # => <Action id: 1, title: "Some action", comments: "Some comments"
49
+ ```
50
+
51
+ ## How the gem works
52
+
53
+ This gem does two things:
54
+
55
+ - Modifies the `default_scope` of the model so that it fetches all the attributes except the ones marked as lazy.
56
+ - Defines a reader method per lazy attribute that will load the corresponding column under demand.
57
+
58
+ ### Eager loading of attributes defined as lazy
59
+
60
+ The first time you access a lazy attribute, a new database query will be executed to load it. If you are going
61
+ to operate on a number of objects and want to have the lazy attributes eagerly loaded, use Active Record's
62
+ `.select()` in the initial query. For example:
63
+
64
+ ```ruby
65
+ Action.select(:comments)
66
+ ```
67
+
68
+ ## Credits
69
+
70
+ Thanks to the [`lazy_columns` gem](https://github.com/jorgemanrubia/lazy_columns) for the original idea.
71
+
72
+ ## Development
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update
75
+ the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag
76
+ for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/activerecord_lazy_columns.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordLazyColumns
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ require_relative "activerecord_lazy_columns/version"
6
+
7
+ module ActiveRecordLazyColumns
8
+ # Extension for Active Record to add `lazy_columns` method with an ability
9
+ # to lazily load specific columns.
10
+ #
11
+ module ModelExtension
12
+ attr_writer :lazy_loaded_columns
13
+
14
+ def lazy_loaded_columns
15
+ @lazy_loaded_columns || []
16
+ end
17
+
18
+ # Declare some columns to be loaded lazily.
19
+ #
20
+ # @example
21
+ # class Action < ApplicationRecord
22
+ # lazy_columns :comments
23
+ # end
24
+ #
25
+ def lazy_columns(*columns)
26
+ columns = columns.map(&:to_s)
27
+ self.lazy_loaded_columns = columns
28
+
29
+ default_scope { select(column_names - columns) }
30
+ columns.each { |column| define_lazy_reader_for(column) }
31
+ end
32
+
33
+ private
34
+ def define_lazy_reader_for(column)
35
+ define_method(column) do
36
+ unless has_attribute?(column)
37
+ value = self.class.unscoped.where(id: [id]).pick(column)
38
+ write_attribute(column, value)
39
+ clear_attribute_changes([column])
40
+ end
41
+
42
+ read_attribute(column)
43
+ end
44
+ end
45
+ end
46
+
47
+ # @private
48
+ module RelationExtension
49
+ def count(column_name = nil)
50
+ if column_name.nil? && klass.lazy_loaded_columns.any?
51
+ column_name = :all
52
+ end
53
+
54
+ super
55
+ end
56
+ end
57
+ end
58
+
59
+ ActiveSupport.on_load(:active_record) do
60
+ extend ActiveRecordLazyColumns::ModelExtension
61
+
62
+ ActiveRecord::Relation.prepend(ActiveRecordLazyColumns::RelationExtension)
63
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_lazy_columns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fatkodima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-09-16 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: '7.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.2'
27
+ description:
28
+ email:
29
+ - fatkodima123@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/activerecord_lazy_columns.rb
38
+ - lib/activerecord_lazy_columns/version.rb
39
+ homepage: https://github.com/fatkodima/activerecord_lazy_columns
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/fatkodima/activerecord_lazy_columns
44
+ source_code_uri: https://github.com/fatkodima/activerecord_lazy_columns
45
+ changelog_uri: https://github.com/fatkodima/activerecord_lazy_columns/blob/master/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.4.19
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Adds support for lazy-loading columns in Active Record models
65
+ test_files: []