formtastic_autocomplete 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e47db92830d55f97c2cecdb602c16f8f04f1c732
4
+ data.tar.gz: c2f6b4daf7053174ec6285a975d42aea3a7cbbcb
5
+ SHA512:
6
+ metadata.gz: f325d28e36dc291a12c0aaef47c671383f936dd0bfdec8e98e1de06c8bd1f359e09df38fe07751a0e2293f32dba9e09c7545a6b8edb1e8c3ff0a79cb24ec3fe5
7
+ data.tar.gz: a4584c100809b9c98126f2dc9b93502d1329ad6437030832ff06a453f2b84d83454fdaecf79ca85c452ccfd5ca9e89c5e03cae6b218b42998e1ca944025f4a98
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in formtastic_autocomplete.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matt Huggins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # FormtasticAutocomplete
2
+
3
+ `formtastic_autocomplete` is a gem that enables use of jQuery autocomplete inputs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'formtastic_autocomplete'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install formtastic_autocomplete
20
+
21
+ ## Usage
22
+
23
+ Require the Javascript in your `application.js` (or other preferred) file via sprockets:
24
+
25
+ ```javascript
26
+ //= require 'formtastic_autocomplete'
27
+ ```
28
+
29
+ Alternatively import the CSS in your `application.css.scss` (or other preferred) file. For
30
+ sprockets-based apps:
31
+
32
+ ```javascript
33
+ //= require 'formtastic_autocomplete';
34
+ ```
35
+
36
+ For SCSS imports:
37
+
38
+ ```javascript
39
+ @import 'formtastic_autocomplete';
40
+ ```
41
+
42
+ When rendering form via Formtastic's `semantic_form_for` method, supply the new input via the `:as`
43
+ option along with a `:source` option to specify the data source URL or array of objects:
44
+
45
+ ```ruby
46
+ semantic_form_for @ingredient do |f|
47
+ f.input :name, as: :autocomplete, source: autocomplete_ingredients_path
48
+ f.input :unit, as: :autocomplete, source: ['cup', 'tablespoon', 'teaspoon']
49
+ f.input :quantity
50
+ end
51
+ ```
52
+
53
+ The value supplied for the `:source` option should match the format expected per the [jQuery
54
+ autocomplete documentation](http://api.jqueryui.com/autocomplete/#option-source).
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,28 @@
1
+ jQuery(function ($) {
2
+ $(document).on('focus', 'input.autocomplete', function () {
3
+ var $input = $(this);
4
+ var $value = $input.next('input[type=hidden]');
5
+
6
+ if (!$input.data('autocomplete-loaded')) {
7
+ $input
8
+ .autocomplete({
9
+ delay: $input.data('delay') || 100,
10
+ source: $input.data('source'),
11
+ focus: function (event, ui) {
12
+ $input.val(ui.item.label);
13
+ $value.val(ui.item.value);
14
+ return false;
15
+ },
16
+ select: function (event, ui) {
17
+ $input.val(ui.item.label);
18
+ $value.val(ui.item.value);
19
+ return false;
20
+ }
21
+ })
22
+ .data({
23
+ 'autocomplete-value': $value,
24
+ 'autocomplete-loaded': true
25
+ });
26
+ }
27
+ });
28
+ });
@@ -0,0 +1,47 @@
1
+ $autocomplete-border-color: #999 !default;
2
+ $autocomplete-separator-color: #ddd !default;
3
+ $autocomplete-background-color: #fff !default;
4
+ $autocomplete-background-active-color: #fffcb2 !default;
5
+ $autocomplete-foreground-color: #000 !default;
6
+ $autocomplete-foreground-active-color: #000 !default;
7
+
8
+ // autocomplete styles
9
+ .ui-autocomplete {
10
+ position: absolute;
11
+ list-style: none;
12
+ margin: 0;
13
+ padding: 0;
14
+ border: 1px solid $autocomplete-border-color;
15
+ cursor: default;
16
+ font-size: 0.8em;
17
+
18
+ .ui-menu-item {
19
+ background-color: $autocomplete-background-color;
20
+ border-bottom: solid 1px $autocomplete-separator-color;
21
+ margin: 0;
22
+ padding: 4px;
23
+
24
+ a {
25
+ color: $autocomplete-foreground-color;
26
+ display: block;
27
+ padding: 3px;
28
+ font-weight: normal;
29
+ text-decoration: none;
30
+ cursor: pointer;
31
+ }
32
+
33
+ &:last-child {
34
+ border-bottom: none;
35
+ }
36
+
37
+ &.ui-state-focus,
38
+ &.ui-state-hover,
39
+ &.ui-state-active {
40
+ background-color: $autocomplete-background-active-color;
41
+
42
+ a {
43
+ color: $autocomplete-foreground-active-color;
44
+ }
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'formtastic_autocomplete/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'formtastic_autocomplete'
8
+ spec.version = FormtasticAutocomplete::VERSION
9
+ spec.authors = ['Matt Huggins']
10
+ spec.email = ['matt.huggins@gmail.com']
11
+ spec.description = %q{jQuery autocomplete for Formtastic form fields}
12
+ spec.summary = %q{jQuery autocomplete for Formtastic form fields}
13
+ spec.homepage = 'https://github.com/mhuggins/formtastic_autocomplete'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'formtastic'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rails'
26
+ end
@@ -0,0 +1,67 @@
1
+ module Formtastic
2
+ module Inputs
3
+ class AutocompleteInput < StringInput
4
+ def input
5
+ @builder.text_field(method, input_html_options) <<
6
+ @builder.hidden_field(method, hidden_html_options)
7
+ end
8
+
9
+ private
10
+
11
+ def input_html_options
12
+ super.deep_merge(
13
+ name: '',
14
+ class: 'autocomplete',
15
+ id: options[:id] || input_tag_id,
16
+ value: options[:value] || input_tag_value,
17
+ data: { source: options[:source] }
18
+ )
19
+ end
20
+
21
+ def hidden_html_options
22
+ {
23
+ value: options[:value] || hidden_tag_value
24
+ }
25
+ end
26
+
27
+ def input_tag_id
28
+ "#{sanitized_object_name}_#{sanitized_method}_autocomplete"
29
+ end
30
+
31
+ def input_tag_value
32
+ return unless association
33
+
34
+ if association.respond_to?(:label)
35
+ association.label
36
+ elsif association.respond_to?(:title)
37
+ association.title
38
+ elsif association.respond_to?(:name)
39
+ association.name
40
+ else
41
+ warn "#{association.class} must respond to label, title, or name"
42
+ nil
43
+ end
44
+ end
45
+
46
+ def hidden_tag_value
47
+ association.id if association
48
+ end
49
+
50
+ def sanitized_object_name
51
+ @sanitized_object_name ||= object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, '_').sub(/_$/, '')
52
+ end
53
+
54
+ def sanitized_method
55
+ @sanitized_method ||= method.to_s.sub(/\?$/, '')
56
+ end
57
+
58
+ def association
59
+ @association ||= begin
60
+ association_name = method.to_s
61
+ association_name.gsub!(/_id\z/, '')
62
+ @builder.object.send(association_name.to_sym)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'formtastic_autocomplete/version'
2
+ require_relative 'formtastic/inputs/autocomplete_input'
3
+
4
+ module FormtasticAutocomplete
5
+ class << self
6
+ def load!
7
+ register_rails_engine if rails?
8
+ end
9
+
10
+ private
11
+
12
+ def register_rails_engine
13
+ require_relative 'formtastic_autocomplete/engine'
14
+ end
15
+
16
+ def rails?
17
+ defined?(::Rails)
18
+ end
19
+ end
20
+ end
21
+
22
+ FormtasticAutocomplete.load!
@@ -0,0 +1,11 @@
1
+ module FormtasticAutocomplete
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer 'formtastic_autocomplete.assets.precompile' do |app|
5
+ %w(stylesheets javascripts).each do |dir|
6
+ app.config.assets.paths << root.join('assets', dir)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module FormtasticAutocomplete
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formtastic_autocomplete
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Huggins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: formtastic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: jQuery autocomplete for Formtastic form fields
70
+ email:
71
+ - matt.huggins@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - assets/javascripts/formtastic_autocomplete.js
82
+ - assets/stylesheets/_formtastic_autocomplete.scss
83
+ - formtastic_autocomplete.gemspec
84
+ - lib/formtastic/inputs/autocomplete_input.rb
85
+ - lib/formtastic_autocomplete.rb
86
+ - lib/formtastic_autocomplete/engine.rb
87
+ - lib/formtastic_autocomplete/version.rb
88
+ homepage: https://github.com/mhuggins/formtastic_autocomplete
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: jQuery autocomplete for Formtastic form fields
112
+ test_files: []