Will Paginate — Plugin

September 29, 2008

Will Paginate:

1. Installation:

cmd>git clone git://github.com/mislav/will_paginate.git

2. Git Installation

You don’t have git installed already then install it

Install git from here.

3. Try with this Basic Code:

a. Product model(products Table) contains these fields

id
name
price
description

b. DB has 100 records

c. controller:

def productlist
@products = Product.paginate :page => params[:page]
end

d. productlist.hml.erb

<%= will_paginate @products %>

<ul>
<% @products.each do |c| %>
<li><%= c.id %> – <%= c.name %> – <%= c.price %> – <%=
c.description %></li>
<% end %>
</ul>

Output looks Like:

Links for more help –> http://agilewebdevelopment.com/plugins/will_paginate

Active Scaffold

Active scaffold is rails plugin used for,

Create,Edit,Update,delete,Search and sorting records in Ajax.

If you want to install it in your rails application then,

1. Please follow this url –> Active Scaffold

2. Install git in your computer –> Git

Hi All,

Sorry for a big gap…

Command Prompt commands in Ruby:(Windows Only)

Goto Command prompt:

c:> ipconfig

It will gives the details about ur ip address.How can i get the same from Ruby code.

system(‘ipconfig’)

Output:

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :

IP Address. . . . . . . . . . . . : 192.168.1.25

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

Program 2:

commandprompt.rb

system(‘ping 192.168.1.1′)
puts “———————”
system(‘ipconfig’)

>ruby commandprompt.rb

Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.1:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms

———————

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :

IP Address. . . . . . . . . . . . : 192.168.1.25

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

>Exit code: 0

Program3:

Shutdown,restart,logoff your windows machine using Ruby codes.

shutdown.rb

system(’shutdown.exe’)

Output:

Usage: shutdown.exe [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-
c "comment"] [-d up:xx:yy]

No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c “comment” Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without war
ning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer le
ss than 256)
yy is the minor reason code (positive integer le
ss than 65536)

shutdown.rb

system(’shutdown.exe -r -f -t 0′)

Run it…

Output:

System shutdown in default.

Reference:

http://rubyonwindows.blogspot.com/

http://www.ruby-forum.com/topic/150301

Watir Cheat sheet

December 17, 2007

Watir Cheat Sheet

 

Getting Started with Watir

require ‘watir’

Load the Watir library

ie = Watir::IE.start

