The If Works This dirt was a building before

Rails

IncludeByDefault progress

IncludeByDefault, as mentioned in my last post, hit some snags with ActiveRecord generating duplicate table aliases when doing cascaded includes, e.g. Tag.find(8).posts.find(:all, :include => :tags) So, I set out to work around it, only to run into further problems. I went with option C: let find operations get all the way to the database, and [...]

Including in circles

Not long had I been using my new plugin when I discovered it made this happen when trying to eager-load on a many-to-many association: SELECT DISTINCT news_stories.id FROM news_stories LEFT OUTER JOIN countries_news_stories ON countries_news_stories.news_id = news_stories.id LEFT OUTER JOIN countries ON countries.id = countries_news_stories.country_id INNER JOIN countries_news_stories ON news_stories.id = countries_news_stories.news_id WHERE (countries_news_stories.country_id = [...]

New Rails plugin: IncludeByDefault

It’s true, I’m a plugin writing machine. Seriously though, this one’s tiny. I took all of five minutes to write. What it does is, it lets you specify a default value for the :include option on ActiveRecord::Base.find, so you can automate eager loading of associations. I’ll use an example I’m comfortable with: class BlogEntry < [...]

Automating JSON evaluation in Rails

As I’m sure many of you already know, Prototype (Rails’ JavaScript framework) will automatically eval() JSON from Ajax responses if you pass it in the X-JSON header. It even says so right here. The Rails team don’t want to automate this on the server side as recent versions of Prototype are moving away from this [...]

Grape will ignore a few things now

Another update to Grape: it will now ignore the doc, log, tmp and vendor/rails directories in its searches. No point crawling through the huge files that can build up in log or tmp, and doc is mostly auto-generated anyway. I have a habit of keeping large photoshop files and database backups in the doc folder [...]

Intercepting JavaScript methods, Ruby-style

Ruby’s open classes and modules, along with alias_method, make it really easy to add functionality to existing methods. Take this example from my plugin AttrLocked: class ActionView::Helpers::InstanceTag def tag_with_attribute_locking(name, options = nil) options = (options || {}).update(“disabled” => attribute_locked?) tag_without_attribute_locking(name, options) end alias_method(:tag_without_attribute_locking, :tag) alias_method(:tag, :tag_with_attribute_locking) end All that does is intercept any calls to [...]

Update for Grape: ‘view source’ mode

My little Rails project source code search plugin, Grape, now includes the ability to display search results in context with their surrounding source code, rather than just listing matching file paths. Par example: >rake grape q=Initializer Found 3 files matching your search. – /config/boot.rb : 14, 16, 30, 40, 44 – /config/environment.rb : 13 – [...]

Finding files with tiny fruit

I had to rename a bunch of variables in my current project and I wanted to know which files needed adjusting. Problem is, I’m on Windows, and searching for anything in Windows blows really hard. Even Google Desktop with the any-text-file-indexer wasn’t as helpful as I’d like. So, I wrote this tiny tiny plugin that [...]

Stop repeating your labels

I don’t know about you, but I do like a bit of form_for. I’m not so fond of having to repeat myself when writing labels though: <% form_for(:post, …) do |f| %> <label for="post_title">Title</label> <%= f.text_field(:title) %> <label for="post_body">Body</label> <%= f.text_area(:body) %> <% end %> You have to write the object name out every time, [...]

Table locking in AttrLocked

As promised, AttrLocked has been updated to include table locking. If you have any tables in your app that you want to be read-only, just put this in your model: class SomeReadOnlyTable < ActiveRecord::Base table_locked end That will stop and save/update/delete operations on the model’s table, and you won’t be able to modify the attributes [...]

← Before After →