GithubHelp home page GithubHelp logo

indmill / finder_filter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pelargir/finder_filter

2.0 3.0 0.0 87 KB

An easy way to add common finders to your Rails controllers.

License: MIT License

Ruby 100.00%

finder_filter's Introduction

= Synthesis

An easy way to add common finders to your Rails controllers.

== Installation

  gem sources -a http://gems.github.com (you only have to do this once)
  sudo gem install pelargir-finder_filter

To use with a Rails 2.1 app, edit the environment.rb file and add an
entry for the gem to the Rails::Initializer config block.

== Usage

finder_filter is intended to replace one-off before filters that you might
commonly write in your Rails controllers. For example:

  class UsersController < ActionController::Base
    before_filter :find_user, :only => [:show, :edit]
    
    def show
      # do something with @user
    end
    
    def edit
      # do something with @user
    end
    
    def find_user
      @user = User.find(params[:id)
    end
  end

finder_filter reduces this pattern to a single line:

  class UsersController < ActionController::Base
    finder_filter :only => [:show, :edit]
    
    def show; end
    def edit; end
  end
  
Or, if you want to specify the model to find:

  class UsersController < ActionController::Base
    finder_filter :user, :only => [:show, :edit]
    
    def show; end
    def edit; end
  end

To find based on a column other than ID:

  finder_filter :user, :by => :name
    # equivalent to:
    #   @user = User.find_by_name(params[:id])

To find based on a param other than ID:

  finder_filter :user, :param => :permalink
    # equivalent to:
    #   @user = User.find(params[:permalink])

Finally, you can specify that prepend_before_filter is used:

	finder_filter :user, :only => [:show, :edit], :prepend => true
		# generates:
		#		prepend_before_filter :find_user, :only => [:show, :edit]

The standard Rails :only and :except options can also be used:

  before_filter :find_user, :only => [:show, :edit]
  before_filter :find_user, :except => [:index]

== Tests

To run the tests:

  rake test

== Dependencies

actionpack > 2.0.1
mocha > 0.5.6
test-spec > 0.4.0

== Git

  git://github.com/pelargir/finder_filter.git

== Contributors

Matthew Bass, Kevin Smith

finder_filter's People

Stargazers

Angus H. avatar Steven Mohapi-Banks avatar

Watchers

Steven Mohapi-Banks avatar James Cloos avatar  avatar

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.