Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue on gen.resource.browser with some column types #997

Merged
merged 2 commits into from
Feb 10, 2020
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
6 changes: 6 additions & 0 deletions spec/support/generator_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ module GeneratorHelper
private def should_generate_migration(named name : String)
Dir.new("./db/migrations").any?(&.ends_with?(name)).should be_true
end

private def should_generate_migration(named name : String, with content : String)
filename = Dir.new("./db/migrations").find(&.ends_with?(name))
filename.should_not be_nil
File.read("./db/migrations/#{filename}").should contain(content)
end
end
13 changes: 11 additions & 2 deletions spec/tasks/gen/resource_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ include CleanupHelper
include GeneratorHelper

describe Gen::Action do
it "generates actions, model, form and query" do
it "generates actions, model, operation and query" do
jwoertink marked this conversation as resolved.
Show resolved Hide resolved
with_cleanup do
Gen::Migration.silence_output do
io = generate Gen::Resource::Browser, "User", "name:String"
io = generate Gen::Resource::Browser, "User", "name:String", "signed_up:Time"

should_create_files_with_contents io,
"./src/actions/users/index.cr": "class Users::Index < BrowserAction",
Expand All @@ -32,11 +32,20 @@ describe Gen::Action do
"./src/queries/user_query.cr": "class UserQuery < User::BaseQuery",
"./src/operations/save_user.cr": "class SaveUser < User::SaveOperation"
should_generate_migration named: "create_users.cr"
should_generate_migration named: "create_users.cr", with: "add signed_up : Time"
io.to_s.should contain "at: #{"/users".colorize.green}"
end
end
end

it "displays an error when given a more complex type" do
with_cleanup do
io = generate Gen::Resource::Browser, "Alphabet", "a:JSON::Any"

io.to_s.should contain("Other complex types can be added manually")
end
end

it "displays an error if given no arguments" do
with_cleanup do
io = generate Gen::Resource::Browser, ""
Expand Down
20 changes: 16 additions & 4 deletions tasks/gen/resource/browser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "avram"
class Gen::Resource::Browser < LuckyCli::Task
summary "Generate a resource (model, operation, query, actions, and pages)"
getter io : IO = STDOUT
VALID_TYPES = {"Bool", "Float64", "Int16", "Int32", "Int64", "String", "Time", "UUID"}

class InvalidOption < Exception
def initialize(message : String)
Expand Down Expand Up @@ -38,7 +39,7 @@ class Gen::Resource::Browser < LuckyCli::Task

private def columns : Array(Lucky::GeneratedColumn)
column_definitions.map do |column_definition|
column_name, column_type = column_definition.split(":")
column_name, column_type = parse_definition(column_definition)
Lucky::GeneratedColumn.new(name: column_name, type: column_type)
end
end
Expand Down Expand Up @@ -112,7 +113,11 @@ class Gen::Resource::Browser < LuckyCli::Task

private def validate_has_valid_columns!
if !columns_are_valid?
error "Must provide valid columns for the resource: lucky gen.resource.browser #{resource_name.camelcase} name:String"
error <<-ERR
Must provide valid columns for the resource: lucky gen.resource.browser #{resource_name.camelcase} name:String

Other complex types can be added manually. See https://luckyframework.org/guides/database/migrations#add-column for more details.
ERR
end
end

Expand All @@ -134,12 +139,19 @@ class Gen::Resource::Browser < LuckyCli::Task

private def columns_are_valid? : Bool
column_definitions.any? && column_definitions.all? do |column_definition|
column_parts = column_definition.split(":")
column_parts = parse_definition(column_definition)
column_name = column_parts.first
column_parts.size == 2 && column_name == column_name.underscore
column_type = column_parts.last
column_parts.size == 2 &&
column_name == column_name.underscore &&
VALID_TYPES.includes?(column_type)
end
end

private def parse_definition(column_definition : String) : Array(String)
column_definition.split(':', 2)
end

private def display_success_messages
success_message(resource_name, "./src/models/#{underscored_resource}.cr")
success_message("Save" + resource_name, "./src/operations/save_#{underscored_resource}.cr")
Expand Down