Posts

Showing posts from November, 2008

Prawnto, Generating PDF in Rails Applications

Rails wiki lists a number of libraries that can be used to generate PDF files in ruby. Prawnto was very suitable for my need (generating PDF files that contain text with differentfont-size and adding some formatting). Prawn is able to generate more complex PDF documents including adding photos. You can check the installation steps onlinePrawnto requires you to add code to your controller and to a .prawn layout file. There are several demos available online and can be used as guidance.Below is an example for generating a PDF format of an article. The article has a title and an array of blocks, each block has title and content. The controller code is:def show
   @title="My Article"
   @blocks= [
         {:title=>'Block One', :content=>'content one'},
         {:title=>'Block Two', :content=>'content two'},
         {:title=>'Block Three', :content=>'content three'},
         {:title=>'Block Four', :content…

Writing Prettier Ruby Code with instance_eval

instance_eval is a method of ruby Object class that lets you pass a string of ruby code or a block to be evaluation in the scope of that object.Through instance_eval you can actually add new methods to a particular object. This can help you write more pretty and readable code. for example, we can define a new average method for an array of numbers>> a = [1, 2, 3, 4, 5, 6, 7, 8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
>> a.sum
=> 36
>> a.size
=> 8
>> a.average
NoMethodError: undefined method `average' for [1, 2, 3, 4, 5, 6, 7, 8]:Array
    from (irb):14
>> a.sum / a.size
=> 4
>>With instance_eval we can add that new method here
a.instance_eval do
    def average ; sum/size end
end
>> a.average
=> 4
In a lot of cases this approach can be useful.

REXML Error when running rcov

Rcov is one of the tools we use at eSpace to measure automated tests coverage for our rails applications.By rcov through gem install rcov, I had rcov(0.8.1.2.0) installed.After upgrading to the latest ubuntu 8.10, I realized that the installed rcov is not compatible with ruby 1.8.7 that i have. Rcov failed at generating the html reports with this stacktrace/usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `[]': no implicit conversion from nil to integer (TypeError)
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `wrap'
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `wrap'
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:90:in `write_text'
    from /usr/lib/ruby/1.8/rexml/formatters/default.rb:50:in `write'
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:75:in `write_element'
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:73:in `each'
    from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:73:in `write_element'
    fr…

Creating Functional Tests for Restful Authencation based websites

At eSpace we stress on having automated tests to cover our code.If you are using Restful Authentication plugin in your rails application, You will have to bypass the authentication filter that is processed before your controller actions.Restful authentication helps you does that through the login_as() method of theAuthenticatedTestHelper module. Let me show you how.In your functional test, login before you send the request.   class PublicationsControllerTest < ActionController::TestCase
       fixtures :users

       def test_method
          login_as (:user_one)
          Your test code...
       end
   end
That user is loaded from the users fixtures and will act as the current logged in user. The fixtures must have something like:user_one:
    id: 1
    login: test
    email: test@espace.com.eg
    salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
    crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
The login_as method is defined in the AuthenticatedTestHelper modul…