GithubHelp home page GithubHelp logo

nulogy / autocomplete_for Goto Github PK

View Code? Open in Web Editor NEW
4.0 45.0 2.0 90 KB

Model-side logic for autocompleting belongs_to associations

License: MIT License

Ruby 81.32% JavaScript 2.83% CSS 1.83% HTML 14.01%

autocomplete_for's Introduction

Current Version

Build Status

This version was introduced to support Rails 5. It depends on activerecord ~> 5.0 and contains several large gem upgrades. This is not a backwards compatible upgrade, and introduces many breaking changes if there is no gem compatibility.

AutocompleteFor

This plugin implements model-side logic for autocompleting belongs_to associations. It handles validations, autocompleting the same association via different fields (say name or login) and the ability to specify a custom finder block.

It works out-of-the-box with existing front-end autocompletion solutions because all it does is set the value of the belongs_to association based on an attribute that can be set directly or via update_attributes (see examples below.)

Example

    class Author < ActiveRecord::Base
      # has a login and open_id column
    end

    class Post < ActiveRecord::Base
      belongs_to :author

      # set the author_login field to autocomplete by author login
      autocomplete_for :author, :login do
        self.author = Author.find(:first, :conditions => {:login => @author_login})
      end

      # set the author_open_id field to autocomplete by author open_id
      autocomplete_for :author, :open_id do
        self.author = Author.find(:first, :conditions => {:open_id => @author_open_id})
      end
    end

Using autocomplete_for in your models to set belongs_to associations:

    author = Author.create! :login => 'baz'
    post = Post.create! :author_login => 'baz'    # automatically finds the author with the login 'baz'
    puts post.author.login                        # will output 'baz'

Errors are generated automatically if the given information does not correspond to a valid model:

    author = Author.create! :login => 'baz'
    post = Post.create :author_login => 'quux'
    puts post.errors[:author_login]               # will print an error message

Running the tests

You can run the unit tests using rake

autocomplete_for's People

Contributors

amckinnell avatar arturopie avatar jayuen avatar mwraich avatar rodney-l avatar sodapopcan avatar sskirby avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jayuen arturopie

autocomplete_for's Issues

Should support stripping of 'humanized' attribute

If I have the following:

autocomplete_for :project, :code do
  self.project = self.site.projects.find_by_code @project_code.strip!
end

I need to explicitly strip @project_code in order to prevent incorrect matches due to extraneous whitespace. Would be more DRY if the generated instance variable was stripped when initialized.

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.