This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a395448
commit 6a64cb0
Showing
8 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters