Archive for the 'Plugins' Category

wee_lightbox rails plugin

August 6th, 2008 by pyrat

Overview

This is a packaged version of lightbox for viewing images on the screen in the popular lightbox design pattern. The plugin includes some view helpers and an automatic install script to make life easier.

Please refer to the lightbox docs for more info. Included are some rails helpers for easy integration into your site. The lightbox javascript is slightly tweaked to configure the location of images and increase the speed of resize.

You can edit lightbox.js to further tweak these if you wish.

Install

If you install from github with rails 2.1+ it will install the files into your public directory. If you install with another method, you may need to issue a recursive copy command manually.

Also this install script is untested on windows.

Usage Example

In your layout add the following in the head section.

<%= javascript_include_tag :defaults %> 
<%= yield :page_includes %>

In your template:

<%= load_lightbox %>

For each image that you would like to make a lightbox to

<%= lightbox_to('/images/small.gif', '/images/large.gif', "Tasty image caption")

If you want to have more than one lightbox on a page and have the lightbox operate with a slideshow between images..

<%= lightbox_to('/images/small.gif', '/images/large.gif', "Tasty image caption", "group_name")

Where group name needs to be the same for all images in the slideshow.

Github project page

Wee Date Picker Rails Plugin

July 18th, 2008 by pyrat

After a bit of a sabbatical here is another plugin for you to feast on.

This is a super simple datepicker for your rails apps. It is essentially just a packaged version of DatePicker v2.9 by frequency-decoder.com with rails helpers for easy integration.

Install

If you install the plugin using git the installer should run and the files will be copied across recursively to your public directory.

Example

In the head section of the layout.

<%= yield :date_includes %>

In the template.

<%= load_date_picker %>

Then for each date field you need to do the following. Remember: field_names have to be unique on your page.

<%= date_picker :start_date, Date.today %>

Where the first argument is the field_name and the second is the initial value. This must be a date object.

This is a tiny plugin which needs a bit of work but hopefully it helps. It is available on the github project page

Update: I wrote the installer script myself.

FAQ_U Rails FAQ Generator Plugin

December 20th, 2007 by pyrat

Faq u

For a christmas present I give you the Rails FAQ Generator plugin FAQ_U.

This is a simple layout-less unbranded Frequently Asked Questions system. The idea is you generate the default system and then customise it to the requirements of the particular project that you are working on.

Remember to add some authentication to the faq_admin controller.

Requires HAML. To install it:

gem install --no-ri haml
haml --rails path/to/app

To install the plugin:

./script/plugin install http://vorlich.ath.cx/code/excited/plugins/faq_u

To run the generator:

./script/generate faq

This generates a FAQ admin controller, FAQ view controller, the necessary views, a model, db migration and a collection of
tests.

After the generator is complete run the migration to get the database up to scratch.

rake db:migrate

The faq admin is available at */faq_admin* and the front end at */faq*. As you can see below it looks pretty filthy, but as you can see below that, with the introduction of a layout it looks nicer.

unstyled

styled

A would be nice would be a nice AJAX ordering interface in the faq_admin. Feel free to send me some svn patches of any improvements you make. Please write some tests to prove the new functionality works. Cheers now, Merry Christmas.

Rupdf - Simple Ruby PDF Rails Plugin

December 15th, 2007 by pyrat

Rupdf

This is a plugin which I have developed internally with Iformis. I realised it would be nice to share yet another pdf plugin with the rails community. This is designed to render pdfs with layouts. This is especially useful when working on projects where the pdf has to conform to a set design and where the data is presented in a generate report style. Banks often operate their tools in this manner.

Simple PDF reporting rails plugin designed to render layout based pdfs. Built on top of Ruport which is in turn built on top of pdf-writer.

Requires Ruport

Step 1: Install Ruport

gem install ruport

Step 2: Install the plugin (from RAILS_ROOT)

./script/plugin install http://iformis.svnrepository.com/svn/rupdf

Step 3: Create a class extending Rupdf::Base

Note: The only methods calls required are define_header, define_body and define_footer
The other methods are helper methods. Remember that html doesnt work in the body.

