simple_form_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: 8608b077bda353dfc18c7033b902d9ce36c40db8
4
+ data.tar.gz: 3c66110aea9c60f529f8d891ef71bc88c29f1608
5
+ SHA512:
6
+ metadata.gz: a4c59f26e49b4bb6f022c743e98703c9a59252cb845daffbdf18c8136b0f4dd23800f57823b95801d86b50c4e2c35d2ab3b75a3e76640de78bbb886750323a6d
7
+ data.tar.gz: 069c838abcab1d5283a2a359aa941e905f1cb5dba7fb365038add338225a7b0e3c697a51f7b707312e2bf2d0ae413b4fdfdf939ebd29d86caa6019e871781683
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 simple_form_autocomplete.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 mhuggins
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,64 @@
1
+ # SimpleFormAutocomplete
2
+
3
+ `simple_form_autocomplete` is a Ruby gem that enables use of [jQuery
4
+ Autocomplete](http://jqueryui.com/autocomplete/) inputs with
5
+ [SimpleForm](https://github.com/plataformatec/simple_form).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'simple_form_autocomplete'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install simple_form_autocomplete
22
+
23
+ ## Usage
24
+
25
+ Require the Javascript in your `application.js` (or other preferred) file via sprockets:
26
+
27
+ ```javascript
28
+ //= require 'simple_form_autocomplete'
29
+ ```
30
+
31
+ Alternatively import the CSS in your `application.css.scss` (or other preferred) file. For
32
+ sprockets-based apps:
33
+
34
+ ```javascript
35
+ //= require 'simple_form_autocomplete';
36
+ ```
37
+
38
+ For SCSS imports:
39
+
40
+ ```javascript
41
+ @import 'simple_form_autocomplete';
42
+ ```
43
+
44
+ When rendering form via SimpleForm's `simple_form_for` method, supply the new input via the `:as`
45
+ option along with a `:source` option to specify the data source URL or array of objects:
46
+
47
+ ```ruby
48
+ simple_form_for @ingredient do |f|
49
+ f.input :name, as: :autocomplete, source: autocomplete_ingredients_path
50
+ f.input :unit, as: :autocomplete, source: ['cup', 'tablespoon', 'teaspoon']
51
+ f.input :quantity
52
+ end
53
+ ```
54
+
55
+ The value supplied for the `:source` option should match the format expected per the [jQuery
56
+ autocomplete documentation](http://api.jqueryui.com/autocomplete/#option-source).
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 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,46 @@
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
+ .ui-autocomplete {
9
+ position: absolute;
10
+ list-style: none;
11
+ margin: 0;
12
+ padding: 0;
13
+ border: 1px solid $autocomplete-border-color;
14
+ cursor: default;
15
+ font-size: 0.8em;
16
+
17
+ .ui-menu-item {
18
+ background-color: $autocomplete-background-color;
19
+ border-bottom: solid 1px $autocomplete-separator-color;
20
+ margin: 0;
21
+ padding: 4px;
22
+
23
+ a {
24
+ color: $autocomplete-foreground-color;
25
+ display: block;
26
+ padding: 3px;
27
+ font-weight: normal;
28
+ text-decoration: none;
29
+ cursor: pointer;
30
+ }
31
+
32
+ &:last-child {
33
+ border-bottom: none;
34
+ }
35
+
36
+ &.ui-state-focus,
37
+ &.ui-state-hover,
38
+ &.ui-state-active {
39
+ background-color: $autocomplete-background-active-color;
40
+
41
+ a {
42
+ color: $autocomplete-foreground-active-color;
43
+ }
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,66 @@
1
+ module SimpleForm
2
+ module Inputs
3
+ class AutocompleteInput < Base
4
+ def input
5
+ @builder.text_field(attribute_name, input_html_options) <<
6
+ @builder.hidden_field(attribute_name, hidden_html_options)
7
+ end
8
+
9
+ private
10
+
11
+ def input_html_options
12
+ super.deep_merge(
13
+ name: '',
14
+ id: options[:id] || input_tag_id,
15
+ value: options[:value] || input_tag_value,
16
+ data: { source: options[:source] }
17
+ )
18
+ end
19
+
20
+ def hidden_html_options
21
+ {
22
+ value: options[:value] || hidden_tag_value
23
+ }
24
+ end
25
+
26
+ def input_tag_id
27
+ "#{sanitized_object_name}_#{sanitized_attribute_name}_autocomplete"
28
+ end
29
+
30
+ def input_tag_value
31
+ return unless association
32
+
33
+ if association.respond_to?(:label)
34
+ association.label
35
+ elsif association.respond_to?(:title)
36
+ association.title
37
+ elsif association.respond_to?(:name)
38
+ association.name
39
+ else
40
+ warn "#{association.class} must respond to label, title, or name"
41
+ nil
42
+ end
43
+ end
44
+
45
+ def hidden_tag_value
46
+ association.id if association
47
+ end
48
+
49
+ def sanitized_object_name
50
+ @sanitized_object_name ||= object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, '_').sub(/_$/, '')
51
+ end
52
+
53
+ def sanitized_attribute_name
54
+ @sanitized_method_name ||= attribute_name.to_s.sub(/\?$/, '')
55
+ end
56
+
57
+ def association
58
+ @association ||= begin
59
+ association_name = attribute_name.to_s
60
+ association_name.gsub!(/_id\z/, '')
61
+ @builder.object.send(association_name.to_sym)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ require 'simple_form_autocomplete/version'
2
+ require 'simple_form/inputs/autocomplete_input'
3
+
4
+ module SimpleFormAutocomplete
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 'simple_form_autocomplete/engine'
14
+ end
15
+
16
+ def rails?
17
+ defined?(::Rails)
18
+ end
19
+ end
20
+ end
21
+
22
+ SimpleFormAutocomplete.load!
@@ -0,0 +1,11 @@
1
+ module SimpleFormAutocomplete
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer 'simple_form_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 SimpleFormAutocomplete
2
+ VERSION = '1.0.0'
3
+ end
@@ -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 'simple_form_autocomplete/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'simple_form_autocomplete'
8
+ spec.version = SimpleFormAutocomplete::VERSION
9
+ spec.authors = ['Matt Huggins']
10
+ spec.email = ['matt.huggins@gmail.com']
11
+ spec.description = %q{jQuery autocomplete for SimpleForm form fields}
12
+ spec.summary = %q{jQuery autocomplete for SimpleForm form fields}
13
+ spec.homepage = ''
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 'activesupport'
22
+ spec.add_dependency 'simple_form'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_form_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-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: simple_form
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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 SimpleForm 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/simple_form_autocomplete.js
82
+ - assets/stylesheets/_simple_form_autocomplete.scss
83
+ - lib/simple_form/inputs/autocomplete_input.rb
84
+ - lib/simple_form_autocomplete.rb
85
+ - lib/simple_form_autocomplete/engine.rb
86
+ - lib/simple_form_autocomplete/version.rb
87
+ - simple_form_autocomplete.gemspec
88
+ homepage: ''
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 SimpleForm form fields
112
+ test_files: []