rails_admin_tag_list 0.1.5 → 0.2.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 614ab64869f76d073241333bd070eea676e8daa2
4
+ data.tar.gz: 1c90897236400f2e6222b7ef1ba7becce56e69a4
5
+ SHA512:
6
+ metadata.gz: 0ada63826b3c409b79da17fabd82c232fe624be1e60172e360bb959ff0c9340017ff06c228c33094a194893b42570c4842654fcf764bfe951aa7a7a1e97f1247
7
+ data.tar.gz: 2f43199002c2f365b609205c8c903940df012f892973a1f6be92837b560e7c53456e1973d3887c8540f39c3772a57b0819637caf09b9f0a70c39187cda6603b0
data/README.md CHANGED
@@ -43,13 +43,18 @@ There is your model:
43
43
 
44
44
  > `attr_accessible :tag_list, :skill_list`
45
45
 
46
- This gem comes with two tag field partial's named `form_tag_list` (default) and `tag_list_with_suggestions`. You can try the second one:
46
+ **edit field view**
47
+
48
+ In addition to default field view (named `form_tag_list`) this gem provides two custom views `tag_list_with_suggestions` and `tag_list_with_autocomplete`. To enable any of them specify partial name:
47
49
 
48
50
  RailsAdmin.config do |config|
49
- config.models do
51
+ config.model Player do
50
52
  edit do
51
53
  fields_of_type :tag_list do
52
54
  partial 'tag_list_with_suggestions'
55
+
56
+ # the option sets max count of suggestions (default is 100); set -1 to abolish the limit
57
+ ratl_max_suggestions -1
53
58
  end
54
59
  end
55
60
  end
@@ -1,30 +1,47 @@
1
1
  module RailsAdminTagList
2
2
  module SuggestionsHelper
3
- def tag_suggestions(field, options = {})
4
- defaults = {
5
- :order => { :count => :desc },
6
- :length => 5
7
- }
8
- options = defaults.deep_merge(options)
3
+ def ratl_suggestions(field, options={})
4
+ if tags_from_enum = ratl_tags_from_enum(field)
5
+ tags_from_enum
6
+ else
7
+ options = options.merge(max_count: field.ratl_max_suggestions)
9
8
 
10
- model = field.abstract_model.model_name.constantize
11
- tags_name = field.name.to_s.gsub(/_list/, '').pluralize.to_sym
12
- tags = model.tag_counts_on(tags_name)
13
- tags = sort_tags(tags, options[:order])
14
- tags[0..options[:length]].map(&:name)
9
+ tags = ratl_tags(field)
10
+ sorted_tags = ratl_sort_tags(tags, options[:order])
11
+ sorted_tags[0..options[:max_count]].map(&:name)
12
+ end
15
13
  end
16
14
 
17
15
  private
18
- def sort_tags(tags, options)
19
- if options.is_a?(Hash)
20
- if options[:count]
21
- tags = tags.sort_by(&:count)
22
- tags = tags.reverse if options[:count] == :desc
23
- end
24
- elsif [:rand, :random, :shuffle].include?(options)
25
- tags = tags.shuffle
16
+
17
+ def ratl_tags_from_enum(field)
18
+ record = field.bindings[:object]
19
+ enum_method = '%s_enum' % field.name
20
+ if record.respond_to? enum_method
21
+ record.send(enum_method)
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ def ratl_tags(field)
28
+ model = field.abstract_model.model_name.constantize
29
+ tags_name = field.name.to_s.gsub(/_list/, '').pluralize.to_sym
30
+
31
+ model.tag_counts_on(tags_name)
32
+ end
33
+
34
+ def ratl_sort_tags(tags, order)
35
+ case order.to_sym
36
+ when :count
37
+ tags.sort_by { |tag| -tag.count }
38
+ when :name
39
+ tags.sort_by { |tag| tag.name }
40
+ when :rand, :random, :shuffle
41
+ tags.shuffle
42
+ else
43
+ raise ArgumentError.new("RailsAdminTagList field unrecognized sorting order")
26
44
  end
27
- tags
28
45
  end
29
46
  end
30
47
  end
