lib/sections_rails.rb in sections_rails-0.6.0 vs lib/sections_rails.rb in sections_rails-0.6.1

- old
+ new

@@ -1,66 +1,73 @@ +require 'sections_rails/string_tools' + module SectionsRails require "sections_rails/railtie" if defined?(Rails) def section combined_name, options = {} - out = [] + result = [] # Split the parameter into file name and directory name. - split_names = combined_name.to_s.split '/' - filename = split_names[-1] - directory = (split_names.size > 1 ? split_names[0..-2] : []).join '/' - directory += '/' if directory.size > 0 + directory, filename = split_path combined_name directory_path = "#{Rails.root}/app/sections/#{directory}#{filename}" # Directory of section: /app/sections/admin/logo # Add assets of section when in dev mode. file_path = "#{directory_path}/#{filename}" # Base path of filename in section: /app/sections/admin/logo/logo if Rails.env != 'production' # Include JS assets. if options.has_key? :js if options[:js] - out << javascript_include_tag("#{directory}#{filename}/#{options[:js]}") + # Custom :js filename given --> load the given JS file. + result << javascript_include_tag("#{directory}#{filename}/#{options[:js]}") + else + # js: false given --> don't do anything here. end else + # No :js option given --> load the default JS file. if File.exists?("#{file_path}.js") || File.exists?("#{file_path}.js.coffee") || File.exists?("#{file_path}.coffee") - out << javascript_include_tag("#{directory}#{filename}/#{filename}") + result << javascript_include_tag("#{directory}#{filename}/#{filename}") end end # Include CSS assets. if options.has_key? :css if options[:css] - out << stylesheet_link_tag("#{directory}#{filename}/#{options[:css]}") + # Custom :css option given --> render the given file. + result << stylesheet_link_tag("#{directory}#{filename}/#{options[:css]}") + else + # css: false given --> don't render any css. end else + # No :css option given --> render the default :css file. if File.exists?("#{file_path}.css") || File.exists?("#{file_path}.css.scss") || File.exists?("#{file_path}.css.sass") || File.exists?("#{file_path}.scss") || File.exists?("#{file_path}.sass") - out << stylesheet_link_tag("#{directory}#{filename}/#{filename}") + result << stylesheet_link_tag("#{directory}#{filename}/#{filename}") end end end # Render the section partial into the view. partial_path = "#{directory_path}/_#{filename}.html" if options.has_key? :partial if options[:partial] == :tag # :partial => :tag given --> render the tag. - out << content_tag(:div, '', :class => filename) + result << content_tag(:div, '', :class => filename) elsif options[:partial] # some value for :partial given --> render the given partial. - out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals]) + result << render(:partial => "/../sections/#{directory}#{filename}/#{options[:partial]}", :locals => options[:locals]) else - # :partial => false or nil given --> render nothing + # partial: false or nil given --> render nothing end else # No :partial option given --> render the file or tag per convention. if File.exists?("#{partial_path}.erb") || File.exists?("#{partial_path}.haml") - out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals]) + result << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals]) else - out << content_tag(:div, '', :class => filename) + result << content_tag(:div, '', :class => filename) end end - out.join("\n").html_safe + result.join("\n").html_safe end end ActionView::Base.send :include, SectionsRails