Skip to content

Commit

Permalink
Revert "Fields are now required in default. Use foo? for nilable fi…
Browse files Browse the repository at this point in the history
…elds. (amberframework#144)"

This reverts commit c9cfc3d.
  • Loading branch information
maiha committed Mar 31, 2018
1 parent 481f7eb commit 00ae2eb
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
6 changes: 3 additions & 3 deletions spec/granite_orm/fields/primary_key_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions spec/granite_orm/fields/timestamps_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/granite_orm/transactions/create_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/granite_orm/transactions/save_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/granite_orm/associations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions src/granite_orm/fields.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 %}

Expand All @@ -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 %}
Expand Down
7 changes: 1 addition & 6 deletions src/granite_orm/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 00ae2eb

Please sign in to comment.