Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 3 additions & 6 deletions src/amber/cli/helpers/migration.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "inflector"

module Amber::CLI::Helpers::Migration
def create_index_for_reference_fields_sql
sql_statements = reference_fields.map do |field|
Expand All @@ -10,15 +8,14 @@ module Amber::CLI::Helpers::Migration

def create_table_sql
<<-SQL
CREATE TABLE #{Inflector.pluralize(@name)} (
CREATE TABLE #{@name_plural} (
#{@primary_key},
#{create_table_fields_sql}
);
SQL
end

def drop_table_sql
"DROP TABLE IF EXISTS #{Inflector.pluralize(@name)};"
"DROP TABLE IF EXISTS #{@name_plural};"
end

def primary_key
Expand All @@ -37,7 +34,7 @@ module Amber::CLI::Helpers::Migration
private def create_index_for_reference_field_sql(field : Field)
index_name = "#{@name.underscore}_#{field.name}_id_idx"
<<-SQL
CREATE INDEX #{index_name} ON #{Inflector.pluralize(@name)} (#{field.name}_id);
CREATE INDEX #{index_name} ON #{@name_plural} (#{field.name}_id);
SQL
end

Expand Down
3 changes: 1 addition & 2 deletions src/amber/cli/recipes/model.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "../templates/field.cr"
require "inflector"

module Amber::Recipes
class Model < Teeplate::FileTree
Expand Down Expand Up @@ -42,7 +41,7 @@ module Amber::Recipes
end

def table_name
@table_name ||= "#{Inflector.pluralize(@name)}"
@table_name ||= @name_plural
end
end
end
1 change: 1 addition & 0 deletions src/amber/cli/recipes/recipe.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Amber::Recipes
exit 1
end

@name_plural = Inflector.pluralize(word)

@marksiemers marksiemers Jun 18, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does word come from? Isn't this pluralizing @name?

Also, any reason not to create a getter, or write a custom one like this:

def plural_name
  @plural_name ||= Inflector.pluralize(@name)
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @marksiemers 🎉

You're completely right, let me fix that 💯

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, any reason not to create a getter, or write a custom one like this:

Is a getter really needed? I think initialize method already does the job, no?

@name_plural is initialized just one time inside the constructor, so no memoization requirement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done without the getter, assuming it will never be needed publicly.

If it will ever be needed publicly, then creating a private getter now will make things more consistent when that change comes.

I know this isn't ruby, but one of the ruby idioms that I follow is that you minimize directly accessing instance variables (ideally only in the constructor/initialize method).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marksiemers Yeah, nice suggestion 👍

I think we can refactor this in a new PR (@name_plural and others) 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, nice to see you here again 😄 👍

@directory = File.join(directory)
unless Dir.exists?(@directory)
Dir.mkdir_p(@directory)
Expand Down
3 changes: 1 addition & 2 deletions src/amber/cli/recipes/scaffold/controller.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "../../templates/field.cr"
require "inflector"

module Amber::Recipes::Scaffold
class Controller < Teeplate::FileTree
Expand Down Expand Up @@ -34,7 +33,7 @@ module Amber::Recipes::Scaffold
end

add_routes :web, <<-ROUTE
resources "/#{Inflector.pluralize(@name)}", #{class_name}Controller
resources "/#{@name_plural}", #{class_name}Controller
ROUTE
end

Expand Down
3 changes: 1 addition & 2 deletions src/amber/cli/templates/api/controller.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "../field.cr"
require "inflector"

module Amber::CLI::Api
class Controller < Teeplate::FileTree
Expand All @@ -23,7 +22,7 @@ module Amber::CLI::Api
field_hash

add_routes :api, <<-ROUTE
resources "/#{Inflector.pluralize(@name)}", #{class_name}Controller, except: [:new, :edit]
resources "/#{@name_plural}", #{class_name}Controller, except: [:new, :edit]
ROUTE
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> index json" do
<%= class_name %>.clear
model = create_<%= @name.downcase %>
response = subject.get "/<%= Inflector.pluralize(@name) %>"
response = subject.get "/<%= @name_plural %>"

response.status_code.should eq(200)
response.body.should contain("Fake")
Expand All @@ -49,7 +49,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> show json" do
<%= class_name %>.clear
model = create_<%= @name.downcase %>
location = "/<%= Inflector.pluralize(@name) %>/#{model.id}"
location = "/<%= @name_plural %>/#{model.id}"

