Posted by jason on Jan 15, 2010 in
Uncategorized
Embedded fonts are almost a reality in web development. All the modern browsers support the @font-face attribute. Microsoft gets a lot of criticism for innovation, but they have had this ability since 1997 in Internet Explorer 4.0. Unfortunately, they do not simply let you use a TTF font or an OTF font. It uses an EOT file.
The key to embedding fonts is to use the @font-face css rule.
@font-face {
font-family: "myfontname";
src: url(myfont.ttf) format("truetype");
}
Once you’ve added this, you can simply use your font as you would any other font.
<p style="font-family:myfontname">My font name looks like this</p>
It is really simple…
EXCEPT
If you happen to choose a free font that is similarly named, you will run into troubles using Google Chrome. I chose a font called Dakota that is almost identical to Handwriting Dakota on my Mac. When I installed this font to try it out, my Mac warned me that it was a duplicate font, except the menu tool to remove duplicates was disabled. No matter what I tried, I could not get it to work in Chrome. Safari and Firefox both worked just fine. The only way I could fix it was to edit the font and change the name. I used the free tool Font Forge to do this. While I was in the font, I had to fix a bunch of issues with the font and I changed the name to Dakkota. Once I did that, I got it working in all three browsers on my Mac.
Tomorrow, I’m going to tackle IE and the EOT file format…
Posted by admin on Jan 11, 2010 in
Ruby
I am working on a website where I have an admin section to add data and public read only sections. I like to have a clean hierarchy of URLs. I first implemented it using the standard generator code. I had a /restaurants and a /locations url for both. Locations doesn’t make sense as a top level URL because Restaurants have locations. I’d rather see things like /restaurants/1/locations. In fact, that isn’t quite right either. I only want to show the restaurants for the logged in user. I really want it to look like /admin/restaurants/1/locations.
Nested Resources
So the first thing I did was to make locations be a resource of restaurants. In my config/routes.rb file, I did the following:
map.resources :restaurants, :has_many => [ :locations ]
In the app/controllers/locations_controller.rb, I created a before_filter to set the @restaurant reference. Every place where I used Location.find, I changed it to @restaurants.locations.find. This only gets the locations for the restaurant from the restaurant’s locations attribute.
class LocationsController < ApplicationController
before_filter :get_restaurant
def index
@locations = @restaurant.locations
...
end
private
def get_restaurant
@restaurant = Restaurant.find(params[:restaurant_id])
end
end
Now my URLs look like /restaurants/1/locations.
You will need to change the form_for on your new.html.erb and edit.html.erb. If you are using partials for both of these (you should be) then edit that file. Change the form_for tag to
<% form_for([@restaurant, @location]) do |f| %>
Namespace
Now, I’d like to move these into the /admin url. I do this by modifying the config.routes.rb file again.
map.namespace :admin do |admin|
admin.resources :restaurants, :has_many => [ :locations ]
end
This creates the admin namespace with URLs that look like /admin/restaurants/1/locations. When you add this namespace, Rails will then look for your controllers inside the module Admin. So you will need to create the app/controllers/admin directory and move the restaurants_controller.rb and locations_controller.rb inside it. Do the same for the app/views/admin directory. Move the locations and restaurants folder inside it. You will need to modify the /app/controllers/admin/restaurants_controller.rb and the /app/controllers/admin/locations_controller.rb files. Add Admin:: in front of the class name like follows:
class Admin::LocationsController < ApplicationController
When I opened up /admin/restaurants/1/locations in my browser, I got errors saying things like restaurant_location_path is invalid. To correct this, I had to modify the views to prepend “admin_” to all the path functions and add the location parameter. A safe guess is that Ruby on Rails dynamically generates these functions.
<td><%= link_to 'Show', admin_restaurant_location_path(@restaurant, location) %></td>
<td><%= link_to 'Edit', edit_admin_restaurant_location_path(@restaurant, location) %></td>
<td><%= link_to 'Destroy', admin_restaurant_location_path(@restaurant, location), :confirm => 'Are you sure?', :method => :delete %></td>
I really don’t like embedding the “admin” into the code because I could change my mind and want it all to live inside /administration. I don’t want to have to change all my code just because of where it lives. Too tightly coupled. so, I am not going to use the function created for me. Instead, I’ll build the URL myself. If you do not specify the controller or namespace, it uses the current one. Change
<td><%= link_to 'Show', url_for(:action => 'show', :id => location) %></td>
<td><%= link_to 'Edit', url_for(:action => 'edit', :id => location) %></td>
<td><%= link_to 'Destroy', url_for(:action => 'destroy',:id => location), :confirm => 'Are you sure?', :method => :delete %></td>
This will build the same URLs as using the “admin_” functions but without hardcoding the admin path. You need to do the same thing inside the controllers. For the link back to the parent, you will need to add the controller as a parameter.
<%= link_to 'Back', admin_restaurant_path(@restaurant) %>
Change to
<%= link_to 'Back', url_for(:controller => 'restaurants', :action => 'show', :id => @restaurant) %>
The next thing you will need to change is the form edit.
<% form_for( :location, @location, :url => { :action => "create"} ) do |f| %>
Posted by jason on Jan 6, 2010 in
Ruby
This is just a reminder to me about commands that I use.
rails appname
rake --tasks
rake db:create
rake db:drop
rake db:migrate
script/generate controller myobject
script/generate model Person name:string age:integer
script/generate scaffold Person name:string age:integer
Posted by jason on Jan 6, 2010 in
Ruby
Rails offers an ORM layer. This layer is not a very strict layer. To filter or sort objects, you are actually writing SQL code. There doesn’t seem to be much complaining in the community about this mis-feature. In fact in the Agile Web Development with Rails book, the author basically makes snide comments about people who think that the object hierarchy and the database model should not be tied tightly together. This may just be a cultural thing. Ruby on Rails seems to be used mostly on brand new projects. Given this situation, it is understandable why Rails developers don’t understand the importance of uncoupling the database from the object graph.
One of my first consulting jobs was with AMC Theaters. I was on a tiny project that needed to read from a database that we had absolutely no control over. Unfortunately for us, the tables and fields often had entirely different purposes than what they were labeled as. These corporate databases can last decades and changing them is not possible. Many different groups have written code around these names and changing a field in a table has ramifications way beyond our little group. It would cause applications to break and there is no funding to fix all the applications just because we don’t like the field name. Consequently, you are left with the task of mapping a field in a table that has a non-sense name to a field in the object that actually makes sense when you read the code. This is where Ruby fails to deliver with it’s implementation of ORM through ActiveRecord.
Since I’m learning Rails so I can create new ideas quickly, I will happily use the current ORM. But this is one area where Rails needs to improve.
Posted by jason on Jan 6, 2010 in
Ruby
Site wide layout
One common method of creating common look and feels in web development is to split up all the common code into a header file and a footer file and include those files in every single interface page. This is better than embedding all that html code inside each page, but it is still clunky. You can not easily see where your tags open and close because they can be opened in one file and closed in another file. More advanced mechanisms put the overall code in one file but have middle sections where code specific html goes. So, your html template file would look something like:
<html>
<body>
<%= yield %>
</body>
</html>
It is easy to see the structure of an html page this way. It also cleans up the code for your view. Ruby on Rails implements this style of templating and calls it layouts. Because of Rails’ convention over configuration, to implement a site wide template, you merely need to create /app/views/layouts/application.html.erb and put your site wide html code there.
Override default layout
You can override the layout that will be used by adding a layout declaration inside your controller class like this:
class HelloController < ApplicationController
layout "mylayout"
def index
end
end
Create /app/views/layouts/mylayout.html.erb
Two different layouts, same view
You may have different layouts depending on whether the user is logged in or not. This is easy to accomplish by using a symbol for the layout.
class HelloController < ApplicationController
layout :isLoggedInLayout
def index
end
private
def isLoggedInLayout
"loggedIn"
else
"notLoggedIn"
end
end
You would have two templates, one /app/views/layouts/loggedIn.html.erb, the other /app/views/layouts/notLoggedIn.html.erb.
Posted by jason on Jan 1, 2010 in
Ruby
In Rails 2.0, fixtures has been improved. You no longer need to specify ID. You can check out the screencast. unfortunately, it does not support the has_one (as far as I can tell).
I have a location object that specifies a phone number, a name, and an address object. It is specified as follows:
class Location < ActiveRecord::Base
has_one :address
end
My address class is simply:
class Address < ActiveRecord::Base
end
My address fixture file addresses.yml looks like this:
address1:
address1: 1234 E Main St
address2:
city: Denver
state: CO
postal_code: 80014
My locations.yml file looks like:
location1:
phone: 555-555-5555
address: address1
If I try to do rake test:units, I get errors saying the ‘address’ column is unknown. Interestingly, if I change the has_one: to belongs_to:, it works just fine.
My work around is to revert to the old style and go ahead and specify ids.
addresses.yml:
address1:
id: 1
address1: 1234 E Main St
address2:
city: Denver
state: CO
postal_code: 80014
locations.yml
location1:
phone: 555-555-5555
address_id: 1