Skip to content

Commit

Permalink
add belongs_to custom type support (#142)
Browse files Browse the repository at this point in the history
* add belongs_to custom type support

* add tests for mysql and sqlite

* fix custom types
  • Loading branch information
drujensen authored Mar 28, 2018
1 parent 6d160a2 commit 13ccfc0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/granite_orm/associations/belongs_to_custom_type_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} belongs_to" do
it "supports custom types for the join" do
book = Book.new
book.name = "Screw driver"
book.save

review = BookReview.new
review.book = book
review.body = "Best book ever!"
review.save

review.book.name.should eq "Screw driver"
end
end
end
{% end %}
47 changes: 47 additions & 0 deletions spec/spec_models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ end
if adapter == "pg"
primary_key_sql = "BIGSERIAL PRIMARY KEY".id
foreign_key_sql = "BIGINT".id
custom_primary_key_sql = "SERIAL PRIMARY KEY".id
custom_foreign_key_sql = "INT".id
created_at_sql = "created_at TIMESTAMP".id
updated_at_sql = "updated_at TIMESTAMP".id
timestamp_fields = "timestamps".id
elsif adapter == "mysql"
primary_key_sql = "BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY".id
foreign_key_sql = "BIGINT".id
custom_primary_key_sql = "INT NOT NULL AUTO_INCREMENT PRIMARY KEY".id
custom_foreign_key_sql = "INT".id
created_at_sql = "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP".id
updated_at_sql = "updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".id
timestamp_fields = "timestamps".id
elsif adapter == "sqlite"
primary_key_sql = "INTEGER NOT NULL PRIMARY KEY".id
foreign_key_sql = "INTEGER".id
custom_primary_key_sql = "INTEGER NOT NULL PRIMARY KEY".id
custom_foreign_key_sql = "INTEGER".id
created_at_sql = "created_at VARCHAR".id
updated_at_sql = "updated_at VARCHAR".id
timestamp_fields = "timestamps".id
Expand Down Expand Up @@ -279,6 +285,45 @@ end
end
end

class Book < Granite::ORM::Base
adapter {{ adapter_literal }}
table_name books
has_many :book_reviews

primary id : Int32
field name : String

def self.drop_and_create
exec("DROP TABLE IF EXISTS #{ quoted_table_name };")
exec <<-SQL
CREATE TABLE #{ quoted_table_name } (
id {{ custom_primary_key_sql }},
name VARCHAR(255)
)
SQL
end
end

class BookReview < Granite::ORM::Base
adapter {{ adapter_literal }}
table_name book_reviews
belongs_to :book, book_id : Int32

primary id : Int32
field body : String

def self.drop_and_create
exec("DROP TABLE IF EXISTS #{ quoted_table_name };")
exec <<-SQL
CREATE TABLE #{ quoted_table_name } (
id {{ custom_primary_key_sql }},
book_id {{ custom_foreign_key_sql }},
body VARCHAR(255)
)
SQL
end
end

Parent.drop_and_create
Teacher.drop_and_create
Student.drop_and_create
Expand All @@ -291,5 +336,7 @@ end
ReservedWord.drop_and_create
Callback.drop_and_create
Kvs.drop_and_create
Book.drop_and_create
BookReview.drop_and_create
end
{% end %}
6 changes: 5 additions & 1 deletion src/granite_orm/associations.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Granite::ORM::Associations
# define getter and setter for parent relationship
macro belongs_to(model_name)
field {{model_name.id}}_id : Int64
belongs_to {{model_name}}, {{model_name.id}}_id : Int64
end

macro belongs_to(model_name, decl)
field {{decl.var}} : {{decl.type}}

# retrieve the parent relationship
def {{model_name.id}}
Expand Down

0 comments on commit 13ccfc0

Please sign in to comment.