@@ -1 +1 @@
1
- = form.send field.view_helper, field.method_name, field.html_attributes
1
+ = form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, class: 'form-control', required: field.required})
@@ -0,0 +1,45 @@
1
+ = form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, class: 'form-control', required: field.required})
2
+
3
+ :javascript
4
+ $(function() {
5
+ var availableTags = #{ratl_suggestions(field, order: :name)};
6
+
7
+ function split( val ) {
8
+ return val.split( /,\s*/ );
9
+ }
10
+ function extractLast( term ) {
11
+ return split( term ).pop();
12
+ }
13
+
14
+ $( "##{form.dom_id(field)}" )
15
+ // don't navigate away from the field on tab when selecting an item
16
+ .bind( "keydown", function( event ) {
17
+ if ( event.keyCode === $.ui.keyCode.TAB &&
18
+ $( this ).data( "ui-autocomplete" ).menu.active ) {
19
+ event.preventDefault();
20
+ }
21
+ })
22
+ .autocomplete({
23
+ minLength: 0,
24
+ source: function( request, response ) {
25
+ // delegate back to autocomplete, but extract the last term
26
+ response( $.ui.autocomplete.filter(
27
+ availableTags, extractLast( request.term ) ) );
28
+ },
29
+ focus: function() {
30
+ // prevent value inserted on focus
31
+ return false;
32
+ },
33
+ select: function( event, ui ) {
34
+ var terms = split( this.value );
35
+ // remove the current input
36
+ terms.pop();
37
+ // add the selected item
38
+ terms.push( ui.item.value );
39
+ // add placeholder to get the comma-and-space at the end
40
+ terms.push( "" );
41
+ this.value = terms.join( ", " );
42
+ return false;
43
+ }
44
+ });
45
+ });
@@ -1,7 +1,7 @@
1
- = form.send field.view_helper, field.method_name, field.html_attributes
1
+ = form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, class: 'form-control', required: field.required})
2
2
 
3
- - tag_suggestions(field, :length => 5, :order => { :count => :desc }).each do |tag|
4
- = link_to tag, '#', :class => 'tag_suggestion', :data => { :input_id => form.dom_id(field) }
3
+ - ratl_suggestions(field, order: :count).each do |tag|
4
+ = link_to %{<i class="icon-tag"></i> #{tag}}.html_safe, '#', class: 'tag_suggestion', data: { input_id: form.dom_id(field), tag_value: tag }
5
5
 
6
6
  :javascript
7
7
  jQuery(function(){
@@ -13,7 +13,7 @@
13
13
  values = jQuery.map(tag_list.val().split(','), function(val){ return jQuery.trim(val) });
14
14
  // Remove empty strings from values
15
15
  values = jQuery.grep(values, function(n) { return n.length > 0 });
16
- tag = $(this).text();
16
+ tag = $(this).data('tag-value');
17
17
  if (jQuery.inArray(tag, values) == -1) {
18
18
  values.push(tag);
19
19
  } else {
@@ -26,12 +26,20 @@
26
26
  });
27
27
  });
28
28
 
29
- %style
30
- :sass
31
- .tag_suggestion
32
- color: #777
33
- border-bottom: 1px dotted #b94a48
34
- &:hover
35
- color: #aaa
36
- border-bottom: 1px dotted #953b39
37
- text-decoration: none
29
+ :sass
30
+ .tag_list_type
31
+ input
32
+ display: inline-block
33
+ margin-right: 10px
34
+
35
+ .tag_suggestion
36
+ margin: 0 5px
37
+ border-bottom: 1px dotted #b94a48
38
+ color: #777
39
+ text-decoration: none
40
+ &:hover
41
+ color: #aaa
42
+ border-bottom: 1px dotted #953b39
43
+ text-decoration: none
44
+ &:focus
45
+ text-decoration: none
@@ -19,15 +19,17 @@ module RailsAdmin
19
19
  register_instance_option(:pretty_value) do
20
20
  value.join(', ')
21
21
  end
22
+
22
23
  # Accessor for field's label.
23
24
  #
24
25
  # @see RailsAdmin::AbstractModel.properties
25
26
  register_instance_option(:help) do
