lib/lotus/generators/action.rb in lotusrb-0.3.2 vs lib/lotus/generators/action.rb in lotusrb-0.4.0
- old
+ new
@@ -40,16 +40,18 @@
def start
assert_existing_app!
assert_action!
opts = {
- app: app,
- controller: @controller_name,
- action: @action_name,
- action_path: _action_path_without_suffix,
- view_path: _view_path_without_suffix,
- template_path: _template_path,
+ app: app,
+ controller: @controller_name,
+ action: @action_name,
+ action_path: _action_path_without_suffix,
+ relative_action_path: _relative_action_path,
+ relative_view_path: _relative_view_path,
+ view_path: _view_path_without_suffix,
+ template_path: _template_path,
}
test_type = case options[:test]
when 'rspec'
'rspec'
@@ -96,28 +98,44 @@
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
+ def app
+ if env.container?
+ super
+ else
+ env.require_application_environment
+ Utils::String.new(Lotus::Application.applications.first).namespace
+ end
+ end
+
# @since 0.3.0
# @api private
def generate_route
path = target.join(_routes_path)
path.dirname.mkpath
FileUtils.touch(path)
# Insert at the top of the file
cli.insert_into_file _routes_path, before: /\A(.*)/ do
- "get '/#{ @controller }', to: '#{ @name }'\n"
+ "get '#{ _route_url }', to: '#{ @name }'\n"
end
end
+ # @since 0.4.0
+ # @api private
+ def _route_url
+ options.fetch(:url, "/#{ @controller }")
+ end
+
# @since 0.3.0
# @api private
def _routes_path
- app_root.join("config", "routes#{ SUFFIX }")
+ routes_root = env.container? ? app_root : env.root
+ routes_root.join("config", "routes#{ SUFFIX }")
end
# @since 0.3.0
# @api private
def _action_path
@@ -149,16 +167,34 @@
end
# @since 0.3.0
# @api private
def _action_spec_path
- spec_root.join(app_name, 'controllers', @controller, "#{ @action }_spec#{ SUFFIX }")
+ spec_root.join(app_name.to_s, 'controllers', @controller, "#{ @action }_spec#{ SUFFIX }")
end
# @since 0.3.0
# @api private
def _view_spec_path
- spec_root.join(app_name, 'views', @controller, "#{ @action }_spec#{ SUFFIX }")
+ spec_root.join(app_name.to_s, 'views', @controller, "#{ @action }_spec#{ SUFFIX }")
+ end
+
+ # @since 0.4.0
+ # @api private
+ def _relative_action_path
+ result = '../../../'
+ result << '../' if env.container?
+ result << _action_path_without_suffix.to_s
+ result
+ end
+
+ # @since 0.4.0
+ # @api private
+ def _relative_view_path
+ result = '../../../'
+ result << '../' if env.container?
+ result << _view_path_without_suffix.to_s
+ result
end
end
end
end