Wednesday, October 28, 2009

Easy Grpahs with Scruffy on RoR

Scruffy Library
Scruffy Examples

1 Install scruffy gem
gem install scruffy

2 Prerequisites for scruffy gem.. (RMagick,ImageMagick)

So download from http://rubyforge.org/frs/download.php/38052/RMagick-2.5.0-ImageMagick-6.4.1-5-Q8.zip

i) Extract this to temperory folder.

ii) Run the ImageMagick installer

iii) Install RMagick gem

3) After this try samples from

Scruffy Examples

Started to work, Except it shown in examples
I face same problem while i was trying

One blog post which helped me to fix this was
http://www.ruby-forum.com/topic/193988%20

It suggests to replace the line 35, of the base.rb (for me it is C:\Ruby\lib\ruby\gems\1.8\gems\scruffy-0.2.6\lib\scruffy\renderers\base.rb)with

svg.svg(:xmlns => "http://www.w3.org/2000/svg", 'xmlns:xlink' =>"http://www.w3.org/1999/xlink",:viewBox => "0 0 #{options[:size].first} #{options[:size].last}") {


4) Now You can see same graps as shown examples.

Example:

Controllers Code
....

app/graphs_controller


def index

@graph = Scruffy::Graph.new
@graph.title = "Comparative Agent Performance"
@graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
@graph.add :stacked do |stacked|
stacked.add :bar, 'Siva', [30, 60, 49, 29, 100, 120]
stacked.add :bar, 'Krishna', [120, 240, 0, 100, 140, 20]
stacked.add :bar, 'Motamarri', [10, 10, 90, 20, 40, 10]
end
@graph.point_markers = ['Jan', 'Feb','Mar','Apr', 'May', 'Jun','Jul']

@graph.render(:width => 800, :as => 'JPG',:to => "#{RAILS_ROOT}/public/images/AgentGraph.jpg")
# (stores the image in "/public/images" folder in ".jpg" format)

end


Display Graphs in view page
"app/views/graphs/index.html.erb"

<%= image_tag("
AgentGraph.jpg %>")


Wednesday, October 21, 2009

Building Complex Forms using Rails 2.3.2

# Model's code

class Programmer < ActiveRecord::Base

has_one :project_manager

has_many :assignments

accepted_nested_attributes_for :assignments #Rails 2.3+

#Can also used for has_one relation ship

#Can create nested attributes for nested models.

accepts_nested_attributes_for :assignments, :reject_if => proc { |attributes|
attributes['name'].blank? }
#You can also specify requirements for any new records
that are added via nested attributes using the :reject_if option:
end

class assignment < ActiveRecord::Base
belongs_to :programmer
end




#views code
#"programmers/new"
<% form_for @programmer do |f| %>
<%=f.error_messages %> #validates both programmer and assignment model fields
<%= f.label :name %>
<%= f.text_field :name %>

<% f.fields_for :assignments do |assignment| %>

<%= assignment.label :name %>
<%= assignments.text_field :name %>
<% end %>

<%= submit_tag %>
<% end %>



# Controller's code

class programmersController < ApplicationController

def new
@programmer = Programmer.new
@programmer.assignments.build
end

def create
@programmer =Programmer.new(params[:programmer])
if @programmer.save #it autometically saves 'programmer_id 'and fields of
assignments table.
rediretc_to programmers_path
end
end

def update
@programmer = Programmer.find(params[:id])
@programmer.update_attributes(params[:programmer]) ?
redirect_to(programmer_path(@programmer)) : render(:action => :edit)
end

end