require 'action_view'
require 'action_controller/mime' # for Rails 2
render :file=>'foo.html.erb', :locals=>{:user=>User.first}
render :file=>'lib/data_feed.xml.builder', :type=>:rxml, :locals=>{:user=>User.first)
def render(options)
klass = Class.new(ActionView::Base)
klass.class_eval{ def url_for(x);x;end } # to be able to use link_to
klass.new.render(options)
end
Tag Archives: Rails
All ActionView Helpers on Strings
Insert all the helpers you want and “go!”.strip_tags.auto_link.truncate(25)
# ActionView text helpers
# https://grosser.it/2009/05/30/all-actionview-helpers-on-strings/
class String
%w[auto_link excerpt highlight sanitize simple_format strip_tags word_wrap].each do |method|
define_method method do |*args|
ActionController::Base.helpers.send method, self, *args
end
end
# called with a number or with :length=>10
def truncate(*args)
if args.first.is_a?(Hash)
ActionController::Base.helpers.truncate(self, *args)
else
ActionController::Base.helpers.truncate(self, :length=>args[0])
end
end
end
How to Run Each Test or Spec One by One
On my new project we had the problem that some specs failed when ran on their own, and some specs produces strange output (like “use object_id”), so I build a helper that runs each test on its own and could pinpoint the problem.
rake spec:one_by_one
it is now included in the single test rails plugin
Parallel Specs Rails Plugin: Speedup Test Excecution 2/4/8 X
What good is a dual-core when you do not use it !?
rake spec:parallel[1] –> 86 seconds
rake spec:parallel[2] –> 47 seconds
rake spec:parallel[4] –> 26 seconds
grab the parallel specs rails plugin here!
It requires a bit of setup, but hey: Can you spare 5 minutes now to save 1 minute 100 times a day ?
Resources without Model Namespaces
polymorphic_path(A::B::User) != users_url
which normally would not be a problem, only that polymorphic_path is used in all form_for / link_to etc helpers…
The simpeles solution i found is overwriting User.model_name with:
def self.model_name
ActiveSupport::ModelName.new('User')
end