jQuery helpers for Rails 3.0.0.beta
Posted on 02/08/2010 at 06:58 PM
I’ve just ported the unobtrusive javascript helpers in Rails 3 from Prototype to jQuery-1.4.1, but I haven’t tested it to work with earlier versions of jQuery yet. Let me know if you find this useful or have any issues installing the gem.
For those that don’t know, Rails 3 now makes use of HTML5 custom data attributes on the DOM instead of generating javascript inline when using it’s built in javascript helpers. If you’re not planning to use HTML5 in your app, you might benefit from another plugin I wrote called js-data-helper.
Thanks to @technicalpickles for helping me package this up as a gem.
The code is on Github.
http://github.com/CodeOfficer/jquery-helpers-for-rails3
The gem is on Gemcutter
http://gemcutter.org/gems/jquery_helpers
Install:
(sudo) gem install jquery_helpers
Usage:
rails generate jquery_helpers [options]
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Supress status output
-s, [--skip] # Skip files that already exist
Description:
A port of the unobtrusive Prototype helpers in Rails 3 to jQuery
Example:
./script/generate jquery_helpers
This will create:
public/javascripts/jquery.rails.js
public/javascripts/jquery.rails.min.js
Don’t forget to include ‘jquery.rails.js’ in your layout along with jQuery.
1
Comments
Intridea discovers a great tech community in Portland Maine
Posted on 06/21/2009 at 02:25 AM
This week I had the pleasure of attending Hackon, a FREE 3 day technology event thrown by Intridea here in Portland, Maine. It took place at the Portland Harbor Hotel and was organized by Adam Bair, a local friend, Ruby enthusiast and employee of Intridea. Adam did a wonderful Job of organizing the event. Many thanks to Intridea for making it all possible!
The first 2 days of the event were mostly an open format. Guests brought their laptops and worked off the Hotel’s wireless. I honestly don’t think I “worked” very much that 1st day. Instead, I had a wonderful time greeting the attendees, many of whom I had not met before. Surprisingly not everyone was a developer but we had tons to talk about and Day 1 flew by without warning. Thankfully, I did manage to spend most of my time programming on Day 2.
I had a very interesting conversation with Abe Fettig, a Google employee living here in town. He was nice enough to spend a couple hours live coding some Javascript at my request, giving me a great review of prototypal inheritance. I learned so much from our talk. That conversation was really something money couldn’t buy and I’m still soaking it in! Thanks Abe!
Throughout the event, I discussed with Abe and others a rather complex project I have been working on that makes heavy use of Javascript. I introduced quite a few of these people to Sammy, a relatively new jQuery plugin I have decided to use in my project. I am a huge fan of this plugin! It allows you to define Javascript applications as objects attached to your DOM in such a way that makes complicated client side apps easier to manage. Its heavily based on Ruby’s Sinatra gem and very easy to use.
Day 3 offered a number of very interesting presentations:
I am anxious to see if Indridea will host Hackon again next year. I still can’t believe this was a free event. In Maine, its so difficult to find the variety and caliber of tech folks that were in attendance in one place. I imagine that TechMaine would benefit from hosting similar events.
And oohh look, I guess some photos were taken of the event!
3
Comments
My Initial Impressions of Tweetie for Mac
Posted on 04/21/2009 at 12:24 AM
Tweetie for Mac came out yesterday. I’ve anxiously awaited its release since I’m already such a fan of the ever popular Tweetie for iPhone. However, I was a bit underwhelmed with what it has to offer. Tweetie for Mac’s strongest feature is its sexy aesthetic quality, which just “looks” different. That feature alone can make it seem superior to other cloned desktop Twitter clients, but I don’t think its enough to make it a truly great app. Not yet, and not without some additional effort. I’m waiting for some updates.
Some Initial complaints I have are:
No Growl Integration
Don’t force me to have my Twitter app in the foreground in order to see tweets as they come in. Most good mac applications already take advantage of growl integration. Growl is a system level notification system that lets you customize how various applications get your attention. Lack of Growl integration is a deal breaker for me. I already have a ton of windows open for my web work and screen real-estate is a crucial commodity, even with dual 20 inch screens.
No Compact Layout
It would be nice if one could have the option of using a more compact layout. Resizing the main window, the smallest size you can achieve is 355x422 pixels. Considering you only see 3 to 4 tweets at this size, its very hard to keep Tweetie from dominating your screen space, and still be useful.
No Saved Searches
Tweetie for iPhone has this and its one of things I enjoy most about it.
No Find Nearby Tweets Feature
I’m not sure if Apple’s CoreLocation framework is available for desktop use yet, but this is something the iPhone app offers and its just awesome. I have met some amazing people recently through Tweetie for iPhone. I’ve even met a few for coffee. Its wonderful to connect that way, and I’d love if my desktop Twitter client could serve the same purpose.
Heavy API Useage
I have found Tweetie to be a bit chatty. It seems to make a lot of calls back to Twitter’s API service and I’ve been locked out of using Twitter twice now. When that happens I think you have to wait an hour before you can receive tweets again. No good!
I’m currently using Twitteriffic on my desktop computers but I won’t say I’m particularly loyal to it. I’m mostly dependent on it’s Growl integration. I’ve tried TweetDeck and others, each have wonderful features but also their own issues. The Air apps often don’t feel like real mac applications. Thats annoying to me. I want the Twitter client I use to behave in a mac-like way. I don’t mind being shown ads for free use, and I’ll certainly pay for good software. Tweetie for Mac has a great aesthetic, it reminds me of Things. I’m hopeful they will add some of the features I require to use it.
0
Comments
7 Best of Breed Rails Plugins I Can’t Live Without
Posted on 04/01/2009 at 09:14 PM
I am often asked about the various gems/plugins I use in my Rails apps and thought it might make for a good blog post. Most of these I have a good history with, while some I settled on only recently. Either way, this list contains what I consider to be the best of the best, for what each of them helps to accomplish. I hope you will check them out if you have not yet had a chance to.
REVISIONING
acts_as_revisable (rich)
A framework for building heavily versioned applications.
http://github.com/rich/acts_as_revisable/
acts_as_revisable is a joy to use. It doesn’t have you create separate tables for versions like some other popular versioning plugins. The interface is very well thought out, and I’ve yet to find something it doesn’t do that I’ve needed. Its heavily documented and just works!
TAGGING
acts-as-taggable-on (mbleigh)
A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://github.com/mbleigh/acts-as-taggable-on/
A tried various tagging solutions over the last year and finally settled on this one by mbleigh. acts-as-taggable-on-steroids and a few others never seemed to work on models build with single table inheritance (STI) or that had polymorphic relationships.
AUTHENTICATION
authlogic (binarylogic)
A clean, simple, and unobtrusive ruby authentication solution.
http://github.com/binarylogic/authlogic/
Like most of the Rails community, I earned my stripes using Restful Authentication, a sexy authentication generator that got you up and running in no time at all, but it wasn’t long after that I began to find things I didn’t like about it. Overall, Restful Authentication felt very non-object oriented. It polluted my user model with code I hadn’t written and it didn’t even feel restful. Authlogic on the other hand makes no assumptions on how you’d like to authenticate users. It lets you treat your UserSession as a model and take advantage of ActiveRecord’s build in callbacks. Its also a Gem and easy to update. Nowadays you won’t find me using anything else. Its also heavily documented.
BACKGROUND JOBS
delayed_job (tobi)
Database backed asynchronous priority queue—Extracted from Shopify.
http://github.com/tobi/delayed_job/
I don’t have a lot of experience with background process plugins, I’ll admit that up front. Hands down though, this one has some slick features and is very actively developed. No complaints.
GIT WORKFLOW
git_remote_branch (webmat)
A tool to simplify working with remote branches.
http://github.com/webmat/git_remote_branch/
If you are a frequent brancher, and need to sync local branches with your remote server there is no better way than using git_remote_branch. You can publish and track local branches in a single command. I find myself doing this a lot when I have created a branch on my laptop but want to continue where I left off on my other machine at home. It also has a helpful ‘explain’ feature to tell you exactly which commands will run should you use one of its features.
GEM BUILDING
jeweler (technicalpickles)
Simple and opinionated helper for creating and managing Rubygem projects on GitHub.
http://github.com/technicalpickles/jeweler/
I knew nothing about writing gems, and pushing them to github ... 20 minutes later I had. Nuff said.
STATE MACHINES
state_machine (pluginaweek)
Adds support for creating state machines for attributes on any Ruby class.
http://github.com/pluginaweek/state_machine/
This one I found by a friend’s recommendation. I had been using Rubyist’s Acts As State Machine (now known as AASM) and had many issues with it early on. Specifically I had been trying to cancel a transition in a callback and eventually found out it wasn’t supported. My understanding is that this plugin was originally built for Rails and later refactored to to be less dependent on it. state_machine however was built from day one to work with any Ruby class, and later integrations were added for ActiveRecord, Datamapper and Sequel. Its the best thing out there and I can’t recommend it enough. The code is a joy to look though and its heavily documented.
4
Comments
CSS Grid Frameworks: 960gs Without Margins
Posted on 04/01/2009 at 06:24 AM
I’ve been using the Blueprint and 960gs CSS Frameworks for a while now and have had but one complaint: they both assume you want to create your layouts with margins between the columns. Unfortunately, in my own use of these frameworks I’ve found that I work more with inner columns than exterior ones, and the default margin styles were a nuisance then.
So ... I forked Nathan Smith’s 960gs and recalculated the grid styling to remove the column margins. Now when I declare any two columns side by side, they will be flush against each other. Perhaps others will find this useful as well. I left the reset.css and text.css styles as they were.
You can find my code here.
One final note: I’ve been using 960gs a lot more than Blueprint lately because I’ve found Blueprints text styles make too many assumptions on my behalf for how I would like my text to layout. 960gs in comparison is quite minimal.
3
Comments
My March 25th Presentation on State Machines
Posted on 04/01/2009 at 05:57 AM
Materials for a talk I did on March 25th at the Maine Ruby Users Group (merug.org) are now on Github (materials). The topic was State Machines and I’ll be doing the same presentation again at the New Hampshire Ruby Users Group (nhrug.org) next month, on April 30th.
This was a fun topic for me, and I can’t wait to do it again. Thanks to everyone for their kind words afterwards. Renae Bair posted a great review of the evening and of the atmosphere of our group in general. Thanks to Casey Rosenthal for his Test Driven presentation on the differences of Ruby 1.8 and 1.9 and for Nick Hall’s presentation on Ruby Proce55ing.
See you all again next month!
0
Comments
Setting up Cucumber/RSpec with Rails 2.3 for Leopard
Posted on 03/13/2009 at 06:58 PM
Update: See comments for resolution to a Nokogiri dependency issue.
Install the gems:
sudo gem install rails --source http://gems.rubyonrails.org
sudo gem install dchelimsky-rspec
sudo gem install dchelimsky-rspec-rails
sudo gem install cucumber
sudo gem install webrat
Create your app:
rails myapp
cd myapp
Freeze Rails to vendor:
rake rails:freeze:edge
Manage your gem dependencies through rails, edit config/environments/test.rb. Add the following lines:
config.gem "dchelimsky-rspec", :lib => false, :version => ">= 1.1.99.13"
config.gem "dchelimsky-rspec-rails", :lib => false, :version => ">= 1.1.99.13"
config.gem 'aslakhellesoy-cucumber', :lib => false, :version => ">= 0.1.99.23
config.gem 'webrat', :lib => false
Unpack your gems:
rake gems:unpack RAILS_ENV=test
rake gems:unpack:dependencies RAILS_ENV=test
And generate your testing folders:
script/generate rspec
script/generate cucumber
That’s it, have fun.
7
Comments
ActiveRecords and methods on your model associations
Posted on 03/08/2009 at 05:16 AM
I left PHP to program in Ruby a little over a year ago now, though technically I think I focused more on learning Rails during my first month. Like most who made the journey, I found Ruby a joy to work with, and Rails to have everything I needed to make great web apps. But to this day, the part of Rails I struggle the most with is ActiveRecord. There’s just so much magic in there ...
I want to share a syntax I learned this evening for declaring methods on an association in ActiveRecord.
In my system, Users can post content either as themselves, or as approved Affiliate identity. But since other Users might also want to post as this Affiliate, I use a has_many :through relationship to manage this. Imagine an Affiliate is a local business or organization, and Users might belong to multiple instances of them. This may explain things better.
class Affiliate < ActiveRecord::Base
has_many :affiliations
has_many :users, :through => :affiliations
attr_accessor :status
end
class Affiliation < ActiveRecord::Base
belongs_to :affiliate
belongs_to :user
attr_accessor :status
end
class User < ActiveRecord::Base
has_many :affiliations
has_many :affiliates, :through => :affiliations
end
I needed to create a select menu for Users creating content in my system so they could select an optional alternate Affiliate identity to post as. The thing is ... these relationships aren’t cut and dry. Both the Affiliate and the Affiliation need to have a status of ‘approved’ or that User cant post as that Affiliate identity.
That said, I needed to populate this select menu for the User. At first I started thinking like this:
class User < ActiveRecord::Base
has_many :affiliations
has_many :affiliates, :through => :affiliations
def approved_affiliates
affiliates.with_status(:approved).find(
:all,
:include => [:affiliations],
:conditions => ["affiliations.status = 'approved'"]
)
end
end
@affiliates = current_user.approved_affiliates.collect {|p| [ p.name, p.id.to_s ] }
But looking in my User model, this didn’t seem very elegant and I wanted to find a way to keep the method definition attached to the code where I declare the association. I was already using a named_scope in my Affiliates model “with_status(:approved)” (not shown here) and so I thought I might be able to combine it with a similar named scope on the join.
So, really what I needed was an association method to scope the Affiliate AND the Affiliation to have statuses of ‘approved’. Try as I might I couldn’t figure out the syntax for this. If you know how it can be done, please do share.
What I ended up with, and am quite happy with is ...
class User < ActiveRecord::Base
has_many :affiliations
has_many :affiliates, :through => :affiliations do
def with_combined_statuses(status)
find(:all, :include => [:affiliations], :conditions => ["affiliations.status = ? AND affiliates.status = ?", status, status])
end
end
end
@affiliates = current_user.affiliates.with_combined_statuses('approved').collect {|p| [ p.name, p.id.to_s ] }
So, I learned you can pass a block to the association and declare methods there, as well, those methods can accept parameters.
Yay me
0
Comments
What I’ve been up to!?
Posted on 03/07/2009 at 11:24 PM
I haven’t posted in a while, but I’ve been quite busy in other places. I’m hoping to finish up a new Rails blogging engine I’ve been working on so that I can start writing some advanced ruby/rails tutorials.
This version of Codeofficer.com was thrown together back in my PHP days using an app called Expression Engine. I still think EE is neat software for the average Joe, but as soon as you want to begin creating semi advanced posts with images and such it resists your creativity. Its replacement should be ready soon.
I’ve more active on Twitter and Github otherwise, feel free to follow me there as well.
0
Comments
CodeOfficer.com just moved over to slicehost
Posted on 01/29/2009 at 09:42 AM
I moved the site last night to a new slice over at slicehost. I’m psyched to have a new environment to play in. I’ve been running off a PHP ExpressionEngine installation for a couple years, but that will soon be old news. I’ve developed a new content creation app for myself and plan to make use of it here. After its launch, I’ll be doing some posts to describe how I made it, as I think it may be of interest to others. Anyway just a heads up.
0
Comments