SuperInPlaceControls: A new rails plugin from Flvorful
We just finished up our newest plugin: SuperInPlaceControls.
This is meant as a drop in replacement for the current in_place_edit_for method that was recently moved into a plugin. SuperInPlaceControls works with Rails 2.0, validations, and empty fields.
Check out the SuperInPlaceControls page for more info, demos, examples and docs.
Have fun.
—jake
Rescuing Ruby 9
Now, before anyone goes crazy, this article has nothing to do with saving ruby from something, i just couldnt think of a better title. We will be talking about rescue and some cool tricks that can be done with it.
As most of you vets know, every thing in Ruby is an object. What most noobs dont realize is how powerful that can be and how concise your code can become. Of course, the flip side of that problem is what i call, newbie-come-expert. It’s basically the stage in a noob’s study of a language where they think they know enough to write really cool software. What actually happens is they end up with code like this:
@objects.map {|e| [e.id, e.title]}.sort {|a, b| a.last <=> b.last}.map {|e| e.join(" ")}.join("\n")
Lovely.
Dont worry noobs, everyone does it. I’m still trying to undo it from some of my older code.
Anyway, my point is, sometimes Ruby’s dynamicism (say that 3 times fast) can lead to messy code so use this tip wisely.
On to the actual tip.
So, like I said before, EVERYTHING in Ruby is an object, even the remains of a rescue clause or an inline rescue which is what we will be playing with here.
How many of you have done this:
@person = Person.find(some_id)
only to get the following error:
ActiveRecord::RecordNotFound: Couldn't find Person with ID=22
It’s pretty annoying, mainly because this type of thing could easily happen in a live app. A user goes to find something that isnt there. One way around it is to have a rescue at the end of the method, but what if you have multiple finds that could throw errors? What if you still need to be able to proceed with whatever you are doing, even if the object isnt found?
This is where inline rescue’s can help.
Now you can pull all of your finds together and end them with a rescue.
Example: Let’s say you had the following code:@person = Person.find(params[:id]) @employee = Employee.find(params[:employee_id])
Now, if either of these finds fail, you will get an error. Instead of rescuing the entire method, you can resuce each statement and make the return value nil and check for nil like you normally would.
@person = Person.find(params[:id]) rescue nil @employee = Employee.find(params[:employee_id]) rescue nil
Now when you cant find the ID, it will rescue and just set the variables to nil and move on.
You can also set the rescues to another default datatype instead of nil.
Now, you may be saying, “Wait, why would a user need to enter an id”. Well, one example is an order id, or a tracking number. Anyway, I have run across this a couple of times and thought i’d write something about it.
until next time.
—jake
New Rails Plugin - Acts As Noteable
So I was at work and I needed to add notes to multiple models in the same project (before i only had to worry about one model with notes, so it was pretty straightforward). So i bit down and knocked out this little plugin.
I borrowed heavily from Acts As Rateable by Juixe
Installation
Run the following command:
script/plugin install http://acts-as-noteable.googlecode.com/svn/trunk/acts_as_noteable
The install will try and copy some scripts over, this works sometimes. If you get a rake error do the following:
- cd vendor/plugins/acts_as_noteable
- rake update_scripts
Create a new rails migration:
script/generate migration add_notes_to_project
and add the following self.up and self.down methods
def self.up
create_table "notes", :force => true do |t|
t.column "from", :string, :limit => 50, :default => ""
t.column "body", :text, :default => ""
t.column "created_at", :datetime, :null => false
t.column "noteable_id", :integer, :default => 0, :null => false
t.column "noteable_type", :string, :limit => 15, :default => "", :null => false
end
end
def self.down
drop_table :notes
end
(one day i’ll add a migration script to the plugin to automatically add this :) )
After that run “rake migrate” to create the table.
Now all you need to do is call acts_as_noteable in your model:
class Model < ActiveRecord::Base acts_as_noteable end
Now your model is notable, but that’s not all.
For only 19.95 you get a Helper method to help you create a nice form. Ok, so i am not actually charging, but whatever.
Here’s how the helper works.
In the view you want to display notes in put the following:
<%= display_notes(object) %>
This will create a div filled with the note entries for the object and a AJAX form to add new notes to the object as well as delete notes from the object. The add and delete methods are stored in the notes_controller.rb that gets copied over into your app/controllers directory.
The full implementation for this method is:
<%= display_notes(obj, partial_name = "notes/note", controller_name = "notes") %>
I added the partial name and controller name in case you have multiple controllers in different folders, this way you can display notes for objects whose controllers are buried deep in a folder.
Example: I have an “admin” folder for all the backend stuff and I needed a way to reference the notes controller from different places, otherwise RAILS will assume you are trying to reference a controller from the current folder your in.
After you have called display_notes, your done.
Note** This plugin has not been tested with Rails 2.0 yet
peace
—jake