already initialized constant in fixtures
Following the dynamic fixture example from most of the rail’s books and tutorials available out there… you would be tempted to add something like this to the top of your fixture. ( this example is particularly relevant to the users fixture for use with the LoginEngine)
<% SALT = "nacl" %>
and then you would use this constant in your fixture somewhere… like maybe
salt: <%= SALT %>
salted_password: <%= LoginEngine::AuthenticatedUser.salted_password
(SALT, LoginEngine::AuthenticatedUser.hashed('secret'))%>
This is good for a single use of this fixture. But if more than one unit tests or functional tests load this fixture, you will start getting a warning “already initialize constant SALT” for all but the first use of the fixture.
Though it is just a warning, I did not find a mention of this in any of the books/tutorials.
Just a small check for an already defined constant will fix this warning.
<% SALT = "nacl" unless self.class.const_defined?("SALT") %>
assert_tag for a hyperlink in a functional test’s response
I spent some time yesterday trying this out.
I am writing it here to save your time.
Here is the functional test’s code to check for a hyperlink with text “Back to Index” which links to the index action of the controller being tested.
link = find_tag :tag => "a", :content =>"Back to Inbox"
assert_equal @controller.url_for(:action => 'index', :only_path => true), link.attributes["href"]
It could have been done in a single assert_tag … but the statement becomes too long.
Simple star rating in Rails
Those who wants to include simple 5 star rating mechanism in their ROR application.Can include a couple of functions in their helper to provide a form a rating field and display the result.Of course no 1/2 ratings can be achieved through this way. One needs to include the below given two function to some helper. And in view inside form tag call…
star_rating_field("object", "fieldname", options)
Options are location, prefix, active_img, grey_img, hover_img…
For displaying the result of the rating in star images format…call from view
img_tag_for_star_rating(rating_value,options)
Options are location, active_img, grey_img, hover_img…
Download the function to include in helper from here …I am finding it very difficult to put the code in wordpress without tampering so better to put it somewhere else
Update: the file gets removed everytime.I m trying to out the code here only…
#General function for star rating
# Use these function as helpers
# No half ratings are handled here
# options can hav ...
# location=>location of images( you need to have star_active.gif, star_grey.gif, star_hover.gif in this directory or provide names of image file in options)
# prefix => prefix to append to each ids in DOM for descrimination of more then one star rating in the same page
# grey_img => name of inactive/grey image of star
# active_img => name of active image of star
# hover_img => name of hover image of star
def star_rating_field(obj_name, field_name, options={:location=>'/images',:hover_img=>"star_hover.gif", :active_img=>"star_active.gif", :grey_img=>"star_grey.gif"})
# append a hidden field to store value of viz system field
# this field is to be changed as hidden after testing
hid = text_field(obj_name, field_name)
hid_id = "#{obj_name}_#{field_name}"
pref_id = options[:prefix].nil? ? 'star' : options[:prefix]+'_star'
img_location = options[:location]
# set initial value by javascript
jscript = ""
jscript += "var a; var v = parseInt($('#{hid_id}').value); for(var i = 1;i"
img = ''
# add images to field
for i in 1..5
img += "
“+’ ‘
end
return img + hid + jscript
end
def jscript_for_star_rating(pref_id, img_location)
jscript = ” function set_star_status(n, id) { if( $(id).value != n){ for(var j=1; j”star_active.gif”, :grey_img=>”star_grey.gif”})
image = ”
for i in 1..5
if i ”
else
image += “
”
end
end
return image
end
Note:Do remember to include prototype library from script.aculo.us in your page.
UPDATE2:phew…finally trying to add the file again.Hope it work fine this time
try this=>
Automatic Ajaxification With KRJS
KRJS plugin developed by Chew Choon Keat could be called an extension to RJS templates by Cody Fauser . In his post KRJS: RJS without messing the Views Keat explains how KRJS can be used to update functionality in views without even touching the views.
Ruby on Rails Presentation
I will be making a Ruby on Rails presentation at the August php meetup.
This would be my second presentation on RoR, after the one at BarCampDelhi.
And this time there will be no ppts ;)
Thanks to ValueOne for hosting the meetup.


