From 005bd767955bd8eb8f6deb75c19b1e195bfadde2 Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Tue, 8 Dec 2020 14:36:21 -0600 Subject: [PATCH] Update to enable views working with SchemaEnforcer --- spec/view_spec.cr | 2 +- src/avram/model.cr | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/view_spec.cr b/spec/view_spec.cr index a4584e318..c0fd4bb3c 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -21,7 +21,7 @@ describe "views" do nickname_info.count.should eq 3 end - pending "works with SchemaEnforcer" do + it "works with SchemaEnforcer" do AdminUser.ensure_correct_column_mappings! NicknameInfo.ensure_correct_column_mappings! end diff --git a/src/avram/model.cr b/src/avram/model.cr index 22ef85435..1f9a8480a 100644 --- a/src/avram/model.cr +++ b/src/avram/model.cr @@ -73,6 +73,7 @@ abstract class Avram::Model setup(Avram::Model.setup_getters) setup(Avram::Model.setup_column_info_methods) setup(Avram::Model.setup_association_queries) + setup(Avram::Model.setup_view_schema_enforcer_validations) setup(Avram::BaseQueryTemplate.setup) setup(Avram::SchemaEnforcer.setup) end @@ -202,6 +203,11 @@ abstract class Avram::Model schema_enforcer_validations << EnsureMatchingColumns.new(model_class: {{ type.id }}) end + macro setup_view_schema_enforcer_validations(type, *args, **named_args) + schema_enforcer_validations << EnsureExistingTable.new(model_class: {{ type.id }}) + schema_enforcer_validations << EnsureMatchingColumns.new(model_class: {{ type.id }}, check_required: false) + end + macro setup_getters(columns, *args, **named_args) {% for column in columns %} {% db_type = column[:type].is_a?(Generic) ? column[:type].type_vars.first : column[:type] %}