Why to wait for rails3, and why not!
Rails3 is on its way and everybody is just waiting for its release. But what can you do in the meantime to cure some obvious mistakes of the current version?
We decided to pick out 3 nice parts of rails3 and use them right now - we just couldn't wait any longer.
Bundler The rails gem feature was just a big error. You require a gem, then you want to run rake gem:install and this throws an error because it is missing a gem. Where is the point? Bundler is independent from rails, it is a simple but sufficient solution to really get going with gems (vs. rails plugins). We installed it some time ago and we had few problems since. Only moving to the newest version of bundler made some noise. The rest was just great.
XSS A change under the hood. Rails 3 will escape all output, the developer has to explicitly state what not to escape. Until now it was the other way around. This small plugin (http://github.com/nzkoz/rails_xss ) and a switch from erb to erubis (erubis is also faster, by the way) made this very simple
UJS Why should you spend time writing rjs or doing all jQuery AJAX by hand? We eagerly waited for the jquery-ujs javascript code and now finally it is out. This is just perfect, download it, add it to your project and start writing nice new mark-up and everything works better. In Rails3 the linkto and formfor helper will even generate the markup, but that is the minor part of the change. The important step is: no rjs anymore and better jquery-ajax code. Great.
These are great new developments and it is easy to use them right now in your project. Just do it.
The other features we are still waiting for:
- new routing
- complete rack support
- a plugin API
- active model and new active record
- a more complete i18n support
- new railties, especially better rails engines support and system events
- no more aliasmethodchain, instead module, mixin and classes .. why and why2 ... this just makes so much more sense...
a very good overview about all new features and changes by ryan daigle .. from edgerails.info
Work work-flowing
Bram Moolenaar gives very good advices on how to improve the text-editing workflow.
Essentially it is about improving your workflow in small steps:
- Start reflecting what you are doing not as efficient as you would like to, over and over again.
- Then find a way to improve it, take the time to find a solution.
- Make it a habit, then start again reflecting.
I created some habits while working with Rails, which are simple and small
Aliases:
sc - script/console
ss - script/server
gst - git status
small functions in bash
restart passenger:
function res {
echo "restarting passenger"
touch tmp/restart.txt
}
run parallel specs:
function pspec {
echo "specing"
rake parallel:spec[2]
}
These are my favorites right now. The important thing is not the functions themselves, but the habits. I use them over and over again. I cannot work without them anymore.
Authlogic: logged_in? depends on last_request_at
Authlogic, a popular authentication plugin for RoR, has its share of magic, some well documented, some less.
One thing that kept me busy yesterday was the absence of the logged_in? method on instances of the User class.
Turns out, authlogic, again, adapts its abilities depending on which columns the users table has. For the logged_in? method to exist, the column last_request_at must exist.
Now in the code there is a little bit of confusion, because in the authlogic lib file logged_in_status.rb there are actually two places dealing with this dependency:
def logged_in?
raise "Can not determine the records login state because\
there is no last_request_at column" if !respond_to?(:last_request_at)
!last_request_at.nil? && last_request_at > logged_in_timeout.seconds.ago
end
The exception should have brought me onto the right track, but, as it turns out, the instance methods themselves are only included, if the column last_logged_in exists, see the line
return if !klass.column_names.include?("last_request_at")
of the same file. So there you have it: no exception, just a silent absence of the method logged_in?.
Happy authenticating!
has_flags reborn
We started to use bitflags more and more within our project, they are quite flexible and powerful for many tasks. The best thing, you can add new boolean attributes without any migration. There is a very old plugin, but still good to use for this task: has_flags
The guys from thoughtbot have a nice presentation about refactoring (slides). At the end they talk about simplifying the role system via boolean flags. Instead of a has_many_through association, just use a boolean field for every role and you will get “current_user.admin?”. I liked the idea of kicking out 2 tables and having the same functionallity at the end. And yes, I used the bitflags for it. With the bitflags I have the same flexibility as with the 2-tables-setup, but the same simplicity as with the boolean-field.
A basic setup looks like this:
You will have following getter and setter methods, just like with the has_many/has_many_through associationhas_flags [ :admin, :user, :moderator ], { :column => ‘role_flag’, :group => “role” }
- current_user.user, current_user.admin etc
- current_user.roles
- current_user.role_ids
- current_user.role
You can find the updated bit_flags plugin on gitub
We are hiring!
Yes, we are looking forward into a bright future with jovoto and lots of work ahead. For this reason we are searching for talented web/rails developers.
We are searching a Ruby on Rails Junior/Senior Developer
Skills you should have:
- 2+ years experience in Web Production with a focus (1 year+) on Ruby on Rails or similar environmens
- Intermediate/advanced level of understanding of Ruby on Rails best practices.
- Experience with XHTML, CSS and JavaScript. You must be able to hand-code, analyze, debug, and deliver clean, maintainable and cross-browser compatible code that works in all major browsers
- Strong background in Web Design, ability to design for usability, information architecture, and willingness to keep up with the latest in internet technologies
- Willingness and ability to test your Rails, Javascript, and interface code with rSpec, Selenium/Webrat, or other test suites.
- Excellent organizational, analytical, problem solving, and research skills
- Speaking English and German.
Nice to have:
- contribution to open-source projects
- experience with scrum/agile development
About jovoto: jovoto is formed by a young dynamic team of designers, concept creators and software developers who are building a community for creative contests.
If you are interested in this position, write an e-mail and send a short cv and work examples to jobs@jovoto.com.
Croping thumbnails with attachment_fu
[UPTATE] = This behavior is now included in attachment_fu, see commit
We are using attachmentfu for handling images and creating thumbnails (with minimagick as processor). With standard attachmentfu you have same standard image-magick options to crop thumbnails. But they don’t really work for us. We want to crop any image to 3 standard sizes, without distorting them. But unfortunately this happens in many cases.
We added 2 new methods to resize_image, you can use them like this:
has_attachment :processor => 'MiniMagick',
:thumbnails => { :thumb => '75x75c', :big => '450x450c'}
Here same examples:
a. “75x75c”
b. “25x75c”
c. “125x25c”
![]()
[QTip] *.erb templates
If you are using Javascript/Ajax Methods on your site, you want to offer a version for ajax and for standard calls. For quite same time a used and mantained duplicate templates, this time is over!
Because there is a simple solution if you need a template for any format:
_partial_name.erb without any html/js/xml or whatsoever
An example if you render a partial in your request, like render :partial => “comment”, depending on the format of the request, it will render _comment.js.erb or _comment.html.erb. Yes, this is where the duplication comes in.
But _comment.erb will be render in any format
That is so simple. Yes.