CSS Asset Tagger Rails Plugin
December 3rd, 2009
We’ve released a new plugin for Rails that adds asset timestamps to assets found in an apps stylesheets.
The plugin is available at http://github.com/redlinesoftware/css_asset_tagger
Simply install the plugin and it will tag any assets in your css files with asset timestamps. More information on asset timestamps can be found at http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
We wrote a previous article on using asset timestamps with nginx, but similar usage applies to Apache as well for example.
The main purpose for writing this plugin is that stylesheets that use images for various things don’t get tagged with the asset timestamps when they’re written as plain old css files. These images can’t make use of expiration techniques very easily without the timestamps. So to make things work as they do with images in rails views, this plugin will add the same timestamps to assets in css files as they are in rails views.
Before…
#navigation_bar {
background-image: url(/images/background-nav.png);
}
After…
#navigation_bar {
background-image: url(/images/background-nav.png?1234567890);
}
If you’re using yslow and some decent expiration on the server and you don’t like seeing images from your css files being shown in your report card, this plugin should make those all go away and hopefully give you a better grade. :)
Extending Named Scopes
February 14th, 2009
Quick code tip…
I’m using named_scope with a lambda as the 2nd paramater, but I also want to extend the named_scope with some custom methods.
named_scope :for_league, lambda{|league_id| {league conditions} } do
def default
end
def do_something_custom
end
end
I use the same extended methods in more than one place, so I figured I’d use something similar to the :extend option used with association methods like has_many, but I can’t pass an :extend option if I’m also using a lambda.
The way around this is to just include the methods directly in the block.
module Associations
def default
end
def do_something_custom
end
end
named_scope :for_league, lambda{|league_id| {league conditions} } do
include Associations
end
Works like a charm. Now I can just reuse the Associations module where I need it.
will_paginate and remote AJAX links
January 30th, 2008
Updated for will_paginate 2.3.x
So you’re using the will_paginate plugin and want to use remote AJAX links. You’re not using the will_paginate plugin? Simply install it with…
script/plugin install git://github.com/mislav/will_paginate.git
or install the gem and specify it in your config/environment.rb file
sudo gem install mislav-will_paginate
config.gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'
will_paginate is an alternative to the classic_pagination plugin, which is the pagination plugin that basically took the pre Rails 2.0 pagination implementation and packaged it in plugin form. I won’t discuss the differences between the two plugins here, but there’s a good video explaining the two from Railscasts here.
Out of the box, will_paginate doesn’t allow remote AJAX links for page links, so I’ll show you an easy way to add such functionality…
Read the rest of this entryHuman Attribute Override Plugin Tutorial
January 1st, 2008
This is my second tutorial covering the plugins that we’ve developed.
This tutorial covers the human attribute override plugin.
The plugin allows humanized versions of attributes to be overridden with custom strings to provide a better conversion than human_name may provide.
Why?
The main reason for creating this plugin is that Rails doesn’t always provide an acceptable “humanized” version of an attribute name.
Column.new('num_employees', ...).human_name # => 'Num employees'
You could argue that perhaps the attribute should be named number_of_employees..
Column.new('number_of_employees', ...).human_name # => 'Number of employees'
But this is hardly an acceptable solution for various reasons
- The attribute name is too wordy.
- Changing an attribute name for the sake of it’s humanized version reading better results in unneeded refactoring.
- You’re using a legacy database that uses a specific naming convention.
Conditional Cache Plugin Tutorial
November 26th, 2007
Update
This plugin has now been DEPRECATED for Rails 2.2+ in favour of the built in :if and :cache_path options.
The following changes can be made to use :cache_path instead of the :tag option.
- Change all instances of :tag to :cache_path
- If the :tag referred to a sybmol such as
:tag => :standard_tag, you simply need to change the method name that the symbol refers to asstandard_tag_url(just add _url to the end of the method name). - The host name no longer prefixes the cache fragment when :cache_path uses anything other than a hash, so make sure to begin your cache_path with the string generated from
request.url(includes protocol) orrequest.host + request.request_uri(no protocol) if your cache fragment keys depend on the host name.
Updated for Rails 2.1
This is my first of many tutorials to come covering the plugins that we’ve developed.
This tutorial covers the conditional cache plugin, which is actually the first plugin released by Redline.
Why?
By default, the caches_action method allows you to specify one or more actions to apply caching to.
class TestController < ApplicationController
caches_action :index, :new
def index
...
end
def new
...
end
end
In the above example, action caching is being applied to the index and new action. Beyond that we have no additional control over when these actions should be cached or how these actions should be cached. It’s either all or nothing.
So to fix this “all or nothing” problem, the conditional cache plugin adds additonal functionality to the caches_action method by adding 2 additional parameter options that can be passed to the method along with the existing list of actions.
Read the rest of this entrySelection javascript lib
October 26th, 2007
I’ve created a basic first draft for a javascript selection library. The library allows the user to select many elements on a web page with a simple mouse drag.
I call this version 0.1 as it has only been tested in Firefox and has some minor issues that need some cleaning up, but I figured I might as well make it public so people can take a look, try it, give feedback, or send patches.
You can grab the files and see a demo of it here
The library requires prototype and builder… both of which come with Rails.
Patches and feedback are welcome.
Conditional Cache updates
October 25th, 2007
I’ve recently fixed a bug in our conditional cache plugin that I introduced while making it compatible with Rails 1.2.4.
The bug has been fixed and the plugin also works with Rails 1.2.5.
The bug was causing the wrong cache paths to be created, so if you updated the plugin, update again ASAP.
Conditional Cache plugin now compatible with Rails 1.2.4
October 12th, 2007
The conditional cache plugin has been updated to be compatible with Rails 1.2.4. If you’re still using a previous version of Rails, there is no need to upgrade the plugin.
Update: Compatible with Rails 1.2.5 now
New in_place_editor plugin
July 23rd, 2007
I have just released a new plugin that can be found at http://agilewebdevelopment.com/plugins/improved_in_place_editor
This plugin allows all options of the Ajax.InPlaceEditor control to be set. This will also work with the new rewrite of the control, which is currently in the scriptaculous trunk.
This plugin replaces the current rails version of in_place_editor and is drop-in compatible.
Quick summary (more details at the url above)...
Missing options such as onFailure can now be specified…
<%= in_place_editor 'field_id', :on_failure => "function(transport) {alert(\"Error: \" + transport.responseText.stripTags());}" %>
Some options require quoting to be valid in the JS, such as :highlight_color.[1]
1 :highlight_color is the option name for the rewritten version of the control, :highlightcolor is the old name.
The 2 ways to specify the option are…
1) Manually quote the option<%= in_place_editor 'field_id', :highlight_color => "’#000000’" %>
2) Use the :quoted option
<%= in_place_editor 'field_id', :quoted => {:highlight_color => ’#000000’} %>
Check out our other available plugins at http://agilewebdevelopment.com/plugins/owner/72
Updated conditional_cache plugin
February 20th, 2007
Just a quick note... I've updated our conditional_cache plugin (http://www.agilewebdevelopment.com/plugins/conditional_caching) to be more in sync with the current rails cache code that this plugin overrides.
Enjoy!from_db ActiveRecord extension
January 4th, 2007
Here's a really simple, but useful ActiveRecord extension I wrote.
class ActiveRecord::Base
def from_db
self.class.find self.id
end
end
What this does is reload the current object from the database and returns it. Read more about how this can be useful...
Read the rest of this entry