Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/avram/database_validations.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./validations/**"

module Avram::DatabaseValidations(T)
module Avram::DatabaseValidations(AvramModel)
# Validates that the given attribute is unique in the database
# All validation methods return `Bool`. `false` if any error is added, otherwise `true`
#
Expand All @@ -25,7 +25,7 @@ module Avram::DatabaseValidations(T)
# pass and Avram tries to save the record.
private def validate_uniqueness_of(
attribute : Avram::Attribute,
query : Avram::Criteria(T::BaseQuery, _),
query : Avram::Criteria(AvramModel::BaseQuery, _),
message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_uniqueness_of)
) : Bool
no_errors = true
Expand Down Expand Up @@ -64,7 +64,7 @@ module Avram::DatabaseValidations(T)
# ```
private def validate_uniqueness_of(
attribute : Avram::Attribute,
query : T::BaseQuery,
query : AvramModel::BaseQuery,
message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_uniqueness_of)
) : Bool
no_errors = true
Expand Down Expand Up @@ -100,7 +100,7 @@ module Avram::DatabaseValidations(T)
attribute : Avram::Attribute,
message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_uniqueness_of)
) : Bool
validate_uniqueness_of(attribute: attribute, query: T::BaseQuery.new, message: message)
validate_uniqueness_of(attribute: attribute, query: AvramModel::BaseQuery.new, message: message)
end

private def limit_query(query)
Expand Down
10 changes: 5 additions & 5 deletions src/avram/delete_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "./param_key_override"
require "./inherit_column_attributes"
require "./needy_initializer_and_delete_methods"

abstract class Avram::DeleteOperation(T)
abstract class Avram::DeleteOperation(AvramModel)
include Avram::NeedyInitializerAndDeleteMethods
include Avram::DefineAttribute
include Avram::Validations
Expand All @@ -25,14 +25,14 @@ abstract class Avram::DeleteOperation(T)
macro inherited
@@permitted_param_keys = [] of String

@record : T
@record : AvramModel
@params : Avram::Paramable
getter :record, :params
property delete_status : OperationStatus = OperationStatus::Unperformed
end

def self.param_key
T.name.underscore
AvramModel.name.underscore
end

def delete : Bool
Expand Down Expand Up @@ -85,7 +85,7 @@ abstract class Avram::DeleteOperation(T)

def before_delete; end

def after_delete(_record : T); end
def after_delete(_record : AvramModel); end

# :nodoc:
def publish_delete_failed_event
Expand All @@ -102,7 +102,7 @@ abstract class Avram::DeleteOperation(T)
)
end

private def delete_or_soft_delete(record : T) : T
private def delete_or_soft_delete(record : AvramModel) : AvramModel
if record.is_a?(Avram::SoftDelete::Model)
record.soft_delete
else
Expand Down
26 changes: 13 additions & 13 deletions src/avram/save_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ require "./validations"
require "./operation_errors"
require "./upsert"

abstract class Avram::SaveOperation(T)
abstract class Avram::SaveOperation(AvramModel)
include Avram::DefineAttribute
include Avram::Validations
include Avram::OperationErrors
include Avram::ParamKeyOverride
include Avram::NeedyInitializerAndSaveMethods
include Avram::Callbacks
include Avram::DatabaseValidations(T)
include Avram::DatabaseValidations(AvramModel)
include Avram::NestedSaveOperation
include Avram::MarkAsFailed
include Avram::InheritColumnAttributes
Expand All @@ -34,7 +34,7 @@ abstract class Avram::SaveOperation(T)
@@permitted_param_keys = [] of String
end

@record : T?
@record : AvramModel?
@params : Avram::Paramable

# :nodoc:
Expand All @@ -45,7 +45,7 @@ abstract class Avram::SaveOperation(T)
abstract def attributes

def self.param_key
T.name.underscore
AvramModel.name.underscore
end

def initialize(@params)
Expand All @@ -55,7 +55,7 @@ abstract class Avram::SaveOperation(T)
@params = Avram::Params.new
end

delegate :database, :table_name, :primary_key_name, to: T
delegate :database, :table_name, :primary_key_name, to: AvramModel

private def publish_save_failed_event
Avram::Events::SaveFailedEvent.publish(
Expand Down Expand Up @@ -233,15 +233,15 @@ abstract class Avram::SaveOperation(T)
end
end

def save! : T
def save! : AvramModel
if save
record.not_nil!
else
raise Avram::InvalidOperationError.new(operation: self)
end
end

def update! : T
def update! : AvramModel
save!
end

Expand Down Expand Up @@ -272,19 +272,19 @@ abstract class Avram::SaveOperation(T)

def before_save; end

def after_save(_record : T); end
def after_save(_record : AvramModel); end

def after_commit(_record : T); end
def after_commit(_record : AvramModel); end

private def insert : T
private def insert : AvramModel
self.created_at.value ||= Time.utc if responds_to?(:created_at)
self.updated_at.value ||= Time.utc if responds_to?(:updated_at)
@record = database.query insert_sql.statement, args: insert_sql.args do |rs|
@record = T.from_rs(rs).first
end
end

private def update(id) : T
private def update(id) : AvramModel
self.updated_at.value = Time.utc if responds_to?(:updated_at)
@record = database.query update_query(id).statement_for_update(changes), args: update_query(id).args_for_update(changes) do |rs|
@record = T.from_rs(rs).first
Expand All @@ -294,13 +294,13 @@ abstract class Avram::SaveOperation(T)
private def update_query(id)
Avram::QueryBuilder
.new(table_name)
.select(T.column_names)
.select(AvramModel.column_names)
.where(Avram::Where::Equal.new(primary_key_name, id.to_s))
end

private def insert_sql
insert_values = attributes_to_hash(column_attributes).compact
Avram::Insert.new(table_name, insert_values, T.column_names)
Avram::Insert.new(table_name, insert_values, AvramModel.column_names)
end

private def attributes_to_hash(attributes) : Hash(Symbol, String?)
Expand Down