Skip to content

Commit

Permalink
Merge c697b8a into fce93b1
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweinmann committed Mar 28, 2020
2 parents fce93b1 + c697b8a commit 57119db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Simply add schema_validations to your Gemfile.
```ruby
gem "schema_validations"
```

## Which validations are covered?

Constraints:
Expand Down Expand Up @@ -88,7 +88,7 @@ SchemaValidations.setup do |config|
# Whether to automatically create validations based on database constraints.
# (Can be set false globally to disable the gem by default, and set true per-model to enable.)
config.auto_create = true

# Restricts the set of field names to include in automatic validation.
# Value is a single name, an array of names, or nil.
config.only = nil
Expand All @@ -97,26 +97,29 @@ SchemaValidations.setup do |config|
# Value is a single type, an array of types, or nil.
# A type is specified as, e.g., `:validates_presence_of` or simply `:presence`.
config.only_type = nil

# A list of field names to exclude from automatic validation.
# Value is a single name, an array of names, or nil.
# (Providing a value per-model will completely replace a globally-configured list)
config.except = nil

# A list of validation types to exclude from automatic validation.
# Value is a single type, an array of types, or nil.
# (Providing a value per-model will completely replace a globally-configured list)
config.except_type = nil

# The base set of field names to always exclude from automatic validation.
# Value is a single name, an array of names, or nil.
# (This whitelist applies after all other considerations, global or per-model)
config.whitelist = [:created_at, :updated_at, :created_on, :updated_on]

# The base set of validation types to always exclude from automatic validation.
# Value is a single type, an array of types, or nil.
# (This whitelist applies after all other considerations, global or per-model)
config.whitelist_type = nil

# Whether to check if the column has changed before validating uniqueness.
config.uniqueness_if_changed = true
end
```

Expand Down
7 changes: 7 additions & 0 deletions lib/schema_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ class Config < Valuable
# A type is specified as, e.g., +:validates_presence_of+ or simply +:presence+.
has_value :only_type, :default => nil

##
# :attr_accessor: uniqueness_if_changed
#
# Whether to check if the column has changed before validating uniqueness.
# Boolean, default is +true+.
has_value :uniqueness_if_changed, :klass => :boolean, :default => true

def dup #:nodoc:
self.class.new(Hash[attributes.collect{ |key, val| [key, Valuable === val ? val.class.new(val.attributes) : val] }])
end
Expand Down
16 changes: 9 additions & 7 deletions lib/schema_validations/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ def add_uniqueness_validation(column) #:nodoc:
options[:scope] = scope if scope.any?
options[:allow_nil] = true
options[:case_sensitive] = false if has_case_insensitive_index?(column, scope)
options[:if] = (proc do |record|
if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
record.public_send(:"#{column.name}_changed?")
else
false
end
end)
if schema_validations_config.uniqueness_if_changed
options[:if] = (proc do |record|
if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
record.public_send(:"#{column.name}_changed?")
else
false
end
end)
end

validate_logged :validates_uniqueness_of, name, options
end
Expand Down

0 comments on commit 57119db

Please sign in to comment.