Tuesday, February 26, 2008

Getting TinyMCE 3.0 Spellchecker to work with Rails

In new version of TinyMCE editor there are many changes in plug ins. The Plug in code is changed. The popular spell checker plug in now uses JSON as transport mechanism instead of XML. In my example we are using Aspell to check spellings, so you must have Aspell installed on your system for this example to work

Issue this command on CLI to check if Aspell is installed:

% aspell


You should see help output related to command

First of all you need to install JSON gem to parse JSON requests

% sudo gem install json


Then write this action in a controller:

require json

def spellcheck
raw = request.env['RAW_POST_DATA']
req = JSON.parse(raw)
lang = req["params"][0]
if req["method"] == 'checkWords'
text_to_check = req["params"][1].join(" ")
else
text_to_check = req["params"][1]
end
suggestions = check_spelling_new(text_to_check, req["method"], lang)
render :json => {"id" => nil, "result" => suggestions, "error" => nil}.to_json
return
end


Now also write this method in private section of controller:

def check_spelling_new(spell_check_text, command, lang)
json_response_values = Array.new
spell_check_response = `echo "#{spell_check_text}" | aspell -a -l #{lang}`
if (spell_check_response != '')
spelling_errors = spell_check_response.split(' ').slice(1..-1)
if (command == 'checkWords')
i = 0
while i < spelling_errors.length
spelling_errors[i].strip!
if (spelling_errors[i].to_s.index('&') == 0)
match_data = spelling_errors[i + 1]
json_response_values << match_data
end
i += 1
end
elsif (command == 'getSuggestions')
arr = spell_check_response.split(':')
suggestion_string = arr[1]
suggestions = suggestion_string.split(',')
for suggestion in suggestions
suggestion.strip!
json_response_values << suggestion
end
end
end
return json_response_values
end


now you need to open /plugins/spellchecker/editor_plugin.js file under your Tiny MCE directory. Search for this line;

t.url = url;


and change it to:

t.url = 'http://www.yourhostname.com';


then search for this line:

var t = this, url = t.editor.getParam("spellchecker_rpc_url", this.url+'/rpc.php');


and change it to:

var t = this, url = t.editor.getParam("spellchecker_rpc_url", this.url+'/spellcheck');


and to make this work just add this route to routes.rb under config folder

map.connect '/spellcheck', :controller => 'your_controller_name', :action => 'spellcheck'
#replace your_controller_name with name of our controller in which the spellcheck action is written

Wednesday, November 7, 2007

No default PTY in Capistrano 2.1

Add this line your Capistrano scripts to work with new Capistrano 2.1 gem.

default_run_options[:pty] = true


Capistrano no longer requests a pty on each command, which means your .profile (or .bashrc etc) will be properly loaded on each command. Some commands will go into non-interactive mode automatically. If you’re not seeing commands at CLI, You can return to old behavior by adding this line to your deploy.rb

For more changes in Capistrano 2.1 
http://weblog.jamisbuck.org/2007/10/14/capistrano-2-1

Wednesday, October 24, 2007

Multiview Templates in Rails 2.0

Along with many other enhancements and changes in Rails 2.0 now there are new multi view templates. I think multi view templates are going to be most visible and important change in Rails 2.0.

What essentially has happened is that template format has been separated from  rendering engine. This allows you to parse any type of template (csv, haml, rtf etc)  with erb rendering engine. So this takes respond_to  next level by having different template format for each respond_to.

So the new format for templates is action.format.renderer (i.e. show.html.erb, show.rtf.erb etc). you can declare own mime-type aliases in the config/initializers/mime_types.rb file. This file is included by default in all new applications.

For other new features coming in rails 2.0

http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release

Saturday, August 18, 2007

Using .Net SOAP Services with wsdl2ruby

Easist way to use SOAP service in pure ruby is with wsdl2ruby. Simplest code would be to:


require 'soap/wsdlDriver'
wsdl = 'http://www.dotnetsite.com/MyService/Service.asmx?WSDL
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
puts driver.GetVals(:parameter => SOAP::SOAPInt.new(136))


Where GetVals is .Net web method to call. Generating stub files with wsdl2ruby.rb against a WSDL could what arguments you should pass.


% wsdl2ruby.rb --wsdl Foo.wsdl --type client --force


I would like to thank Hiroshi NAKAMURA for this last information

Wednesday, July 4, 2007

Using Gravatar plugin to embed avatars in rails views

Gravatar offers service to keep your globally recognizable avatars (gravatar). This tiny plugin offers rails view helper to embed gravatars in your views. It can also be used in blog, forums applications to insert gravatar to be visible instead of email. Here is how to install it:

$ ruby script/plugin install http://tools.assembla.com/svn/hasham/plugins/gravatar_tag


Here is sample usage in an erb template:

<%= gravatar_tag "user@domain.com", :size => "60x60"%>


The first parameter which is a email that user signed up with on gravatar.com is required. The other parameters are same as rails image_tag view helper.

Monday, July 2, 2007

SVN Cache made simple with Capistrano 2

You normally don't want to do complete checkout of your source control on every deployment with Capistrano. The SVN cache keeps copy of source code on server in separate directory on each deploy this copy of source code is updated and deployed to releases directory.

Implementing this kind of SVN cache is super simple in Capistrano 2, Just set deploy_via variable to remote_cache like this:

set :deploy_via, :remote_cache

Sunday, July 1, 2007

Mongrel is multi threaded, but rails is not thread safe

The main reason why we need to run multiple mongrel instances (pack of mongrels) for any high traffic website is that Ruby on Rails code is not thread safe. This is not the case with other Ruby frameworks like Camping, Merb and Og + Nitro. There is a synchronized block around the calls to Dispatcher.dispatch (in dispatch.rb) rest is multithreaded. so to get any sort of concurrency in serving request we need to run multiple mongrel instances.

In my experience 128 MB RAM is required to run single instance of mongrel server. which means you should not run more than 8 mongrels on your 1 GB RAM VPS. If the rails could be thread safe it would require lot less server resources to deploy rails with mongrels.