class Simple < Rupdf::Base
 
  define_variables :report_title
  renders :pdf, :for => Rupdf::Renderer
 
  define_header do
    add_header(report_title)
  end
 
  define_body do
    add_text("hello man\n\n\n")
    add_text("bye man.")
 
    add_text("bye man.")
    add_text("hello man\n\n\n")
    add_text("bye man.")
    add_text("hello man\n\n\n")
    add_text("bye man.")
 
    # add image (path defined at runtime)
    image(smile_path)
 
    add_text("hello man\n\n\n")
    add_text("bye man.")
    add_text("hello man\n\n\n")
 
  end
 
  define_footer do
    footer_text = %(
    This is the beautiful footer text.
    )
    add_footer(footer_text)
  end
 
 
 
  def add_header(title)
    rounded_text_box("<b>#{title}</b>") do |o|
      header_color = Color::RGB.from_html("#FFDE16")
      o.fill_color = header_color
      o.stroke_color = header_color
      o.radius     = 0
      o.width      = options.header_width || 550
      o.height     = options.header_height || 80
      o.font_size  = options.header_font_size || 12
      o.x          = pdf_writer.absolute_right_margin - o.width
      o.y          = pdf_writer.absolute_top_margin
    end
 
 
 
  end
 
  def add_footer(text, options = nil)
 
    unless options
      options = OpenStruct.new(:font_size => 6)
    end
 
    rounded_text_box(text) do |o|
      footer_color = Color::RGB.from_html("#EAECEE")
      o.fill_color = footer_color
      o.stroke_color = footer_color
      o.radius     = 0
      o.width      = options.header_width || 550
      o.height     = options.header_height || 60
      o.font_size  = options.font_size || 12
      o.x          = pdf_writer.absolute_right_margin - o.width
      o.y          = pdf_writer.absolute_bottom_margin + o.height
 
    end
 
  end
 
end

Step 4: Tie it into a controller and pass the variables at runtime.

class TestController < ApplicationController
 
  def pdf
    simple = Simple.new
    pdf = Rupdf::Renderer.render_pdf do |o|
      o.report_title = 'This is a test of a var being passed.'
      o.smile_path = RAILS_ROOT + '/public/images/smile.jpg'
    end
    send_pdf(pdf)
  end
 
end

The send_pdf function sends the rendered pdf to the browser.

If you need to perform more complicated pdf rendering operation
please refer to the API Documentation for pdf-writer. The API docs for
ruport will also be useful if you are involved in presenting tabular
data from activerecord.

Example programming code along with how to call it is supplied in the
examples directory. I suggest you use these as a base for pdf generation code you write.

This has been tested with both Rails 1.2 and Rails 2.01. Thanks for listening.

Attachment Fu Validates As Attachment Hack

November 28th, 2007 by pyrat

Attachment Fu Hack

Recent err the blog posted about the my evil twin hack technique

In this post they detail how to hack plugins nicely and provide an example on how to hack attachment_fu. It turns out that I have been spending quite a bit of time with attachment_fu on various projects recently and have started to need it to do slightly different things.

Below is a hack I have written to improve validates_as_attachment. Using the evil twin technique this will just ask for a file to be uploaded if you dont upload anything. Instead of spewing out a whole load of errors that dont help the user much.

klass = Technoweenie::AttachmentFu::ClassMethods
klass.module_eval do
 
def validates_as_attachment
validate :uploaded_data_is_present
validates_presence_of :size, :content_type, :filename, :if => :uploaded_data?
validate              :attachment_attributes_valid?
end
 
end
 
klazz = Technoweenie::AttachmentFu::InstanceMethods
klazz.module_eval do
def uploaded_data?
  return false unless filename
  true
end
 
# validates the size and content_type attributes according to the current model's options
def attachment_attributes_valid?
if uploaded_data?
[:size, :content_type].each do |attr_name|
enum = attachment_options[attr_name]
errors.add attr_name, ActiveRecord::Errors.default_error_messages[:inclusion] unless enum.nil? || enum.include?(send(attr_name))
end
end
end
 
def uploaded_data_is_present
unless uploaded_data?
errors.add_to_base("You must select a file to upload.")
end
end
 
end

This serves as a drop in replacement for validates_as_attachment

Checkbox Select Rails Plugin

November 5th, 2007 by pyrat

checkbox

I had the problem of there not being a simple form rendering solution for has_and_belongs_to_many ActiveRecord relations. I found a partial solution here: http://railshacks.blogspot.com/2007/09/helper-checkboxcollection.html

I have modified the original code slightly, and extracted it to a plugin. To install it:

  ./script/plugin install http://vorlich.ath.cx/code/excited/plugins/checkbox_select

In your model you are manipulating in the form:

  class Advertisement < ActiveRecord::Base
 
    include CheckboxSelectable
    has_and_belongs_to_many :categories
    validates_presence_of :name
    validates_uniqueness_of :name
 
  end

The include is the important bit.

In your view:

  <%= checkbox_select('advertisement', 'categories', @categories, 'name') %>

In your controller add the update_check_list method:

  def create
    @advertisement = Advertisement.new(params[:advertisement])
    @advertisement.update_check_list(params, 'Category')
 
    respond_to do |format|
      if @advertisement.save
        flash[:message] = 'Advertisement was successfully created.'
        format.html { redirect_to advertisements_url }
        format.xml  { head :created, :location => advertisement_url(@advertisement) }
      else
        assigns
        format.html { render :action => "new" }
        format.xml  { render :xml => @advertisement.errors.to_xml }
      end
    end
  end

I still need to write a collection of tests for this plugin. If you have the time please write them for me!