Skip to content

Commit

Permalink
normalize license to remove blank strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Jan 30, 2024
1 parent 5fa3171 commit 4743562
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Model < ApplicationRecord
before_validation :strip_separators_from_path, if: :path_changed?
before_validation :slugify_name, if: :name_changed?

before_validation :normalize_license
# In Rails 7.1 we will be able to do this instead:
# normalizes :license, with: -> license { license.blank? ? nil : license }

attr_reader :organize
def organize=(value)
@organize = ActiveRecord::Type::Boolean.new.cast(value)
Expand Down Expand Up @@ -105,6 +109,10 @@ def new?

private

def normalize_license
self.license = nil if license.blank?
end

def strip_separators_from_path
self.path = path&.trim_path_separators
end
Expand Down
6 changes: 6 additions & 0 deletions spec/models/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
m.license = nil
expect(m).to be_valid
end

it "normalizes blank licenses to nil" do
m = build(:model, license: "")
m.validate
expect(m.license).to be_nil
end
end

it "strips leading and trailing separators from paths" do
Expand Down

0 comments on commit 4743562

Please sign in to comment.