Method: Hanami::ApplicationConfiguration#controller_pattern

Defined in:
lib/hanami/application_configuration.rb

#controller_pattern(value) ⇒ Object #controller_patternString

Defines a relative pattern to find controllers.

By default this equals to "Controllers::%{controller}::%{action}" That means controllers must be structured like this: Bookshelf::Controllers::Dashboard::Index, where Bookshelf is the application module, Controllers is the first value specified in the pattern, Dashboard the controller and Index the action.

This pattern MUST always contain "%{controller}" and %{action}. This pattern SHOULD be used accordingly to #view_pattern value.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami'

module Bookshelf
  class Application < Hanami::Application
    configure do
      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

  module Controllers::Dashboard
    class Index
      include Bookshelf::Action

      def call(params)
        # ...
      end
    end
  end
end

Bookshelf::Application.configuration.controller_pattern
  # => "Controllers::%{controller}::%{action}"

# All the controllers MUST live under Bookshelf::Controllers

# GET '/' # => Bookshelf::Controllers::Dashboard::Index

Setting the value

require 'hanami'

module Bookshelf
  class Application < Hanami::Application
    configure do
      controller_pattern "%{controller}Controller::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

  module DashboardController
    class Index
      include Bookshelf::Action

      def call(params)
      end
    end
  end
end

Bookshelf::Application.configuration.controller_pattern
  # => "%{controller}Controller::%{action}"

# All the controllers are directly under the Bookshelf module

# GET '/' # => Bookshelf::DashboardController::Index

Setting the value for a top level name structure

require 'hanami'

module Bookshelf
  class Application < Hanami::Application
    configure do
      controller_pattern "%{controller}Controller::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end
end

module DashboardController
  class Index
    include Bookshelf::Action

    def call(params)
    end
  end
end

Bookshelf::Application.configuration.controller_pattern
  # => "%{controller}Controller::%{action}"

# All the controllers are at the top level namespace

# GET '/' # => DashboardController::Index

Overloads:

  • #controller_pattern(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the controller pattern

  • #controller_patternString

    Gets the value

    Returns:

    • (String)

See Also:

  • Configuration#view_pattern

Since:

  • 0.1.0



1133
1134
1135
1136
1137
1138
1139
# File 'lib/hanami/application_configuration.rb', line 1133

def controller_pattern(value = nil)
  if value
    @controller_pattern = value
  else
    @controller_pattern ||= 'Controllers::%{controller}::%{action}'
  end
end