1:54 PM EDT Wednesday, June 25 2008
Today I was working on a Rails site where users have profiles that anyone can view, but if you are viewing your own profile, there are links on the page to edit various parts of the profile. I want the links to show up if you are viewing your own profile, but not be there if you are viewing someone else's profile. To implement this is trivial, as any Rails developer would tell you. I simply added a helper method:
def link_to_if_owner(title,url,options={})
if current_user == @user
link_to title, url, options
end
end
And changed the occurrences of <%= link_to "Edit", edit_something_path %> to <%= link_to_if_owner "Edit", edit_something_path %>. This probably took 15 seconds, and could not be more clear and succinct. When I compare this to doing Java/J2EE development, this task would probably have involved a custom JSP tag and would have been a complicated solution with much ceremony.
This is what is referred to as the means of abstraction in SICP. They assert that the power of a programming language or framework should be measured by its means of abstraction. The means of abstraction is how you take a set of operations and build a smaller operation that you can build on top of. This is just one example of many that shows why Ruby/Rails is a more powerful combination than Java/J2EE.
Posted in
Technology |
Tags
Rails, Ruby, Java, J2EE |
0 Comments
10:15 AM EDT Tuesday, May 6 2008
One thing that all web developers do at some is view the HTML source of the page your application has generated. The reason why is because what your web application does is dynamically generate HTML and you want to see what HTML it generated. You can think of Ruby, as a metaprogramming language, as a language that dynamically generates code. If you are doing Rails, you see this all of the time with association methods like has_and_belongs_to_many. What has_and_belongs_to_many does is generate methods on your class. For example, if you have an Product class that has_and_belongs_to_many categories, it will add a category_ids= method to your class. This all happens at runtime, so you can't see the method, but if you use Ruby2Ruby, you can. sudo gem install ruby2ruby and then run script/console at the root of your Rails app. Then just run these simple commands:
>> require 'ruby2ruby'
>> puts Ruby2Ruby.translate(Article)
The output will be the ruby code that your class now has. Somewhere in that code you will see:
def category_ids=(new_value)
ids = (new_value or []).reject { |nid| nid.blank? }
send("#{reflection.name}=", reflection.class_name.constantize.find(ids))
end
Which is the category_ids= method that was generated by has_and_belongs_to_many.
Posted in
General |
Tags
Ruby2Ruby, Rails, Ruby |
0 Comments
6:30 PM EDT Wednesday, April 23 2008
The suggested way of checking if an ActiveRecord model has errors with RSpec is:
@something.errors.should be_empty
When that fails, you get a report that says:
expected empty? to return true, got false
That's not very helpful because it doesn't tell you what the errors are. If you do this:
@something.errors.full_messages.should == []
You get a more informative failure message:
expected: [],
got: ["Whatever can't be blank"] (using ==)
Posted in
Technology |
Tags
RSpec, Rails, Ruby |
3 Comments
12:11 PM EDT Monday, April 21 2008
I just stumbled upon a neat little Rails feature and figured I'd share with the world. I'm working on a Facebook application and I'm going through some of the examples in Developing Facebook Platform Applications with Rails. In chapter 4, one of the tasks is:
Let's add a boolean hit column to our attack model. I'll wait while you create that migration.
It doesn't tell you how to do that, which I like. This is a simple enough task the reader should be able to do in their own, no need to clutter up the book with the code.
So I set out to do that, but for whatever reason my mind is in a bit of a fog today. I don't what it is, I've been making an inordinate number of typos. So when my fingers went to type the command to generate the migration, I just instinctively did this:
script/generate migration add_hit_to_attack hit:boolean
I accidentally added the hit:boolean column specification as if this were a script/generate model Whatever command. But it turns out Rails is able to read my mind. I opened up the migration file that was generated and there was this:
class AddHitToAttack < ActiveRecord::Migration
def self.up
add_column :attacks, :hit, :boolean
end
def self.down
remove_column :attacks, :hit
end
end
The code to add the column was already there. I checked the docs and sure enough there is support for this naming convention:
You can name your migration in either of these formats to generate add/remove
column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable
Example:
`./script/generate migration AddSslFlag`
With 4 existing migrations, this creates the AddSslFlag migration in
db/migrate/005_add_ssl_flag.rb
`./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`
This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with
this in the Up migration:
add_column :posts, :title, :string
add_column :posts, :body, :text
add_column :posts, :published, :boolean
And this in the Down migration:
remove_column :posts, :published
remove_column :posts, :body
remove_column :posts, :title
Posted in
Technology |
Tags
Rails, Migrations, Ruby |
0 Comments
4:39 PM EDT Saturday, April 19 2008
Ryan Bates, the author of Railscasts, just reached his 100th episode. Railscasts is a fantastic free resource for learning about Ruby on Rails. Congratulations, Ryan!
To celebrate his 100th episode, he's running a contest with some great prizes. The contest is to submit your own 5 Rails tips, much like Ryan did in Episode 100 of Railscasts. This blog post is my entry into that contest. I have posted 5 articles today which include tips on various aspects of Rails. They are:
Sending Liquid-based Email with Rails
Find out how to let the administrative users of your application update email templates.
Importing Data with Rails
Learn how to use Rails and object-oriented techniques to import data into your Rails app.
Serving Images Stored in the Database with Rails
See how to take advantage of Rails' page caching to serve images that are stored in your database.
Using ActiveRecord Composed Of
Make use of the composed_of feature in ActiveRecord to treat multiple fields within a model as one object.
Using Lowpro To Create Autotab using Unobtrusive JavaScript and Rails
Provide your users with an easy shortcut for entering in multi-field data types like phone numbers without filling up your views with lots of JavaScript code.
If you find these tips helpful, be sure to let Ryan know! :)
Posted in
Technology |
Tags
Railscasts, Rails, Ruby, 5RailsTips |
0 Comments