From abdc967fd68b0de31eba63fd3ad2ae9378b3e012 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Fri, 17 Nov 2017 21:39:34 -0500 Subject: [PATCH] Add `on` option to form needs Closes #78 --- spec/form_needs_spec.cr | 32 ++++++++++++++++--- src/lucky_record/needy_initializer.cr | 45 +++++++++++++++++++-------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/spec/form_needs_spec.cr b/spec/form_needs_spec.cr index 62683f6..9194a94 100644 --- a/spec/form_needs_spec.cr +++ b/spec/form_needs_spec.cr @@ -1,10 +1,6 @@ require "./spec_helper" -private class NeedsForm < User::BaseForm - needs created_by : String - needs nilable_value : String? - needs optional : String = "bar" - +class Needs::BaseForm < User::BaseForm def prepare setup_required_fields end @@ -16,6 +12,17 @@ private class NeedsForm < User::BaseForm end end +private class NeedsForm < Needs::BaseForm + needs created_by : String + needs nilable_value : String? + needs optional : String = "bar" +end + +private class NeedsWithOnOptionForm < Needs::BaseForm + needs created_by : String, on: :create + needs updated_by : String, on: :update +end + describe "LuckyRecord::Form needs" do it "does not change the default initializer" do params = {"name" => "Paul"} @@ -43,4 +50,19 @@ describe "LuckyRecord::Form needs" do form.created_by.should eq("Jane") end end + + it "can have needs for just create or update" do + params = {"name" => "Paul"} + create_user + user = UserQuery.new.first + + NeedsWithOnOptionForm.save(params, created_by: "Bob") do |form, _record| + form.created_by.should eq("Bob") + form.updated_by.should be_nil + end + NeedsWithOnOptionForm.update(user, params, updated_by: "Laura") do |form, _record| + form.created_by.should be_nil + form.updated_by.should eq("Laura") + end + end end diff --git a/src/lucky_record/needy_initializer.cr b/src/lucky_record/needy_initializer.cr index b15e5eb..b3289a1 100644 --- a/src/lucky_record/needy_initializer.cr +++ b/src/lucky_record/needy_initializer.cr @@ -1,6 +1,7 @@ module LuckyRecord::NeedyInitializer macro included - NEEDS = [] of Nil + NEEDS_ON_CREATE = [] of Nil + NEEDS_ON_UPDATE = [] of Nil macro inherited inherit_page_settings @@ -10,17 +11,35 @@ module LuckyRecord::NeedyInitializer end macro needs(type_declaration) - {% NEEDS << type_declaration %} + needs {{ type_declaration }}, on: :update + needs {{ type_declaration }}, on: :create + end + + macro needs(type_declaration, on) + {% if ![:create, :update].includes?(on) %} + {% raise "on option must be :create or :update" %} + {% end %} + {% if on == :update %} + {% NEEDS_ON_UPDATE << type_declaration %} + {% else %} + {% NEEDS_ON_CREATE << type_declaration %} + {% end %} @{{ type_declaration.var }} : {{ type_declaration.type }}? property {{ type_declaration.var }} end macro inherit_page_settings - NEEDS = [] of Nil + \{% if !@type.constant(:NEEDS_ON_CREATE) %} + NEEDS_ON_CREATE = [] of Nil + NEEDS_ON_UPDATE = [] of Nil + \{% end %} \{% if !@type.ancestors.first.abstract? %} - \{% for type_declaration in @type.ancestors.first.constant :NEEDS %} - \{% NEEDS << type_declaration %} + \{% for type_declaration in @type.ancestors.first.constant :NEEDS_ON_CREATE %} + \{% NEEDS_ON_CREATE << type_declaration %} + \{% end %} + \{% for type_declaration in @type.ancestors.first.constant :NEEDS_ON_CREATE %} + \{% NEEDS_ON_UPDATE << type_declaration %} \{% end %} \{% end %} @@ -37,14 +56,14 @@ module LuckyRecord::NeedyInitializer macro generate_save_methods def self.save( params, - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_CREATE %} {{ type_declaration }}, {% end %} ) form = new( params ) - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_CREATE %} form.{{ type_declaration.var }} = {{ type_declaration.var }} {% end %} if form.save @@ -56,14 +75,14 @@ module LuckyRecord::NeedyInitializer def self.save!( params, - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_CREATE %} {{ type_declaration }}, {% end %} ) form = new( params ) - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_CREATE %} form.{{ type_declaration.var }} = {{ type_declaration.var }} {% end %} form.save! @@ -72,7 +91,7 @@ module LuckyRecord::NeedyInitializer def self.update( record, with params, - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_UPDATE %} {{ type_declaration }}, {% end %} ) @@ -80,7 +99,7 @@ module LuckyRecord::NeedyInitializer record, params ) - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_UPDATE %} form.{{ type_declaration.var }} = {{ type_declaration.var }} {% end %} if form.save @@ -93,7 +112,7 @@ module LuckyRecord::NeedyInitializer def self.update!( record, with params, - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_UPDATE %} {{ type_declaration }}, {% end %} ) @@ -101,7 +120,7 @@ module LuckyRecord::NeedyInitializer record, params ) - {% for type_declaration in NEEDS %} + {% for type_declaration in NEEDS_ON_UPDATE %} form.{{ type_declaration.var }} = {{ type_declaration.var }} {% end %} form.update!