Hi Guys,
Waitr is a ruby gem used in testing. It has many uses, mostly it is used to control the web browser(all html dom elements as objects).
installation command:-
gem install waitr
Some sample use of watir
1. Starting a new Web browser
require 'watir'
ie = Watir::Browser.new
ie.goto("http://www.google.com")
2. Starting a firefox browser
require 'watir'
Watir::Browser.default = "firefox"
ff = Watir::Browser.start("http://www.google.com")
3.Setting a text field with some value
ie.text_field(:name, "username").set "Vaddi"
4. Setting value of select list
ie.select_list( :name , "category").select("Comedy")
For all the DOM elements, it has objects(for text box - 'text_field', for drop down - 'select_list')
thanks,
Monday, December 7, 2009
Friday, December 4, 2009
for loop - js
In javascript, to read the array index and their appropriate value.
Use the following way,
var a = Array.new(); declare new array with name 'a'
a = [1,2,3,4,5]; assign some values to array
var indexes = function show(value, indx){
alert(indx);
alert(value);
}
a.forEach(indexes);
'forEach' method is used to collect the values from the array.
By using the method 'forEach', we can collect each element from array.
Try this..
Use the following way,
var a = Array.new(); declare new array with name 'a'
a = [1,2,3,4,5]; assign some values to array
var indexes = function show(value, indx){
alert(indx);
alert(value);
}
a.forEach(indexes);
'forEach' method is used to collect the values from the array.
By using the method 'forEach', we can collect each element from array.
Try this..
Monday, November 30, 2009
Add favicon image to your site
Its very easy to add favicon to ur site by following steps
include the line in your layout head
html link tag
with attributes
rel="icon" href="favicon_path"
type="image/x-icon"
favicon_path = image path of favicon
That's it...
Try with this..
include the line in your layout head
html link tag
with attributes
rel="icon" href="favicon_path"
type="image/x-icon"
favicon_path = image path of favicon
That's it...
Try with this..
Friday, November 27, 2009
has_many_friends association
Hi Guys,
I have stuck while building the association for model user,
user has_many friends, friend - nothing but a user
For that i have created a model called 'Friendship', user to join user and friends. And build the association with the following relations
friendship table has the following fields
user_id
friend_id
class User < ActiveRecord::Base
has_many :friendships
has_many :friendships_by_me, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :friends_by_me, :through => :friendships_by_me, :source => :friendshipped_by_me
has_many :friendships_for_me, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :friends_for_me, :through => :friendships_for_me, :source => :friendshipped_for_me
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friendshipped_for_me, :class_name => "User", :foreign_key => 'user_id'
belongs_to :friendshipped_by_me, :class_name => "User", :foreign_key => 'friend_id'
end
Thats it!!!
Now run the following commands
user = User.first
user.friends_by_me => [users] returns the collection of friends added by user.
user.friends_for_me => [users] returns the collection of friends who add user as their friend.
Note: i have referred the plugin 'has_many_friends'.
Thanks,
vaddi
I have stuck while building the association for model user,
user has_many friends, friend - nothing but a user
For that i have created a model called 'Friendship', user to join user and friends. And build the association with the following relations
friendship table has the following fields
user_id
friend_id
class User < ActiveRecord::Base
has_many :friendships
has_many :friendships_by_me, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :friends_by_me, :through => :friendships_by_me, :source => :friendshipped_by_me
has_many :friendships_for_me, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :friends_for_me, :through => :friendships_for_me, :source => :friendshipped_for_me
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friendshipped_for_me, :class_name => "User", :foreign_key => 'user_id'
belongs_to :friendshipped_by_me, :class_name => "User", :foreign_key => 'friend_id'
end
Thats it!!!
Now run the following commands
user = User.first
user.friends_by_me => [users] returns the collection of friends added by user.
user.friends_for_me => [users] returns the collection of friends who add user as their friend.
Note: i have referred the plugin 'has_many_friends'.
Thanks,
vaddi
Sunday, November 15, 2009
Drop down methods
java script methods for drop down.
selectedIndex - to get the selected index value
options[].value - to get the value of the selected item
options[].text - to get the option for the selected item
ex:
var list_box = document.getElementById('id_of_dropdown_box');
list_box.selectedIndex; --> returns the value of selected index(drop down index starts from 0) '1' check
options[0].value --> value of the zero th element '1' check
options[0].text --> text of the zero th element 'Ruby' check
selectedIndex - to get the selected index value
options[].value - to get the value of the selected item
options[].text - to get the option for the selected item
ex:
var list_box = document.getElementById('id_of_dropdown_box');
list_box.selectedIndex; --> returns the value of selected index(drop down index starts from 0) '1' check
options[0].value --> value of the zero th element '1' check
options[0].text --> text of the zero th element 'Ruby' check
ruby - 'yield' method
Hi Guys,
Hope most of our ROR developers are using 'yield' method in the layout file. In ruby we are not using the yield method(called from any method). I have come across the 'yield' method, its very interesting to know about the use of that method.
We can use 'yield' method inside any ruby method to execute the code inside the block.
example-1:
class Sample
def check_yield
yield
yield
yield
end
end
in the class 'Sample', i have called the yield method three times inside the method.
obj = Sample.new()
obj.check_yield{puts "hi am in method block"}
Output:
hi am in method block
hi am in method block
hi am in method block
Now i have created an object for the class 'Sample', and called the method 'check_yield' with the block({ }). When i executed the method it prints the results for three times. Because we are calling the yield for three times, the call to yield method executes the code inside the block({puts "hi am in method block"}). That's it.
We can also use the yield method with params.
example-2:
class Sample
def even_nos_upto(param)
for i in 0..param
yield(i) if i%2 == 0
end
end
end
obj = Sample.new()
obj.even_nos_upto(10){|x| puts x}
Output:
0
2
4
6
8
10
In Array, we are using the method 'collect', 'each' to get each value of the array.
[1,2,3,4,5].collect{|x| puts x}
Inside the collect method in Array class, the yield method called to return each value.
class Array
def my_collect
for i in 0..(self.length-1)
value = self[i]
yield(value)
end
end
def collect_greater_than(limit)
for i in 0..(self.length-1)
value = self[i]
yield(value) if value > 5
end
end
end
[1,2,3,4,5,6,7].collect{|x| puts x} # default method collect
[1,2,3,4,5,6,7].my_collect{|x| puts x} # method my_collect
[1,2,3,4,5,6,7].collect_greater_than(5){|x| puts x} # collect with condition
Output:
1
2
3
4
5
6
7
1
2
3
4
5
6
7
6
7
In the above sample, the method 'collect' & 'my_collect' works like a same. And the method collect_greater_than bit different from the first one, it returns the values based on the condition we have used.
Hope most of our ROR developers are using 'yield' method in the layout file. In ruby we are not using the yield method(called from any method). I have come across the 'yield' method, its very interesting to know about the use of that method.
We can use 'yield' method inside any ruby method to execute the code inside the block.
example-1:
class Sample
def check_yield
yield
yield
yield
end
end
in the class 'Sample', i have called the yield method three times inside the method.
obj = Sample.new()
obj.check_yield{puts "hi am in method block"}
Output:
hi am in method block
hi am in method block
hi am in method block
Now i have created an object for the class 'Sample', and called the method 'check_yield' with the block({ }). When i executed the method it prints the results for three times. Because we are calling the yield for three times, the call to yield method executes the code inside the block({puts "hi am in method block"}). That's it.
We can also use the yield method with params.
example-2:
class Sample
def even_nos_upto(param)
for i in 0..param
yield(i) if i%2 == 0
end
end
end
obj = Sample.new()
obj.even_nos_upto(10){|x| puts x}
Output:
0
2
4
6
8
10
In Array, we are using the method 'collect', 'each' to get each value of the array.
[1,2,3,4,5].collect{|x| puts x}
Inside the collect method in Array class, the yield method called to return each value.
class Array
def my_collect
for i in 0..(self.length-1)
value = self[i]
yield(value)
end
end
def collect_greater_than(limit)
for i in 0..(self.length-1)
value = self[i]
yield(value) if value > 5
end
end
end
[1,2,3,4,5,6,7].collect{|x| puts x} # default method collect
[1,2,3,4,5,6,7].my_collect{|x| puts x} # method my_collect
[1,2,3,4,5,6,7].collect_greater_than(5){|x| puts x} # collect with condition
Output:
1
2
3
4
5
6
7
1
2
3
4
5
6
7
6
7
In the above sample, the method 'collect' & 'my_collect' works like a same. And the method collect_greater_than bit different from the first one, it returns the values based on the condition we have used.
Friday, October 30, 2009
has many polymorphic - double sided
I have used the following model relation to get the has many polymorphic double sided relation.
class Album < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :foreign_key => "album_id", :class_name => "AlbumFile"
has_many :photos, :source => :file, :through => :album_files, :source_type => "Photo", :class_name => "Photo"
has_many :videos, :source => :file, :through => :album_files, :source_type => "Viedo", :class_name => "Video"
end
class AlbumFile < ActiveRecord::Base
belongs_to :file, :polymorphic => true
end
class Photo < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end
class Video < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end
thats it..
Now we have associated Album with photos, videos through the mapping table album_files.
Following works for this
album = Album.create()
album.photos << Photo.create()
album.vidoes << Video.create()
album.album_files.count - returns the total count i.e, 2
album.photos - returns the count 1
album.vidoes - returns the count 1
class Album < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :foreign_key => "album_id", :class_name => "AlbumFile"
has_many :photos, :source => :file, :through => :album_files, :source_type => "Photo", :class_name => "Photo"
has_many :videos, :source => :file, :through => :album_files, :source_type => "Viedo", :class_name => "Video"
end
class AlbumFile < ActiveRecord::Base
belongs_to :file, :polymorphic => true
end
class Photo < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end
class Video < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end
thats it..
Now we have associated Album with photos, videos through the mapping table album_files.
Following works for this
album = Album.create()
album.photos << Photo.create()
album.vidoes << Video.create()
album.album_files.count - returns the total count i.e, 2
album.photos - returns the count 1
album.vidoes - returns the count 1
Subscribe to:
Posts (Atom)