Skip to content
This repository has been archived by the owner on Oct 28, 2019. It is now read-only.

Commit

Permalink
Add on option to form needs
Browse files Browse the repository at this point in the history
Closes #78
  • Loading branch information
paulcsmith committed Nov 18, 2017
1 parent a9144a3 commit abdc967
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
32 changes: 27 additions & 5 deletions 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
Expand All @@ -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"}
Expand Down Expand Up @@ -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
45 changes: 32 additions & 13 deletions 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
Expand All @@ -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 %}

Expand All @@ -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
Expand All @@ -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!
Expand All @@ -72,15 +91,15 @@ module LuckyRecord::NeedyInitializer
def self.update(
record,
with params,
{% for type_declaration in NEEDS %}
{% for type_declaration in NEEDS_ON_UPDATE %}
{{ type_declaration }},
{% end %}
)
form = new(
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
Expand All @@ -93,15 +112,15 @@ module LuckyRecord::NeedyInitializer
def self.update!(
record,
with params,
{% for type_declaration in NEEDS %}
{% for type_declaration in NEEDS_ON_UPDATE %}
{{ type_declaration }},
{% end %}
)
form = new(
record,
params
)
{% for type_declaration in NEEDS %}
{% for type_declaration in NEEDS_ON_UPDATE %}
form.{{ type_declaration.var }} = {{ type_declaration.var }}
{% end %}
form.update!
Expand Down

0 comments on commit abdc967

Please sign in to comment.