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

Return error if you try to generate a model that already exists #1127

Merged
merged 3 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions spec/tasks/gen/model_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ describe Gen::Model do
io.to_s.should contain("Model name should only contain letters")
end
end

it "displays an error if the model has already been generated" do
with_cleanup do
ARGV.push("User")

Gen::Migration.silence_output do
io = IO::Memory.new
Gen::Model.new.call(io)
end

io = IO::Memory.new
Gen::Model.new.call(io)
io.to_s.should contain("A model by the name 'User' already exists.")
end
end
end
8 changes: 7 additions & 1 deletion tasks/gen/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Gen::Model < LuckyCli::Task
resource_name_is_present &&
resource_name_is_camelcase &&
resource_name_matches_format &&
columns_are_supported
columns_are_supported &&
resource_name_not_taken
end

private def resource_name_is_present
Expand All @@ -59,6 +60,11 @@ class Gen::Model < LuckyCli::Task
columns_are_valid?
end

private def resource_name_not_taken
@error = "A model by the name '#{resource_name.camelcase}' already exists."
jwoertink marked this conversation as resolved.
Show resolved Hide resolved
!File.exists?("./src/models/#{template.underscored_name}.cr")
end

private def template
Lucky::ModelTemplate.new(resource_name, columns)
end
Expand Down