auto_awesomplete 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of auto_awesomplete might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a451644fbe3b7430bf0b31b5da7bc2931acd09e
4
+ data.tar.gz: 9640951e06b38440aebb576afdf8b86688ff4199
5
+ SHA512:
6
+ metadata.gz: 141ad418efb226bd67f1e6f3bf25bca55b5a3e18520eb3b753e1ca5b261c882f80b4f746b42f5b33ea24aa4498e61a92b7d9447d1e2de2404a20b515c39c993f
7
+ data.tar.gz: 09aee7c6cc778faa8de0cf0cb12b2ebe6c7d93a973f9c3e7081d4a1d731527eac053e6a8cba3c13df89b54c294feacec6c1c86a68b84b10854b1c93882e6e2ea
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea/*
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ivan Zabrovskiy
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,139 @@
1
+ # AutoAwesomplete
2
+
3
+ Gem provide API (scripts, helpers, controller and base class for ajax-search)
4
+ for initialize ajax awesomplete elements.
5
+
6
+ The `AutoAwesomplete` based on [awesomplete](https://github.com/fishbullet/awesomplete) gem.
7
+
8
+ [![Gem Version](https://badge.fury.io/rb/auto_awesomplete.png)](http://badge.fury.io/rb/auto_awesomplete)
9
+
10
+ ## Installation
11
+
12
+ ### First
13
+
14
+ Install [awesomplete](https://github.com/fishbullet/awesomplete#getting-started)
15
+ and include javascript and stylesheet assets.
16
+
17
+ ### Second
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'auto_awesomplete'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ ### Third
28
+
29
+ Select one of ways to include js in pipeline. Ways described below in section `Javascript include variants`
30
+
31
+ ### Fourth
32
+
33
+ Check controller and route collation. Gem contain controller `AutoAwesompletesController` and route
34
+
35
+ get 'auto_awesompletes/:class_name'
36
+
37
+ ### Fifth
38
+
39
+ Prepare folder `app/awesomplete_search_adapters`. This folder needed for storage custom `SearchAdapter`.
40
+
41
+ ## Compatibility
42
+
43
+ Gem tested and works in Rails 3.2 (with manual install awesomplete assets) and Rails 4.0.
44
+ You can test compatibility with other versions by yourself.
45
+
46
+ ## Easiest way to use
47
+
48
+ Use [AutoAwesomplete2Tag](https://github.com/Tab10id/auto_awesomplete_tag). It provide helper:
49
+
50
+ * awesomplete_ajax_tag
51
+
52
+ and you can define ajax awesomplete element like any other view elements in rails.
53
+
54
+ ## Example
55
+
56
+ (stub)
57
+
58
+ ## Usage
59
+
60
+ ### Javascript include variants
61
+
62
+ You have two ways to include javascript files. First: in gem presents helper method
63
+
64
+ * ajax_awesomplete_init_script
65
+
66
+ These helpers call `javascript_include_tag` and it is useful for initialize awesomplete
67
+ scripts on a single page. Example of usage in a view:
68
+
69
+ - ajax_awesomplete_init_script
70
+
71
+ = ajax_awesomplete_tag :my_select, my_options_for_awesomplete, class: 'small'
72
+
73
+ Second variant: include files in javascript assets. For this add the
74
+ following to your `app/assets/javascripts/application.js`:
75
+
76
+ //= require auto_awesomplete/ajax
77
+
78
+ ### Ajax awesomplete usage
79
+
80
+ For initialize ajax awesomplete you must set `auto-ajax-awesomplete` css-class for input element.
81
+ Then you have two ways. Easy way for simple selection: specify `default_class_name`, `default_text_column` and
82
+ `default_id_column` as params for `:data-awesomplete-href`
83
+ Other way for custom selection: create `SearchAdapter`. This adapter has following requirements:
84
+
85
+ * class must be inherited from `AutoAwesomplete::SearchAdapter::Base`
86
+ * file must be put in autoload folder (`app/awesomplete_search_adapter` for example)
87
+ * name of a adapter class must end with `SearchAdapter`
88
+ * must has function `search_default(term, page, options)`
89
+ (description of the function and return value goes below)
90
+
91
+ Returned value of `search_default` function must return Array as json
92
+
93
+ **TIP:** in `search_default` you can use functions from `AutoAwesomplete::SearchAdapter::Base`
94
+
95
+ Finally input must has `:data-awesomplete-href` attribute. This
96
+ parameter specify url for ajax load select options. You can use helper
97
+
98
+ auto_awesompletes_path(class_name: :my_class_name)
99
+ or
100
+
101
+ auto_awesompletes_path(default_class_name: my_class,
102
+ default_text_column: :name,
103
+ default_id_column: :id)
104
+
105
+ ### Example of minimalistic SearchAdapter
106
+ class SystemRoleSearchAdapter < AutoAwesomplete::SearchAdapter::Base
107
+ class << self
108
+ def search_default(term, page, options)
109
+ roles = default_finder(SystemRole, term, page: page).pluck(:name).to_json
110
+ end
111
+ end
112
+ end
113
+
114
+ ### More about SearchAdapter
115
+
116
+ `SearchAdapter` has some additional functions. First, you can define multiple search
117
+ functions in one adapter. For example in adapter for User you want to find among all
118
+ users, find users only in one department and so on. For this purpose define methods like
119
+
120
+ def search_unusual_case(term, page, options)
121
+ # must has same behavior as search_default
122
+ end
123
+
124
+ near the `search_default` in `SearchAdapter`. Requirement for non-default search methods:
125
+
126
+ * it must has same behavior as search_default
127
+ * name of methods must start with `search_`
128
+
129
+ For use custom searcher specify it into `:data-awesomplete-href` attribute:
130
+
131
+ auto_awesompletes_path(class_name: MyClassName, search_method: :unusual_case)
132
+
133
+ ## Contributing
134
+
135
+ 1. Fork it ( http://github.com/Tab10id/auto_awesomplete/fork )
136
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
137
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
138
+ 4. Push to the branch (`git push origin my-new-feature`)
139
+ 5. Create new Pull Request
@@ -0,0 +1,25 @@
1
+ class AutoAwesompletesController < ApplicationController
2
+ def search
3
+ begin
4
+ if params[:class_name].present?
5
+ adapter = "::#{params[:class_name].camelize}SearchAdapter".constantize
6
+ elsif params[:default_class_name].present?
7
+ adapter = ::AutoAwesomplete::SearchAdapter::Default
8
+ else
9
+ render json: {error: "not enough search parameters'"}.to_json,
10
+ status: 500
11
+ return
12
+ end
13
+ rescue NameError
14
+ render json: {error: "not found search adapter for '#{params[:class_name]}'"}.to_json,
15
+ status: 500
16
+ return
17
+ end
18
+
19
+ term = params.delete(:term)
20
+ page = params.delete(:page)
21
+ search_method = params.delete(:search_method)
22
+
23
+ render json: adapter.search_from_autocomplete(term, page, search_method, params).to_json
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auto_awesomplete/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'auto_awesomplete'
8
+ spec.version = AutoAwesomplete::VERSION
9
+ spec.authors = ['Dmitry Lisichkin', 'Ivan Zabrovskiy']
10
+ spec.email = %w(dima@sb42.ru lorowar@gmail.com)
11
+ spec.summary = %q{Base methods for wrapping a Awesomplete and easy initialize it.}
12
+ spec.description = <<-DESC
13
+ Gem provide scripts and helpers for initialize different awesomplete elements:
14
+ static and ajax. Moreover this gem is foundation for other gems.
15
+ For example for AutoAwesompleteTag.
16
+ DESC
17
+ spec.homepage = 'https://github.com/Tab10id/auto_awesomplete'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'railties', '>= 3.1'
26
+ spec.add_dependency 'coffee-rails'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.5'
29
+ spec.add_development_dependency 'rails', '~> 3.2.12'
30
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ get 'auto_awesompletes/(:class_name)', to: 'auto_awesompletes#search', as: 'auto_awesompletes'
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'auto_awesomplete/version'
2
+ require 'auto_awesomplete/helpers'
3
+ require 'auto_awesomplete/engine'
4
+
5
+ module AutoAwesomplete
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :SearchAdapter
9
+ end
@@ -0,0 +1,9 @@
1
+ module AutoAwesomplete
2
+ class Engine < ::Rails::Engine
3
+ # Get rails to add app, lib, vendor to load path
4
+
5
+ initializer :javascripts do |app|
6
+ app.config.assets.precompile << 'auto_awesomplete/ajax.js'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'auto_awesomplete/helpers/rails_helper.rb'
2
+
3
+ ActionView::Base.send :include, AutoAwesomplete::Helpers::RailsHelper
@@ -0,0 +1,10 @@
1
+ module AutoAwesomplete::Helpers
2
+ module RailsHelper
3
+ def ajax_awesomplete_init_script
4
+ unless @auto_awesomplete_ajax_script_included
5
+ @auto_awesomplete_ajax_script_included = true
6
+ javascript_include_tag('auto_awesomplete/ajax')
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module AutoAwesomplete
2
+ module SearchAdapter
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Base
6
+ autoload :Default
7
+ end
8
+ end
@@ -0,0 +1,73 @@
1
+ module AutoAwesomplete
2
+ module SearchAdapter
3
+ class Base
4
+ class << self
5
+ def limit
6
+ 10
7
+ end
8
+
9
+ def search_from_autocomplete(term, page, search_method, options)
10
+ if search_method.nil?
11
+ search_default(term, page, options)
12
+ else
13
+ self.public_send("search_#{search_method}", term, page, options)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def default_finder(searched_class, term, options)
20
+ columns = options[:column].present? ? options[:column] : 'name'
21
+ conditions = default_search_conditions(term, options[:basic_conditions], columns)
22
+
23
+ if term.nil?
24
+ [ searched_class.where(options[:basic_conditions]) ]
25
+ else
26
+ result_limit = options[:limit] || limit
27
+ query = searched_class.where( conditions ).limit( result_limit ).order(columns)
28
+ query = query.select(options[:select]) if options[:select].present?
29
+ options[:uniq] ? query.uniq : query
30
+ end
31
+ end
32
+
33
+ def default_search_conditions(term, basic_conditions, columns)
34
+ term_filter = ''
35
+ conditions = []
36
+
37
+ unless columns.is_a?(Array)
38
+ columns = columns.split(/[\s,]+/)
39
+ end
40
+
41
+ unless term.nil?
42
+ words = term.split(' ')
43
+ words.each_with_index do |word, index|
44
+ term_filter += ' AND ' if index > 0
45
+
46
+ columns.each_with_index do |column, idx|
47
+ term_filter += ' OR ' if idx > 0
48
+ term_filter += "#{column} LIKE ?"
49
+ conditions << "%#{word}%"
50
+ end
51
+ end
52
+
53
+ term_filter = term_filter.empty? ? nil : "(#{term_filter})"
54
+ basic_conditions_part = basic_conditions.present? ? "(#{basic_conditions }) " : nil
55
+ conditions.unshift([term_filter, basic_conditions_part].compact.join(' AND '))
56
+ end
57
+ end
58
+
59
+ def get_awesomplete_label(item, label_method)
60
+ if label_method.present? && item.respond_to?(label_method)
61
+ item.public_send(label_method)
62
+ else
63
+ if item.respond_to?(:to_awesomplete)
64
+ item.to_awesomplete
65
+ else
66
+ item.to_s
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,21 @@
1
+ module AutoAwesomplete
2
+ module SearchAdapter
3
+ class Default < Base
4
+ class << self
5
+ def search_default(term, page, options)
6
+ begin
7
+ default_arel = options[:default_class_name].camelize.constantize
8
+ rescue NameError
9
+ return {error: "not found class '#{options[:default_class_name]}'"}.to_json
10
+ end
11
+
12
+ default_values = default_finder(default_arel, term, page: page, column: options[:default_text_column])
13
+ default_values.map do |default_value|
14
+ get_awesomplete_label(default_value, options[:label_method])
15
+ end.to_json
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,3 @@
1
+ module AutoAwesomplete
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,34 @@
1
+ ((autoAwesomplete, $) ->
2
+ autoAwesomplete.updateList = (awesomplete, path, term) ->
3
+ $.ajax({
4
+ url: path,
5
+ dataType: 'json',
6
+ data: { term: term }
7
+ ,
8
+ success: (data) ->
9
+ awesomplete.list = JSON.parse(data)
10
+ awesomplete.evaluate()
11
+ return
12
+ })
13
+
14
+ return
15
+
16
+ autoAwesomplete.ajaxInit = ->
17
+ $inputs = $(':not(.awesomplete) > input.auto-ajax-awesomplete')
18
+ $inputs.each ->
19
+ $input = $(this)
20
+ awesomplete = new Awesomplete(this)
21
+ $input.on 'input', ->
22
+ $textInput = $(this)
23
+ path = $textInput.data('awesomplete-href')
24
+ term = $textInput.val()
25
+ autoAwesomplete.updateList(awesomplete, path, term)
26
+ return
27
+ return
28
+ return
29
+ return
30
+ ) window.autoAwesomplete = window.autoAwesomplete or {}, jQuery
31
+
32
+ jQuery ($) ->
33
+ autoAwesomplete.ajaxInit()
34
+ return
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_awesomplete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Lisichkin
8
+ - Ivan Zabrovskiy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '3.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: coffee-rails
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '1.5'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.5'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rails
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 3.2.12
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.12
70
+ description: |2
71
+ Gem provide scripts and helpers for initialize different awesomplete elements:
72
+ static and ajax. Moreover this gem is foundation for other gems.
73
+ For example for AutoAwesompleteTag.
74
+ email:
75
+ - dima@sb42.ru
76
+ - lorowar@gmail.com
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - .gitignore
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - app/controllers/auto_awesompletes_controller.rb
86
+ - auto_awesomplete.gemspec
87
+ - config/routes.rb
88
+ - lib/auto_awesomplete.rb
89
+ - lib/auto_awesomplete/engine.rb
90
+ - lib/auto_awesomplete/helpers.rb
91
+ - lib/auto_awesomplete/helpers/rails_helper.rb
92
+ - lib/auto_awesomplete/search_adapter.rb
93
+ - lib/auto_awesomplete/search_adapter/base.rb
94
+ - lib/auto_awesomplete/search_adapter/default.rb
95
+ - lib/auto_awesomplete/version.rb
96
+ - vendor/assets/javascripts/auto_awesomplete/ajax.js.coffee
97
+ homepage: https://github.com/Tab10id/auto_awesomplete
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.0.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Base methods for wrapping a Awesomplete and easy initialize it.
121
+ test_files: []
122
+ has_rdoc: