trln-chosen-rails 1.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.md +152 -0
- data/Rakefile +16 -0
- data/lib/chosen-rails.rb +15 -0
- data/lib/chosen-rails/engine.rb +13 -0
- data/lib/chosen-rails/engine3.rb +11 -0
- data/lib/chosen-rails/railtie.rb +6 -0
- data/lib/chosen-rails/rspec.rb +93 -0
- data/lib/chosen-rails/source_file.rb +75 -0
- data/lib/chosen-rails/tasks.rake +11 -0
- data/lib/chosen-rails/version.rb +6 -0
- data/trln-chosen-rails.gemspec +26 -0
- data/vendor/assets/images/chosen-sprite.png +0 -0
- data/vendor/assets/images/chosen-sprite@2x.png +0 -0
- data/vendor/assets/javascripts/chosen-jquery.js +3 -0
- data/vendor/assets/javascripts/chosen-prototype.js +3 -0
- data/vendor/assets/javascripts/chosen.jquery.coffee +513 -0
- data/vendor/assets/javascripts/chosen.proto.coffee +523 -0
- data/vendor/assets/javascripts/lib/abstract-chosen.coffee +384 -0
- data/vendor/assets/javascripts/lib/select-parser.coffee +56 -0
- data/vendor/assets/stylesheets/chosen-base.scss +427 -0
- data/vendor/assets/stylesheets/chosen-compass.scss +63 -0
- data/vendor/assets/stylesheets/chosen.scss +59 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 799c2b94b78b9cebcc6dd504ce0203405ef03e2e75e806f316b74e627f79c355
|
4
|
+
data.tar.gz: 3eff2a510a4dc00b1bc04a8ef083e36efc414898c3e19a802ad4b371ca4dce61
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b4eca6cadd8ccd4fb62842632d3286a432314e816a61ea20f8cdeb7e1ad435c12b84bb01c7475ededc8d2a517859203fa44ccecdf596dbfb4272c648e4bd6aa
|
7
|
+
data.tar.gz: e40d0b62872704a7035b33fd64dae00740befd81b4aab0aa9e26ef35b089caeec97bf4cf5324587869b802a52b896ec24981b60af72de258d731ad73fff0c648
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011-2013 by Tse-Ching Ho
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
# Chosen for rails asset pipeline
|
2
|
+
|
3
|
+
[Chosen](https://github.com/harvesthq/chosen) is a library for making long, unwieldy select boxes more user friendly.
|
4
|
+
|
5
|
+
The `trln-hosen-rails` gem integrates the `Chosen` with the Rails asset pipeline. This project is a fork of the original `chosen-rails` which removes the dependency on the now deprecated `sass` gem.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
### Install chosen-rails gem
|
10
|
+
|
11
|
+
Include `trln-chosen-rails` in Gemfile
|
12
|
+
|
13
|
+
```rb
|
14
|
+
gem 'trln-chosen-rails'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then run `bundle install`
|
18
|
+
|
19
|
+
Please consider [jquery-turbolinks](https://github.com/kossnocorp/jquery.turbolinks) if you have turbolinks issues for Rails 4 +.
|
20
|
+
|
21
|
+
### Include chosen javascript assets
|
22
|
+
|
23
|
+
Add to your `app/assets/javascripts/application.js` if use with jQuery
|
24
|
+
|
25
|
+
```coffee
|
26
|
+
//= require chosen-jquery
|
27
|
+
```
|
28
|
+
|
29
|
+
Or with Prototype
|
30
|
+
|
31
|
+
```coffee
|
32
|
+
//= require chosen-prototype
|
33
|
+
```
|
34
|
+
|
35
|
+
### Include chosen stylesheet assets
|
36
|
+
|
37
|
+
Add to your `app/assets/stylesheets/application.css`
|
38
|
+
|
39
|
+
```scss
|
40
|
+
*= require chosen
|
41
|
+
```
|
42
|
+
|
43
|
+
### Enable chosen javascript by specific css class
|
44
|
+
|
45
|
+
Add to one coffee script file, like `scaffold.js.coffee`
|
46
|
+
|
47
|
+
```coffee
|
48
|
+
$ ->
|
49
|
+
# enable chosen js
|
50
|
+
$('.chosen-select').chosen
|
51
|
+
allow_single_deselect: true
|
52
|
+
no_results_text: 'No results matched'
|
53
|
+
width: '200px'
|
54
|
+
```
|
55
|
+
|
56
|
+
Notice: `width` option is required since `Chosen 0.9.15`.
|
57
|
+
|
58
|
+
And this file must be included in `application.js`
|
59
|
+
|
60
|
+
```coffee
|
61
|
+
//= require chosen-jquery
|
62
|
+
//= require scaffold
|
63
|
+
```
|
64
|
+
|
65
|
+
Also add the class to your form field
|
66
|
+
|
67
|
+
```erb
|
68
|
+
<%= f.select :author,
|
69
|
+
User.all.map { |u| [u.name, u.id] },
|
70
|
+
{ include_blank: true },
|
71
|
+
{ class: 'chosen-select' }
|
72
|
+
%>
|
73
|
+
```
|
74
|
+
|
75
|
+
If you use simple form as form builder
|
76
|
+
|
77
|
+
```erb
|
78
|
+
<%= f.association :author,
|
79
|
+
collection: User.all,
|
80
|
+
include_blank: true,
|
81
|
+
input_html: { class: 'chosen-select' }
|
82
|
+
%>
|
83
|
+
```
|
84
|
+
|
85
|
+
### Deployment
|
86
|
+
|
87
|
+
Since version 0.13.0, non-digested assets of `chosen-rails` will simply be copied from digested assets.
|
88
|
+
|
89
|
+
## RSpec helpers
|
90
|
+
`chosen-rails` provides RSpec feature helper methods that allow users to select or unselect options from both single and multiple chosen elements. Add the following to your `spec/rails_helper.rb` (or `spec/spec_helper.rb`):
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'chosen-rails/rspec'
|
94
|
+
```
|
95
|
+
|
96
|
+
This automatically configures RSpec by adding:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
RSpec.configure do |config|
|
100
|
+
config.include Chosen::Rspec::FeatureHelpers, type: :feature
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Configuration includes two additional methods for all `type: :feature` specs:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
chosen_select(value, options = {})
|
108
|
+
chosen_unselect(value, options = {})
|
109
|
+
```
|
110
|
+
|
111
|
+
Both methods require `from: '...'` inside the `options` hash that is either a CSS selector or a field's label (requires the `for` attribute on the label!).
|
112
|
+
|
113
|
+
### Example usage
|
114
|
+
To handle a single select:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
chosen_select('Leo Tolstoy', from: 'Author')
|
118
|
+
chosen_unselect('Leo Tolstoy', from: '#author')
|
119
|
+
chosen_select('Fyodor Dostoyevsky', from: '#author')
|
120
|
+
```
|
121
|
+
|
122
|
+
To handle a multiple select:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
chosen_select('Leo Tolstoy', 'Fyodor Dostoyevsky', 'Anton Chekhov', from: 'Authors')
|
126
|
+
# or, by single value:
|
127
|
+
chosen_select('Leo Tolstoy', from: '#authors')
|
128
|
+
|
129
|
+
chosen_unselect('Fyodor Dostoyevsky', ' Anton Chekhov', from: 'Authors')
|
130
|
+
# or, by single value:
|
131
|
+
chosen_unselect('Leo Tolstoy', from: '#authors')
|
132
|
+
```
|
133
|
+
|
134
|
+
## Gem maintenance
|
135
|
+
|
136
|
+
Maintain `chosen-rails` gem with `Rake` commands.
|
137
|
+
|
138
|
+
Update origin chosen source files.
|
139
|
+
|
140
|
+
```bash
|
141
|
+
rake update-chosen
|
142
|
+
```
|
143
|
+
|
144
|
+
Publish gem.
|
145
|
+
|
146
|
+
```bash
|
147
|
+
rake release
|
148
|
+
```
|
149
|
+
|
150
|
+
## License
|
151
|
+
|
152
|
+
use MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require File.expand_path('../lib/chosen-rails/source_file', __FILE__)
|
4
|
+
|
5
|
+
desc "Update with Harvest's Chosen Library"
|
6
|
+
task 'update-chosen', 'repository_url', 'branch' do |task, args|
|
7
|
+
remote = args['repository_url'] || 'https://github.com/harvesthq/chosen'
|
8
|
+
branch = args['branch'] || 'master'
|
9
|
+
files = SourceFile.new
|
10
|
+
files.fetch remote, branch
|
11
|
+
files.eject_javascript_class_from_closure
|
12
|
+
files.remove_compass_lines
|
13
|
+
files.add_depend_on_asset
|
14
|
+
files.change_url_to_image_url
|
15
|
+
files.cleanup
|
16
|
+
end
|
data/lib/chosen-rails.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'chosen-rails/version'
|
2
|
+
|
3
|
+
module Chosen
|
4
|
+
module Rails
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
case ::Rails.version.to_s
|
9
|
+
when /^(4|5)/
|
10
|
+
require 'chosen-rails/engine'
|
11
|
+
when /^3\.[12]/
|
12
|
+
require 'chosen-rails/engine3'
|
13
|
+
when /^3\.[0]/
|
14
|
+
require 'chosen-rails/railtie'
|
15
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Chosen
|
2
|
+
module Rspec
|
3
|
+
module FeatureHelpers
|
4
|
+
def chosen_select(value, *args)
|
5
|
+
options = args.extract_options!
|
6
|
+
|
7
|
+
input = chosen_input(options)
|
8
|
+
|
9
|
+
args.unshift(value).uniq.each { |item| chosen_select!(input, item) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def chosen_unselect(value, *args)
|
13
|
+
options = args.extract_options!
|
14
|
+
|
15
|
+
input = chosen_input(options)
|
16
|
+
|
17
|
+
args.unshift(value).uniq.each { |item| chosen_unselect!(input, item) }
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def chosen_input(options)
|
23
|
+
fail ArgumentError, 'Required argument from: not set' unless options.has_key?(:from)
|
24
|
+
|
25
|
+
from = options.delete(:from)
|
26
|
+
|
27
|
+
begin
|
28
|
+
input = chosen_find_container(from, options)
|
29
|
+
rescue Capybara::ElementNotFound
|
30
|
+
input = chosen_find_input(from, options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def chosen_find_container(from, options)
|
35
|
+
from = from.to_s
|
36
|
+
|
37
|
+
id = from.underscore
|
38
|
+
id = "##{id}" unless from.start_with?('#')
|
39
|
+
id = "#{id}_chosen" unless from.end_with?('_chosen')
|
40
|
+
|
41
|
+
find(:css, id, options)
|
42
|
+
rescue Capybara::ElementNotFound
|
43
|
+
label = find('label', { text: from }.merge(options))
|
44
|
+
|
45
|
+
find(:css, "##{label[:for].underscore}_chosen", options)
|
46
|
+
end
|
47
|
+
|
48
|
+
def chosen_find_input(from, options)
|
49
|
+
from = from.to_s
|
50
|
+
from = "##{from}" unless from.start_with?('#')
|
51
|
+
|
52
|
+
find(:css, from.underscore, options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def chosen_multiselect?(input)
|
56
|
+
input.first('.chosen-container-multi').present?
|
57
|
+
end
|
58
|
+
|
59
|
+
def chosen_select!(input, item)
|
60
|
+
if input.tag_name == 'select'
|
61
|
+
input.find(:option, item).select_option
|
62
|
+
else
|
63
|
+
input.click
|
64
|
+
|
65
|
+
within "##{input[:id]} .chosen-drop .chosen-results" do
|
66
|
+
result = find('.active-result', text: item, match: :prefer_exact)
|
67
|
+
|
68
|
+
result.click if result.visible?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def chosen_unselect!(input, item)
|
74
|
+
if input.tag_name == 'select'
|
75
|
+
input.find(:option, item).unselect_option
|
76
|
+
else
|
77
|
+
if chosen_multiselect?(input)
|
78
|
+
input.first('.search-choice', text: item)
|
79
|
+
.first('.search-choice-close')
|
80
|
+
.click
|
81
|
+
else
|
82
|
+
input.first('.search-choice-close').click
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
RSpec.configure do |config|
|
91
|
+
config.include Chosen::Rspec::FeatureHelpers, type: :system
|
92
|
+
config.include Chosen::Rspec::FeatureHelpers, type: :feature
|
93
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class SourceFile < Thor
|
5
|
+
include Thor::Actions
|
6
|
+
|
7
|
+
desc 'fetch source files', 'fetch source files from GitHub'
|
8
|
+
def fetch remote, branch
|
9
|
+
self.destination_root = 'vendor/assets'
|
10
|
+
get "#{remote}/raw/#{branch}/public/chosen-sprite.png", 'images/chosen-sprite.png'
|
11
|
+
get "#{remote}/raw/#{branch}/public/chosen-sprite@2x.png", 'images/chosen-sprite@2x.png'
|
12
|
+
get "#{remote}/raw/#{branch}/sass/chosen.scss", 'stylesheets/chosen-base.scss'
|
13
|
+
get "#{remote}/raw/#{branch}/coffee/lib/abstract-chosen.coffee", 'javascripts/lib/abstract-chosen.coffee'
|
14
|
+
get "#{remote}/raw/#{branch}/coffee/lib/select-parser.coffee", 'javascripts/lib/select-parser.coffee'
|
15
|
+
get "#{remote}/raw/#{branch}/coffee/chosen.jquery.coffee", 'javascripts/chosen.jquery.coffee'
|
16
|
+
get "#{remote}/raw/#{branch}/coffee/chosen.proto.coffee", 'javascripts/chosen.proto.coffee'
|
17
|
+
get "#{remote}/raw/#{branch}/package.json", 'package.json'
|
18
|
+
bump_version
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'eject class from closure', 'eject javascript library class from closure'
|
22
|
+
def eject_javascript_class_from_closure
|
23
|
+
self.destination_root = 'vendor/assets'
|
24
|
+
inside destination_root do
|
25
|
+
append_to_file 'javascripts/lib/abstract-chosen.coffee' do
|
26
|
+
"\nwindow.AbstractChosen = AbstractChosen\n"
|
27
|
+
end
|
28
|
+
append_to_file 'javascripts/lib/select-parser.coffee' do
|
29
|
+
"\n\nwindow.SelectParser = SelectParser\n"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'remove compass lines', 'remove compass lines'
|
35
|
+
def remove_compass_lines
|
36
|
+
self.destination_root = 'vendor/assets'
|
37
|
+
gsub_file 'stylesheets/chosen-base.scss', /^\s*\@include.*\n/, ''
|
38
|
+
gsub_file 'stylesheets/chosen-base.scss', /^\@import.*\n/, ''
|
39
|
+
# gsub_file 'stylesheets/chosen-base.scss', /\n(\$chosen-sprite:)/, '\1'
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'add depend_on_asset', 'add depend_on_asset'
|
43
|
+
def add_depend_on_asset
|
44
|
+
self.destination_root = 'vendor/assets'
|
45
|
+
scss = <<-SCSS.gsub(/^\s{6}/, '')
|
46
|
+
//= depend_on_asset "chosen-sprite.png"
|
47
|
+
//= depend_on_asset "chosen-sprite@2x.png"
|
48
|
+
SCSS
|
49
|
+
prepend_to_file 'stylesheets/chosen-base.scss', scss
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'change url to image url', 'change url to image url'
|
53
|
+
def change_url_to_image_url
|
54
|
+
self.destination_root = 'vendor/assets'
|
55
|
+
gsub_file 'stylesheets/chosen-base.scss', /url/, 'image-url'
|
56
|
+
end
|
57
|
+
|
58
|
+
desc 'clean up useless files', 'clean up useless files'
|
59
|
+
def cleanup
|
60
|
+
self.destination_root = 'vendor/assets'
|
61
|
+
remove_file 'package.json'
|
62
|
+
end
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def bump_version
|
67
|
+
inside destination_root do
|
68
|
+
package_json = JSON.load(File.open('package.json'))
|
69
|
+
version = package_json['version']
|
70
|
+
gsub_file '../../lib/chosen-rails/version.rb', /CHOSEN_VERSION\s=\s'(\d|\.)+'$/ do |match|
|
71
|
+
%Q{CHOSEN_VERSION = '#{version}'}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|