Skip to content

Commit

Permalink
Rename VirtualOperation -> Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcsmith committed Jul 16, 2019
1 parent b684ad3 commit daaf559
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 44 deletions.
8 changes: 4 additions & 4 deletions spec/define_attribute_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class VirtualOperation < Post::SaveOperation
private class Operation < Post::SaveOperation
attribute password_confirmation : String
attribute terms_of_service : Bool
attribute best_kind_of_bear : String = "black bear"
Expand Down Expand Up @@ -88,18 +88,18 @@ describe "attribute in forms" do
end

it "sets named args for attributes, leaves other empty" do
VirtualOperation.create(title: "My Title", best_kind_of_bear: "brown bear") do |operation, post|
Operation.create(title: "My Title", best_kind_of_bear: "brown bear") do |operation, post|
operation.best_kind_of_bear.value.should eq("brown bear")
operation.terms_of_service.value.should be_nil
post.should_not be_nil

VirtualOperation.update(post.not_nil!, best_kind_of_bear: "koala bear") do |operation, post|
Operation.update(post.not_nil!, best_kind_of_bear: "koala bear") do |operation, post|
operation.best_kind_of_bear.value.should eq("koala bear")
end
end
end
end

private def operation(attrs = {} of String => String)
VirtualOperation.new(attrs)
Operation.new(attrs)
end
50 changes: 25 additions & 25 deletions spec/virtual_operation_spec.cr → spec/operation_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class TestVirtualOperation < Avram::VirtualOperation
private class TestOperation < Avram::Operation
attribute name : String
attribute age : Int32

Expand All @@ -9,7 +9,7 @@ private class TestVirtualOperation < Avram::VirtualOperation
end
end

private class TestVirtualOperationWithMultipleValidations < Avram::VirtualOperation
private class TestOperationWithMultipleValidations < Avram::Operation
attribute name : String
attribute age : Int32

Expand All @@ -33,15 +33,15 @@ private class CanUseSameVirtualAttributeTwiceInModelBackedSaveOperation < User::
attribute password : String
end

private class CanUseSameVirtualAttributeTwiceInVirtualOperation < Avram::VirtualOperation
private class CanUseSameVirtualAttributeTwiceInOperation < Avram::Operation
attribute name : String
end

private class ParamKeySaveOperation < Avram::VirtualOperation
private class ParamKeySaveOperation < Avram::Operation
param_key :custom_param
end

describe Avram::VirtualOperation do
describe Avram::Operation do
it "has create/update args for non column attributes" do
UserWithVirtual.create(password: "p@ssword") do |operation, _user|
operation.password.value = "p@ssword"
Expand All @@ -54,52 +54,52 @@ describe Avram::VirtualOperation do
end

it "sets a param_key based on the underscored class name" do
TestVirtualOperation.param_key.should eq "test_virtual_operation"
TestOperation.param_key.should eq "test_operation"
end

it "allows overriding the param_key" do
ParamKeySaveOperation.param_key.should eq "custom_param"
end

it "sets up initializers for params and no params" do
virtual_operation = TestVirtualOperation.new
virtual_operation.name.value.should be_nil
virtual_operation.name.value = "Megan"
virtual_operation.name.value.should eq("Megan")
operation = TestOperation.new
operation.name.value.should be_nil
operation.name.value = "Megan"
operation.name.value.should eq("Megan")

params = Avram::Params.new({"name" => "Jordan"})
virtual_operation = TestVirtualOperation.new(params)
virtual_operation.name.value.should eq("Jordan")
operation = TestOperation.new(params)
operation.name.value.should eq("Jordan")
end

it "parses params" do
params = Avram::Params.new({"age" => "45"})
virtual_operation = TestVirtualOperation.new(params)
virtual_operation.age.value.should eq 45
virtual_operation.age.errors.should eq [] of String
operation = TestOperation.new(params)
operation.age.value.should eq 45
operation.age.errors.should eq [] of String

params = Avram::Params.new({"age" => "not an int"})
virtual_operation = TestVirtualOperation.new(params)
virtual_operation.age.value.should be_nil
virtual_operation.age.errors.should eq ["is invalid"]
operation = TestOperation.new(params)
operation.age.value.should be_nil
operation.age.errors.should eq ["is invalid"]
end

it "includes validations" do
params = Avram::Params.new({"name" => ""})
virtual_operation = TestVirtualOperation.new(params)
virtual_operation.name.errors.should eq [] of String
virtual_operation.valid?.should be_true
operation = TestOperation.new(params)
operation.name.errors.should eq [] of String
operation.valid?.should be_true

virtual_operation.validate
operation.validate

virtual_operation.name.errors.should eq ["is required"]
virtual_operation.valid?.should be_false
operation.name.errors.should eq ["is required"]
operation.valid?.should be_false
end

