Skip to content

Commit

Permalink
Handle false when a column has a default value (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored and drujensen committed Dec 21, 2019
1 parent cbf7669 commit 55f11af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions spec/granite_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe Granite::Base do
f.table.should eq "teachers"
end

it "should allow false as a column value" do
model = BoolModel.create active: false

model.active.should be_false
model.id.should eq 1

fetched_model = BoolModel.find! model.id
fetched_model.active.should be_false
end

describe "instantiation" do
describe "with default values" do
it "should instaniate correctly" do
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ end
column float : Float64
end

class BoolModel < Granite::Base
connection {{ adapter_literal }}
table "bool_model"

column id : Int64, primary: true
column active : Bool = true
end

struct MyType
include JSON::Serializable

Expand Down
8 changes: 7 additions & 1 deletion src/granite/columns.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ module Granite::Columns
@{{column.id}} = {% if ann[:converter] %}
{{ann[:converter]}}.from_rs result
{% else %}
Granite::Type.from_rs(result, {{ann[:nilable] ? column.type : column.type.union_types.reject { |t| t == Nil }.first}}) {% if column.has_default_value? && !column.default_value.nil? %} || {{column.default_value}} {% end %}
value = Granite::Type.from_rs(result, {{ann[:nilable] ? column.type : column.type.union_types.reject { |t| t == Nil }.first}})

{% if column.has_default_value? && !column.default_value.nil? %}
return {{column.default_value}} if value.nil?
{% end %}

value
{% end %}
{% end %}
end
Expand Down

0 comments on commit 55f11af

Please sign in to comment.