Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/avram/callbacks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ module Avram::Callbacks
ERR
end
%}
def after_save(%object : T)
def after_save(%object : AvramModel)
{% if @type.methods.map(&.name).includes?(:after_save.id) %}
previous_def
{% else %}
Expand Down Expand Up @@ -341,7 +341,7 @@ module Avram::Callbacks
ERR
end
%}
def after_delete(%object : T)
def after_delete(%object : AvramModel)
{% if @type.methods.map(&.name).includes?(:after_delete.id) %}
previous_def
{% else %}
Expand Down Expand Up @@ -480,7 +480,7 @@ module Avram::Callbacks
ERR
end
%}
def after_commit(%object : T)
def after_commit(%object : AvramModel)
{% if @type.methods.map(&.name).includes?(:after_commit.id) %}
previous_def
{% else %}
Expand Down
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
8 changes: 4 additions & 4 deletions src/avram/needy_initializer_and_delete_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module Avram::NeedyInitializerAndDeleteMethods

macro generate_delete(attribute_method_args, attribute_params, with_params, with_bang)
def self.delete{% if with_bang %}!{% end %}(
record : T,
record : AvramModel,
params : Hash,
**named_args
)
Expand All @@ -105,7 +105,7 @@ module Avram::NeedyInitializerAndDeleteMethods
end

def self.delete{% if with_bang %}!{% end %}(
record : T,
record : AvramModel,
{% if with_params %}params,{% end %}
{% for type_declaration in OPERATION_NEEDS %}
{{ type_declaration }},
Expand Down Expand Up @@ -147,7 +147,7 @@ module Avram::NeedyInitializerAndDeleteMethods
{% end %}

def initialize(
@record : T,
@record : AvramModel,
@params : Avram::Paramable,
{{ needs_method_args.id }}
{{ attribute_method_args.id }}
Expand All @@ -156,7 +156,7 @@ module Avram::NeedyInitializerAndDeleteMethods
end

def initialize(
@record : T,
@record : AvramModel,
{{ needs_method_args.id }}
{{ attribute_method_args.id }}
)
Expand Down
8 changes: 4 additions & 4 deletions src/avram/needy_initializer_and_save_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module Avram::NeedyInitializerAndSaveMethods

macro generate_update(attribute_method_args, attribute_params, with_params, with_bang)
def self.update{% if with_bang %}!{% end %}(
record : T,
record : AvramModel,
params : Hash,
**named_args
)
Expand All @@ -145,7 +145,7 @@ module Avram::NeedyInitializerAndSaveMethods
end

def self.update{% if with_bang %}!{% end %}(
record : T,
record : AvramModel,
{% if with_params %}with params,{% end %}
{% for type_declaration in OPERATION_NEEDS %}
{{ type_declaration }},
Expand Down Expand Up @@ -189,7 +189,7 @@ module Avram::NeedyInitializerAndSaveMethods
{% end %}

def initialize(
@record : T,
@record : AvramModel,
@params : Avram::Paramable,
{{ needs_method_args.id }}
{{ attribute_method_args.id }}
Expand All @@ -207,7 +207,7 @@ module Avram::NeedyInitializerAndSaveMethods
end

def initialize(
@record : T,
@record : AvramModel,
{{ needs_method_args.id }}
{{ attribute_method_args.id }}
)
Expand Down
4 changes: 2 additions & 2 deletions src/avram/nested_save_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module Avram::NestedSaveOperation
t.stringify.starts_with?("Avram::SaveOperation(")
end.type_vars.first %}

{% assoc = T.constant(:ASSOCIATIONS).find do |assoc|
{% assoc = AvramModel.constant(:ASSOCIATIONS).find do |assoc|
assoc[:relationship_type] == :has_one &&
assoc[:type].resolve.name == model_type.name
end %}

{% unless assoc %}
{% raise "#{T} must have a has_one association with #{model_type}" %}
{% raise "#{AvramModel} must have a has_one association with #{model_type}" %}
{% end %}

@_{{ name }} : {{ type }} | Nil
Expand Down
32 changes: 16 additions & 16 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 All @@ -255,7 +255,7 @@ abstract class Avram::SaveOperation(T)
# This method should always return `true` for a create or `false`
# for an update, independent of the stage we are at in the operation.
def new_record? : Bool
{{ T.constant(:PRIMARY_KEY_NAME).id }}.value.nil?
{{ AvramModel.constant(:PRIMARY_KEY_NAME).id }}.value.nil?
end

private def insert_or_update
Expand All @@ -272,35 +272,35 @@ 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
@record = AvramModel.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
@record = AvramModel.from_rs(rs).first
end
end

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
6 changes: 3 additions & 3 deletions src/avram/upsert.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module Avram::Upsert
# end
# ````
macro upsert_lookup_columns(*attribute_names)
def self.upsert!(*args, **named_args) : T
def self.upsert!(*args, **named_args) : AvramModel
operation = new(*args, **named_args)
existing_record = find_existing_unique_record(operation)

Expand All @@ -81,8 +81,8 @@ module Avram::Upsert
yield operation, operation.record
end

def self.find_existing_unique_record(operation) : T?
T::BaseQuery.new
def self.find_existing_unique_record(operation) : AvramModel?
AvramModel::BaseQuery.new
{% for attribute in attribute_names %}
.{{ attribute.id }}.nilable_eq(operation.{{ attribute.id }}.value)
{% end %}
Expand Down