Monday, 30 April 2012

Wrapping pre tag text using css

What Is Pre-Formatted Text?

Pre-formatted text is added to your web pages using the PRE tag.

It is used to be very common to see web pages with blocks of pre-formatted text. Using the PRE tag to define sections of the page as formatted by the typing itself was a quick and easy way to get the text to display as you expected it to. This is because pre-formatted text is defined as text in which the structure is defined by typographic conventions rather than by the HTML.

Try the PRE tag on your website with various different spacings and carriage returns. For example, try pasting the following into your web page HTML (leave the spaces exactly as they are written):

    Read each statement
   the meanings....
    Then share them with someone special.

Basically HTML collapses the white space in the document. This means that carriage returns, spaces, and tab characters are all collapsed to one space. If you typed the above quote into a typical HTML tag like the P tag, you would end up with one line of text:

    Read each statement carefully and understand the meanings....Then share them with someone special.

The PRE tag leaves the whitespace characters alone. So line breaks, spaces, and tabs are all maintained in the browser. Putting the quote inside a PRE tag results in:

    Read each statement
   the meanings....
    Then share them with someone special.

But the PRE tag does more than just maintain the spaces. In most browseres, it is written in a monospace font. This makes the characters in the text all equal in width. In other words, the letter i takes up as much space as the letter w. But you can change this with style sheets.

Wrapping pre tag text in a div using css

When using pre tags in html to display code blocks it will ignore the div boundaries and not wrap the text by default. By adding some css for pre tags this can be accomplished as follows.

Hint: Another option is to use overflow:auto which will add scroll bars for the content.

pre, code{
 /*Wrap pre tag text for all popular browsers*/
 white-space: pre-wrap; /* css-3 */
 white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */
 white-space: -pre-wrap; /* Opera 4-6 */
 white-space: -o-pre-wrap; /* Opera 7 */
 word-wrap: break-word;  /* Internet Explorer 5.5+ */
 /*Or use overflow:auto ----------- */

This will allow your long code to wrap properly and look like this.


Friday, 13 April 2012

Creating plugin as mountable engine in rails 3.1 and higher version

What is a Rails Engine?

Engine are miniature Rails applications that you embed into your main application. You can share an Engine across different applications. Since Rails 3.0, every Rails application is nothing more than an Engine, allowing you to share it very easily.

In Rails 3.1 and higher version provides the ability to create a plugin as engine using a single command.

1. Creating engine
      rails plugin new billing --mountable

   It creates a billing folder in the main app.
   I will move this folder into lib folder  or you can keep it there only.

2. Gem file
   This billing module will function as gem in application. In order to function as gem we need  to define this gem in gem file as
      gem 'billing', :path => 'lib/billing'
    Write this code in gem file in path option specify the path while this gem is stored in your application.
   After writing this run bundle install command

3. Mounting engine
    Next important thing is billing/config/routes.rb file:
      Billing::Engine.routes.draw do   
   These are empty routes, but as you can see, they belong to the engine, not to the host application.

   Since engine is now a rack app, you can simply mount it in your application’s routes:
      Rails.application.routes.draw do
        mount Billing::Engine => "/billing"
   This will mount Billing::Engine at /billing path.

4. Isolated Engine
   Normally when you create controllers, helpers and models inside an engine, they are treated as if they were created inside the application itself. This means that all helpers and named routes from the application will be available to your engine’s controllers as well.
However, sometimes you want to isolate your engine from the application, especially if your engine has its own router. To do that, you simply need to call isolate_namespace. This method requires you to pass a module where all your controllers, helpers and models should be nested to:

      module Billing
        class Engine < Rails::Engine
          isolate_namespace Billing
   With such an engine, everything that is inside the MyEngine module will be isolated from the application.

   Write this code inside billing/lib/billing/engine.rb file
   Using isolated engines  the behavior of routes changes. Normally, when you namespace your controllers, you also need to do namespace all your routes. With an isolated engine, the namespace is applied by default, so you can ignore it in routes:

      Billing::Engine.routes.draw do
        resources :invoices
   The routes above will automatically point Biliing::InvoicesController. Furthermore, you don’t need to use longer url helpers like billing_invoices_path. Instead, you should simply use invoices_path as you would do with your application.

5. Migrations
  Engines can have their own migrations. The default path for migrations is exactly the same as in application: db/migrate.

   To create a migration for engine you have go to that path where engine is located
eg:- cd /lib/billing
 Now generate the migration for invoices.
   Notice that migration for that is called create_billing_invoices instead of create_invoices. Also almost all of the files are places in billing/ subdirectory. When you open app/models/billing/invoice.rb, you will see:
      module Billing
        class Invoice < ActiveRecord::Base
   Everything is namespaced for a good reason.We want to avoid conflicts between engine and host application.
   When you run rake db:migrate this migration will not be created. In order to create this migration you have to copy this migration into main app using command
      rake billing:install:migrations
      rake db:migrate

6.  Cross application routes 
   In order to use engines routes in main app


   In order to use application routes in engine 


I have created a simple app related to engine. You can check it here