(‘http://localhost:8080′)

Start IE using the local timeclock server.

ie.close

Close IE.

ie = Watir::IE.attach(:title, ‘title‘)

Attach to an existing IE window, so you can drive it with Watir.

 

Manipulating Controls with Watir

ie.text_field(:name, ‘name‘).set(‘value‘)

Set the text field specified name specified value.

ie.button(:value, ‘value‘).click

Click the button with the specified value (label)

ie.button(:name, ‘name‘).click

Click the button with the specified name.

ie.checkbox(:name, ‘name‘).set

Check the check box named “name”. (Uncheck: clear)

ie.object(:attribute, ‘name‘).flash

Cause the specified control to flash.

 

A Special Testing Function For Timeclock

 

require ‘toolkit/testhook’

ensure_no_user_data ‘name

Delete any database records for the named user.

 

Accessing Page Contents with Watir

ie.contains_text(‘text‘)

Return true if the current page has the specified text somewhere on the page. Otherwise, false.

ie.title

Return the title of the current page.

ie.html

Return all the HTML in the body of the page

ie.table(:id, ‘recent_records).to_a

Return an array containing the text in the table’s rows and columns.

ie.table(:id, ‘recent_records’)[2][1].text

Return the text from the first column of the second row of the table id’d ‘recent_records.

 

Assertions

assert_equal(expected, test_method)

Test whether the expected value matches the actual value returned by the test method

assert_match(regexp, test_method)

Test whether the regular expression matches the actual value returned by the test method.

assert(expression)

Test whether the expression is true.

Ferret in rails

December 15, 2007

View

<%= start_form_tag :action => ’search_number’ %>

<div align=”center”>
<h3>Search Phonenumber By</h3>
<table>
<tr>

<td><select name=”searchfield” prompt=”select”>
<option selected>select</option>
<option value=”phonenumber” >Phonenumber</option>
<option value=”name” >Name</option>
<option value=”city” >City</option>
</select>
</td>
<td>
<%= text_field_tag ’searchkey’ %>
<%= submit_tag “Search” %>

</tr>
</table>
</div>

<%= end_form_tag %>

Controller:

def search_number
if params[:searchfield]
session[:field]= params[:searchfield]
end
field = session[:field]

if params[:searchkey]
session[:query] = params[:searchkey]
end
query= session[:query]
@total, @phones = User.full_text_search(field,query, :page => (params[:page]||1))
@pages = pages_for(@total)
if @phones.length > 0
render :action=>’search_number’
else
render :text=>’No Result’
end

end

Profile Updation in Rails

December 15, 2007

Account Controller –>

session[:user_id]= self.current_user.id

HOME Controller inside:

def list
end

def search
@user = User.find(session[:user_id])
if @user.update_attributes (params[:user])
flash[:notice] = ‘User Name was successfully updated.’
redirect_to :action => ‘index’, :id => @user
else
redirect_to :action => ‘list’
end

end

LIST.RHTML

<%= start_form_tag :action => ’search’ %>
<%= text_field ‘user’,'name’ %>
<%= text_field ‘user’,'age’ %>
<%= submit_tag ‘Update Profile’ %>

<% end_form_tag %>

INDEX.RHTML

<%= flash[:notice] %>

Pagination in Rails

December 15, 2007

Pagination :

Inside method

if params[:Pincode]
session[:location] = params[:Pincode]
end
location = session[:location]
# @locations_pages, @locations = paginate :companies, :per_page =>2, :conditions=>["location like ?", "%#{location}%"]

@locations_pages, @zipdetail = paginate :zipdatas, :per_page =>5,:conditions  =>["Pincode like?","%#{location}%"]

View :

<% for c in @zipdetail %>
<table border=’1′>
<tr><td> <%= c.Pincode %> </td>
<td> <%= c.OfficeName %> </td>
<td> <%= c.Status %> </td>
<td> <%= c.Suboffice %> </td>
<td> <%= c.HeadOffice %> </td>
<td> <%= c.OfficeName %> </td>
<td> <%= c.Location %> </td>
<td> <%= c.Telephone %> </td>
<td> <%= c.SPCC %> </td>
<td> <%= c.Postal %> </td>
<td> <%= c.Division %> </td>
<td> <%= c.Region %> </td>
<td> **************** </td>

</tr>
</table>
<% end %>

<%= link_to ‘[previous]‘, { :page => @locations_pages.current

.previous } \
if @locations_pages.current.previous %>
<%= pagination_links(@locations_pages) %>
<%= link_to ‘[next]‘, { :page => @locations_pages.current.next } \
if @locations_pages.current.next %>

<%= link_to ‘Back to Search’, :action => ’show’ %>
<%= link_to ‘Home Page’, {:action => ‘index’, :controller => ‘home’}, {:accesskey => 3} %>

Shortcut icon in Rails

December 14, 2007

SHORTCUT ICON TITLE:

1. Add this line inside of header(Layout):

<link rel=”shortcut icon” href=”/images/rails.png”>

2. Paste rails.png to d:\project\public\images\rails.png

MainURl : http://weblog.techno-weenie.net/articles/acts_as_attachment

Reference : http://technoweenie.stikipad.com/plugins/show/Acts+as+Attachment –> Tutorial

d:>rails attach

d:\attach>ruby script/plugin source http://svn.techno-weenie.net/projects/plugins

d:\attach>ruby script/plugin install acts_as_attachment

d:\attach>ruby script/generate attachment_model dvd_cover

d:\attach>ruby script/generate controller dvd_covers index new show

OPEn the rb File d:\attach\db02_create_dvd_covers.rb

create_table :dvd_covers do |t|
t.column “dvd_id”, :integer
t.column “content_type”, :string
t.column “filename”, :string
t.column “size”, :integer
t.column “parent_id”, :integer
t.column “thumbnail”, :string
t.column “width”, :integer
t.column “height”, :integer
end

Open the rb File d:\attach\app\model\dvd_cover.rb

class DvdCover < ActiveRecord::Base
belongs_to :dvd
acts_as_attachment :storage => :file_system, :max_size => 300.kilobytes, :content_type => :image
validates_as_attachment
end


Open the rb File d:\attach\app\dvd_covers_controller.rb

class DvdCoversController < ApplicationController
def index
@dvd_covers = DvdCover.find(:all)
end

def new
@dvd_cover = DvdCover.new
end

def show
@dvd_cover = DvdCover.find params[:id]
end

def create
@dvd_cover = DvdCover.create! params[:dvd_cover]
redirect_to :action => ’show’, :id => @dvd_cover
rescue ActiveRecord::RecordInvalid
render :action => ‘new’
end
end

Open the rhtml File d:\attach\app\views\dvd_covers\index.rhtml

<h1>DVD Covers</h1>

<ul>
<% @dvd_covers.each do |dvd_cover| -%>
<li><%= link_to dvd_cover.filename, :action => ’show’, :id => dvd_cover %></li>
<% end -%>
</ul>

<p><%= link_to ‘New’, :action => ‘new’ %></p>

Open the rhtml File d:\attach\app\views\dvd_covers\new.rhtml

<h1>New DVD Cover</h1>

<% form_for :dvd_cover, :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>
<p><%= f.file_field :uploaded_data %></p>
<p><%= submit_tag :Create %></p>
<% end -%>

Open the rhtml File d:\attach\app\views\dvd_covers\show.rhtml

<p><%= @dvd_cover.filename %></p>
<%= image_tag @dvd_cover.public_filename, :size => @dvd_cover.image_size %>
<%= link_to ‘Back’, :action => ‘new’ %>

d:\attach>rake db:migrate

now goto this url http://localhost:3000/dvd_covers in ur browser

THUMBNAIL:

main URL: http://weblog.techno-weenie.net/articles/acts_as_attachment/thumbnailing

MODEl :

acts_as_attachment :storage => :file_system, :thumbnails => { :normal => ‘300>’, :thumb => ‘75′ }


SHOW PAGE:

<p>Original: <%= link_to @dvd_cover.filename, @dvd_cover.public_filename %></p>
<% @dvd_cover.thumbnails.each do |thumb| -%>
<p><%= thumb.thumbnail.to_s.humanize %>: <%= link_to thumb.filename, thumb.public_filename %></p>
<% end -%>

<% DvdCover.attachment_options[:thumbnails].keys.each do |key| -%>
<p><%= key.to_s.humanize %>: <%= link_to key, @dvd_cover.public_filename(key) %></p>
<% end -%>

Controller :

def index
@dvd_covers = DvdCover.find(:all, :conditions => ‘parent_id is null’)
end

URL : http://technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated

script/plugin source http://svn.techno-weenie.net/projects/plugins

script/plugin install acts_as_authenticated

script/generate authenticated user account

rake db:migrate

script/generate authenticated_mailer user

script/generate migration add_column_activation

Inside Migration File:

add_column :users, :activation_code, :string, :limit => 40

add_column :users, :activated_at, :datetime

remove_column :users,:activation_code

remove_column :users, :activated_at

rake db:migrate

Account Controller:

def activate

@user = User.find_by_activation_code(params[:id])

if @user and @user.activate

self.current_user = @user

redirect_back_or_default(:controller => ‘/account’, :action => ‘index’)

flash[:notice] = “Your account has been activated.”

end

end

User_notifier.rb

def signup_notification(user)

setup_email(user)

@subject += ‘Please activate your new account’

@body[:url] = “http://localhost:3000/account/activate/#{user.activation_code}”

end

def activation(user)

setup_email(user)

@subject += ‘Your account has been activated!’

@body[:url] = “http://localhost:3000/account/index”

end

protected

def setup_email(user)

@recipients = “#{user.email}”

@from = “mailer@raveendran.org”

@subject = “Signup from Raveendran”

@sent_on = Time.now

@body[:user] = user

end

User.rb

before_create :make_activation_code

Before Protected:

def activate

@activated = true

update_attributes(:activated_at => Time.now.utc, :activation_code => nil)

end

def recently_activated?

@activated

end

After Protected:

def make_activation_code

self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )

end

Environment.rb

config.active_record.observers = :user_observer

After End (it means after do|config| loop):

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {

:address => “mail.raveendran.com”,

:domain => “raveendran.com”,

:port => 25,

:authentication => :login,

:user_name => “mailer@raveendran.com”,

:password => “mailer”

}

ActionMailer::Base.perform_deliveries = true

ActionMailer::Base.raise_delivery_errors = true

ActionMailer::Base.default_charset = “utf-8″