Skip to content

Commit

Permalink
Fix #394 by stripping the module name from the table name (#395)
Browse files Browse the repository at this point in the history
* Attempt to fix #394

* Update spec/spec_models.cr

Co-authored-by: George Dietrich <george@dietrich.app>

Co-authored-by: Dru Jensen <drujensen@gmail.com>
Co-authored-by: George Dietrich <george@dietrich.app>
  • Loading branch information
3 people committed May 4, 2020
1 parent 3d849eb commit 536a9b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions spec/granite/table/table_spec.cr
Expand Up @@ -9,6 +9,10 @@ describe Granite::Table do
it "sets the table name based on class name if not specified" do
SongThread.table_name.should eq "song_thread"
end

it "strips the namespace when defining the default table now" do
MyApp::Namespace::Model.table_name.should eq "model"
end
end

describe ".primary_name" do
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_models.cr
Expand Up @@ -509,6 +509,12 @@ end
column my_enum : MyEnum?, column_type: "TEXT", converter: Granite::Converters::Enum(MyEnum, String)
end

class MyApp::Namespace::Model < Granite::Base
connection {{ adapter_literal }}

column id : Int64, primary: true
end

{% if env("CURRENT_ADAPTER") == "pg" %}
class ConverterModel < Granite::Base
connection {{ adapter_literal }}
Expand Down
2 changes: 1 addition & 1 deletion src/granite/table.cr
Expand Up @@ -45,7 +45,7 @@ module Granite::Tables
def table_name : String
{% begin %}
{% table_ann = @type.annotation(Granite::Table) %}
{{table_ann && !table_ann[:name].nil? ? table_ann[:name] : @type.name.underscore.stringify}}
{{table_ann && !table_ann[:name].nil? ? table_ann[:name] : @type.name.underscore.stringify.split("::").last}}
{% end %}
end
end
Expand Down

0 comments on commit 536a9b4

Please sign in to comment.