-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module SetterOverrideMixin | ||
|
||
#this allows multiple overrides of the setter method | ||
#to be safely combined by calling the new implementation | ||
#and passing the result to the old one | ||
# | ||
#pass in the column to override, a unique name for your override, | ||
#and a block containing the normalization logic for the value | ||
def safe_override_setter(col, mixin_name, &block) | ||
setter_name = "#{col}=" | ||
|
||
if method_defined?(setter_name.to_sym) | ||
prev_setter_name = "#{mixin_name}_#{col}=" | ||
alias_method prev_setter_name, setter_name | ||
|
||
define_method setter_name do |val| | ||
normalized_val = yield val | ||
|
||
self.send(prev_setter_name, normalized_val) | ||
end | ||
|
||
else | ||
define_method setter_name do |val| | ||
normalized_val = yield val | ||
super(normalized_val) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module WithNulledBlanks | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
include SetterOverrideMixin | ||
def columns_with_nulled_blanks(*cols) | ||
cols.each do |col| | ||
safe_override_setter(col, 'nulled_blanks') do |val| | ||
if val.blank? | ||
nil | ||
else | ||
val | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'rails_helper' | ||
|
||
#Ex: Variant stop should both remove commas before casting | ||
#and save empty values as null | ||
describe 'SetterOverrideMixin' do | ||
|
||
it 'should make whitespace or empty string NULL before saving to the db' do | ||
variant = Fabricate(:variant) | ||
|
||
variant.start = "" | ||
variant.save | ||
expect(variant.start).to be_nil | ||
|
||
variant.start = " " | ||
variant.save | ||
expect(variant.start).to be_nil | ||
|
||
end | ||
|
||
it 'should also remove commas before casting to an int' do | ||
variant = Fabricate(:variant) | ||
variant.start = "1,000" | ||
variant.save | ||
expect(variant.start).to eq(1000) | ||
end | ||
|
||
it 'should preserve the value otherwise' do | ||
variant = Fabricate(:variant) | ||
|
||
variant.start = 1000 | ||
variant.save | ||
expect(variant.start).to eq(1000) | ||
|
||
variant.start = "200" | ||
variant.save | ||
expect(variant.start).to eq(200) | ||
end | ||
|
||
it 'should work with suggested changes' do | ||
variant = Fabricate(:variant, reference_bases: "ABCD") | ||
user = Fabricate(:user) | ||
applying_user = Fabricate(:user) | ||
org = Fabricate(:organization) | ||
|
||
variant.start = "1,000" | ||
change = variant.suggest_change!(user, {}, {}) | ||
change.apply(applying_user, org, false) | ||
variant.reload | ||
|
||
expect(variant.start).to eq(1000) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'rails_helper' | ||
|
||
describe 'WithNulledBlanks' do | ||
|
||
it 'should define a new class method on the model its mixed into' do | ||
expect(Variant.methods.include?(:columns_with_nulled_blanks)).to be(true) | ||
end | ||
|
||
it 'should make whitespace or empty string NULL before saving to the db' do | ||
variant = Fabricate(:variant) | ||
|
||
variant.reference_bases = "" | ||
variant.save | ||
expect(variant.reference_bases).to be_nil | ||
|
||
variant.reference_bases = " " | ||
variant.save | ||
expect(variant.reference_bases).to be_nil | ||
end | ||
|
||
it 'should preserve the value otherwise' do | ||
variant = Fabricate(:variant) | ||
|
||
variant.reference_bases = "ABC" | ||
variant.save | ||
expect(variant.reference_bases).to eq("ABC") | ||
end | ||
|
||
it 'should work with suggested changes' do | ||
variant = Fabricate(:variant, reference_bases: "ABCD") | ||
user = Fabricate(:user) | ||
applying_user = Fabricate(:user) | ||
org = Fabricate(:organization) | ||
|
||
variant.reference_bases = "" | ||
change = variant.suggest_change!(user, {}, {}) | ||
change.apply(applying_user, org, false) | ||
variant.reload | ||
|
||
expect(variant.reference_bases).to be_nil | ||
end | ||
end |