Module: Extensions::InheritedNestedLayouts::ActionController::Base

Defined in:
lib/extensions/inherited_nested_layouts/action_controller/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_hierarchy(klass) ⇒ Array<String>

Gets the superclass hierarchy for the given class. Object is not part of the returned result.

Parameters:

  • klass (Class)

    The class to obtain the hierarchy of.

Returns:

  • (Array<String>)

    The superclass hierarchy for the given class.


41
42
43
44
45
46
47
48
49
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 41

def self.class_hierarchy(klass)
  result = []
  while klass != Object
    result << klass
    klass = klass.superclass
  end

  result
end

.class_layout(klass, self_, formats) ⇒ String

Gets the layout for objects of the given class.

Parameters:

  • klass (Class)

    The class to obtain the layout of. This must be a subclass of ActionController::Base

  • self_ (ActionController::Base)

    The instance to query against the class hierarchy.

Returns:

  • (String)

    The layout to use for instances of +klass+.


57
58
59
60
61
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 57

def self.class_layout(klass, self_, formats)
  layout_method = klass.instance_method(:_layout)
  layout = layout_method.bind(self_)
  layout.call(formats)
end

Instance Method Details

#current_layoutString

Gets the current layout used by this controller.

Returns:

  • (String)

    The layout used by the current controller.


6
7
8
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 6

def current_layout
  _layout(formats)
end

#layout_hierarchyArray<String>

Gets the layout hierarchy, from the outermost to the innermost layout.

Returns:

  • (Array<String>)

    The layout hierarchy for this controller.


26
27
28
29
30
31
32
33
34
35
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 26

def layout_hierarchy
  extension_module = Extensions::InheritedNestedLayouts::ActionController::Base
  @layout_hierarchy ||=
    extension_module.class_hierarchy(self.class).
    select { |klass| klass < ActionController::Base }.
    map { |klass| extension_module.class_layout(klass, self, formats) }.
    reject(&:nil?).
    uniq.
    reverse!
end

#parent_layout(of_layout: nil) ⇒ String

Gets the parent layout of the given layout, as specified in the layout hierarchy.

Parameters:

  • of_layout (String)

    The layout to obtain the parent of. If this is nil, obtains the current controller's parent layout.

Returns:

  • (String)

    The parent layout of the given layout.


15
16
17
18
19
20
21
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 15

def parent_layout(of_layout: nil)
  layout = of_layout || current_layout
  layout_index = layout_hierarchy.find_index(layout)
  return nil if layout_index.nil? || layout_index == 0

  layout_hierarchy[layout_index - 1]
end

#render(*args) ⇒ Object

Overrides ActionController::Rendering#render to keep track of the :layout rendering option.


64
65
66
67
68
69
70
# File 'lib/extensions/inherited_nested_layouts/action_controller/base.rb', line 64

def render(*args)
  options = args.extract_options!
  layout_hierarchy << options[:layout] if options.try(:key?, :layout)

  args << options
  super
end