l'essentiel est invisible pour les yeux

Monday, May 19, 2008

[rails] RSpec matchers for render(:nothing => true)

This is matchers for render(:nothing => true).

Add some code into spec/spec_helper.rb is as follows:


# Matchers for render(:nothing => true)
class RenderNothing
def initialize
end

def matches?(controller)
@actual = controller.rendered_file
@actual == nil
end

def failure_message
return "render_nothing expected (render :nothing => true), got #{@actual.inspect}"
end

def negative_failure_message
return "render_nothing expected (render :nothing => true) not to equal #{@actual.inspect}"
end
end

def render_nothing
RenderNothing.new
end


Use render_nothing.

it "should not render anything" do
post :destroy, :id => 1
response.should render_nothing
end

Sunday, May 18, 2008

[rails] RSpec render_layout expects render specified layout file

Original idea via http://rubyforge.org/pipermail/rspec-users/2007-October/004026.html

Add some code into your spec/spec_helper.rb is as follows:


class RenderLayout
def initialize(expected)
@expected = 'layouts/' + expected
end

def matches?(controller)
@actual = controller.layout
@actual == @expected
end

def failure_message
return "render_layout expected #{@expected.inspect}, got #{@actual.inspect}", @expected, @acutual
end

def negative_failure_message
return "render_layout expected #{@expected.inspect} not to equal #{@actual.inspect}", @expected, @actual
end
end

def render_layout(expected)
RenderLayout.new(expected)
end

your_controller_spec.rb

it "should render 'application' layout file" do
get :index
response.should render_layout('application')
end

Tuesday, May 06, 2008

[rails] Running Erubis on Edge Rails

ActionView class was refactoring on Edge Rails, see Ticket #10437 for more details. The template engines are integrated into new ActionView::Template class, but we have to modify some scripts in order to run Erubis on Edge Rails.

0. Install Erubis and Edge Rails

% sudo gem install erubis
% cd /path/to/rails_app/ && rake rails::freeze:edge

1. Create a template handler for Erubis.

Copy and paste below code at #{RAILS_ROOT}/lib/erubis_rails_helper.rb.
require 'erubis'
require 'erubis/preprocessing'

module Erubis
class Eruby
include ErboutEnhancer
end

class FastEruby
include ErboutEnhancer
end
end

module ActionView
module TemplateHandlers
class Erubis < TemplateHandler
include Compilable

# Erubis engine class
cattr_accessor :engine_class
@@engine_class = ::Erubis::Eruby

# Properties
cattr_accessor :init_properties
@@init_properties = {}

# Enable preprocessing if true
cattr_accessor :preprocessing
@@preprocessing = false

def compile(template)
if preprocessing
preprocessor = ::Erubis::PreprocessingEruby.new(template, init_properties)
template = preprocessor.evaluate(@view)
end
engine_class.new(template, init_properties).src
end
end
end
end
ActionView::Template.register_default_template_handler :erb, ActionView::TemplateHandlers::Erubis
ActionView::Template.register_template_handler :rhtml, ActionView::TemplateHandlers::Erubis

2. Configure settings of Erubis in enviroment.rb.

Append below scripts in config/enviroment.rb
require 'erubis_rails_helper'
ActionView::TemplateHandlers::Erubis.engine_class = Erubis::FastEruby
ActionView::TemplateHandlers::Erubis.preprocessing = true
Restart the web server.

Saturday, May 03, 2008

[rails] released jrails_in_place_editing

The jrails_in_place_editing is implementation of inplace editor with jQuery, so this plugin is dependent on jRails. This uses David Hauenstein's jQuery inplace editor plugin. Thank you.

INSTALL (Note: Edge Rails)


./script/plugin install git://github.com/rakuto/jrails_in_place_editing.git

USAGE

# Controller
class BlogController < ApplicationController
in_place_edit_for :post, :title
end

# Customize the action that update the value
class BlogController < ApplicationController
# set_#{object}_#{title}
def set_post_title
post = Post.find(params[:id])
post.title = params[:value]
post.save

# It should render a text
render :text => post.title
end
end

# View
<%= in_place_editor_field :post, :title %>

# Pass some options
<%= in_place_editor_field :post, :title, {}, {:field_type => 'textarea', :textarea_cols => 25, :textarea_rows => 10}%>
<%= in_place_editor_field :post, :title, {}, {:field_type => 'textarea', :textarea_rows => 10}%>

For more details, see jrails_inplace_editing/javascripts/jquery.inplace.js