Class: Hanami::Commands::Generate::Action

Inherits:
Abstract show all
Defined in:
lib/hanami/commands/generate/action.rb

Constant Summary collapse

ACTION_SEPARATOR_MATCHER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.8.0

/[\/,#]/
CONTROLLER_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.8.0

Utils::String::UNDERSCORE_SEPARATOR
ROUTE_ENDPOINT_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0

'#'.freeze
QUOTED_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0

/(\"|\'|\\)/
UP_DIRECTORY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.8.0

'..'.freeze
DIRECTORY_TEST_NESTING_LEVELS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.8.0

3
DEFAULT_HTTP_METHOD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default HTTP method used when generating an action.

Since:

  • 0.5.0

'GET'.freeze
RESOURCEFUL_HTTP_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTTP methods used when generating resourceful actions.

Since:

  • 0.6.0

{
  'Create'  => 'POST',
  'Update'  => 'PATCH',
  'Destroy' => 'DELETE'
}.freeze
RESOURCEFUL_ROUTE_URL_SUFFIXES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

For resourceful actions, what to add to the end of the base URL

Since:

  • 0.6.0

{
  'Show'    => '/:id',
  'Update'  => '/:id',
  'Destroy' => '/:id',
  'New'     => '/new',
  'Edit'    => '/:id/edit',
}.freeze

Instance Attribute Summary

Attributes inherited from Abstract

#options, #target_path

Instance Method Summary collapse

Methods inherited from Abstract

#template_source_path

Methods included from Generators::Generatable

#add_mapping, #argument_blank?, #generator, #process_templates, #start, #target_path, #template_source_path

Methods inherited from Command

inherited

Constructor Details

#initialize(options, application_name, controller_and_action_name) ⇒ Action

Returns a new instance of Action


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hanami/commands/generate/action.rb', line 62

def initialize(options, application_name, controller_and_action_name)
  super(options)
  if !environment.container?
    application_name = File.basename(Dir.pwd)
  end

  @controller_and_action_name = Utils::String.new(controller_and_action_name).underscore.gsub(QUOTED_NAME, '')

  *controller_name, @action_name = @controller_and_action_name.split(ACTION_SEPARATOR_MATCHER)

  @application_name = Utils::String.new(application_name).classify

  @controller_directory = controller_name
  @controller_name  = controller_name.join(CONTROLLER_SEPARATOR)

  @controller_class_name = Utils::String.new(@controller_name).classify
  @action_class_name = Utils::String.new(@action_name).classify

  @controller_url = @controller_name # FIXME Extract a new class: Utils::Url to handle conversion from paths or this naming

  assert_application_name!
  assert_controller_class_name!
  assert_action_name!
  assert_url!
  assert_http_method!
end

Instance Method Details

#destroyObject


119
120
121
122
# File 'lib/hanami/commands/generate/action.rb', line 119

def destroy
  generator.gsub_file(routes_path, /^.*#{@controller_and_action_name}.*\n/, '', verbose: false)
  super
end

#map_templatesObject


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hanami/commands/generate/action.rb', line 89

def map_templates
  add_mapping("action_spec.#{test_framework.framework}.tt", action_spec_path)

  if skip_view?
    add_mapping('action_without_view.rb.tt', action_path)
  else
    add_mapping('action.rb.tt', action_path)
    add_mapping('view.rb.tt', view_path)
    add_mapping('template.tt', template_path)
    add_mapping("view_spec.#{test_framework.framework}.tt", view_spec_path)
  end
end

#post_process_templatesObject


102
103
104
# File 'lib/hanami/commands/generate/action.rb', line 102

def post_process_templates
  generate_route
end

#template_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0


108
109
110
111
112
113
114
115
116
117
# File 'lib/hanami/commands/generate/action.rb', line 108

def template_options
  {
    app:                  @application_name,
    controller:           @controller_class_name,
    action:               @action_class_name,
    relative_action_path: relative_action_path,
    relative_view_path:   relative_view_path,
    template_path:        template_path,
  }
end