Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/amber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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/**"
Expand Down
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 #{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 +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

Expand Down
10 changes: 5 additions & 5 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 All @@ -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) }
Expand Down Expand Up @@ -42,7 +42,7 @@ module Amber::Recipes
end

def table_name
@table_name ||= "#{Inflector.pluralize(@name)}"
@table_name ||= name_plural
end
end
end
11 changes: 11 additions & 0 deletions src/amber/cli/recipes/recipe.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "teeplate"
require "liquid"
require "base64"
require "inflector"

require "random/secure"
require "../helpers/helpers"
Expand Down Expand Up @@ -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.
#
Expand All @@ -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
Expand All @@ -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
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
4 changes: 2 additions & 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 @@ -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) }
Expand All @@ -22,7 +22,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
Loading