diff options
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/base.rb | 15 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/asset_tag_helper.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/action_view/inline_template.rb | 20 | ||||
-rw-r--r-- | actionpack/lib/action_view/template.rb | 38 | ||||
-rw-r--r-- | actionpack/lib/action_view/test_case.rb | 64 |
5 files changed, 117 insertions, 28 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index e83c8b6bd3..12dd7d2bc9 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -1,6 +1,9 @@ module ActionView #:nodoc: class ActionViewError < StandardError #:nodoc: end + + class MissingTemplate < ActionViewError #:nodoc: + end # Action View templates can be written in three ways. If the template file has a +.erb+ (or +.rhtml+) extension then it uses a mixture of ERb # (included in Ruby) and HTML. If the template file has a +.builder+ (or +.rxml+) extension then Jim Weirich's Builder::XmlMarkup library is used. @@ -202,15 +205,17 @@ module ActionView #:nodoc: class ObjectWrapper < Struct.new(:value) #:nodoc: end - def self.load_helpers #:nodoc: - Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file| + def self.helper_modules #:nodoc: + helpers = [] + Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file| next unless file =~ /^([a-z][a-z_]*_helper).rb$/ require "action_view/helpers/#{$1}" helper_module_name = $1.camelize if Helpers.const_defined?(helper_module_name) - include Helpers.const_get(helper_module_name) + helpers << Helpers.const_get(helper_module_name) end end + return helpers end def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: @@ -279,7 +284,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn elsif options[:partial] render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) elsif options[:inline] - template = Template.new(self, options[:inline], false, options[:locals], true, options[:type]) + template = InlineTemplate.new(self, options[:inline], options[:locals], options[:type]) render_template(template) end end @@ -320,7 +325,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn end end - private + private def wrap_content_for_layout(content) original_content_for_layout = @content_for_layout @content_for_layout = content diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index d57d1e0903..0cce96b184 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -101,7 +101,7 @@ module ActionView # something like Live HTTP Headers for Firefox to verify that the cache is indeed working (and that the assets are not being # requested over and over). module AssetTagHelper - ASSETS_DIR = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : "public" + ASSETS_DIR = defined?(Rails.public_path) ? Rails.public_path : "public" JAVASCRIPTS_DIR = "#{ASSETS_DIR}/javascripts" STYLESHEETS_DIR = "#{ASSETS_DIR}/stylesheets" @@ -474,7 +474,7 @@ module ActionView ActionView::Base.computed_public_paths[cache_key] ||= begin - source += ".#{ext}" if File.extname(source).blank? && ext + source += ".#{ext}" if ext && File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}")) if source =~ %r{^[-a-z]+://} source @@ -566,7 +566,7 @@ module ActionView def expand_javascript_sources(sources) if sources.include?(:all) - all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort + all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort @@all_javascript_sources ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq else expanded_sources = sources.collect do |source| @@ -579,7 +579,7 @@ module ActionView def expand_stylesheet_sources(sources) if sources.first == :all - @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort + @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort else sources.collect do |source| determine_source(source, @@stylesheet_expansions) diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb new file mode 100644 index 0000000000..87c012d181 --- /dev/null +++ b/actionpack/lib/action_view/inline_template.rb @@ -0,0 +1,20 @@ +module ActionView #:nodoc: + class InlineTemplate < Template #:nodoc: + + def initialize(view, source, locals = {}, type = nil) + @view = view + @finder = @view.finder + + @source = source + @extension = type + @locals = locals || {} + + @handler = self.class.handler_class_for_extension(@extension).new(@view) + end + + def method_key + @source + end + + end +end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 0cd6784fd8..bc3d8d5e52 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -2,22 +2,18 @@ module ActionView #:nodoc: class Template #:nodoc: attr_accessor :locals - attr_reader :handler, :path, :source, :extension, :filename, :path_without_extension, :method + attr_reader :handler, :path, :extension, :filename, :path_without_extension, :method - def initialize(view, path_or_source, use_full_path, locals = {}, inline = false, inline_type = nil) + def initialize(view, path, use_full_path, locals = {}) @view = view @finder = @view.finder - unless inline - # Clear the forward slash at the beginning if exists - @path = use_full_path ? path_or_source.sub(/^\//, '') : path_or_source - @view.first_render ||= @path - @source = nil # Don't read the source until we know that it is required - set_extension_and_file_name(use_full_path) - else - @source = path_or_source - @extension = inline_type - end + # Clear the forward slash at the beginning if exists + @path = use_full_path ? path.sub(/^\//, '') : path + @view.first_render ||= @path + @source = nil # Don't read the source until we know that it is required + set_extension_and_file_name(use_full_path) + @locals = locals || {} @handler = self.class.handler_class_for_extension(@extension).new(@view) end @@ -32,7 +28,7 @@ module ActionView #:nodoc: end def method_key - @method_key ||= (@filename || @source) + @filename end def base_path_for_exception @@ -58,9 +54,8 @@ module ActionView #:nodoc: @filename = @finder.pick_template(@path_without_extension, @extension) else @extension = @finder.pick_template_extension(@path).to_s - unless @extension - raise ActionViewError, "No template found for #{@path} in #{@finder.view_paths.inspect}" - end + raise_missing_template_exception unless @extension + @filename = @finder.pick_template(@path, @extension) @extension = @extension.gsub(/^.+\./, '') # strip off any formats end @@ -68,9 +63,14 @@ module ActionView #:nodoc: @filename = @path end - if @filename.blank? - raise ActionViewError, "Couldn't find template file for #{@path} in #{@finder.view_paths.inspect}" - end + raise_missing_template_exception if @filename.blank? + end + + def raise_missing_template_exception + full_template_path = @path.include?('.') ? @path : "#{@path}.#{@view.template_format}.erb" + display_paths = @finder.view_paths.join(':') + template_type = (@path =~ /layouts/i) ? 'layout' : 'template' + raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}") end # Template Handlers diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb new file mode 100644 index 0000000000..b2e6589d81 --- /dev/null +++ b/actionpack/lib/action_view/test_case.rb @@ -0,0 +1,64 @@ +require 'active_support/test_case' + +module ActionView + class NonInferrableHelperError < ActionViewError + def initialize(name) + super "Unable to determine the helper to test from #{name}. " + + "You'll need to specify it using tests YourHelper in your " + + "test case definition" + end + end + + class TestCase < ActiveSupport::TestCase + class_inheritable_accessor :helper_class + @@helper_class = nil + + class << self + def tests(helper_class) + self.helper_class = helper_class + end + + def helper_class + if current_helper_class = read_inheritable_attribute(:helper_class) + current_helper_class + else + self.helper_class = determine_default_helper_class(name) + end + end + + def determine_default_helper_class(name) + name.sub(/Test$/, '').constantize + rescue NameError + raise NonInferrableHelperError.new(name) + end + end + + ActionView::Base.helper_modules.each do |helper_module| + include helper_module + end + include ActionController::PolymorphicRoutes + include ActionController::RecordIdentifier + + setup :setup_with_helper_class + + def setup_with_helper_class + self.class.send(:include, helper_class) + end + + class TestController < ActionController::Base + attr_accessor :request, :response + + def initialize + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + end + + private + def method_missing(selector, *args) + controller = TestController.new + return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) + super + end + end +end |