Skip to content

Instantly share code, notes, and snippets.

@tsnow
Created June 8, 2011 20:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsnow/1015333 to your computer and use it in GitHub Desktop.
Save tsnow/1015333 to your computer and use it in GitHub Desktop.
For stackoverflow Q#6267059
require 'rubygems'
gem 'actionpack', '~>3.0.0'
gem 'activesupport', '~>3.0.0'
require 'action_pack'
require 'action_view'
require 'active_support'
require 'active_support/core_ext'
module ApplicationHelper
def does_things_with_content_tag
content_tag :p
end
def does_things_with_image_tag
image_tag 'a filename'
end
end
require 'ostruct'
class ViewlessHelpable
include ActionView::Helpers
include ApplicationHelper
ActionView::Helpers.constants.grep(/Helper$/i).each do |const|
include ActionView::Helpers.const_get(const)
end
def call
things = does_things_with_content_tag
raise things unless things == '<p></p>'
end
def invalid_without_config
things = begin
#image_tag requires config.asset_path
does_things_with_image_tag
rescue NameError => exc;
#=> NameError: undefined local variable or method ‘config’ for #<ViewlessHelpable:0x10157f880>
# method compute_public_path in asset_tag_helper.rb at line 742
true
end
raise exc unless things
end
end
class ViewlessHelpableWithImages < ViewlessHelpable
def config
o =OpenStruct.new
o.asset_path = "a path"
o
end
def controller
Object.new
end
def call
super()
things = does_things_with_image_tag
raise things unless things == '<img alt="A path" src="a path" />'
end
end
require 'cgi'
class ViewlessHelpableWithImagePaths < ViewlessHelpable
def path_to_image(source)
"/images/#{CGI.escape(source)}"
end
def call
super()
things = does_things_with_image_tag
raise things unless things == '<img alt="A+filename" src="/images/a+filename" />'
end
end
ViewlessHelpable.new.call
ViewlessHelpable.new.invalid_without_config
ViewlessHelpableWithImages.new.call
ViewlessHelpableWithImagePaths.new.call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment