Class: SectionsRails::Section
- Inherits:
-
Object
- Object
- SectionsRails::Section
- Includes:
- SectionAssetRenderering, SectionPartialRendering
- Defined in:
- lib/sections_rails/section.rb
Overview
Provides intelligent support for dealing with sections.
Instance Method Summary collapse
-
#directory_name ⇒ Object
Returns the names of any subdirectories that the section is in.
-
#filename ⇒ Object
Returns the file name of the section.
-
#folder_filepath ⇒ Object
Path of the folder on the file system.
-
#initialize(section_name, view = nil, options = {}) ⇒ Section
constructor
A new instance of Section.
-
#referenced_sections(recursive = true) ⇒ Object
Returns the sections that this section references.
-
#render(&block) ⇒ Object
Renders this section, i.e.
Constructor Details
#initialize(section_name, view = nil, options = {}) ⇒ Section
Returns a new instance of Section.
13 14 15 16 17 18 19 |
# File 'lib/sections_rails/section.rb', line 13 def initialize section_name, view = nil, = {} @section_name = section_name.to_s @options = # This is necessary for running view helper methods. @view = view end |
Instance Method Details
#directory_name ⇒ Object
Returns the names of any subdirectories that the section is in. Example: the section named ‘folder1/folder2/section’ has the directory_name ‘folder1/folder2’.
23 24 25 |
# File 'lib/sections_rails/section.rb', line 23 def directory_name @directory_name ||= File.dirname(@section_name).gsub(/^\.$/, '') end |
#filename ⇒ Object
Returns the file name of the section. Example ‘section’
29 30 31 |
# File 'lib/sections_rails/section.rb', line 29 def filename @filename ||= File.basename @section_name, '.*' end |
#folder_filepath ⇒ Object
Path of the folder on the file system. Example: ‘app/sections/folder/section’
35 36 37 |
# File 'lib/sections_rails/section.rb', line 35 def folder_filepath @folder_filepath ||= File.join SectionsRails.config.path, directory_name, filename end |
#referenced_sections(recursive = true) ⇒ Object
Returns the sections that this section references. If ‘recursive = true’ is given, searches recursively for sections referenced by the referenced sections. Otherwise, simply returns the sections that are referenced by this section.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sections_rails/section.rb', line 42 def referenced_sections recursive = true result = PartialParser.find_sections partial_content # Find all sections within the already known sections. if recursive i = -1 while (i += 1) < result.size Section.new(result[i]).referenced_sections(false).each do |referenced_section| result << referenced_section unless result.include? referenced_section end end end result.sort! end |
#render(&block) ⇒ Object
Renders this section, i.e. returns the HTML for this section.
59 60 61 62 63 64 65 66 |
# File 'lib/sections_rails/section.rb', line 59 def render &block raise "Section #{folder_filepath} doesn't exist." unless Dir.exists? folder_filepath result = [] render_assets result if Rails.env != 'production' render_partial result, &block result.join("\n").html_safe end |