0.0
No commit activity in last 3 years
No release in over 3 years
Handles tokenInput boilerplate so you don't have to
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 4.2.1, ~> 4.2
 Project Readme

trivial-tokens

This gem provides a simple wrapper for the jquery tokenInput plugin.

It is built using the asset-gem jquery-tokkeninput-rails for provision of the CSS and JS for tokenInput.

The gem provides:

A class method 'tokenize' on ActiveRecord models, which takes the name of a relation on the model as argument. This method adds getter/setter methods for the 'tokenized' form of the relation - ie, a comma delimited string of IDs.

A controller around_action :index callback to provide handling for the 'q' parameter that tokenInput expects.

A generator to provide the default javascript to create a tokenInput field for the tokenized attribute on forms.

Jquery tokeninput assets (CSS & JS)

Usage

Trivial Tokens assumes you wish to provide a token input for a rails association of either has_many, or has_and_belongs_to_many type, so ensure that this is set up.

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
  #...
end

To add the model methods trivial_tokens requires: add a call to tokenize, passing the association name:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
  tokenize :tags
  #...
end

In the model's controller, permit the tokenised_tags parameter:

class Article < ActiveRecord::Base
  #...
  def article_params
    params.require(:article).permit(:tokenized_tags, :other_params)
  end
end

In the association's controller, add a call to tokenize:

class TagsController < ApplicationController
  tokenize
  #...
end

Next, in application.js, require tokenInput:

//= require jquery.tokeninput

And in application.css:

*= require token-input

or

*= require token-input-facebook

or

*= require token-input-mac

depending on your needs.

To generate the minimum javascript required for the jquery tokenInput functionality, run the token_input generator. This generator takes the model and its association as arguments:

$ rails generate trivial_tokens:token_input article tag

This will generate a javascript file in app/assets/javascripts which will call tokenInput on fields with id:

#article_tokenized_tags

So go ahead and add a tokenized_tags field to your form view:

<!-- ... -->
<div class="field"> 
  <%= f.label :tokenized_tags %> 
  <%= f.text_field :tokenized_tags %>
</div>
<!-- ... --> 

that's it!