Skip to content

Commit

Permalink
Skip view RESTful view generation (#114)
Browse files Browse the repository at this point in the history
* Skip view RESTful view generation

* Ensure `--skip-view` is respected

* Refactoring

* Added missing spec from the past: verify that with `hanami-view` bundled, we generate views for slices

* Reproduce the same behavior for slices

* `RESTFUL_ACTIONS` => `RESTFUL_COUNTERPART_VIEWS`

* Check for RESTful view existence, instead of checking action
  • Loading branch information
jodosha authored Oct 24, 2023
1 parent a355e45 commit dd049fd
Show file tree
Hide file tree
Showing 2 changed files with 954 additions and 139 deletions.
52 changes: 46 additions & 6 deletions lib/hanami/cli/generators/app/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def call(app, controller, action, url, http, format, skip_view, slice, context:
}.freeze
private_constant :ROUTE_RESTFUL_URL_SUFFIXES

# @api private
# @since 2.1.0
RESTFUL_COUNTERPART_VIEWS = {
"create" => "new",
"update" => "edit"
}.freeze
private_constant :RESTFUL_COUNTERPART_VIEWS

PATH_SEPARATOR = "/"
private_constant :PATH_SEPARATOR

Expand All @@ -77,7 +85,7 @@ def generate_for_slice(controller, action, url, http, format, skip_view, slice,
fs.mkdir(directory = fs.join(slice_directory, "actions", controller))
fs.write(fs.join(directory, "#{action}.rb"), t("slice_action.erb", context))

unless skip_view
if generate_view?(skip_view, action, directory)
fs.mkdir(directory = fs.join(slice_directory, "views", controller))
fs.write(fs.join(directory, "#{action}.rb"), t("slice_view.erb", context))

Expand All @@ -97,12 +105,15 @@ def generate_for_app(controller, action, url, http, format, skip_view, context)
fs.mkdir(directory = fs.join("app", "actions", controller))
fs.write(fs.join(directory, "#{action}.rb"), t("action.erb", context))

unless skip_view
fs.mkdir(directory = fs.join("app", "views", controller))
fs.write(fs.join(directory, "#{action}.rb"), t("view.erb", context))
view = action
view_directory = fs.join("app", "views", controller)

fs.mkdir(directory = fs.join("app", "templates", controller))
fs.write(fs.join(directory, "#{action}.#{format}.erb"),
if generate_view?(skip_view, view, view_directory)
fs.mkdir(view_directory)
fs.write(fs.join(view_directory, "#{view}.rb"), t("view.erb", context))

fs.mkdir(template_directory = fs.join("app", "templates", controller))
fs.write(fs.join(template_directory, "#{view}.#{format}.erb"),
t(template_with_format_ext("template", format), context))
end
end
Expand All @@ -117,6 +128,35 @@ def route(controller, action, url, http)
http)} "#{route_url(controller, action, url)}", to: "#{controller.join('.')}.#{action}")
end

# @api private
# @since 2.1.0
def generate_view?(skip_view, view, directory)
return false if skip_view
return generate_restful_view?(view, directory) if rest_view?(view)

true
end

# @api private
# @since 2.1.0
def generate_restful_view?(view, directory)
corresponding_action = corresponding_restful_view(view)

!fs.exist?(fs.join(directory, "#{corresponding_action}.rb"))
end

# @api private
# @since 2.1.0
def rest_view?(view)
RESTFUL_COUNTERPART_VIEWS.keys.include?(view)
end

# @api private
# @since 2.1.0
def corresponding_restful_view(view)
RESTFUL_COUNTERPART_VIEWS.fetch(view, nil)
end

def template_with_format_ext(name, format)
ext =
case format.to_sym
Expand Down
Loading

0 comments on commit dd049fd

Please sign in to comment.