Skip to content

Commit

Permalink
Infer namespaced models (#144)
Browse files Browse the repository at this point in the history
Closes #143
  • Loading branch information
paulcsmith authored Jul 10, 2019
1 parent a51d9cf commit 6c9ea38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions spec/model_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require "./spec_helper"

class NamedSpaced::Model < BaseModel
table do
end
end

private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.email, users.age"

Expand Down Expand Up @@ -183,7 +188,10 @@ describe Avram::Model do
end

it "can infer the table name when omitted" do
query = InferredTableNameModel::BaseQuery.all
query.to_sql.should eq ["SELECT #{InferredTableNameModel::COLUMN_SQL} FROM inferred_table_name_models"]
InferredTableNameModel.table_name.should eq(:inferred_table_name_models)
end

it "can infer table name for namedspaced models" do
NamedSpaced::Model.table_name.should eq(:named_spaced_models)
end
end
2 changes: 2 additions & 0 deletions src/avram/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Avram::Model
# This setting is used to show better errors
MACRO_CHECKS = {setup_complete: false}

class_getter table_name

macro register_setup_step(call)
{% if MACRO_CHECKS[:setup_complete] %}
{% call.raise "Models have already been set up. Make sure to register set up steps before models are required." %}
Expand Down
2 changes: 1 addition & 1 deletion src/run_macros/infer_table_name.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require "wordsmith"

print Wordsmith::Inflector.pluralize(ARGV[0]).underscore
print Wordsmith::Inflector.pluralize(ARGV[0]).gsub("::", "").underscore

0 comments on commit 6c9ea38

Please sign in to comment.