Archive for the ‘Gems’ Category

http_accept_language released as a gem

I released an old Rails plugin as gem today. Slowly but surely, all my plugins will be converted to gems.

This time it’s an old one: http_accept_language

  • Splits the http-header into languages specified by the user
  • Returns empty array if header is illformed.
  • Corrects case to xx-XX
  • Sorted by priority given, as much as possible.
  • Gives you the most important language
  • Gives compatible languages

For more information, read the README on GitHub.

  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS
  • Twitter

Basic Named Scopes

Cal it tiny, I don’t care. I’ve made a gem named BasicNamedScopes.

Basic Usage

I was fed up with writing:

Post.all(:conditions => { :published => true },
  :select => :title, :include => :author)

So with BasicNamedScopes, you can now write:

Post.conditions(:published => true).select(:title).with(:author)

All named scopes are called the same, except for include, which is now called with, because include is a reserved method.

Reuse them by making class methods:

class Post < ActiveRecord::Base
  def self.published
    conditions(:published => true)
  end

  def self.visible
    conditions(:visible => true)
  end

  def self.index
    published.visible
  end
end

Also, the all-method is a named scope now, so you can chain after callling all, for greater flexibility.

Post.all.published

Arrays can be used as multple parameters too, sparing you some brackets.

Post.with(:author, :comments).conditions("name LIKE ?", query)

The read_only and lock scopes default to true, but can be adjusted.

Post.readonly         # => same as Post.all(:readonly => true)
Post.readonly(false)  # => same as Post.all(:readonly => false)

Why?

NamedScopes are really handy and they should play a more central theme in ActiveRecord. While I heard that Rails 3 will support similar syntax, there is no reason to wait any longer.

I find defining named scopes very ugly, especially when dealing with parameters. Just compare the amount of curly braces!

# Using normal named scope:
named_scope :name_like,
  lambda { |query| { :conditions => ["name LIKE ?", query] } }

# Using BasicNamedScopes
def self.name_like(query)
  conditions("name LIKE ?", query)
end

Also, regular named scopes don’t support using other named scopes at all!

I found myself implementing these named scopes (mostly conditions, but others too) so often, that a little gem like this would be the obvious choice. Use it if a gem like searchlogic is overkill for your needs.

Installing

The gem is called “basic_named_scopes”. You know how to install it.

gem install basic_named_scopes

Use it in Rails:

config.gem "basic_named_scopes"
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS
  • Twitter

Prawn and controller tests

prawn_logoThere is a real annoying gotcha in using controller tests to test an action that renders a pdf with Prawn. You’ll get a NoMethodException on “nil.downcase”. The troubling part is that it totally puts you off by providing the wrong lines and backtrace.

This has been mentioned somewhere on some mailinglists, but to make it a bit more findable, I’d thought I’d post it here.

The solutionworkaround is to set the server protocol, like this:

    it "should show the pdf" do
      request.env["SERVER_PROTOCOL"] = "http"
      get :show, :id => "report", "format" => "pdf"
      response.should render_template(:show)
    end
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS
  • Twitter

Chainable.not.gem

Well then: happy holidays! This x-mas, there have been a few nice Ruby presents, like the merging of Rails with Merb and Github has a shiny new feature for creating project sites.

I too will present you a Ruby x-mas present. It’s a nice, but useless present, not totally out of line with the usual x-mas presents.

It is a gem called not. With not you can get rid of exclamation marks and the not keyword, and putting it later on in the sentence, thus making it better English.

An example:

# for core Ruby methods:
@foo.not.nil?
# or any custom method:
@user.not.active?
@user.not.save!

If you want it, you can install it as a gem:

sudo gem install not

or as a Rails plugin:

./script/plugin install git://github.com/iain/not.git

I hope you’ll like it! Happy holidays!

Update

I moved the gem to gemcutter. If you haven’t tumbled yet, please do so, before installing my gem:

gem install gemcutter
gem tumble
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS
  • Twitter
Return top

About me

I am a Ruby programmer from Rotterdam, Netherlands. I started working with Ruby on Rails about 3 years ago and I've fallen in love with the language ever since.

When learning Ruby I noticed that the most useful information comes from blogs of other Ruby developers. I try to contribute to that.

I also am into writing plugins and gems. I hope you like my contributions and leave a comment if you do.