Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Associations from Fields #55

Merged
merged 1 commit into from
Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/adapter/pg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Granite::Adapter::Pg < Granite::Adapter::Base
num_subs = clause.count("?")

num_subs.times do |i|
clause = clause.sub("?", "$#{i+1}")
clause = clause.sub("?", "$#{i + 1}")
end
end

Expand Down
34 changes: 34 additions & 0 deletions src/granite_orm/associations.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Granite::ORM::Associations
# define getter and setter for parent relationship
macro belongs_to(model_name)
field {{model_name.id}}_id : Int64

# retrieve the parent relationship
def {{model_name.id}}
if parent = {{model_name.id.camelcase}}.find {{model_name.id}}_id
parent
else
{{model_name.id.camelcase}}.new
end
end

# set the parent relationship
def {{model_name.id}}=(parent)
@{{model_name.id}}_id = parent.id
end
end

# define getter for related children
macro has_many(children_table)
def {{children_table.id}}
{% children_class = children_table.id[0...-1].camelcase %}
{% name_space = @type.name.gsub(/::/, "_").downcase.id %}
{% table_name = SETTINGS[:table_name] || name_space + "s" %}
foreign_key = "{{children_table.id}}.{{table_name[0...-1]}}_id"
query = "JOIN {{table_name}} on {{table_name}}.id = #{foreign_key} WHERE {{table_name}}.id = ?"

return [] of {{children_class}} unless id
{{children_class}}.all(query, id)
end
end
end
4 changes: 3 additions & 1 deletion src/granite_orm/base.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "./associations"
require "./callbacks"
require "./fields"
require "./querying"
Expand All @@ -9,9 +10,10 @@ require "./version"

# Granite::ORM::Base is the base class for your model objects.
class Granite::ORM::Base
include Settings
include Associations
include Callbacks
include Fields
include Settings
include Table
include Transactions

Expand Down
33 changes: 0 additions & 33 deletions src/granite_orm/fields.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,6 @@ module Granite::ORM::Fields
{% SETTINGS[:timestamps] = true %}
end

# define getter and setter for parent relationship
macro belongs_to(model_name)
field {{model_name.id}}_id : Int64

# retrieve the parent relationship
def {{model_name.id}}
if parent = {{model_name.id.camelcase}}.find {{model_name.id}}_id
parent
else
{{model_name.id.camelcase}}.new
end
end

# set the parent relationship
def {{model_name.id}}=(parent)
@{{model_name.id}}_id = parent.id
end
end

# define getter for related children
macro has_many(children_table)
def {{children_table.id}}
{% children_class = children_table.id[0...-1].camelcase %}
{% name_space = @type.name.gsub(/::/, "_").downcase.id %}
{% table_name = SETTINGS[:table_name] || name_space + "s" %}
foreign_key = "{{children_table.id}}.{{table_name[0...-1]}}_id"
query = "JOIN {{table_name}} on {{table_name}}.id = #{foreign_key} WHERE {{table_name}}.id = ?"

return [] of {{children_class}} unless id
{{children_class}}.all(query, id)
end
end

macro __process_fields
# Create the properties
{% for name, type in FIELDS %}
Expand Down
3 changes: 1 addition & 2 deletions src/granite_orm/validators.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Granite::ORM::Validators
macro validate(field, message, block)
@@validators << {field: {{field}}, message: {{message}}, block: {{block}}}
end

def valid?
@@validators.each do |validator|
unless validator[:block].call(self)
Expand All @@ -24,4 +24,3 @@ module Granite::ORM::Validators
errors.empty?
end
end