diff --git a/src/avram/callbacks.cr b/src/avram/callbacks.cr index f1cea0c90..9343ca9d2 100644 --- a/src/avram/callbacks.cr +++ b/src/avram/callbacks.cr @@ -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 %} @@ -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 %} @@ -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 %} diff --git a/src/avram/database_validations.cr b/src/avram/database_validations.cr index 9e6555623..2811e4301 100644 --- a/src/avram/database_validations.cr +++ b/src/avram/database_validations.cr @@ -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` # @@ -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 @@ -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 @@ -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) diff --git a/src/avram/delete_operation.cr b/src/avram/delete_operation.cr index 9cf9de383..e03c2cb80 100644 --- a/src/avram/delete_operation.cr +++ b/src/avram/delete_operation.cr @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/avram/needy_initializer_and_delete_methods.cr b/src/avram/needy_initializer_and_delete_methods.cr index dda7d3a1a..bb76c7a22 100644 --- a/src/avram/needy_initializer_and_delete_methods.cr +++ b/src/avram/needy_initializer_and_delete_methods.cr @@ -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 ) @@ -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 }}, @@ -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 }} @@ -156,7 +156,7 @@ module Avram::NeedyInitializerAndDeleteMethods end def initialize( - @record : T, + @record : AvramModel, {{ needs_method_args.id }} {{ attribute_method_args.id }} ) diff --git a/src/avram/needy_initializer_and_save_methods.cr b/src/avram/needy_initializer_and_save_methods.cr index 0cea2eab8..16385c83e 100644 --- a/src/avram/needy_initializer_and_save_methods.cr +++ b/src/avram/needy_initializer_and_save_methods.cr @@ -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 ) @@ -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 }}, @@ -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 }} @@ -207,7 +207,7 @@ module Avram::NeedyInitializerAndSaveMethods end def initialize( - @record : T, + @record : AvramModel, {{ needs_method_args.id }} {{ attribute_method_args.id }} ) diff --git a/src/avram/nested_save_operation.cr b/src/avram/nested_save_operation.cr index ec3eba4ac..fd2e3817d 100644 --- a/src/avram/nested_save_operation.cr +++ b/src/avram/nested_save_operation.cr @@ -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 diff --git a/src/avram/save_operation.cr b/src/avram/save_operation.cr index d6d5dff87..7b7e0f042 100644 --- a/src/avram/save_operation.cr +++ b/src/avram/save_operation.cr @@ -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 @@ -34,7 +34,7 @@ abstract class Avram::SaveOperation(T) @@permitted_param_keys = [] of String end - @record : T? + @record : AvramModel? @params : Avram::Paramable # :nodoc: @@ -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) @@ -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( @@ -233,7 +233,7 @@ abstract class Avram::SaveOperation(T) end end - def save! : T + def save! : AvramModel if save record.not_nil! else @@ -241,7 +241,7 @@ abstract class Avram::SaveOperation(T) end end - def update! : T + def update! : AvramModel save! end @@ -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 @@ -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?) diff --git a/src/avram/upsert.cr b/src/avram/upsert.cr index 9d1ae5815..f547ca809 100644 --- a/src/avram/upsert.cr +++ b/src/avram/upsert.cr @@ -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) @@ -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 %}