lib/lotus/generators/action.rb in lotusrb-0.4.1 vs lib/lotus/generators/action.rb in lotusrb-0.5.0
- old
+ new
@@ -1,7 +1,8 @@
require 'lotus/generators/abstract'
require 'lotus/utils/string'
+require 'lotus/routing/route'
module Lotus
module Generators
# @since 0.3.0
# @api private
@@ -28,10 +29,15 @@
# @since 0.3.0
# @api private
DEFAULT_TEMPLATE = 'erb'.freeze
+ # Default HTTP method used when generating an action.
+ # @since 0.5.0
+ # @api private
+ DEFAULT_HTTP_METHOD = 'GET'.freeze
+
# @since 0.3.0
# @api private
def initialize(command)
super
@@ -46,10 +52,11 @@
# @since 0.3.0
# @api private
def start
assert_existing_app!
assert_action!
+ assert_http_method!
opts = {
app: app,
controller: @controller_name,
action: @action_name,
@@ -106,10 +113,18 @@
if @action.nil?
raise Lotus::Commands::Generate::Error.new("Unknown action, please add action's name with this syntax controller_name#action_name")
end
end
+ # @since 0.5.0
+ # @api private
+ def assert_http_method!
+ if !Lotus::Routing::Route::VALID_HTTP_VERBS.include?(_http_method.upcase)
+ raise Lotus::Commands::Generate::Error.new("Unknown HTTP method '#{_http_method}', please use one of #{Lotus::Routing::Route::VALID_HTTP_VERBS.join(', ')}.")
+ end
+ end
+
def app
if env.container?
super
else
env.require_application_environment
@@ -125,11 +140,17 @@
FileUtils.touch(path)
# Insert at the top of the file
cli.insert_into_file _routes_path, before: /\A(.*)/ do
- "get '#{ _route_url }', to: '#{ _route_endpoint }'\n"
+ "#{ _http_method } '#{ _route_url }', to: '#{ _route_endpoint }'\n"
end
+ end
+
+ # @since 0.5.0
+ # @api private
+ def _http_method
+ options.fetch(:method, DEFAULT_HTTP_METHOD).downcase
end
# @since 0.4.0
# @api private
def _route_url