class Wallaby::View::CustomPrefixes

Custom prefix builder to add more lookup prefix paths to given {#prefixes}.

Attributes

action_name[R]

@!attribute [r] action_name Action name to be added @return [String]

options[R]

@!attribute [r] options Options for extending the given prefixes @return [Hash]

prefixes[R]

@!attribute [r] prefixes Base prefixes to extend @return [Array<String>] @see ActionViewable#_prefixes

themes[R]

@!attribute [r] themes Themes to be inserted @return [Array<Hash>] @see Themeable#.themes

Public Class Methods

execute( prefixes:, action_name:, themes: nil, options: nil, &block ) click to toggle source

Extend given prefixes with action name and theme name @example To extend given prefixes:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'index'
)

# => [
#   'users/index',
#   'users',
#   'application/index',
#   'application'
# ]

@example To extend given prefixes with themes:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'index',
  themes: [{ theme_name: 'secure', theme_path: 'users' }]
)

# => [
#   'users/index',
#   'users',
#   'secure/index',
#   'secure',
#   'application/index',
#   'application'
# ]

@example To extend given prefixes with mapped action:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'edit',
  options: { 'edit' => 'form' }
)

# => [
#   'users/form',
#   'users',
#   'application/form',
#   'application'
# ]

@param prefixes [Array<String>] @param action_name [String] @param themes [String, nil] @param options [Hash, nil] @return [Array<String>]

# File lib/wallaby/view/custom_prefixes.rb, line 72
def self.execute(
  prefixes:, action_name:, themes: nil, options: nil, &block
)
  new(
    prefixes: prefixes, action_name: action_name,
    themes: themes, options: options
  ).execute(&block)
end
new(prefixes:, action_name:, themes:, options:) click to toggle source

Create the instance @param prefixes [Array<String>] @param action_name [String] @param themes [String] @param options [Hash]

# File lib/wallaby/view/custom_prefixes.rb, line 86
def initialize(prefixes:, action_name:, themes:, options:)
  @prefixes = prefixes
  @action_name = action_name
  @themes = themes
  @options = (options || {}).with_indifferent_access
end

Public Instance Methods

execute(&block) click to toggle source

Extend given prefixes with action name and theme name @return [Array<String>]

# File lib/wallaby/view/custom_prefixes.rb, line 95
def execute(&block)
  new_prefixes(&block).each_with_object([]) do |prefix, array|
    # Extend the prefix with actions
    actions.each { |action| array << "#{prefix}/#{action}" }
    array << prefix
  end
end

Protected Instance Methods

actions() click to toggle source

@return [Array<String>] Action names

# File lib/wallaby/view/custom_prefixes.rb, line 122
def actions
  @actions ||= [action_name, *mapped_action_name].compact
end
insert_themes_into(array) click to toggle source

Insert theme names into the prefixes @param [Array<String>] array

# File lib/wallaby/view/custom_prefixes.rb, line 128
def insert_themes_into(array)
  themes.each do |theme|
    index = array.index theme[:theme_path]
    array.insert(index + 1, theme[:theme_name]) if index
  end
end
mapped_action_name() click to toggle source

Map the {#action_name} using `options` @return [Array<String>] mapped action name

# File lib/wallaby/view/custom_prefixes.rb, line 137
def mapped_action_name
  Array((options[:mapping_actions] || options).try(:[], action_name))
end
new_prefixes() { |array| ... } click to toggle source

@yield [array] To allow the array to be further modified @yieldparam [Array<String>] array @return [Array<String>]

# File lib/wallaby/view/custom_prefixes.rb, line 108
def new_prefixes
  prefixes.dup.try do |array|
    insert_themes_into array

    # Be able to change the array in overriding methods
    # in {ActionViewable#override_prefixes}
    new_array = yield array if block_given?

    # If the above block doesn't return a new array, it returns the old `array`.
    new_array.is_a?(Array) ? new_array : array
  end
end