Monday, 2 April 2012

Dynamic sidebar in rails layout


Today, I am sharing common code that i usually used to set my layout.
My layout contains conditionally sidebar, some action contain sidebar some are not.S
  • One of the way is to create 2 layout file one with no sidebar and one for with sidebar.
  • Other way is one layout file and conditionally set sidebar content.
In this post i am explaining one layout with conditionally sidebar.

I am using grid system, you can use any class who sets width of sidebar.

So my layout file contains
 <div class="grid_16">
  <div class="<%= main_content_css_class%>">
   <%= yield %>
  </div>
  <div class="<%= sidebar_css_class%>">
   <%= yield :sidebar %>
  </div>
</div>
Now my application helper contains

module ApplicationHelper
  def sidebar_enabled?
    current_page = "#{controller.controller_name}.#{controller.action_name}"
 
    current_controller = controller.controller_name
 
    pages = %w(some_controller,some_controller.specific_action)

    return pages.include?(current_page) || pages.include?(current_controller)
  end
  def main_content_css_class
    sidebar_enabled? ? "grid_12" : "grid_16"
  end

  #Returns the CSS class for the 'sidebar' div depending on sidebar requirement
  def sidebar_css_class
    sidebar_enabled? ? "grid_4" : "dont-show"
  end
end
So basically sidebar_enabled? contain list of controller or controller.action_name in which sidebar is required.

You can use reverse way if your layout contains more sidebar.

I hope this is helpful for setting up layout.

Tuesday, 27 March 2012

Thinking Sphinx With Rails Engine

Currently i am working with rails 3.2 application,i am using thinking sphinx for full text search.

In this application i have several module that are works independently so it's nice to have build each module separately(Engine). So Engine also has search which is built on sphinx.


Now after integrating this engine with main application when i start indexing it will not index Engine model may be it's path issue or load issue,after googling and traversing source code couple of solution are found.

One of them is create  file under initializers directory of each engine or add code where it is initialized so when rails is load below code is hooked up.

module ThinkingSphinx
  class Context
    def load_models
      Billing::Category
      Billing::Item
    end
  end
end
It will solve my purpose and indexing run perfectly fine.
There are others way also but i find this is easy. Is there any other way kindly share. 

Thursday, 29 September 2011

Generate PDF using Erb or HAML and pdfkit


In my current work i want to generate pdf and pre store into my application and whenever user wants to download pdf user can download pdf easily. I can't generate pdf on the fly because of some limitation.

Previously we are using prawn but as complexity increases and time decreases we started looking into another solution, we came across mainly pdfkit,wicked_pdf,acts_as_flying_saucer library.

We initially started with pdfkit to test our need it full fill majority all requirement,but as i say i want to generate PDF and store into my rails app.So it is easy to generate from HTML but we need to generate PDF from erb template (HAML can also work) in backend.

Basically  it is 3 step process
  1. Read ERB(Haml,...) file and store into string.
  2. Parse file using erb with necessary parameter.
  3. Generate PDF

So i am sharing my small idea it works for me.
require 'rubygems'
require 'pdfkit'
require 'erb'

# Read ERB File
  content = File.read('path/of/erb/template')
# Create new ERB template by passing required variable
 template = ERB.new(content)
# Create new object to PDFKIt
 kit = PDFKIT.new(template.result(binding))
# Save into PDF 
 file = kit.to_file('/path/to/save/pdf') 



We can set configuration for pdfkit and erb as per our requirement.
I hope this is helpful,if any other way we can generate PDF kindly share it.