Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work with ActiveRecord::Store #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.3
2.7.2
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: ruby
rvm:
- 2.5.3
before_install: gem install bundler -v 1.16.1
- 2.7.2
before_install: gem install bundler -v 2.2.17
script:
- bundle exec rake
- bundle exec rake
59 changes: 29 additions & 30 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,50 @@ PATH
remote: .
specs:
nullifyable (0.1.0)
activerecord (>= 4.0)
activerecord (>= 6.1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fesilvajacquier Why do we need to change this dependency? I'm OK upgrading dev dependencies, but is there a reason to drop support for rails 5.x?


GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.2)
activesupport (= 5.2.2)
activerecord (5.2.2)
activemodel (= 5.2.2)
activesupport (= 5.2.2)
arel (>= 9.0)
activesupport (5.2.2)
activemodel (6.1.3.2)
activesupport (= 6.1.3.2)
activerecord (6.1.3.2)
activemodel (= 6.1.3.2)
activesupport (= 6.1.3.2)
activesupport (6.1.3.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ansi (1.5.0)
arel (9.0.0)
builder (3.2.3)
concurrent-ruby (1.1.4)
i18n (1.3.0)
builder (3.2.4)
concurrent-ruby (1.1.8)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
minitest-reporters (1.3.5)
minitest (5.14.4)
minitest-reporters (1.4.3)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
rake (10.5.0)
ruby-progressbar (1.10.0)
sqlite3 (1.3.13)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
rake (13.0.3)
ruby-progressbar (1.11.0)
sqlite3 (1.4.2)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
bundler (~> 1.16)
minitest (~> 5.0)
minitest-reporters (~> 1.3)
bundler (~> 2.2)
minitest (~> 5.14)
minitest-reporters (~> 1.4)
nullifyable!
rake (~> 10.0)
sqlite3 (~> 1.3)
rake (~> 13.0)
sqlite3 (~> 1.4)

BUNDLED WITH
1.16.1
2.2.17
9 changes: 6 additions & 3 deletions lib/nullifyable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Nullifyable

module ClassMethods
attr_reader :nullifyable_attributes

private

def nullify(*attributes)
Expand All @@ -19,9 +19,12 @@ def nullify(*attributes)
end

private

def nullify_blank_fields
return if self.class.nullifyable_attributes.blank?

self.class.nullifyable_attributes.each do |attr|
self[attr] = nil if self[attr].blank?
end if self.class.nullifyable_attributes.present?
send("#{attr}=", nil) if send(attr).blank?
end
end
end
12 changes: 6 additions & 6 deletions nullifyable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activerecord", ">= 4.0"
spec.add_dependency "activerecord", ">= 6.1"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency 'minitest-reporters', '~> 1.3'
spec.add_development_dependency 'sqlite3', '~> 1.3'
spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "minitest", "~> 5.14"
spec.add_development_dependency 'minitest-reporters', '~> 1.4'
spec.add_development_dependency 'sqlite3', '~> 1.4'
end
15 changes: 15 additions & 0 deletions test/nullifyable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@ def test_it_nullify_blank_string
assert_nil user.first_name
end

def test_it_nullify_blank_string_for_ar_store
user = User.create!(email: "[email protected]", external_id: "")
assert_nil user.external_id
end

def test_it_nullify_space_string
user = User.create!(email: "[email protected]", first_name: " ")
assert_nil user.first_name
end

def test_it_nullify_space_string_for_ar_store
user = User.create!(email: "[email protected]", external_id: " ")
assert_nil user.external_id
end

def test_it_does_not_nullify_zero
user = User.create!(email: "[email protected]", first_name: 0)
assert_equal "0", user.first_name
end

def test_it_does_not_nullify_zero_for_ar_store
user = User.create!(email: "[email protected]", external_id: 0)
assert_equal 0, user.external_id
end

def test_it_nullify_multiple_attributes
user = User.create!(email: "[email protected]", first_name: "", last_name: "")
assert_nil user.first_name
Expand Down
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

class User < ActiveRecord::Base
include Nullifyable
nullify :first_name, :last_name
nullify :first_name, :last_name, :external_id

store_accessor :metadata, :external_id
end

def setup_db
Expand All @@ -24,6 +26,7 @@ def setup_db
t.string :email, :limit => 255, :null => false
t.string :first_name, :limit => 100, :null => true
t.string :last_name, :limit => 100, :null => true
t.json :metadata, default: {}, null: false
end
end
end
Expand Down