Sha256: 659051d0d05d4bd1d853c255d4d687f5ae7b266a04be857b5a58918b901c3bfa

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Hanami
  class Action
    class ViewNameInferrer
      ALTERNATIVE_NAMES = {
        "create" => "new",
        "update" => "edit"
      }.freeze

      class << self
        def call(action_name:, provider:)
          application = provider.respond_to?(:application) ? provider.application : Hanami.application

          action_identifier_base = application.config.actions.name_inference_base
          view_identifier_base = application.config.actions.view_name_inference_base

          identifier = action_identifier_name(action_name, provider, action_identifier_base)

          view_name = [view_identifier_base, identifier].compact.join(".")

          [view_name, alternative_view_name(view_name)].compact
        end

        private

        def action_identifier_name(action_name, provider, name_base)
          provider
            .inflector
            .underscore(action_name)
            .sub(/^#{provider.namespace_path}\//, "")
            .sub(/^#{name_base}\//, "")
            .gsub("/", ".")
        end

        def alternative_view_name(view_name)
          parts = view_name.split(".")

          alternative_name = ALTERNATIVE_NAMES[parts.last]

          [parts[0..-2], alternative_name].join(".") if alternative_name
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hanami-controller-2.0.0.alpha8 lib/hanami/action/view_name_inferrer.rb
hanami-controller-2.0.0.alpha6 lib/hanami/action/view_name_inferrer.rb
hanami-controller-2.0.0.alpha5 lib/hanami/action/view_name_inferrer.rb
hanami-controller-2.0.0.alpha4 lib/hanami/action/view_name_inferrer.rb
hanami-controller-2.0.0.alpha3 lib/hanami/action/view_name_inferrer.rb
hanami-controller-2.0.0.alpha2 lib/hanami/action/view_name_inferrer.rb