Tuesday, August 31, 2010

NoMethodError: undefined method 'coordinates' for #


In trying to use the latest Sunspot code (from github) for geo search with Rails 3.0.0 , I was getting the following error while running Sunspot.index!(Location.all), where Location is my ActiveRecord class for storing locations:
NoMethodError: undefined method 'coordinates' for <Sunspot::DSL::Fields:...>
view raw gistfile1.sh hosted with ❤ by GitHub

After searching through the new code I found that coordinates is no longer a valid field type, but instead location is to be used. location expects an object that responds to lat and lng. Sunspot provides a handly little struct called Coordinates to be this object. So, here is my model code for setting up the search:
class Location < ActiveRecord::Base
searchable do
location :coordinates
end
def coordinates
Sunspot::Util::Coordinates.new(self.lat,self.lng)
end
def coordinates=(sunspot_util_coordinates)
self.lat,self.lng = [sunspot_util_coordinates.lat, sunspot_util_coordinates.lng]
end
end
view raw gistfile1.rb hosted with ❤ by GitHub

notice the virtual attribute called coordinates which wraps the lat & lng ActiveRecord attributes. My search code now looks like this:
Location.search {with(:coordinates).near(42.331527,-83.075953)}
view raw gistfile1.rb hosted with ❤ by GitHub

And my search results are coming in as expected.

Tuesday, August 10, 2010

Rails 3 doesn't like auto_link href_options

Even though the Rails doc indicates one should be able to do something like this:
auto_link(simple_format(h(text)), :href_options => { :class => 'auto-link' })
it doesn't work. looking at the Rails code in text_helper.rb, auto_link is only looking for options[:html], so :href => {} just gets ignored. one actually needs to do this:
auto_link(simple_format(h(text)), :html => {:class => 'auto-link'})
and now it works fine.

Monday, August 9, 2010

jquery auto resize fonts

I ran experiments on font-size versus line length so that I could find the proper formula to automatically resize a font in order to keep the character count per line the same as a person resizes his browser window.
UPDATE: i inverted the resize function to look traditionally linear.
Here is the jquery code that keeps the line length the same:
$(document).ready(function(){
if ($('.resize-font').length) {
$(window).resize(resize);
resize();
}
});
function resize() {
// sizeCoefficient lets me decide how big the fonts are.
// 0.75 gives about 10 words per line, which is good design
var sizeCoefficient = 0.75;
var newFontSize = Math.floor(sizeCoefficient * (0.0394 * $('.resize-font').width() + 0.2489))
$('.resize-font').css('font-size', newFontSize)
}
view raw gistfile1.js hosted with ❤ by GitHub
it works pretty well with Chrome and Safari on Mac.
the initial inspiration for this code came from a post by ingeva on daniweb.

font size versus line length

I ran an experiment on Google Chrome (code below) to determine how line length varied with font size. it looked pretty linear.

Below is my guess for the curve. You can see it on Wolfram Alpha:
(line length) = 25.407 * (font size) + 6.3799

Here is the jquery/html code i used to generate the results:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<title>Font Size Experiments</title>
<style type="text/css">
span {margin:0;padding:0;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
for(i=0;i<500;i++)
{
$('#experiments').append('<span class="experiment" style="font-size:'+i+'px;" id="lorem_'+i+'">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do</span><br/>');
}
$('.experiment').each(function(index) {
$('#results').append($(this).css('font-size')+','+$(this).width()+'\n');
});
});
</script>
</head>
<body>
<pre id="results"></pre>
<div id="experiments"></div>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub

the initial inspiration for fitting the curve of font size to line length came from a post by ingeva on daniweb.

Monday, August 2, 2010

NoMethodError: undefined method sanitize_for_mass_assignment

once upon a time i started getting a strange error:

NoMethodError: undefined method `sanitize_for_mass_assignment'


after poking and prodding it turned out that after running bundle install, bundler grabbed the latest version of the state_machine gem, namely 0.9.4, which makes the call to sanitize_for_mass_assignment, which wasn't found. i likely have some other dependencies missing. however, to get back to my regularly scheduled coding, i just modified my Gemfile to use a previous version of state_machine:

gem 'state_machine', '0.9.3'


now all is good and my rake tests are passing again.