Skip to content
Browse files

tag_list_with_suggestions partial added. version is bumped

  • Loading branch information...
1 parent a4fc763 commit ba14eb840a24e81131818e928eead01dd464aed1 @kryzhovnik committed Apr 4, 2012
View
14 README.md
@@ -43,7 +43,19 @@ There is your model:
> > `attr_accessible :tag_list, :skill_list`
-You can do whatever what allows to do by the rails_admin:
+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:
+
+ RailsAdmin.config do |config|
+ config.models do
+ edit do
+ fields_of_type :tag_list do
+ partial 'tag_list_with_suggestions'
+ end
+ end
+ end
+ end
+
+You can do with tag_list fields whatever what allows to do rails_admin:
**rename lable**
View
30 app/helpers/rails_admin_tag_list/suggestions_helper.rb
@@ -0,0 +1,30 @@
+module RailsAdminTagList
+ module SuggestionsHelper
+ def tag_suggestions(field, options = {})
+ defaults = {
+ :order => { :count => :desc },
+ :length => 5
+ }
+ options = defaults.deep_merge(options)
+
+ model = field.abstract_model.model_name.constantize
+ tags_name = field.name.to_s.gsub(/_list/, '').pluralize.to_sym
+ tags = model.tag_counts_on(tags_name)
+ tags = sort_tags(tags, options[:order])
+ tags[0..options[:length]].map(&:name)
+ end
+
+ private
+ def sort_tags(tags, options)
+ if options.is_a?(Hash)
+ if options[:count]
+ tags = tags.sort_by(&:count)
+ tags = tags.reverse if options[:count] == :desc
+ end
+ elsif [:rand, :random, :shuffle].include?(options)
+ tags = tags.shuffle
+ end
+ tags
+ end
+ end
+end
View
33 app/views/rails_admin/main/_tag_list_with_suggestions.html.haml
@@ -0,0 +1,33 @@
+= form.send field.view_helper, field.method_name, field.html_attributes
+
+- tag_suggestions(field, :length => 5, :order => { :count => :desc }).each do |tag|
+ = link_to tag, '#', :class => 'tag_suggestion', :data => { :input_id => form.dom_id(field) }
+
+:javascript
+ jQuery(function(){
+ var input_id = '#{form.dom_id(field)}'
+ $('.tag_suggestion[data-input-id=' + input_id + ']').click(function(event){
+ var tag_list, new_value;
+ tag_list = $(this).siblings('input#' + input_id);
+
+ if (!tag_list.val().match(/\S/)) {
+ new_value = this.innerHTML;
+ } else {
+ new_value = [tag_list.val(), this.innerHTML].join(', ');
+ };
+ tag_list.val(new_value);
+
+ event.preventDefault();
+ return false;
+ });
+ });
+
+%style
+ :sass
+ .tag_suggestion
+ color: #777
+ border-bottom: 1px dotted #b94a48
+ &:hover
+ color: #aaa
+ border-bottom: 1px dotted #953b39
+ text-decoration: none
View
2 lib/rails_admin_tag_list/version.rb
@@ -1,3 +1,3 @@
module RailsAdminTagList
- VERSION = "0.1.0"
+ VERSION = "0.1.1"
end

0 comments on commit ba14eb8

Please sign in to comment.
Something went wrong with that request. Please try again.