has_slug arrives in a brave new world

Posted on 10/02/2009. Filed under: Technology | Tags: , |

A brave new world full of ->() block literals, ordered hashes and application templates. Of course I’m speaking of not only Ruby 1.9 but also Rails 2.3. Yes people, you’ve heard it here first: my glorious has_slug Rails plugin is now Ruby 1.9 as well as Rails 2.3 compatible.

One might argue that I’m a bit late with this but then again one might argue that green is the new pink. So yeah.. whatever!

Even though this might look like a pure maintenance/compatibility release, one other noteworthy change managed to sneak in: rescue_from_slug_mismatch is gone. Oh yes, stop the presses, I’m breaking backwards compatibility! I hope this doesn’t cause all the two users of has_slug too much trouble but sometimes you just gotta make those hard decisions. Be a good Rails citizen and use rescue_from SlugMismatchError instead. Kthx.

http://github.com/Nielsomat/has_slug/tree/master

Enjoy!

Read Full Post | Make a Comment ( None so far )

Introducing has_slug for Rails

Posted on 29/12/2008. Filed under: Technology | Tags: , |

Straight from README.rdoc:

has_slug

Deliciously slugalizing ActiveRecord & your ActionControllers since Rails 2.2

http://www.github.com/Nielsomat/has_slug

        ./script/plugin install git://github.com/Nielsomat/has_slug.git
        or git submodule add git://github.com/Nielsomat/has_slug.git vendor/plugins/has_slug

has_slug provides an easy way to equip your models with
“permalinks” like so:

        class BlogPost < ActiveRecord::Base
          has_slug(:category, :title)
        end

        post = BlogPost.create(:category => "Time Travelling", :title => "Yatta!")
        # => #<BlogPost id: 1, category: "Time Travelling", title: "Yatta!">

        post.to_param
        # => "1-time-travelling-yatta"

That‘s nice. But surely not revolutionary.

So let‘s turn our attention to the controller which should of course
be able to find the record identified by the above “permalink”.
Turns out that Rails kinda does this by default by virtue of calling to_i
on params[:id] when passing it to ActiveRecord. Easy enough..

But wait, there‘s more!

Of course this automatic conversion means that your carefully worded
“2-honey-you-look-great-in-that-dress” could also be accessed
under “2-youre-way-too-fat-for-that-dress-honey”. Not so good. In
fact, just anything matching /^2[W$]/ would lead to BlogPost.find(2).
Besides upsetting your wife and/or mistress, this might obvisouly also
annoy search engines which have a fetish for unique resource itentifiers
(i.e. URIs).

But fear not for it has all been taken care of for you!

        class BlogPostsController < ApplicationController
          rescue_from_slug_mismatch { |item| redirect_to(blog_post_url(item), :status => :moved_permanently) }

          def show
            check_slug!(@post = BlogPost.find(params[:id]), params[:id])
          end
        end

And behold: GET /blog_posts/2-honey-you-look-great-in-that-dress
will render /blog_posts/show.hmtl.erb while GET
/blog_posts/2-youre-way-too-fat-for-that-dress-honey
will 302
to /blog_posts/2-why-i-love-my-wife.

Even better though, the above example actually still works when written
like this:

        class BlogPostsController < ApplicationController
          def show
            check_slug!(@post = BlogPost.find(params[:id]))
          end
        end

Because awesomely enough, the second parameter to check_slug! defaults to
params[:id] and by default, all ApplicationController descendants are
lovingly wired to rescue you from SlugMismatchErrors by redirecting to
model_url(model).

So what are you waiting for? Just has_slug your models and
consider your application awesomified!

Copyright (c) 2008 Niels Ganser, released under the MIT license. Even
though this software is released “as is”, I accept full
responsibility for any bone fractures that might directly result from using
it. So in the case of such spontaneous fracturing, feel free contact my
armada of lawyers to discuss your ridiculously high compensation claims.

All your feature requests / bug reports / patches are belong to github!
Or niels at herimedia dot com..

Shout-out to Christoffer Sawicki & Henrik Nyh for creating Slugalizer,
upon which this plugin was originally based (obsolete since Rails 2.2)!

Read Full Post | Make a Comment ( 1 so far )

Liked it here?
Why not try sites on the blogroll...