response = subject.get location

Expand All @@ -59,7 +59,7 @@ describe <%= class_name %>ControllerTest do

it "creates a <%= @name %>" do
<%= class_name %>.clear
response = subject.post "/<%= Inflector.pluralize(@name) %>", body: <%= params_name %>
response = subject.post "/<%= @name_plural %>", body: <%= params_name %>

response.status_code.should eq(201)
response.body.should contain "Fake"
Expand All @@ -68,7 +68,7 @@ describe <%= class_name %>ControllerTest do
it "updates a <%= @name %>" do
<%= class_name %>.clear
model = <%= create_model_method %>
response = subject.patch "/<%= Inflector.pluralize(@name) %>/#{model.id}", body: <%= params_name %>
response = subject.patch "/<%= @name_plural %>/#{model.id}", body: <%= params_name %>

response.status_code.should eq(200)
response.body.should contain "Fake"
Expand All @@ -77,7 +77,7 @@ describe <%= class_name %>ControllerTest do
it "deletes a <%= @name %>" do
<%= class_name %>.clear
model = <%= create_model_method %>
response = subject.delete "/<%= Inflector.pluralize(@name) %>/#{model.id}"
response = subject.delete "/<%= @name_plural %>/#{model.id}"

response.status_code.should eq(204)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class <%= class_name %>Controller < ApplicationController
def index
<%= Inflector.pluralize(@name) %> = Repo.all(<%= class_name %>)
<%= @name_plural %> = Repo.all(<%= class_name %>)
respond_with 200 do
json <%= Inflector.pluralize(@name) %>.to_json
json <%= @name_plural %>.to_json
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> index json" do
<%= class_name %>.clear
model = create_<%= @name.downcase %>
response = subject.get "/<%= Inflector.pluralize(@name) %>"
response = subject.get "/<%= @name_plural %>"

response.status_code.should eq(200)
response.body.should contain("Fake")
Expand All @@ -49,7 +49,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> show json" do
<%= class_name %>.clear
model = create_<%= @name.downcase %>
location = "/<%= Inflector.pluralize(@name) %>/#{model.id}"
location = "/<%= @name_plural %>/#{model.id}"

response = subject.get location

Expand All @@ -59,7 +59,7 @@ describe <%= class_name %>ControllerTest do

it "creates a <%= @name %>" do
<%= class_name %>.clear
response = subject.post "/<%= Inflector.pluralize(@name) %>", body: <%= params_name %>
response = subject.post "/<%= @name_plural %>", body: <%= params_name %>

response.status_code.should eq(201)
response.body.should contain "Fake"
Expand All @@ -68,7 +68,7 @@ describe <%= class_name %>ControllerTest do
it "updates a <%= @name %>" do
<%= class_name %>.clear
model = <%= create_model_method %>
response = subject.patch "/<%= Inflector.pluralize(@name) %>/#{model.id}", body: <%= params_name %>
response = subject.patch "/<%= @name_plural %>/#{model.id}", body: <%= params_name %>

response.status_code.should eq(200)
response.body.should contain "Fake"
Expand All @@ -77,7 +77,7 @@ describe <%= class_name %>ControllerTest do
it "deletes a <%= @name %>" do
<%= class_name %>.clear
model = <%= create_model_method %>
response = subject.delete "/<%= Inflector.pluralize(@name) %>/#{model.id}"
response = subject.delete "/<%= @name_plural %>/#{model.id}"

response.status_code.should eq(204)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class <%= class_name %>Controller < ApplicationController
def index
<%= Inflector.pluralize(@name) %> = <%= class_name %>.all
<%= @name_plural %> = <%= class_name %>.all
respond_with 200 do
json <%= Inflector.pluralize(@name) %>.to_json
json <%= @name_plural %>.to_json
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "crypto/bcrypt/password"
class <%= class_name %> < Crecto::Model
include Crypto

schema "<%= Inflector.pluralize(@name) %>" do
schema "<%= @name_plural %>" do
<% @fields.reject{|f| f.hidden }.each do |field| -%>
field :<%= field.name %>, <%= field.cr_type %>
<% end -%>
Expand Down
3 changes: 1 addition & 2 deletions src/amber/cli/templates/model.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "./field.cr"
require "inflector"

module Amber::CLI
class Model < Teeplate::FileTree
Expand All @@ -22,7 +21,7 @@ module Amber::CLI
end

