Skip to content

Commit

Permalink
Change options default to be an empty hash
Browse files Browse the repository at this point in the history
  • Loading branch information
aliismayilov committed Feb 23, 2019
1 parent 8fd0c2a commit c72fdfa
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

module ActiveModel::Validations::HelperMethods
# Strips whitespace from model fields and converts blank values to nil.
def strip_attributes(options = nil)
def strip_attributes(options = {})
StripAttributes.validate_options(options)

before_validation(options && options.slice(:if, :unless) || {}) do |record|
before_validation(options.slice(:if, :unless)) do |record|
StripAttributes.strip(record, options)
end
end

# <b>DEPRECATED:</b> Please use <tt>strip_attributes</tt> (non-bang method)
# instead.
def strip_attributes!(options = nil)
def strip_attributes!(options = {})
warn "[DEPRECATION] `strip_attributes!` is deprecated. Please use `strip_attributes` (non-bang method) instead."
strip_attributes(options)
end
Expand All @@ -37,15 +37,15 @@ module StripAttributes
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/
MULTIBYTE_SUPPORTED = "\u0020" == " "

def self.strip(record_or_string, options = nil)
def self.strip(record_or_string, options = {})
if record_or_string.respond_to?(:attributes)
strip_record(record_or_string, options)
else
strip_string(record_or_string, options)
end
end

def self.strip_record(record, options = nil)
def self.strip_record(record, options = {})
attributes = narrow(record.attributes, options)

attributes.each do |attr, value|
Expand All @@ -57,13 +57,11 @@ def self.strip_record(record, options = nil)
record
end

def self.strip_string(value, options = nil)
if options
allow_empty = options[:allow_empty]
collapse_spaces = options[:collapse_spaces]
replace_newlines = options[:replace_newlines]
regex = options[:regex]
end
def self.strip_string(value, options = {})
allow_empty = options[:allow_empty]
collapse_spaces = options[:collapse_spaces]
replace_newlines = options[:replace_newlines]
regex = options[:regex]

if value.respond_to?(:strip)
value = (value.blank? && !allow_empty) ? nil : value.strip
Expand Down Expand Up @@ -97,10 +95,10 @@ def self.strip_string(value, options = nil)
# Necessary because Rails has removed the narrowing of attributes using :only
# and :except on Base#attributes
def self.narrow(attributes, options = {})
if except = options && options[:except]
if except = options[:except]
except = Array(except).collect { |attribute| attribute.to_s }
attributes.except(*except)
elsif only = options && options[:only]
elsif only = options[:only]
only = Array(only).collect { |attribute| attribute.to_s }
attributes.slice(*only)
else
Expand All @@ -109,7 +107,7 @@ def self.narrow(attributes, options = {})
end

def self.validate_options(options)
if keys = options && options.keys
if keys = options.keys
unless (keys - VALID_OPTIONS).empty?
raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
end
Expand Down

0 comments on commit c72fdfa

Please sign in to comment.