Skip to content
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
4 changes: 4 additions & 0 deletions src/amber/cli/helpers/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ module Amber::CLI::Helpers
Process.new(command, shell: shell, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
end
end

def pluralize(word)
Inflector.pluralize(word)
end
end
8 changes: 3 additions & 5 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,15 @@ module Amber::CLI::Helpers::Migration

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

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

def primary_key
Expand All @@ -37,7 +35,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 #{pluralize(@name)} (#{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 ||= "#{pluralize(@name)}"
end
end
end
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 "/#{pluralize(@name)}", #{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 "/#{pluralize(@name)}", #{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 "/<%= pluralize(@name) %>"

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 = "/<%= pluralize(@name) %>/#{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 "/<%= pluralize(@name) %>", 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 "/<%= pluralize(@name) %>/#{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 "/<%= pluralize(@name) %>/#{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 %>)
<%= pluralize(@name) %> = Repo.all(<%= class_name %>)
respond_with 200 do
json <%= Inflector.pluralize(@name) %>.to_json
json <%= pluralize(@name) %>.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 "/<%= pluralize(@name) %>"

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 = "/<%= pluralize(@name) %>/#{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 "/<%= pluralize(@name) %>", 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 "/<%= pluralize(@name) %>/#{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 "/<%= pluralize(@name) %>/#{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
<%= pluralize(@name) %> = <%= class_name %>.all
respond_with 200 do
json <%= Inflector.pluralize(@name) %>.to_json
json <%= pluralize(@name) %>.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 "<%= pluralize(@name) %>" 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 ||= "#{pluralize(@name)}"
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 "/#{pluralize(@name)}", #{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 "/<%= pluralize(@name) %>"

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 = "/<%= pluralize(@name) %>/#{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 = "/<%= pluralize(@name) %>/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 = "/<%= pluralize(@name) %>/#{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 "/<%= pluralize(@name) %>", body: <%= params_name %>

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>/#{model.id}", body: <%= params_name %>

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>/#{model.id}"

response.headers["Location"].should eq "/<%= Inflector.pluralize(@name) %>"
response.headers["Location"].should eq "/<%= pluralize(@name) %>"
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 %>)
<%= pluralize(@name) %> = 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 "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>"
end
else
flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found"
redirect_to "/<%= Inflector.pluralize(@name) %>"
redirect_to "/<%= pluralize(@name) %>"
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 "/<%= pluralize(@name) %>"
end

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