rails-jquery-tokeninput 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1aecf25283064724093b0b3a715f6cee403898c3
4
- data.tar.gz: 0a473b40b4c3c6e40ed692bacf9251e055e7fc64
3
+ metadata.gz: af0ebbfc2ad3812598e5c94c223d018f88a4aa35
4
+ data.tar.gz: 72b50fe3e191f5fab4ea293ce3ed400a0724ca4a
5
5
  SHA512:
6
- metadata.gz: 16ebc5c2004ceb043277bac51dd66decb12c87b45da3ef60ce849876392bb3bb0aa794919dee24764de1d99c14f454697b6317db7e87c8b63b39f12e7034b550
7
- data.tar.gz: 8b9da0d1b60208136af18181379e7aebd8250210e9499cc48b1e970e3aa6ebc6e6cda715f0848976910d0c063f3a86b1c746f2614f89f1a8eb8e871e53746fcb
6
+ metadata.gz: a0bb73b596a687a3e9f175ca221f78176148c799a78e3e214d73d88a7d136623146a2cda810273a5a1462adfa761fc4c626d1444bcd61f90029f5f0e82baaccf
7
+ data.tar.gz: 73735572d4e8a3ef2bdd19ecb8b3805fcf2804cba6ba0fd0a918aa082364c189a1b97a03b547fa5a425d845d5367319a1938b8b5c7b4a8b0d03d73fd59b7c621
data/README.md CHANGED
@@ -49,7 +49,9 @@ f.input :countries, as: :string, input_html: {
49
49
  }
50
50
  ```
51
51
 
52
- It will automatically pass input values to your controller like an array.
52
+ It will automatically pass input values to your controller like an array (works well with pg_array).
53
+
54
+ It also automatically pre populate existed items.
53
55
 
54
56
  ## Contributing
55
57
 
@@ -2,24 +2,35 @@
2
2
 
3
3
  $(function() {
4
4
  $.each($('input[data-tokeninput]'), function(index, input) {
5
- var inputName = $(input).attr('name')
6
- $(input).attr('name', '');
5
+ var inputName = $(input).attr('name');
6
+ var options = $(input).data('tokeninput').options;
7
7
 
8
- $(input).data('tokeninput').options.onAdd = function(item) {
8
+ var add_input = function(value) {
9
9
  var inputItemName = inputName + '[]';
10
+ $('<input name="' + inputItemName + '" type="text" style="display: none;" value="' + value + '">').insertAfter(input);
11
+ }
12
+
13
+ var add_item = function(item) {
10
14
  var inputItemValue = item[$(input).data('tokeninput').options.tokenValue || 'id'];
15
+ add_input(inputItemValue);
16
+ }
11
17
 
12
- $('<input name="' + inputItemName + '" type="text" style="display: none;" value="' + inputItemValue + '">').insertAfter(input);
13
- };
18
+ $(input).attr('name', '');
19
+
20
+ options.onAdd = add_item;
14
21
 
15
- $(input).data('tokeninput').options.onDelete = function(item) {
22
+ options.onDelete = function(item) {
16
23
  var inputItemValue = item[$(input).data('tokeninput').options.tokenValue || 'id'];
17
24
  $('input[value="' + inputItemValue + '"]').remove();
25
+ add_input('');
18
26
  };
19
27
 
20
- $(input).tokenInput(
21
- $(input).data('tokeninput').collection,
22
- $(input).data('tokeninput').options
23
- );
28
+ if(options.prePopulate) {
29
+ $.each(options.prePopulate, function(index, itemToPrePopulate) {
30
+ add_item(itemToPrePopulate)
31
+ });
32
+ }
33
+
34
+ $(input).tokenInput($(input).data('tokeninput').collection, options);
24
35
  });
25
36
  });
@@ -1,4 +1,5 @@
1
1
  require 'rails/jquery/tokeninput/version'
2
+ require 'rails/jquery/tokeninput/form_helper'
2
3
 
3
4
  module Rails
4
5
  module Jquery
@@ -0,0 +1,43 @@
1
+ module ActionView
2
+ module Helpers
3
+ def text_field(object_name, method, options = {})
4
+ if tokeninput?(options) && need_to_pre_populate?(object_name, method, options)
5
+ options[:data][:tokeninput][:options][:prePopulate] = items_to_prepopulation(object_name, method, options)
6
+ end
7
+ Tags::TextField.new(object_name, method, self, options).render
8
+ end
9
+
10
+ private
11
+
12
+ def tokeninput?(options)
13
+ options[:data] && options[:data][:tokeninput]
14
+ end
15
+
16
+ def need_to_pre_populate?(object_name, method, options)
17
+ passed_or_saved_items(object_name, method, options).any?
18
+ end
19
+
20
+ def passed_or_saved_items(object_name, method, options)
21
+ (params[object_name] && params[object_name][method]) || (options[:object] && options[:object][method]) || []
22
+ end
23
+
24
+ def token_value(options)
25
+ default = :id
26
+ begin
27
+ options[:data][:tokeninput][:options][:tokenValue] || default
28
+ rescue
29
+ default
30
+ end
31
+ end
32
+
33
+ def items_to_prepopulation(object_name, method, options)
34
+ token_value = token_value(options)
35
+
36
+ items = passed_or_saved_items(object_name, method, options)
37
+
38
+ options[:data][:tokeninput][:collection].select do |item|
39
+ items.include?(item[token_value].to_s)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Jquery
3
3
  module Tokeninput
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-jquery-tokeninput
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-29 00:00:00.000000000 Z
11
+ date: 2013-11-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: rails-jquery-tokeninput project integrates Tokeninput jQuery plugin which
14
14
  allows your users to select multiple items from a predefined list, using autocompletion
@@ -25,6 +25,7 @@ files:
25
25
  - Rakefile
26
26
  - app/assets/javascripts/rails-jquery-tokeninput.js
27
27
  - lib/rails/jquery/tokeninput.rb
28
+ - lib/rails/jquery/tokeninput/form_helper.rb
28
29
  - lib/rails/jquery/tokeninput/version.rb
29
30
  - rails-jquery-tokeninput.gemspec
30
31
  - vendor/assets/javascripts/jquery.tokeninput.js