Skip to content
This repository has been archived by the owner on Oct 28, 2019. It is now read-only.

Commit

Permalink
Add belongs_to and has_many
Browse files Browse the repository at this point in the history
Close #53
Close #54
  • Loading branch information
paulcsmith committed Oct 19, 2017
1 parent a395448 commit 6a64cb0
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 0 deletions.
17 changes: 17 additions & 0 deletions db/migrations/20171006163153_create_posts_and_comments.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreatePostsAndComments::V20171006163153 < LuckyMigrator::Migration::V1
def migrate
create :posts do
add title : String
end

create :comments do
add body : String
add post_id : Int32
end
end

def rollback
drop :posts
drop :comments
end
end
13 changes: 13 additions & 0 deletions spec/associations_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "./spec_helper"

describe LuckyRecord::Model do
it "gets the related records" do
post = PostBox.save
comment = CommentBox.new.post_id(post.id).save

post = Post::BaseQuery.new.find(post.id)

post.comments.to_a.should eq [comment]
comment.post.should eq post
end
end
8 changes: 8 additions & 0 deletions spec/support/comment.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "./post"

class Comment < LuckyRecord::Model
table :comments do
field body : String
belongs_to post, Post
end
end
26 changes: 26 additions & 0 deletions spec/support/comment_box.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class CommentBox
getter form

@form = Comment::BaseForm.new

def initialize
body "Best comment ever"
end

def self.save
new.save
end

def save
if form.save
form.record.not_nil!
else
raise "Did not save. Make sure fields are valid"
end
end

macro method_missing(call)
form.{{ call.name.id }}.value = {{ call.args.first }}
self
end
end
8 changes: 8 additions & 0 deletions spec/support/post.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "./comment"

class Post < LuckyRecord::Model
table :posts do
field title : String
has_many comments, Comment
end
end
26 changes: 26 additions & 0 deletions spec/support/post_box.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class PostBox
getter form

@form = Post::BaseForm.new

def initialize
title "My Cool Title"
end

def self.save
new.save
end

def save
if form.save
form.record.not_nil!
else
raise "Did not save. Make sure fields are valid"
end
end

macro method_missing(call)
form.{{ call.name.id }}.value = {{ call.args.first }}
self
end
end
15 changes: 15 additions & 0 deletions src/lucky_record/associations.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module LuckyRecord::Associations
macro has_many(assoc_name, model)
def {{ assoc_name.id }}
{{ model }}::BaseQuery.new.{{ @type.name.underscore }}_id(id)
end
end

macro belongs_to(assoc_name, model)
field {{ assoc_name.id }}_id : Int32

def {{ assoc_name.id }}
{{ model }}::BaseQuery.new.find({{ assoc_name.id }}_id)
end
end
end
2 changes: 2 additions & 0 deletions src/lucky_record/model.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class LuckyRecord::Model
include LuckyRecord::Associations

macro inherited
FIELDS = [] of {name: Symbol, type: Object, nilable: Bool, autogenerated: Bool}

Expand Down

0 comments on commit 6a64cb0

Please sign in to comment.