26
- I18n.t(:tag_list_help, :scope => [:admin, :new], :default => 'Use commas to separate tags')
27
- end
28
- register_instance_option(:partial) do
29
- :form_tag_list
27
+ I18n.t(:tag_list_help, scope: [:admin, :new], default: 'Use commas to separate tags')
30
28
  end
29
+
30
+ register_instance_option(:partial) { :form_tag_list }
31
+
32
+ register_instance_option(:ratl_max_suggestions) { 100 }
31
33
  end
32
34
  end
33
35
  end
@@ -39,9 +41,10 @@ RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
39
41
 
40
42
  if defined?(::ActsAsTaggableOn) && model.taggable?
41
43
  tag_types = model.tag_types
42
- if tag_types.include?(properties[:name])
43
- name = "#{properties[:name].to_s.singularize}_list".to_sym
44
+ property_name = properties.respond_to?(:name) ? properties.name : properties[:name]
44
45
 
46
+ if tag_types.include?(property_name)
47
+ name = "#{property_name.to_s.singularize}_list".to_sym
45
48
  fields << RailsAdmin::Config::Fields::Types::TagList.new(parent, name, properties)
46
49
  end
47
50
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminTagList
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,62 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_tag_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrey Samsonov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
11
+ date: 2016-01-09 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rails
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '3.1'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '3.1'
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: rails_admin
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
- - - ! '>='
17
+ - - ">="
36
18
  - !ruby/object:Gem::Version
37
- version: 0.0.1
19
+ version: '0.6'
38
20
  type: :runtime
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
- - - ! '>='
24
+ - - ">="
44
25
  - !ruby/object:Gem::Version
45
- version: 0.0.1
26
+ version: '0.6'
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: sqlite3
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
30
  requirements:
51
- - - ! '>='
31
+ - - ">="
52
32
  - !ruby/object:Gem::Version
53
33
  version: '0'
54
34
  type: :development
55
35
  prerelease: false
56
36
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
37
  requirements:
59
- - - ! '>='
38
+ - - ">="
60
39
  - !ruby/object:Gem::Version
61
40
  version: '0'
62
41
  description: Implements field type and partial for "tag_list"(*_list) virtual attribute.
@@ -66,37 +45,38 @@ executables: []
66
45
  extensions: []
67
46
  extra_rdoc_files: []
68
47
  files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
69
51
  - app/helpers/rails_admin_tag_list/suggestions_helper.rb
70
- - app/views/rails_admin/main/_tag_list_with_suggestions.html.haml
71
52
  - app/views/rails_admin/main/_form_tag_list.html.haml
53
+ - app/views/rails_admin/main/_tag_list_with_autocomplete.html.haml
54
+ - app/views/rails_admin/main/_tag_list_with_suggestions.html.haml
55
+ - lib/rails_admin_tag_list.rb
72
56
  - lib/rails_admin_tag_list/engine.rb
73
57
  - lib/rails_admin_tag_list/version.rb
74
- - lib/rails_admin_tag_list.rb
75
- - MIT-LICENSE
76
- - Rakefile
77
- - README.md
78
58
  homepage: https://github.com/kryzhovnik/rails_admin_tag_list
79
59
  licenses: []
60
+ metadata: {}
80
61
  post_install_message:
81
62
  rdoc_options: []
82
63
  require_paths:
83
64
  - lib
84
65
  required_ruby_version: !ruby/object:Gem::Requirement
85
- none: false
86
66
  requirements:
87
- - - ! '>='
67
+ - - ">="
88
68
  - !ruby/object:Gem::Version
89
69
  version: '0'
90
70
  required_rubygems_version: !ruby/object:Gem::Requirement
91
- none: false
92
71
  requirements:
93
- - - ! '>='
72
+ - - ">="
94
73
  - !ruby/object:Gem::Version
95
74
  version: '0'
96
75
  requirements: []
97
76
  rubyforge_project:
98
- rubygems_version: 1.8.24
77
+ rubygems_version: 2.4.5.1
99
78
  signing_key:
100
- specification_version: 3
79
+ specification_version: 4
101
80
  summary: Support acts_as_taggable_on gem for rails_admin
102
81
  test_files: []
82
+ has_rdoc: