lib/hanami/cli/generators/app/action.rb in hanami-cli-2.0.0.beta1 vs lib/hanami/cli/generators/app/action.rb in hanami-cli-2.0.0.beta2

- old
+ new

@@ -3,49 +3,30 @@ require "erb" require "dry/files" require "hanami/cli/generators/app/action_context" require "hanami/cli/url" +# rubocop:disable Metrics/ParameterLists module Hanami module CLI module Generators module App class Action def initialize(fs:, inflector:) @fs = fs @inflector = inflector end - # rubocop:disable Metrics/AbcSize - # rubocop:disable Metrics/ParameterLists # rubocop:disable Layout/LineLength - def call(slice, controller, action, url, http, format, skip_view, context: ActionContext.new(inflector, slice, controller, action)) - slice_directory = fs.join("slices", slice) - raise ArgumentError.new("slice not found `#{slice}'") unless fs.directory?(slice_directory) - - fs.inject_line_at_block_bottom( - fs.join("config", "routes.rb"), - slice_matcher(slice), - route(controller, action, url, http) - ) - - fs.chdir(slice_directory) do - fs.mkdir(directory = fs.join("actions", controller)) - fs.write(fs.join(directory, "#{action}.rb"), t("action.erb", context)) - - unless skip_view - fs.mkdir(directory = fs.join("views", controller)) - fs.write(fs.join(directory, "#{action}.rb"), t("view.erb", context)) - - fs.mkdir(directory = fs.join("templates", controller)) - fs.write(fs.join(directory, "#{action}.#{format}.erb"), t(template_format(format), context)) - end + def call(app, controller, action, url, http, format, skip_view, slice, context: ActionContext.new(inflector, app, slice, controller, action)) + if slice + generate_for_slice(controller, action, url, http, format, skip_view, slice, context) + else + generate_for_app(controller, action, url, http, format, skip_view, context) end end # rubocop:enable Layout/LineLength - # rubocop:enable Metrics/ParameterLists - # rubocop:enable Metrics/AbcSize private ROUTE_HTTP_METHODS = %w[get post delete put patch trace options link unlink].freeze private_constant :ROUTE_HTTP_METHODS @@ -73,10 +54,45 @@ attr_reader :fs attr_reader :inflector + def generate_for_slice(controller, action, url, http, _format, _skip_view, slice, context) + slice_directory = fs.join("slices", slice) + raise ArgumentError.new("slice not found `#{slice}'") unless fs.directory?(slice_directory) + + fs.inject_line_at_block_bottom( + fs.join("config", "routes.rb"), + slice_matcher(slice), + route(controller, action, url, http) + ) + + fs.chdir(slice_directory) do + fs.mkdir(directory = fs.join("actions", controller)) + fs.write(fs.join(directory, "#{action}.rb"), t("slice_action.erb", context)) + + # unless skip_view + # fs.mkdir(directory = fs.join("views", controller)) + # fs.write(fs.join(directory, "#{action}.rb"), t("view.erb", context)) + # + # fs.mkdir(directory = fs.join("templates", controller)) + # fs.write(fs.join(directory, "#{action}.#{format}.erb"), t(template_format(format), context)) + # end + end + end + + def generate_for_app(controller, action, url, http, _format, _skip_view, context) + fs.inject_line_at_block_bottom( + fs.join("config", "routes.rb"), + /define/, + route(controller, action, url, http) + ) + + fs.mkdir(directory = fs.join("app", "actions", controller)) + fs.write(fs.join(directory, "#{action}.rb"), t("action.erb", context)) + end + def slice_matcher(slice) /slice[[:space:]]*:#{slice}/ end def route(controller, action, url, http) @@ -119,5 +135,6 @@ end end end end end +# rubocop:enable Metrics/ParameterLists