Class: ViewDriver::SectionsCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/view_driver/sections_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ SectionsCollection

Returns a new instance of SectionsCollection.



6
7
8
9
# File 'lib/view_driver/sections_collection.rb', line 6

def initialize(controller)
  super()
  @controller = controller
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



4
5
6
# File 'lib/view_driver/sections_collection.rb', line 4

def controller
  @controller
end

Instance Method Details

#defined_template(section) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/view_driver/sections_collection.rb', line 17

def defined_template(section)
  template = nil
  each do |sections| 
    candidate = sections.template(section)
    template = candidate unless candidate.nil?
  end
  prepend_controller_path(template)
end

#prepend_controller_path(template) ⇒ Object



26
27
28
# File 'lib/view_driver/sections_collection.rb', line 26

def prepend_controller_path(template)
  template.is_a?(String) && !template.include?("/") ? File.join(@controller.controller_path, template) : template
end

#template_exists?(template) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/view_driver/sections_collection.rb', line 34

def template_exists?(template)
  ViewDriver::SECTIONS_TEMPLATES_EXTENSIONS.any? do |ext| 
    basename = template + ".#{ext}"
    file = File.join(RAILS_ROOT, 'app', 'views', basename)
    unless @controller.perform_caching
      File.exists?(file)
    else
      ApplicationController.existing_sections ||= {}
      ApplicationController.existing_sections[basename].nil? && ApplicationController.existing_sections[basename] = File.exists?(file)
      ApplicationController.existing_sections[basename]
    end
  end
end

#template_to_render(section) ⇒ Object



30
31
32
# File 'lib/view_driver/sections_collection.rb', line 30

def template_to_render(section)
  templates(section).find{|template| template_exists?(template)}
end

#templates(section) ⇒ Object



11
12
13
14
15
# File 'lib/view_driver/sections_collection.rb', line 11

def templates(section)
  defined_template = defined_template(section)
  return [] if defined_template == false
  ([defined_template] + @controller.default_section_templates(section)).compact
end