-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from anthonator/feature/equals
Overrode ==
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,9 @@ def i18n_scope | |
def persisted? | ||
false | ||
end | ||
|
||
def ==(o) | ||
self.to_h == o.to_h | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Soulless | ||
module Validations | ||
class UsingValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
check_options | ||
return if should_not_continue?(value) | ||
|
||
if value.is_a?(Array) | ||
value.each { |v| perform(record, attribute, v) } | ||
else | ||
perform(record, attribute, value) | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_options | ||
fail(ArgumentError, 'The `:model` option is required for the `using` validator') unless options[:model] | ||
fail(ArgumentError, 'The `:model` option should contain a class that responds to `valid?` for the `using` validator') unless options[:model].respond_to?(:new) && options[:model].method_defined?(:valid?) | ||
end | ||
|
||
def should_not_continue?(value) | ||
(options[:allow_nil] && value.nil?) || | ||
(options[:allow_empty] && value.empty?) | ||
end | ||
|
||
def perform(record, attribute, value) | ||
model = options[:model].new(value) | ||
|
||
return if model.valid? | ||
|
||
reference_value = model[options[:reference]] if options[:reference] | ||
|
||
if !reference_value.blank? | ||
errors = {} | ||
errors[reference_value] = model.errors | ||
|
||
record.errors[attribute] << errors | ||
else | ||
record.errors[attribute] << model.errors | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Soulless | ||
VERSION = '1.1.1' | ||
VERSION = '1.2.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters