diff --git a/src/amber.cr b/src/amber.cr index a479511db..3fac468c1 100644 --- a/src/amber.cr +++ b/src/amber.cr @@ -7,7 +7,7 @@ require "kilt" require "kilt/slang" require "redis" require "compiled_license" -require "inflector" + require "./amber/version" require "./amber/controller/**" require "./amber/dsl/**" diff --git a/src/amber/cli/helpers/migration.cr b/src/amber/cli/helpers/migration.cr index f65e991cc..89120b755 100644 --- a/src/amber/cli/helpers/migration.cr +++ b/src/amber/cli/helpers/migration.cr @@ -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| @@ -10,7 +8,7 @@ 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} ); @@ -18,7 +16,7 @@ module Amber::CLI::Helpers::Migration end def drop_table_sql - "DROP TABLE IF EXISTS #{Inflector.pluralize(@name)};" + "DROP TABLE IF EXISTS #{name_plural};" end def primary_key @@ -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 #{name_plural} (#{field.name}_id); SQL end diff --git a/src/amber/cli/recipes/model.cr b/src/amber/cli/recipes/model.cr index bf8bede11..6792143ff 100644 --- a/src/amber/cli/recipes/model.cr +++ b/src/amber/cli/recipes/model.cr @@ -1,5 +1,4 @@ require "../templates/field.cr" -require "inflector" module Amber::Recipes class Model < Teeplate::FileTree @@ -8,11 +7,12 @@ module Amber::Recipes @name : String @fields : Array(Amber::CLI::Field) - @database : String = CLI.config.database @model : String = CLI.config.model + @database : String = CLI.config.database - @template : String | Nil - @recipe : String | Nil + @recipe : String? + @template : String? + @table_name : String? def initialize(@name, @recipe, fields) @fields = fields.map { |field| Amber::CLI::Field.new(field, database: @database) } @@ -42,7 +42,7 @@ module Amber::Recipes end def table_name - @table_name ||= "#{Inflector.pluralize(@name)}" + @table_name ||= name_plural end end end diff --git a/src/amber/cli/recipes/recipe.cr b/src/amber/cli/recipes/recipe.cr index cb9afb45d..7dba63b42 100644 --- a/src/amber/cli/recipes/recipe.cr +++ b/src/amber/cli/recipes/recipe.cr @@ -1,6 +1,7 @@ require "teeplate" require "liquid" require "base64" +require "inflector" require "random/secure" require "../helpers/helpers" @@ -127,7 +128,9 @@ end module Teeplate abstract class FileTree @class_name : String? + @name_plural : String? @display_name : String? + @display_name_plural : String? # Renders all collected file entries. # @@ -144,6 +147,10 @@ module Teeplate entries end + def name_plural + @name_plural ||= Inflector.pluralize(@name) + end + def class_name @class_name ||= @name.camelcase end @@ -152,6 +159,10 @@ module Teeplate @display_name ||= generate_display_name end + def display_name_plural + @display_name_plural ||= Inflector.pluralize(display_name) + end + private def generate_display_name @name.underscore.gsub('-', '_').split('_').map(&.capitalize).join(' ') end diff --git a/src/amber/cli/recipes/scaffold/controller.cr b/src/amber/cli/recipes/scaffold/controller.cr index 560057d41..4aacaef0c 100644 --- a/src/amber/cli/recipes/scaffold/controller.cr +++ b/src/amber/cli/recipes/scaffold/controller.cr @@ -1,5 +1,4 @@ require "../../templates/field.cr" -require "inflector" module Amber::Recipes::Scaffold class Controller < Teeplate::FileTree @@ -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 diff --git a/src/amber/cli/templates/api/controller.cr b/src/amber/cli/templates/api/controller.cr index dbd30519a..eb1d7c815 100644 --- a/src/amber/cli/templates/api/controller.cr +++ b/src/amber/cli/templates/api/controller.cr @@ -1,5 +1,4 @@ require "../field.cr" -require "inflector" module Amber::CLI::Api class Controller < Teeplate::FileTree @@ -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 diff --git a/src/amber/cli/templates/api/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr b/src/amber/cli/templates/api/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr index ed0ab0791..ca8495580 100644 --- a/src/amber/cli/templates/api/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr +++ b/src/amber/cli/templates/api/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr @@ -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") @@ -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 @@ -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" @@ -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" @@ -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 diff --git a/src/amber/cli/templates/api/controller/crecto/src/controllers/{{name}}_controller.cr.ecr b/src/amber/cli/templates/api/controller/crecto/src/controllers/{{name}}_controller.cr.ecr index 65f8ded5f..b6e3e546f 100644 --- a/src/amber/cli/templates/api/controller/crecto/src/controllers/{{name}}_controller.cr.ecr +++ b/src/amber/cli/templates/api/controller/crecto/src/controllers/{{name}}_controller.cr.ecr @@ -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 diff --git a/src/amber/cli/templates/api/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr b/src/amber/cli/templates/api/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr index ed0ab0791..ca8495580 100644 --- a/src/amber/cli/templates/api/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr +++ b/src/amber/cli/templates/api/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr @@ -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") @@ -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 @@ -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" @@ -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" @@ -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 diff --git a/src/amber/cli/templates/api/controller/granite/src/controllers/{{name}}_controller.cr.ecr b/src/amber/cli/templates/api/controller/granite/src/controllers/{{name}}_controller.cr.ecr index ef6124182..ea05a346c 100644 --- a/src/amber/cli/templates/api/controller/granite/src/controllers/{{name}}_controller.cr.ecr +++ b/src/amber/cli/templates/api/controller/granite/src/controllers/{{name}}_controller.cr.ecr @@ -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 diff --git a/src/amber/cli/templates/auth/crecto/src/models/{{name}}.cr.ecr b/src/amber/cli/templates/auth/crecto/src/models/{{name}}.cr.ecr index c0acae219..7116533f7 100644 --- a/src/amber/cli/templates/auth/crecto/src/models/{{name}}.cr.ecr +++ b/src/amber/cli/templates/auth/crecto/src/models/{{name}}.cr.ecr @@ -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 -%> diff --git a/src/amber/cli/templates/model.cr b/src/amber/cli/templates/model.cr index c81fe1f5b..fc098bef3 100644 --- a/src/amber/cli/templates/model.cr +++ b/src/amber/cli/templates/model.cr @@ -1,5 +1,4 @@ require "./field.cr" -require "inflector" module Amber::CLI class Model < Teeplate::FileTree @@ -9,6 +8,7 @@ module Amber::CLI @name : String @fields : Array(Field) @database : String = CLI.config.database + @table_name : String? def initialize(@name, fields) @fields = fields.map { |field| Field.new(field, database: @database) } @@ -22,7 +22,7 @@ module Amber::CLI end def table_name - @table_name ||= "#{Inflector.pluralize(@name)}" + @table_name ||= name_plural end end end diff --git a/src/amber/cli/templates/scaffold/controller.cr b/src/amber/cli/templates/scaffold/controller.cr index ea7e053f4..b7f04ff24 100644 --- a/src/amber/cli/templates/scaffold/controller.cr +++ b/src/amber/cli/templates/scaffold/controller.cr @@ -1,5 +1,4 @@ require "../field.cr" -require "inflector" module Amber::CLI::Scaffold class Controller < Teeplate::FileTree @@ -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 diff --git a/src/amber/cli/templates/scaffold/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr b/src/amber/cli/templates/scaffold/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr index 160359b50..8148d4af8 100644 --- a/src/amber/cli/templates/scaffold/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr +++ b/src/amber/cli/templates/scaffold/controller/crecto/spec/controllers/{{name}}_controller_spec.cr.ecr @@ -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") @@ -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 @@ -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 @@ -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 @@ -81,9 +81,9 @@ 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 @@ -91,9 +91,9 @@ describe <%= class_name %>ControllerTest do 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 @@ -101,9 +101,9 @@ describe <%= class_name %>ControllerTest do 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 diff --git a/src/amber/cli/templates/scaffold/controller/crecto/src/controllers/{{name}}_controller.cr.ecr b/src/amber/cli/templates/scaffold/controller/crecto/src/controllers/{{name}}_controller.cr.ecr index d8366e7a7..6cbe876f0 100644 --- a/src/amber/cli/templates/scaffold/controller/crecto/src/controllers/{{name}}_controller.cr.ecr +++ b/src/amber/cli/templates/scaffold/controller/crecto/src/controllers/{{name}}_controller.cr.ecr @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/amber/cli/templates/scaffold/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr b/src/amber/cli/templates/scaffold/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr index 296b87ccc..3c814d3de 100644 --- a/src/amber/cli/templates/scaffold/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr +++ b/src/amber/cli/templates/scaffold/controller/granite/spec/controllers/{{name}}_controller_spec.cr.ecr @@ -40,16 +40,16 @@ describe <%= class_name %>ControllerTest do it "renders <%= @name %> index template" do <%= class_name %>.clear - response = subject.get "/<%= Inflector.pluralize(@name) %>" + response = subject.get "/<%= name_plural %>" response.status_code.should eq(200) - response.body.should contain("<%= Inflector.pluralize(display_name) %>") + response.body.should contain("<%= name_plural %>") end it "renders <%= @name %> show template" do <%= class_name %>.clear model = create_<%= @name.downcase %> - location = "/<%= Inflector.pluralize(@name) %>/#{model.id}" + location = "/<%= name_plural %>/#{model.id}" response = subject.get location @@ -59,7 +59,7 @@ describe <%= class_name %>ControllerTest do it "renders <%= @name %> new template" do <%= class_name %>.clear - location = "/<%= Inflector.pluralize(@name) %>/new" + location = "/<%= name_plural %>/new" response = subject.get location @@ -70,7 +70,7 @@ describe <%= class_name %>ControllerTest do it "renders <%= @name %> edit template" do <%= class_name %>.clear model = <%= create_model_method %> - location = "/<%= Inflector.pluralize(@name) %>/#{model.id}/edit" + location = "/<%= name_plural %>/#{model.id}/edit" response = subject.get location @@ -80,9 +80,9 @@ 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.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 @@ -90,9 +90,9 @@ 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.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 @@ -100,9 +100,9 @@ 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.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 diff --git a/src/amber/cli/templates/scaffold/controller/granite/src/controllers/{{name}}_controller.cr.ecr b/src/amber/cli/templates/scaffold/controller/granite/src/controllers/{{name}}_controller.cr.ecr index 32c1b94c3..86b4e661c 100644 --- a/src/amber/cli/templates/scaffold/controller/granite/src/controllers/{{name}}_controller.cr.ecr +++ b/src/amber/cli/templates/scaffold/controller/granite/src/controllers/{{name}}_controller.cr.ecr @@ -1,6 +1,6 @@ class <%= class_name %>Controller < ApplicationController def index - <%= Inflector.pluralize(@name) %> = <%= class_name %>.all + <%= name_plural %> = <%= class_name %>.all render("index.<%= @language %>") end @@ -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 @@ -23,7 +23,7 @@ class <%= class_name %>Controller < ApplicationController if <%= @name %>.valid? && <%= @name %>.save flash["success"] = "Created <%= class_name %> successfully." - redirect_to "/<%= Inflector.pluralize(@name) %>" + redirect_to "/<%= name_plural %>" else flash["danger"] = "Could not create <%= class_name %>!" render("new.<%= @language %>") @@ -35,7 +35,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 @@ -44,14 +44,14 @@ class <%= class_name %>Controller < ApplicationController <%= @name %>.set_attributes(<%= @name %>_params.validate!) if <%= @name %>.valid? && <%= @name %>.save flash["success"] = "Updated <%= class_name %> successfully." - redirect_to "/<%= Inflector.pluralize(@name) %>" + redirect_to "/<%= name_plural %>" else flash["danger"] = "Could not update <%= class_name %>!" render("edit.<%= @language %>") end else flash["warning"] = "<%= class_name %> with ID #{params["id"]} Not Found" - redirect_to "/<%= Inflector.pluralize(@name) %>" + redirect_to "/<%= name_plural %>" end end @@ -61,7 +61,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 diff --git a/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.ecr.ecr b/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.ecr.ecr index c01a56e21..efa5a67e4 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.ecr.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.ecr.ecr @@ -1,2 +1,2 @@ -<%="<"%>%- active = context.request.path == "/<%= Inflector.pluralize(@name) %>" ? "active" : "" %> -%= active %>" href="/<%= Inflector.pluralize(@name) %>"><%= display_name %>s +<%="<"%>%- active = context.request.path == "/<%= name_plural %>" ? "active" : "" %> +%= active %>" href="/<%= name_plural %>"><%= display_name %>s diff --git a/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.slang.ecr b/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.slang.ecr index f7aee36cc..c764508d6 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.slang.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/layouts/+_nav.slang.ecr @@ -1,2 +1,2 @@ -- active = context.request.path == "/<%= Inflector.pluralize(@name) %>" ? "active" : "" -a class="nav-item #{active}" href="/<%= Inflector.pluralize(@name) %>" <%= Inflector.pluralize(display_name) %> +- active = context.request.path == "/<%= name_plural %>" ? "active" : "" +a class="nav-item #{active}" href="/<%= name_plural %>" <%= display_name_plural %> diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.ecr.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.ecr.ecr index 7b5ec17c5..76a362aae 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.ecr.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.ecr.ecr @@ -8,7 +8,7 @@ -<%="<"%>%- action = (<%= @name %>.id ? "/<%= Inflector.pluralize(@name) %>/" + <%= @name %>.id.to_s : "/<%= Inflector.pluralize(@name) %>") %> +<%="<"%>%- action = (<%= @name %>.id ? "/<%= name_plural %>/" + <%= @name %>.id.to_s : "/<%= name_plural %>") %>
%= action %>" method="post"> <%="<"%>%= csrf_tag %> <%="<"%>%- if <%= @name %>.id %> @@ -34,5 +34,5 @@ <% end -%> <%="<"%>%= submit("Submit", class: "btn btn-primary btn-sm") -%> - <%="<"%>%= link_to("back", "/<%= Inflector.pluralize(@name) %>", class: "btn btn-light btn-sm") -%> + <%="<"%>%= link_to("back", "/<%= name_plural %>", class: "btn btn-light btn-sm") -%>
diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.slang.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.slang.ecr index 3bc341572..da88cb953 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.slang.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/_form.slang.ecr @@ -3,7 +3,7 @@ - <%= @model == "crecto" ? "changeset" : @name %>.errors.each do |error| li = error.to_s -== form(action: "/<%= Inflector.pluralize(@name) -%>/#{<%= @name -%>.id.to_s}", method: <%= @name %>.id ? :patch : :post) do +== form(action: "/<%= name_plural -%>/#{<%= @name -%>.id.to_s}", method: <%= @name %>.id ? :patch : :post) do == csrf_tag <%- @fields.reject{|f| f.hidden }.each do |field| -%> .form-group @@ -21,4 +21,4 @@ <%- end -%> <%- end -%> == submit("Submit", class: "btn btn-primary btn-sm") - == link_to("back", "/<%= Inflector.pluralize(@name) %>", class: "btn btn-light btn-sm") + == link_to("back", "/<%= name_plural %>", class: "btn btn-light btn-sm") diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.ecr.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.ecr.ecr index d404445db..8d828fe10 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.ecr.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.ecr.ecr @@ -1,9 +1,9 @@
-

<%= Inflector.pluralize(display_name) %>

+

<%= display_name_plural %>

- New + New
@@ -17,16 +17,16 @@ Actions - <%="<"%>%- <%= Inflector.pluralize(@name) %>.each do |<%= @name %>| %> + <%="<"%>%- <%= name_plural %>.each do |<%= @name %>| %> <% @fields.reject{|f| f.hidden }.each do |field| -%> <%="<"%>%= <%= @name %>.<%= field.name %><%= field.reference? ? ".id" : "" %> %> <% end -%> - <%="<"%>%= link_to("read", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}", class: "btn btn-primary btn-sm") -%> - <%="<"%>%= link_to("edit", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") -%> - <%="<"%>%= link_to("delete", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") -%> + <%="<"%>%= link_to("read", "/<%= name_plural %>/#{<%= @name %>.id}", class: "btn btn-primary btn-sm") -%> + <%="<"%>%= link_to("edit", "/<%= name_plural %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") -%> + <%="<"%>%= link_to("delete", "/<%= name_plural %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") -%> diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.slang.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.slang.ecr index 70bc70f72..aca0d427d 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.slang.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/index.slang.ecr @@ -1,8 +1,8 @@ .row .col-sm-11 - h2 <%= Inflector.pluralize(display_name) %> + h2 <%= display_name_plural %> .col-sm-1 - a.btn.btn-success.btn-sm href="/<%= Inflector.pluralize(@name) %>/new" New + a.btn.btn-success.btn-sm href="/<%= name_plural %>/new" New .table-responsive table.table.table-striped thead @@ -12,13 +12,13 @@ <%- end -%> th Actions tbody - - <%= Inflector.pluralize(@name) %>.each do |<%= @name %>| + - <%= name_plural %>.each do |<%= @name %>| tr <%- @fields.reject{|f| f.hidden }.each do |field| -%> td = <%= @name %>.<%= field.name %><%= field.reference? ? ".id" : "" %> <%- end -%> td span - == link_to("read", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}", class: "btn btn-primary btn-sm") - == link_to("edit", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") - == link_to("delete", "/<%= Inflector.pluralize(@name) %>/#{ <%= @name %>.id }?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") + == link_to("read", "/<%= name_plural %>/#{<%= @name %>.id}", class: "btn btn-primary btn-sm") + == link_to("edit", "/<%= name_plural %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") + == link_to("delete", "/<%= name_plural %>/#{ <%= @name %>.id }?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.ecr.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.ecr.ecr index 6b1ea846e..3da8e0c33 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.ecr.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.ecr.ecr @@ -4,7 +4,7 @@

<%="<"%>%= <%= @name %>.<%= field.name %><%= field.reference? ? ".id" : "" %> %>

<% end -%>

- <%="<"%>%= link_to("back", "/<%= Inflector.pluralize(@name) %>", class: "btn btn-light btn-sm") -%> - <%="<"%>%= link_to("edit", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") -%> - <%="<"%>%= link_to("delete", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") -%> + <%="<"%>%= link_to("back", "/<%= name_plural %>", class: "btn btn-light btn-sm") -%> + <%="<"%>%= link_to("edit", "/<%= name_plural %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") -%> + <%="<"%>%= link_to("delete", "/<%= name_plural %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") -%>

diff --git a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.slang.ecr b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.slang.ecr index 2c64ec933..707a67cef 100644 --- a/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.slang.ecr +++ b/src/amber/cli/templates/scaffold/view/src/views/{{name}}/show.slang.ecr @@ -3,6 +3,6 @@ h1 Show <%= display_name %> p = <%= @name %>.<%= field.name %><%= field.reference? ? ".id" : "" %> <% end -%> p - == link_to("back", "/<%= Inflector.pluralize(@name) %>", class: "btn btn-light btn-sm") - == link_to("edit", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") - == link_to("delete", "/<%= Inflector.pluralize(@name) %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") + == link_to("back", "/<%= name_plural %>", class: "btn btn-light btn-sm") + == link_to("edit", "/<%= name_plural %>/#{<%= @name %>.id}/edit", class: "btn btn-success btn-sm") + == link_to("delete", "/<%= name_plural %>/#{<%= @name %>.id}?_csrf=#{csrf_token}", "data-method": "delete", "data-confirm": "Are you sure?", class: "btn btn-danger btn-sm") diff --git a/src/amber/cli/templates/template.cr b/src/amber/cli/templates/template.cr index 416b14de1..a6c4a76c7 100644 --- a/src/amber/cli/templates/template.cr +++ b/src/amber/cli/templates/template.cr @@ -1,5 +1,7 @@ require "teeplate" require "random/secure" +require "inflector" + require "../helpers/helpers" require "./app" require "./migration" @@ -160,8 +162,10 @@ end module Teeplate abstract class FileTree + @name_plural : String? @class_name : String? @display_name : String? + @display_name_plural : String? # Renders all collected file entries. # @@ -178,6 +182,10 @@ module Teeplate entries end + def name_plural + @name_plural ||= Inflector.pluralize(@name) + end + def class_name @class_name ||= @name.camelcase end @@ -186,6 +194,10 @@ module Teeplate @display_name ||= generate_display_name end + def display_name_plural + @display_name_plural ||= Inflector.pluralize(display_name) + end + private def generate_display_name @name.underscore.gsub('-', '_').split('_').map(&.capitalize).join(' ') end