Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor allow_blank escape hatch to work better with params. #956

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ dependencies:
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 1.4
branch: master
# version: ~> 1.4
lucky:
github: luckyframework/lucky
version: ">= 1.0.0"
Expand Down
27 changes: 20 additions & 7 deletions spec/avram/operations/save_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ private class SavePost < Post::SaveOperation
end

# This is to test an escape hatch where you don't want to
# define a Nil type, but your field is optional
# define a Nil type, but your String field can be an empty string
private class AllowBlankComment < Comment::SaveOperation
skip_default_validations

before_save do
body.allow_blank = true
end
permit_columns body, post_id
end

module DefaultUserValidations
Expand Down Expand Up @@ -922,6 +918,15 @@ describe "Avram::SaveOperation" do
comment.body.should eq("")
end
end
it "allows blank strings from params" do
post = PostFactory.create
params = build_params({comment: {post_id: post.id, body: ""}}.to_json, content_type: "application/json")
AllowBlankComment.create(params) do |op, new_comment|
op.valid?.should be_true
comment = new_comment.as(Comment)
comment.body.should eq("")
end
end
it "still allows normal data to be saved" do
post = PostFactory.create
AllowBlankComment.create(post_id: post.id, body: "not blank") do |op, new_comment|
Expand All @@ -930,13 +935,21 @@ describe "Avram::SaveOperation" do
comment.body.should eq("not blank")
end
end
it "fails at postgres when saving nil" do
it "fails at postgres when saving nil from named args" do
post = PostFactory.create
expect_raises(PQ::PQError) do
AllowBlankComment.create(post_id: post.id) do |_op, _new_comment|
end
end
end
it "fails at postgres when saving nil from params" do
post = PostFactory.create
params = build_params({comment: {post_id: post.id}}.to_json, content_type: "application/json")
expect_raises(PQ::PQError) do
AllowBlankComment.create(params) do |_op, _new_comment|
end
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/models/comment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Comment < BaseModel
table do
primary_key custom_id : Int64
timestamps
column body : String
column body : String, allow_blank: true
belongs_to post : Post
end
end
Expand Down
6 changes: 4 additions & 2 deletions src/avram/add_column_attributes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ module Avram::AddColumnAttributes
name: :{{ attribute[:name].id }},
param: permitted_params["{{ attribute[:name] }}"]?,
value: value,
param_key: self.class.param_key)
param_key: self.class.param_key).tap do |attr|
attr.allow_blank = {{ attribute[:allow_blank] }}
end
end

private def default_value_for_{{ attribute[:name] }}
Expand Down Expand Up @@ -96,7 +98,7 @@ module Avram::AddColumnAttributes
def set_{{ attribute[:name] }}_from_param(_value)
# In nilable types, `nil` is ok, and non-nilable types we will get the
# "is required" error.
if _value.blank?
if _value.blank? && !{{ attribute[:name] }}.allow_blank?
{{ attribute[:name] }}.value = nil
return
end
Expand Down
1 change: 1 addition & 0 deletions src/avram/attribute.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Avram::Attribute(T)
def permitted
@_permitted ||= begin
Avram::PermittedAttribute(T).new(name: @name, param: @param, value: @value, param_key: @param_key).tap do |attribute|
attribute.allow_blank = allow_blank?
errors.each do |error|
attribute.add_error error
end
Expand Down
4 changes: 2 additions & 2 deletions src/avram/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ abstract class Avram::Model
{% end %}
end

macro column(type_declaration, autogenerated = false, serialize is_serialized = false)
macro column(type_declaration, autogenerated = false, serialize is_serialized = false, allow_blank = false)
{% if type_declaration.type.is_a?(Union) %}
{% data_type = type_declaration.type.types.first %}
{% nilable = true %}
Expand All @@ -257,7 +257,7 @@ abstract class Avram::Model
{% value = nil %}
{% end %}

{% COLUMNS << {name: type_declaration.var, type: data_type, nilable: nilable.id, autogenerated: autogenerated, value: value, serialized: is_serialized} %}
{% COLUMNS << {name: type_declaration.var, type: data_type, nilable: nilable.id, autogenerated: autogenerated, value: value, serialized: is_serialized, allow_blank: allow_blank} %}
end

macro setup_column_info_methods(columns, *args, **named_args)
Expand Down
2 changes: 1 addition & 1 deletion src/avram/validations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module Avram::Validations
) : Bool
no_errors = true
attributes.each do |attribute|
if attribute.value.blank_for_validates_required?
if attribute.value.blank_for_validates_required? && !attribute.allow_blank?
attribute.add_error(message)
no_errors = false
end
Expand Down