passive_columns 0.2.1 → 0.2.3

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: 56f6f5fdf5c333ca2fe7ba55e3974b21b67662362a21848b1b31cf6216e080a1
4
- data.tar.gz: adbcac3dfba991402e20dc7fc3d2a616498af1998ae4b0d5b4a1a9895254b2c4
3
+ metadata.gz: 4c17e42bdf1a6871eb67811732872bfb4d4c280c91d2b6f4a76442efbf1379f5
4
+ data.tar.gz: dd7937cad9f31dabf925cd15dbf051b06682483bbfeb884a713c113281bce613
5
5
  SHA512:
6
- metadata.gz: 9ca11045bc3d47d290e6797c3b834eb4f1d8770ffd955c3be392b51c5d796bd2049c2ae7c849e85f189ce4e91524928cad8a5d55463fc36c082bc54c74c47aa8
7
- data.tar.gz: df054d53f99a0e292292fe6935ba4dc3e2d707856372043468de0afc121065d9ca515ffb528d7292aba9a5b7f378114ac55b7d8b7e640268ceae897351c18cb1
6
+ metadata.gz: 8c40fdcc4543ea47e4e945819a3e03328203d7157f17c0b2170cddadf45024b5dbafe7e6bf5fa8045678cf4f63b28b667f63fe49f3217ff9abffd9b2c0c9a4ef
7
+ data.tar.gz: 8d13c82243de48de75116e986ba12c96949c5ad9151801dcecc9fd2da0f6f6d8ecef5cad7c23125e98571c77af582771ed9e2cbba43d63a293fdbc4f030d8fb1
data/README.md CHANGED
@@ -66,6 +66,43 @@ user.name
66
66
  By the way, it uses the Rails' `.pick` method to get the value of the column under the hood
67
67
 
68
68
 
69
+ ### Important
70
+
71
+ If you want `passive_columns` to skip validation rules specific to the columns you exclude.<br>
72
+ (in case they were not retrieved / modified)
73
+ ```ruby
74
+ validates :huge_article, presence: true
75
+ # Will be transformed into:
76
+ # validates :huge_article, presence: true, if: -> { attributes.key?('huge_article') }
77
+ ```
78
+
79
+ You must declare validation rules for `passive_columns` separately
80
+ ```ruby
81
+ class Page < ActiveRecord::Base
82
+ include PassiveColumns
83
+ passive_columns :huge_article # Declare columns above the validation rules.
84
+
85
+ validates :name, presence: true
86
+ # Validation rules transformation will work
87
+ validates :huge_article, presence: true # It works for a separate rule.
88
+ # -> the rule is transformed into:
89
+ # -> validates :huge_article, presence: true, if: -> { attributes.key?('huge_article') }
90
+ end
91
+ ```
92
+
93
+ ```ruby
94
+ class Page < ActiveRecord::Base
95
+ include PassiveColumns
96
+ passive_columns :huge_article # Declare columns above the validation rules.
97
+
98
+ # Validation rules transformation WON'T work
99
+ validates :name, :huge_article, presence: true # It doesn't work for combined rules.
100
+ # -> the rule remains the same:
101
+ # -> validates :name, :huge_article, presence: true
102
+ end
103
+ ```
104
+
105
+
69
106
  ## Installation
70
107
  Add this line to your Gemfile:
71
108
 
@@ -123,13 +160,19 @@ One way to avoid this is to check for the presence of the attribute before valid
123
160
  validates :huge_article, presence: true, if: -> { attributes.key?('huge_article') }
124
161
  ```
125
162
 
126
- Unfortunately, boilerplate code is needed for such a simple task.
127
- You just wanted to exclude some columns and be able to manipulate a model without extra steps.
163
+ Unfortunately, boilerplate code is needed for such a simple task. <br>
164
+ But the only thing you wanted was to exclude some columns and be able to manipulate a model without extra steps.
165
+
166
+ By the way, after doing those steps, you still cannot retrieve the column when you need it after loading the scoped model...
128
167
 
129
- `passive_columns` tries to solve this problem by allowing you to exclude columns from the selection
130
- and also allows you to retrieve them on demand when needed.
168
+ So, `passive_columns` tries to solve this problem by allowing you to exclude columns from the selection and also allowing you to retrieve them on demand when needed.
131
169
 
170
+ ---
132
171
 
172
+ #### Inspiration
173
+ There are similar gems that were relatively popular but are no longer supported. Let's give them the honor they deserve:
174
+ - [lazy_columns](https://github.com/jorgemanrubia/lazy_columns)
175
+ - [columns_on_demand](https://github.com/willbryant/columns_on_demand)
133
176
 
134
177
  ## License
135
178
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -26,7 +26,7 @@ module PassiveColumns
26
26
  value = pick_value(column)
27
27
  model[column] = value
28
28
  model.send(:clear_attribute_change, column)
29
- value
29
+ model[column]
30
30
  end
31
31
 
32
32
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PassiveColumns
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.3'
5
5
  end
@@ -83,9 +83,13 @@ module PassiveColumns
83
83
  # Otherwise, the passive column will be retrieved from DB before validation itself.
84
84
  def set_callback(name, *filter_list, &block)
85
85
  opts = filter_list.extract_options!
86
- if name == :validate && opts[:attributes]&.one?
87
- passive_column = opts[:attributes].map(&:to_s) & _passive_columns
88
- opts[:if] = ([-> { attributes.key?(passive_column.first) }] + Array(opts[:if])) if passive_column.present?
86
+
87
+ if name == :validate
88
+ attrs = opts[:attributes] || filter_list[0].try(:attributes)
89
+ if attrs&.one?
90
+ passive_column = attrs.map(&:to_s) & _passive_columns
91
+ opts[:if] = ([-> { attributes.key?(passive_column.first) }] + Array(opts[:if])) if passive_column.present?
92
+ end
89
93
  end
90
94
  super(name, *filter_list, opts, &block)
91
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Golovin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-19 00:00:00.000000000 Z
11
+ date: 2024-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord