github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

bumi / find_by_param

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 29
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Click here to lend your support to: find_by_param and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Find_by_param is a nice and easy way to handle permalinks and dealing with searching for to_param values — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

adding History.txt 
Gregor Schmidt (author)
Mon Jan 04 05:28:25 -0800 2010
commit  db3d6584492ca85dd8d604aaa13c342388a7bc7f
tree    56d32a3eb697993709b3c229fccf738ec5bee8f3
parent  3cc26e8767ffd98fe7be9d3dcd0bedf64037a8f7
find_by_param /
name age
history
message
file .gitignore Wed Nov 25 11:06:29 -0800 2009 using jeweler to generate a gem [Gregor Schmidt]
file History.txt Mon Jan 04 05:32:00 -0800 2010 adding History.txt [Gregor Schmidt]
file MIT-LICENSE Loading commit data...
file README.md Sun Jan 03 13:03:20 -0800 2010 fix URL of rails plugin in readme file [bumi]
file Rakefile Sun Jan 03 12:55:45 -0800 2010 update contact email address [bumi]
file init.rb Wed Nov 25 11:06:29 -0800 2009 using jeweler to generate a gem [Gregor Schmidt]
file install.rb Tue Mar 25 07:15:37 -0700 2008 inital commit [Michael Bumann]
directory lib/ Mon Jan 04 05:18:21 -0800 2010 refactoring to a more intention revealing version [Gregor Schmidt]
directory shoulda_macros/
directory tasks/ Tue Mar 25 07:15:37 -0700 2008 inital commit [Michael Bumann]
directory test/ Mon Jan 04 05:17:05 -0800 2010 fixes #1 - wrong to_param value on collision an... [Gregor Schmidt]
file uninstall.rb Tue Mar 25 07:15:37 -0700 2008 inital commit [Michael Bumann]
file version.yml Wed Nov 25 11:06:29 -0800 2009 using jeweler to generate a gem [Gregor Schmidt]
README.md

find_by_param

find_by_param helps you dealing with permalinks and finding objects by our permalink value

class Post < ActiveRecord:Base
  make_permalink :with => :title
end

now you can do

Post.find_by_param(...)

If you have a permalink-column find_by_param saves the permalink there and uses that otherwise it just uses the provided attribute.

Installation

As gem from gemcutter:

% gem install find_by_param

Building your own

% rake gemspec
% rake install

As Rails plugin

% script/plugin install git://github.com/bumi/find_by_param.git

Examples

Configuration

make_permalink :with => :login
make_permalink :with => :title, :prepend_id => true
make_permalink :with => :name, :forbidden => %w(new edit)

Client code

Post.create(:title => "hey ho let's go!").to_param #=> "hey-ho-lets-go" 
          # ... to_param is the method Rails calls to create the URL values

Post.find_by_param("hey-ho-lets-go") #=> <Post>

Post.find_by_param("is-not-there")   #=> nil
Post.find_by_param!("is-not-there")  #=> raises ActiveRecord::RecordNotFound

Basic Documentation

Options for make_permalink

The following options may be used, when configuring the permalink generation with make_permalink.

  • :with (required)

    The attribute that should be used as permalink

  • :field

    The name of your permalink column. make_permalink first checks if there is a column.

  • :prepend_id => [true|false]

    Do you want to prepend the ID to the permalink? for URLs like: posts/123-my-post-title - find_by_param uses the ID column to search.

  • :escape => [true|false]

    Do you want to escape the permalink value? (strip chars like öä?&) - actually you must do that

  • :forbidden => [Regexp|String|Array of Strings]

    Define which values should be forbidden. This is useful when combining user defined values to generate permalinks in combination with restful routing. Make sure, especially in the case of a Regexp argument, that values may become valid by adding or incrementing a trailing integer.

Class methods provided by find_by_param

The following methods are added as public methods to all classes containing a permalink field.

  • find_by_param(id)

    Look up a value by its permalink value, returns matching instance or nil, if none is found.

  • find_by_param!(id)

    Look up a value by its permalink value, returns matching instance or raises ActiveRecord::RecordNotFound, if none is found.

Issues

  • Alex Sharp (http://github.com/ajsharp) pointed to an issue with STI. Better call make_permalink in every child class and not only in the parent class..
  • write nice docs
  • write nicer tests

Copyright (c) 2007 [Michael Bumann - Railslove.com], released under the MIT license

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server