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

Infer namespaced models #144

Merged
merged 1 commit into from
Jul 10, 2019
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
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