def table_name
@table_name ||= "#{Inflector.pluralize(@name)}"
@table_name ||= @name_plural
end
end
end
3 changes: 1 addition & 2 deletions src/amber/cli/templates/scaffold/controller.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "../field.cr"
require "inflector"

module Amber::CLI::Scaffold
class Controller < Teeplate::FileTree
Expand All @@ -23,7 +22,7 @@ module Amber::CLI::Scaffold
field_hash

add_routes :web, <<-ROUTE
resources "/#{Inflector.pluralize(@name)}", #{class_name}Controller
resources "/#{@name_plural}", #{class_name}Controller
ROUTE
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe <%= class_name %>ControllerTest do

it "renders <%= @name %> index template" do
Repo.delete_all(<%= class_name %>)
response = subject.get "/<%= Inflector.pluralize(@name) %>"
response = subject.get "/<%= @name_plural %>"

response.status_code.should eq(200)
response.body.should contain("<%= display_name %>s")
Expand All @@ -50,7 +50,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> show template" do
Repo.delete_all(<%= class_name %>)
model = create_<%= @name.downcase %>
location = "/<%= Inflector.pluralize(@name) %>/#{model.id}"
location = "/<%= @name_plural %>/#{model.id}"

response = subject.get location

Expand All @@ -60,7 +60,7 @@ describe <%= class_name %>ControllerTest do

it "renders <%= @name %> new template" do
Repo.delete_all(<%= class_name %>)
location = "/<%= Inflector.pluralize(@name) %>/new"
location = "/<%= @name_plural %>/new"

response = subject.get location

Expand All @@ -71,7 +71,7 @@ describe <%= class_name %>ControllerTest do
it "renders <%= @name %> edit template" do
Repo.delete_all(<%= class_name %>)
model = <%= create_model_method %>
location = "/<%= Inflector.pluralize(@name) %>/#{model.id}/edit"
location = "/<%= @name_plural %>/#{model.id}/edit"

response = subject.get location

Expand All @@ -81,29 +81,29 @@ describe <%= class_name %>ControllerTest do

it "creates a <%= @name %>" do
Repo.delete_all(<%= class_name %>)
response = subject.post "/<%= Inflector.pluralize(@name) %>", body: <%= params_name %>
response = subject.post "/<%= @name_plural %>", body: <%= params_name %>

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= @name_plural %>"
response.status_code.should eq(302)
response.body.should eq "302"
end

it "updates a <%= @name %>" do
Repo.delete_all(<%= class_name %>)
model = <%= create_model_method %>
response = subject.patch "/<%= Inflector.pluralize(@name) %>/#{model.id}", body: <%= params_name %>
response = subject.patch "/<%= @name_plural %>/#{model.id}", body: <%= params_name %>

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= @name_plural %>"
response.status_code.should eq(302)
response.body.should eq "302"
end

it "deletes a <%= @name %>" do
Repo.delete_all(<%= class_name %>)
model = <%= create_model_method %>
response = subject.delete "/<%= Inflector.pluralize(@name) %>/#{model.id}"
response = subject.delete "/<%= @name_plural %>/#{model.id}"

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= @name_plural %>"
response.status_code.should eq(302)
response.body.should eq "302"
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class <%= class_name %>Controller < ApplicationController
def index
<%= Inflector.pluralize(@name) %> = Repo.all(<%= class_name %>)
<%= @name_plural %> = Repo.all(<%= class_name %>)
render("index.<%= @language %>")
end

Expand All @@ -9,7 +9,7 @@ class <%= class_name %>Controller < ApplicationController
render("show.<%= @language %>")
else
flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found"
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end
end

Expand All @@ -29,7 +29,7 @@ class <%= class_name %>Controller < ApplicationController
render("new.<%= @language %>")
else
flash["success"] = "Created <%= class_name %> successfully."
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end
end

Expand All @@ -39,7 +39,7 @@ class <%= class_name %>Controller < ApplicationController
render("edit.<%= @language %>")
else
flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found"
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end
end

Expand All @@ -53,11 +53,11 @@ class <%= class_name %>Controller < ApplicationController
render("edit.<%= @language %>")
else
flash["success"] = "Updated <%= class_name %> successfully."
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end
else
flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found"
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end
end

Expand All @@ -67,7 +67,7 @@ class <%= class_name %>Controller < ApplicationController
else
flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found"
end
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= @name_plural %>"
end

def <%= @name %>_params
Expand Down
Loading