Skip to content

Commit

Permalink
Return error if you try to generate a model that already exists (#1127)
Browse files Browse the repository at this point in the history
* return an error message if you try to generate a model that already exists. Fixes #1008

* Update tasks/gen/model.cr

Co-Authored-By: Paul Smith <paulcsmith@users.noreply.github.com>

* fixing failing specs

Co-authored-by: Paul Smith <paulcsmith@users.noreply.github.com>
  • Loading branch information
jwoertink and paulcsmith authored Apr 30, 2020
1 parent f501863 commit 6352349
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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("'User' model already exists at ./src/models/user.cr")
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 = "'#{resource_name.camelcase}' model already exists at #{"./src/models/#{template.underscored_name}.cr"}."
!File.exists?("./src/models/#{template.underscored_name}.cr")
end

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

0 comments on commit 6352349

Please sign in to comment.