active_admin_auto_select 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 497abfdbbf4ae50b7cad40a7e1e3e1c36a6b250f
4
+ data.tar.gz: 01d5aba5cfe02fb4a09b455183327abaad0d7347
5
+ SHA512:
6
+ metadata.gz: 930ffdd6d544c97a1df3d5d71009e0369ac2b4c1f987026e8bdb2adb83e4c89ee7caf4f94d2017d731a115927e6aef6bd9f4784ef0baa4fb3d6b084e2f5c6ae6
7
+ data.tar.gz: c90f26c8735fb58b947a63961506ff72bbce5d92a81885ed34ad6bf59a25c020608a00d805fdcf9b8f00c632d0ea387a423735f7ed85026edc93b54911c9b2ad
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in admin_auto_select.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 spyrbri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # ActiveAdminAutoSelect
2
+
3
+ This gem is a little widget which combines Select2, PostgeSQL and ActiveAdmin and helps you filter objects in ActiveAdmin with autocomplete of the selected fields.
4
+
5
+ ![Image of AutoSelect](http://i.imgur.com/lEJmaQC.png)
6
+ ![Image of AutoSelect](http://i.imgur.com/6s0Xnco.png)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'admin_auto_select'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Add the following directive to your Javascript manifest file:
21
+
22
+ ``
23
+ //= require autoselect
24
+ ``
25
+
26
+ ## Usage
27
+
28
+ After installing the gem, you have to add some lines into the model you want to auto_select.
29
+ For example, if you want to add the widget to the users id filter:
30
+
31
+ #### Call the method defined in the module in order to create the collection_action
32
+
33
+ first_name, :last_name, :email are the fields which help you find the desired user
34
+
35
+ ```ruby
36
+ auto_select :first_name, :last_name, :email
37
+ ```
38
+
39
+ #### Then change the relevant filter in your model
40
+
41
+ Notice: :id_eq is necessary.
42
+ data-scope is the desired scope which will be used by the widget in order to fetch the similar users.
43
+ data-url is also necessary and is the url generated by the action which is defined in the module.
44
+
45
+ ```ruby
46
+ filter :id_eq, label: "User", input_html: { data: { scope: 'user',
47
+ url: 'users/autoselect',
48
+ placeholder: 'Select user' } }
49
+ ```
50
+
51
+ #### Instantiate the js class in your active_admin js file with the generated filter input.
52
+
53
+ ```
54
+ newAutoSelect = new AutoSelect(input: $("#q_id_eq))
55
+ ```
56
+
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/spyrbri/admin_auto_select. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
61
+
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'active_admin_auto_select/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_admin_auto_select"
7
+ spec.version = AdminAutoSelect::VERSION
8
+ spec.authors = ["spyrbri"]
9
+ spec.email = ["spyrbri@gmail.com"]
10
+ spec.summary = %q{This gem helps you select a row of a column in a model with autocomplete in active admin}
11
+ spec.description = %q{This gem helps you build nice filters in Activeadmin. It combines Select2, PostgresSQL and Ajax to create an easy to use select field.}
12
+ spec.homepage = "https://github.com/spyrbri/active_admin_auto_select"
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files`.split("\n")
15
+ spec.require_paths = ["lib"]
16
+ spec.add_development_dependency "bundler", "~> 1.10"
17
+ spec.add_development_dependency "rake", "~> 10.0"
18
+ spec.add_dependency "select2-rails"
19
+ spec.add_dependency "pg"
20
+ end
@@ -0,0 +1,84 @@
1
+ var AutoSelect;
2
+
3
+ AutoSelect = (function() {
4
+ function AutoSelect(options) {
5
+ this.options = options != null ? options : {};
6
+ this.$input = $(this.options['input']);
7
+ this.width = this.options['width'] || 240;
8
+ this.placeholder = this.options['placeholder'];
9
+ }
10
+
11
+ AutoSelect.prototype.init = function() {
12
+ var scope, url;
13
+ scope = this.$input.attr('data-scope');
14
+ url = this.$input.attr('data-url');
15
+ this.$input.select2({
16
+ placeholder: this.placeholder,
17
+ allowClear: true,
18
+ minimumInputLength: 1,
19
+ width: this.width,
20
+ ajax: {
21
+ url: url,
22
+ quietMillis: 300,
23
+ data: function(term, page) {
24
+ return {
25
+ q: term,
26
+ page_limit: 10,
27
+ page: page,
28
+ scope: scope
29
+ };
30
+ },
31
+ results: function(data, page) {
32
+ var more;
33
+ more = data.length >= 10;
34
+ return {
35
+ results: data,
36
+ more: more
37
+ };
38
+ }
39
+ },
40
+ formatResult: (function(_this) {
41
+ return function(item, container, query, escapeFn) {
42
+ var markup, text;
43
+ markup = [];
44
+ window.Select2.util.markMatch(_this.detailText(item), query.term, markup, escapeFn);
45
+ return text = markup.join('');
46
+ };
47
+ })(this),
48
+ formatSelection: (function(_this) {
49
+ return function(item, container, escapeFn) {
50
+ return _this.detailText(item);
51
+ };
52
+ })(this),
53
+ initSelection: (function(_this) {
54
+ return function(e, callback) {
55
+ var id;
56
+ id = $(e).val();
57
+ if (id !== "") {
58
+ return $.getJSON(url, {
59
+ id: id
60
+ }).done(function(data) {
61
+ callback(data);
62
+ return _this.$input.trigger("autocomplete:ajax:success", data);
63
+ });
64
+ }
65
+ };
66
+ })(this)
67
+ });
68
+ return this.$input.change('select2-selecting', function() {
69
+ return $(this).closest('.filter_form').submit();
70
+ });
71
+ };
72
+
73
+ AutoSelect.prototype.detailText = function(item) {
74
+ return _.reject(_.uniq(_.toArray(item)), function(el) {
75
+ return el === '';
76
+ }).join(' - ');
77
+ };
78
+
79
+ return AutoSelect;
80
+
81
+ })();
82
+
83
+ // ---
84
+ // generated by coffee-script 1.9.2
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "admin_auto_select"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ require "active_admin_auto_select/version"
2
+ require 'active_admin_auto_select/rails'
3
+
4
+ module AutoSelectable
5
+ def auto_select(*fields)
6
+ options = fields.extract_options!
7
+ create_collection_action(fields, options, @resource)
8
+ end
9
+
10
+ def create_collection_action(fields, options, resource)
11
+ collection_action :autoselect, :method => :get do
12
+ select_fields = "#{resource.table_name}.id, " << fields.join(', ')
13
+ if (Module.const_get(:CanCanCan) rescue false) ? authorized?(:read, resource) :true
14
+ term = ([ params[:id], params[:q]].compact).try(:first)
15
+ term.gsub!('%', '\\\%')
16
+ term.gsub!('_', '\\\_')
17
+ first_term = term.try(:match, /\w \w/) ? (term.split(' '))[0] : term
18
+ page = params[:page].try(:to_i)
19
+ offset = page ? (page - 1) * 10 + (5 * (page - 1)) : 0
20
+
21
+ #params[:scope] is passed through js when we define the relevate data attribute on the filter
22
+ #default_scope is passed as a proc argument in #auto_select and overrides the default resource scope
23
+ effective_scope = options[params[:scope]] || options['default_scope'] || ->{ resource }
24
+
25
+ #This param exists when we have a filtered result
26
+ if params[:id]
27
+ first_resource = effective_scope.call.
28
+ where("#{resource.table_name}.id = ?", params[:id].to_i).
29
+ select(select_fields).first
30
+ render json: first_resource and return
31
+ else
32
+ concat_fields = fields.join(" || ' '::text || ")
33
+ concat_cols = "((#{concat_fields})" << " || ' '::text || #{resource.table_name}.id::text)"
34
+
35
+ similarity_sql = ActiveRecord::Base.send(:sanitize_sql_array,
36
+ ["(similarity(#{resource.table_name}.id::text, :id) + similarity(#{concat_cols}, :id))", id: term])
37
+
38
+ resource_records = effective_scope.call.
39
+ select(select_fields << ", #{similarity_sql} as similarity").
40
+ where("#{concat_cols} ILIKE :term", term: "%#{first_term}%").
41
+ order("#{similarity_sql} DESC").
42
+ limit(15).offset(offset).
43
+ map { |r| r.attributes.reject { |a| a == 'similarity'} }
44
+ end
45
+
46
+ render json: resource_records
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ ActiveAdmin::ResourceDSL.send :include, AutoSelectable
@@ -0,0 +1,6 @@
1
+ module Autoselect
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module AdminAutoSelect
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_admin_auto_select
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - spyrbri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: select2-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem helps you build nice filters in Activeadmin. It combines Select2,
70
+ PostgresSQL and Ajax to create an easy to use select field.
71
+ email:
72
+ - spyrbri@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - active_admin_auto_select.gemspec
84
+ - app/assets/javascripts/autoselect.js
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/active_admin_auto_select.rb
88
+ - lib/active_admin_auto_select/rails.rb
89
+ - lib/active_admin_auto_select/version.rb
90
+ homepage: https://github.com/spyrbri/active_admin_auto_select
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.8
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: This gem helps you select a row of a column in a model with autocomplete
114
+ in active admin
115
+ test_files: []