legacy_column 0.2.0 → 0.3.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/Gemfile.lock +1 -1
- data/README.md +47 -4
- data/lib/legacy_column/version.rb +1 -1
- data/lib/legacy_column.rb +26 -1
- data/spec/db/schema.rb +17 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/legacy_column_spec.rb +120 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a09851d6a69c5fffa341bbcb0fae4b937903c1b26e2a8d36bb9f4a8b2ca9201a
|
4
|
+
data.tar.gz: d458748a4dc483d16d11e8c6f02f4a794b5cc08bfad3f36f42bbba375816f91f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf307d3a3da65ed7441173135cce638179e810b12c72b791aea18eeb93248740aba8aaf9dcf8e16a7bf3d2af82390f2ef189d6e01b096710a5bb7176a0c65056
|
7
|
+
data.tar.gz: 4b0427368bbf8969f7eadbe243307d71a4306381af8f5027b1ab0b5ba4fd03969375f3d3e324722aabe81ab7cd2bfd88989571634f28f63abf0955f3fa77f008
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -26,6 +26,8 @@ This gem supports:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
+
### Basic Usage
|
30
|
+
|
29
31
|
Just list the column names:
|
30
32
|
```ruby
|
31
33
|
legacy_column :old_email, :old_phone_number
|
@@ -36,20 +38,61 @@ A custom message can be added:
|
|
36
38
|
legacy_column :old_email, :old_phone_number, message: 'Do not touch this!!!'
|
37
39
|
```
|
38
40
|
|
39
|
-
|
41
|
+
### Read Access Detection
|
42
|
+
|
43
|
+
By default, the gem only detects write operations (when columns are modified). To also detect read access, use the `detect_reads` option:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
legacy_column :old_email, :old_phone_number, detect_reads: true
|
47
|
+
```
|
48
|
+
|
49
|
+
This will warn whenever the legacy columns are accessed:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
legacy_column :old_status, message: 'This field is deprecated!', detect_reads: true
|
53
|
+
|
54
|
+
user.old_status # Will log a warning about read access
|
55
|
+
user.old_status = 'active' # Will log a warning about write access
|
56
|
+
```
|
57
|
+
|
58
|
+
### Combined Usage
|
40
59
|
|
60
|
+
You can combine read detection with custom messages:
|
61
|
+
```ruby
|
62
|
+
legacy_column :old_price, :old_name,
|
63
|
+
message: 'These fields will be removed in v2.0!',
|
64
|
+
detect_reads: true
|
65
|
+
```
|
66
|
+
|
67
|
+
## Output Examples
|
68
|
+
|
69
|
+
### Write Detection (Default)
|
41
70
|
```
|
42
|
-
|
71
|
+
USE of legacy column detected.
|
43
72
|
User => old_email
|
44
73
|
Do not touch this!!!
|
45
74
|
```
|
46
75
|
|
47
|
-
|
76
|
+
### Read Detection (When enabled)
|
77
|
+
```
|
78
|
+
READ of legacy column detected.
|
79
|
+
User => old_email
|
80
|
+
Do not touch this!!!
|
81
|
+
```
|
82
|
+
|
83
|
+
## Logging
|
48
84
|
|
49
85
|
The gem will:
|
50
|
-
- Log warnings to `Rails.logger` when Rails is available
|
86
|
+
- Log warnings to `Rails.logger` when Rails is available
|
51
87
|
- Fall back to `puts` for non-Rails environments or when Rails.logger is not available
|
52
88
|
|
89
|
+
## Options
|
90
|
+
|
91
|
+
| Option | Default | Description |
|
92
|
+
|--------|---------|-------------|
|
93
|
+
| `message` | `"This column is set as legacy and should not be used anymore."` | Custom warning message |
|
94
|
+
| `detect_reads` | `false` | Enable read access detection |
|
95
|
+
|
53
96
|
## Contributing
|
54
97
|
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
55
98
|
|
data/lib/legacy_column.rb
CHANGED
@@ -10,18 +10,43 @@ module LegacyColumn
|
|
10
10
|
included do
|
11
11
|
class_attribute :legacy_column_names, instance_writer: false, default: nil
|
12
12
|
class_attribute :legacy_column_message, instance_writer: false, default: nil
|
13
|
+
class_attribute :legacy_column_detect_reads, instance_writer: false, default: false
|
13
14
|
end
|
14
15
|
|
15
16
|
module ClassMethods
|
16
17
|
DEFAULT_MESSAGE = 'This column is set as legacy and should not be used anymore.'
|
17
18
|
|
18
|
-
def legacy_column(*columns, message: nil)
|
19
|
+
def legacy_column(*columns, message: nil, detect_reads: false)
|
19
20
|
return unless columns
|
20
21
|
|
21
22
|
self.legacy_column_names = columns
|
22
23
|
self.legacy_column_message = message || DEFAULT_MESSAGE
|
24
|
+
self.legacy_column_detect_reads = detect_reads
|
23
25
|
|
24
26
|
send('before_validation', :legacy_column)
|
27
|
+
|
28
|
+
if detect_reads
|
29
|
+
setup_read_detection(columns)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def setup_read_detection(columns)
|
36
|
+
mod = Module.new do
|
37
|
+
columns.each do |column|
|
38
|
+
define_method(column) do
|
39
|
+
# Log read access
|
40
|
+
if defined?(Rails) && Rails.logger
|
41
|
+
Rails.logger.warn "\n\nREAD of legacy column detected.\n #{self.class} => #{column}\n #{legacy_column_message}\n\n"
|
42
|
+
else
|
43
|
+
puts "\n\nREAD of legacy column detected.\n #{self.class} => #{column}\n #{legacy_column_message}\n\n"
|
44
|
+
end
|
45
|
+
super() # Call original getter
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
prepend(mod)
|
25
50
|
end
|
26
51
|
end
|
27
52
|
|
data/spec/db/schema.rb
CHANGED
@@ -6,4 +6,21 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
6
6
|
t.string :old_email
|
7
7
|
t.string :old_phone_number
|
8
8
|
end
|
9
|
+
|
10
|
+
create_table :products, :force => true do |t|
|
11
|
+
t.decimal :old_price
|
12
|
+
t.string :old_name
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :items, :force => true do |t|
|
16
|
+
t.string :old_status
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :orders, :force => true do |t|
|
20
|
+
t.decimal :old_total
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :accounts, :force => true do |t|
|
24
|
+
t.decimal :old_balance
|
25
|
+
end
|
9
26
|
end
|
data/spec/db/test.sqlite3
CHANGED
Binary file
|
data/spec/legacy_column_spec.rb
CHANGED
@@ -50,4 +50,124 @@ RSpec.describe LegacyColumn do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
54
|
+
context 'read detection enabled' do
|
55
|
+
before do
|
56
|
+
class Product < ActiveRecord::Base
|
57
|
+
legacy_column :old_price, :old_name, detect_reads: true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should recognize detect_reads setting' do
|
62
|
+
expect(Product.legacy_column_detect_reads).to eq(true)
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'read access detection' do
|
66
|
+
let(:product) do
|
67
|
+
# Create product without triggering write detection by using update_attribute
|
68
|
+
p = Product.create
|
69
|
+
p.update_attribute(:old_price, 100)
|
70
|
+
p.update_attribute(:old_name, 'Test Product')
|
71
|
+
p
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'detects read access to legacy columns' do
|
75
|
+
expect {
|
76
|
+
product.old_price
|
77
|
+
}.to output(/READ of legacy column detected.*Product => old_price.*This column is set as legacy and should not be used anymore/m).to_stdout
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'detects read access to multiple legacy columns' do
|
81
|
+
expect {
|
82
|
+
product.old_name
|
83
|
+
}.to output(/READ of legacy column detected.*Product => old_name.*This column is set as legacy and should not be used anymore/m).to_stdout
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'still returns the actual attribute value' do
|
87
|
+
# Capture output but test return value
|
88
|
+
value = nil
|
89
|
+
expect {
|
90
|
+
value = product.old_price
|
91
|
+
}.to output(/READ of legacy column detected/).to_stdout
|
92
|
+
expect(value).to eq(100)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'read detection with custom message' do
|
98
|
+
before do
|
99
|
+
class Item < ActiveRecord::Base
|
100
|
+
legacy_column :old_status, message: 'Stop reading this!', detect_reads: true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'read access detection with custom message' do
|
105
|
+
let(:item) do
|
106
|
+
i = Item.create
|
107
|
+
i.update_attribute(:old_status, 'active')
|
108
|
+
i
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'shows custom message for read access' do
|
112
|
+
expect {
|
113
|
+
item.old_status
|
114
|
+
}.to output(/READ of legacy column detected.*Item => old_status.*Stop reading this!/m).to_stdout
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'read detection disabled (default)' do
|
120
|
+
before do
|
121
|
+
class Order < ActiveRecord::Base
|
122
|
+
legacy_column :old_total # detect_reads defaults to false
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should not detect reads when disabled' do
|
127
|
+
expect(Order.legacy_column_detect_reads).to eq(false)
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'no read access detection' do
|
131
|
+
let(:order) do
|
132
|
+
o = Order.create
|
133
|
+
o.update_attribute(:old_total, 50)
|
134
|
+
o
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'does not detect read access when disabled' do
|
138
|
+
expect {
|
139
|
+
order.old_total
|
140
|
+
}.to_not output.to_stdout
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'still returns the actual attribute value' do
|
144
|
+
expect(order.old_total).to eq(50)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'combined read and write detection' do
|
150
|
+
before do
|
151
|
+
class Account < ActiveRecord::Base
|
152
|
+
legacy_column :old_balance, message: 'Legacy account field!', detect_reads: true
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe 'both read and write detection' do
|
157
|
+
let(:account) { Account.create }
|
158
|
+
|
159
|
+
it 'detects write access' do
|
160
|
+
expect {
|
161
|
+
account.update(old_balance: 1000)
|
162
|
+
}.to output("\n\nUSE of legacy column detected.\n Account => old_balance\n Legacy account field!\n\n").to_stdout
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'detects read access' do
|
166
|
+
account.update_attribute(:old_balance, 1000) # Set without triggering write detection
|
167
|
+
expect {
|
168
|
+
account.old_balance
|
169
|
+
}.to output(/READ of legacy column detected.*Account => old_balance.*Legacy account field!/m).to_stdout
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
53
173
|
end
|