describe "#errors" do
it "includes errors for all attributes" do
params = Avram::Params.new({"name" => "", "age" => "20"})
operation = TestVirtualOperationWithMultipleValidations.new(params)
operation = TestOperationWithMultipleValidations.new(params)

operation.validate

Expand Down
2 changes: 1 addition & 1 deletion spec/save_operation_callbacks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe "Avram::SaveOperation callbacks" do

it "does not run after_commit if rolled back" do
post = PostBox.create
operation = CallbacksSaveOperation.new(post)
operation = CallbacksSaveOperation.new(post, rollback: true)
operation.callbacks_that_ran.should eq([] of String)

operation.save
Expand Down
41 changes: 41 additions & 0 deletions src/avram/database_validations.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "./validations/**"

module Avram::DatabaseValidations
private def validate_uniqueness_of(
attribute : Avram::Attribute,
query : Avram::Criteria,
message : String = "is already taken"
)
attribute.value.try do |value|
if query.eq(value).first?
attribute.add_error message
end
end
end

private def validate_uniqueness_of(
attribute : Avram::Attribute,
message : String = "is already taken"
)
attribute.value.try do |value|
if build_validation_query(attribute.name, attribute.value).first?
attribute.add_error message
end
end
end

# Must be included in the macro to get access to the generic T class
# in forms that save to the database.
#
# Operations will also have access to this, but will fail if you try to use
# if because there is no T (model class).
macro included
private def build_validation_query(column_name, value) : T::BaseQuery
query = T::BaseQuery.new.where(column_name, value)
record.try(&.id).try do |id|
query = query.id.not.eq(id)
end
query
end
end
end
2 changes: 1 addition & 1 deletion src/avram/nested_save_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Avram::NestedSaveOperation

def mark_nested_save_operations_as_failed
nested_save_operations.each do |f|
f.mark_as_failed
f.as(Avram::MarkAsFailed).mark_as_failed
end
end

Expand Down
6 changes: 4 additions & 2 deletions src/avram/virtual_operation.cr → src/avram/operation.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "./validations"
require "./save_operation_errors"
require "./define_attribute"
require "./save_operation_errors"
require "./param_key_override"

class Avram::VirtualForm
macro inherited
{% raise "Avram::VirtualForm has been renamed to Avram::VirtualOperation. Please inherit from Avram::VirtualOperation." %}
{% raise "Avram::VirtualForm has been renamed to Avram::Operation. Please inherit from Avram::Operation." %}
end
end

class Avram::VirtualOperation
class Avram::Operation
include Avram::DefineAttribute
include Avram::Validations
include Avram::SaveOperationErrors
Expand Down
9 changes: 8 additions & 1 deletion src/avram/param_key_override.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module Avram::ParamKeyOverride
macro included
# Override the param key used for this operation
define_param_key_override

macro inherited
define_param_key_override
end
end

macro define_param_key_override
macro param_key(key)
def self.param_key
\{{ key.id.stringify }}
Expand Down
15 changes: 6 additions & 9 deletions src/avram/save_operation.cr
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
require "./validations"
require "./operation"
require "./database_validations"
require "./callbacks"
require "./nested_save_operation"
require "./needy_initializer_and_save_methods"
require "./define_attribute"
require "./mark_as_failed"
require "./param_key_override"
require "./save_operation_errors"
require "./inherit_column_attributes"

abstract class Avram::SaveOperation(T)
include Avram::Validations
abstract class Avram::SaveOperation(T) < Avram::Operation
include Avram::NeedyInitializerAndSaveMethods
include Avram::DefineAttribute
include Avram::Callbacks
include Avram::DatabaseValidations
include Avram::NestedSaveOperation
include Avram::MarkAsFailed
include Avram::SaveOperationErrors
include Avram::ParamKeyOverride
include Avram::InheritColumnAttributes

enum SaveStatus
Expand All @@ -25,10 +22,10 @@ abstract class Avram::SaveOperation(T)
Unperformed
end

@save_status = SaveStatus::Unperformed

macro inherited
@valid : Bool = true
@save_status = SaveStatus::Unperformed

@@permitted_param_keys = [] of String
@@schema_class = T
end
Expand Down
2 changes: 1 addition & 1 deletion src/avram/validations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Avram::Validations
# Must be included in the macro to get access to the generic T class
# in forms that save to the database.
#
# VirtualOperations will also have access to this, but will fail if you try to use
# Operations will also have access to this, but will fail if you try to use
# if because there is no T (model class).
macro included
private def build_validation_query(column_name, value) : T::BaseQuery
Expand Down

0 comments on commit daaf559

Please sign in to comment.