Git Product home page Git Product logo

find_by_param's Introduction

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

Via RubyGems

% gem install find_by_param

Building your own

% gem build find_by_param.gemspec
% gem install find_by_param-VERSION.gem

As Rails plugin

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

find_by_param is known to work with Rails 2.3, 3.0 and 3.1.

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

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.

  • :param_size => [Number]

    Desired maximum size of the permalink, default is 50.

  • :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 (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 © 2007 [Michael Bumann - Railslove.com], released under the MIT license

find_by_param's People

Contributors

bumi avatar eduardohertz avatar hugomaiavieira avatar reddavis avatar schmidt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

find_by_param's Issues

to_param parameterizing a permalink will result in a dash *too less*

In your code you call escape_permalink which calls the parameterize Rails method.
The problem is that if the value is already a permalink, you'll strip the last "-" and thus your
_url Rails helpers will point to a URL not existing, resulting in an ActiveRecord.RecordNotFound

    `def to_param
      value = self.send(permalink_options[:param]).dup.to_s.downcase rescue ""
      returning "" do |param|
        param << "#{id}" if value.blank? || permalink_options[:prepend_id]
        param << "-" if permalink_options[:prepend_id]
        param << "#{escape_and_truncate_permalink(value)}"
      end
    end`

truncated permalinks ending in '-'

Hi thanks for your work.
I had to make a small change to the escape_truncate_permalink method to get rid of any trailing dashes on truncated permalinks. When using a restful url (e.g. edit_content_path(@content)) it would cut off the trailing dash and therefore wouldn't find the record.

My quick fix was ....

p = self.escape_permalink(value)[0...self.permalink_options[:param_size]]
p.chop! if p.last == '-'
p

Newer version of the gem

Can you release newer version of the gem?

Last release was on Jan 2, 2010.

I am working on spree https://github.com/spree/spree and I need to declared dependency on find_by_param using version number inside gemspec. To the extent I know gemspec ,unlike Gemfile, does not pull code straight from git.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.