Sha256: 445f19d6279d115864ee79406c10a048573ef6a92b67f867ab1b97b69e251979
Contents?: true
Size: 1.77 KB
Versions: 3
Compression:
Stored size: 1.77 KB
Contents
module Wagons module ViewHelper # Renders all partials with names that match "_[ key ]_*.[ format ].[ handler ]" # in alphabetical order. # Accepts an additional option :folder to pass an additional folder to search # extension partials in. def render_extensions(key, options = {}) extensions = find_extension_partials(key, options.delete(:folder)).map do |partial| render options.merge(partial: partial) end safe_join(extensions) end # The view folders relative to app/views in which extensions are searched for. # Uses the folder of the current template. def extension_folders [current_template_folder] end # The folder of the current partial relative to app/views def current_template_folder @virtual_path[/(.+)\/.*/, 1] end private def find_extension_partials(key, folder = nil) folders = extension_folders.dup folders << folder if folder files = find_extension_files(key, folders).sort_by { |f| File.basename(f) } files_to_partial_names(files) end def find_extension_files(key, folders) folder_pattern = glob_pattern(folders) formats = glob_pattern(lookup_context.formats) handlers = glob_pattern(lookup_context.handlers) view_paths.map do |path| Dir.glob(File.join(path.to_s, folder_pattern, "_#{key}_*.#{formats}.#{handlers}")) end.flatten end def files_to_partial_names(files) files.map do |f| m = f.match(/views.(.+?[\/\\])_(.+)\.\w+\.\w+$/) m[1] + m[2] end end def glob_pattern(list) if list.size == 1 list.first else "{#{list.join(',')}}" end end end end ActionView::Base.send(:include, Wagons::ViewHelper) if defined?(ActionView::Base)
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
wagons-0.4.0 | lib/wagons/view_helper.rb |
wagons-0.3.1 | lib/wagons/view_helper.rb |
wagons-0.3.0 | lib/wagons/view_helper.rb |