summaryrefslogtreecommitdiff
path: root/app/models/concerns/nullify_if_blank.rb
blob: 5a5cc51509bfad0a628997dd78d1df7d44beafc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

# Helper that sets attributes to nil prior to validation if they
# are blank (are false, empty or contain only whitespace), to avoid
# unnecessarily persisting empty strings.
#
# Model usage:
#
#   class User < ApplicationRecord
#     include NullifyIfBlank
#
#     nullify_if_blank :name, :email
#   end
#
#
# Test usage:
#
#   RSpec.describe User do
#     it { is_expected.to nullify_if_blank(:name) }
#     it { is_expected.to nullify_if_blank(:email) }
#   end
#
module NullifyIfBlank
  extend ActiveSupport::Concern

  class_methods do
    def nullify_if_blank(*attributes)
      self.attributes_to_nullify += attributes
    end
  end

  included do
    class_attribute :attributes_to_nullify,
      instance_accessor: false,
      instance_predicate: false,
      default: Set.new

    before_validation :nullify_blank_attributes
  end

  private

  def nullify_blank_attributes
    self.class.attributes_to_nullify.each do |attribute|
      assign_attributes(attribute => nil) if read_attribute(attribute).blank?
    end
  end
end