From 00ae2eb5b61f65d06c2d5acb75d4df6c71505b6a Mon Sep 17 00:00:00 2001 From: maiha Date: Sat, 31 Mar 2018 23:22:55 +0900 Subject: [PATCH] Revert "Fields are now required in default. Use `foo?` for nilable fields. (#144)" This reverts commit c9cfc3dcbd7062dfc399ddea256b0df6f65e0225. --- spec/granite_orm/fields/primary_key_spec.cr | 6 +++--- spec/granite_orm/fields/timestamps_spec.cr | 16 ++++++++-------- spec/granite_orm/transactions/create_spec.cr | 2 +- spec/granite_orm/transactions/save_spec.cr | 2 +- src/granite_orm/associations.cr | 6 +++--- src/granite_orm/fields.cr | 18 +++++++----------- src/granite_orm/table.cr | 7 +------ 7 files changed, 24 insertions(+), 33 deletions(-) diff --git a/spec/granite_orm/fields/primary_key_spec.cr b/spec/granite_orm/fields/primary_key_spec.cr index 48abb4e2..ffca9a80 100644 --- a/spec/granite_orm/fields/primary_key_spec.cr +++ b/spec/granite_orm/fields/primary_key_spec.cr @@ -12,12 +12,12 @@ module {{adapter.capitalize.id}} describe "{{ adapter.id }} .new(primary_key: value)" do it "ignores the value in default" do - Parent.new(id: 1).id?.should eq(nil) + Parent.new(id: 1).id.should eq(nil) end it "sets the value when the primary is defined as `auto: false`" do - Kvs.new(k: "foo").k?.should eq("foo") - Kvs.new(k: "foo", v: "v").k?.should eq("foo") + Kvs.new(k: "foo").k.should eq("foo") + Kvs.new(k: "foo", v: "v").k.should eq("foo") end end end diff --git a/spec/granite_orm/fields/timestamps_spec.cr b/spec/granite_orm/fields/timestamps_spec.cr index 4447de55..ed464406 100644 --- a/spec/granite_orm/fields/timestamps_spec.cr +++ b/spec/granite_orm/fields/timestamps_spec.cr @@ -19,8 +19,8 @@ module {{adapter.capitalize.id}} parent = Parent.new(name: "parent").tap(&.save) found_parent = Parent.find(parent.id) - original_timestamp = parent.created_at - read_timestamp = found_parent.created_at + original_timestamp = parent.created_at.not_nil! + read_timestamp = found_parent.created_at.not_nil! original_timestamp.kind.should eq Time::Kind::Utc read_timestamp.kind.should eq {{ time_kind_on_read }} @@ -30,8 +30,8 @@ module {{adapter.capitalize.id}} parent = Parent.new(name: "parent").tap(&.save) found_parent = Parent.find(parent.id) - original_timestamp = parent.updated_at - read_timestamp = found_parent.updated_at + original_timestamp = parent.updated_at.not_nil! + read_timestamp = found_parent.updated_at.not_nil! original_timestamp.kind.should eq Time::Kind::Utc read_timestamp.kind.should eq {{ time_kind_on_read }} @@ -41,8 +41,8 @@ module {{adapter.capitalize.id}} parent = Parent.new(name: "parent").tap(&.save) found_parent = Parent.find(parent.id) - original_timestamp = parent.created_at - read_timestamp = found_parent.created_at + original_timestamp = parent.created_at.not_nil! + read_timestamp = found_parent.created_at.not_nil! original_timestamp.epoch.should eq read_timestamp.epoch end @@ -51,8 +51,8 @@ module {{adapter.capitalize.id}} parent = Parent.new(name: "parent").tap(&.save) found_parent = Parent.find(parent.id) - original_timestamp = parent.updated_at - read_timestamp = found_parent.updated_at + original_timestamp = parent.updated_at.not_nil! + read_timestamp = found_parent.updated_at.not_nil! original_timestamp.epoch.should eq read_timestamp.epoch end diff --git a/spec/granite_orm/transactions/create_spec.cr b/spec/granite_orm/transactions/create_spec.cr index dfb58842..64c62b4d 100644 --- a/spec/granite_orm/transactions/create_spec.cr +++ b/spec/granite_orm/transactions/create_spec.cr @@ -11,7 +11,7 @@ module {{adapter.capitalize.id}} it "does not create an invalid object" do parent = Parent.create(name: "") - parent.id?.should be_nil + parent.id.should be_nil end describe "with a custom primary key" do diff --git a/spec/granite_orm/transactions/save_spec.cr b/spec/granite_orm/transactions/save_spec.cr index ed2e5cdc..b98331ac 100644 --- a/spec/granite_orm/transactions/save_spec.cr +++ b/spec/granite_orm/transactions/save_spec.cr @@ -14,7 +14,7 @@ module {{adapter.capitalize.id}} parent = Parent.new parent.name = "" parent.save - parent.id?.should be_nil + parent.id.should be_nil end it "updates an existing object" do diff --git a/src/granite_orm/associations.cr b/src/granite_orm/associations.cr index d52d9dd7..91c33acd 100644 --- a/src/granite_orm/associations.cr +++ b/src/granite_orm/associations.cr @@ -9,7 +9,7 @@ module Granite::ORM::Associations # retrieve the parent relationship def {{model_name.id}} - if parent = {{model_name.id.camelcase}}.find {{model_name.id}}_id? + if parent = {{model_name.id.camelcase}}.find {{model_name.id}}_id parent else {{model_name.id.camelcase}}.new @@ -27,7 +27,7 @@ module Granite::ORM::Associations {% children_class = children_table.id[0...-1].camelcase %} {% name_space = @type.name.gsub(/::/, "_").downcase.id %} {% table_name = SETTINGS[:table_name] || name_space + "s" %} - return [] of {{children_class}} unless id? + return [] of {{children_class}} unless id foreign_key = "{{children_table.id}}.{{table_name[0...-1]}}_id" query = "WHERE #{foreign_key} = ?" {{children_class}}.all(query, id) @@ -40,7 +40,7 @@ module Granite::ORM::Associations {% children_class = children_table.id[0...-1].camelcase %} {% name_space = @type.name.gsub(/::/, "_").downcase.id %} {% table_name = SETTINGS[:table_name] || name_space + "s" %} - return [] of {{children_class}} unless id? + return [] of {{children_class}} unless id query = "JOIN {{through.id}} ON {{through.id}}.{{children_table.id[0...-1]}}_id = {{children_table.id}}.id " query = query + "WHERE {{through.id}}.{{table_name[0...-1]}}_id = ?" {{children_class}}.all(query, id) diff --git a/src/granite_orm/fields.cr b/src/granite_orm/fields.cr index d066280c..f771534b 100644 --- a/src/granite_orm/fields.cr +++ b/src/granite_orm/fields.cr @@ -31,11 +31,7 @@ module Granite::ORM::Fields # Create the properties {% for name, type in FIELDS %} - property? {{name.id}} : Union({{type.id}} | Nil) - def {{name.id}} - raise {{@type.name.stringify}} + "#" + {{name.stringify}} + " cannot be nil" if @{{name.id}}.nil? - @{{name.id}}.not_nil! - end + property {{name.id}} : Union({{type.id}} | Nil) {% end %} # keep a hash of the fields to be used for mapping @@ -52,9 +48,9 @@ module Granite::ORM::Fields parsed_params = [] of DB::Any {% for name, type in CONTENT_FIELDS %} {% if type.id == Time.id %} - parsed_params << {{name.id}}?.try(&.to_s("%F %X")) + parsed_params << {{name.id}}.try(&.to_s("%F %X")) {% else %} - parsed_params << {{name.id}}? + parsed_params << {{name.id}} {% end %} {% end %} return parsed_params @@ -65,11 +61,11 @@ module Granite::ORM::Fields {% for name, type in FIELDS %} {% if type.id == Time.id %} - fields["{{name}}"] = {{name.id}}?.try(&.to_s("%F %X")) + fields["{{name}}"] = {{name.id}}.try(&.to_s("%F %X")) {% elsif type.id == Slice.id %} - fields["{{name}}"] = {{name.id}}?.try(&.to_s("")) + fields["{{name}}"] = {{name.id}}.try(&.to_s("")) {% else %} - fields["{{name}}"] = {{name.id}}? + fields["{{name}}"] = {{name.id}} {% end %} {% end %} @@ -79,7 +75,7 @@ module Granite::ORM::Fields def to_json(json : JSON::Builder) json.object do {% for name, type in FIELDS %} - %field, %value = "{{name.id}}", {{name.id}}? + %field, %value = "{{name.id}}", {{name.id}} {% if type.id == Time.id %} json.field %field, %value.try(&.to_s("%F %X")) {% elsif type.id == Slice.id %} diff --git a/src/granite_orm/table.cr b/src/granite_orm/table.cr index 40da56ec..44f7f743 100644 --- a/src/granite_orm/table.cr +++ b/src/granite_orm/table.cr @@ -45,12 +45,7 @@ module Granite::ORM::Table @@primary_name = "{{primary_name}}" @@primary_auto = "{{primary_auto}}" - property? {{primary_name}} : Union({{primary_type.id}} | Nil) - - def {{primary_name}} - raise {{@type.name.stringify}} + "#" + {{primary_name.stringify}} + " cannot be nil" if @{{primary_name}}.nil? - @{{primary_name}}.not_nil! - end + property {{primary_name}} : Union({{primary_type.id}} | Nil) def self.table_name @@table_name