-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename VirtualOperation -> Operation
- Loading branch information
1 parent
b684ad3
commit daaf559
Showing
9 changed files
with
91 additions
and
44 deletions.
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
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
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
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,41 @@ | ||
require "./validations/**" | ||
|
||
module Avram::DatabaseValidations | ||
private def validate_uniqueness_of( | ||
attribute : Avram::Attribute, | ||
query : Avram::Criteria, | ||
message : String = "is already taken" | ||
) | ||
attribute.value.try do |value| | ||
if query.eq(value).first? | ||
attribute.add_error message | ||
end | ||
end | ||
end | ||
|
||
private def validate_uniqueness_of( | ||
attribute : Avram::Attribute, | ||
message : String = "is already taken" | ||
) | ||
attribute.value.try do |value| | ||
if build_validation_query(attribute.name, attribute.value).first? | ||
attribute.add_error message | ||
end | ||
end | ||
end | ||
|
||
# Must be included in the macro to get access to the generic T class | ||
# in forms that save to the database. | ||
# | ||
# Operations will also have access to this, but will fail if you try to use | ||
# if because there is no T (model class). | ||
macro included | ||
private def build_validation_query(column_name, value) : T::BaseQuery | ||
query = T::BaseQuery.new.where(column_name, value) | ||
record.try(&.id).try do |id| | ||
query = query.id.not.eq(id) | ||
end | ||
query | ||
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
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
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
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
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