Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Updating auto complete fields to use jquery
Browse files Browse the repository at this point in the history
  • Loading branch information
nerakdon committed Sep 7, 2012
1 parent 32db801 commit 07d252d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
10 changes: 8 additions & 2 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ class MessagesController < BaseController
end

def auto_complete_for_username
@users = User.find(:all, :conditions => [ 'LOWER(login) LIKE ?', '%' + (params[:message][:to]) + '%' ])
render :inline => "<%= auto_complete_result(@users, 'login') %>"
@users = User.find_all_by_profile_public(true)
@users_list = []
for user in @users
@users_list << user.login
end
respond_to do |format|
format.json {render :inline => @users_list.to_json}
end
end

def index
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ def cache_action?
end

def auto_complete_for_tag_name
@tags = ActsAsTaggableOn::Tag.find(:all, :limit => 10, :conditions => [ 'LOWER(name) LIKE ?', '%' + (params[:id] || params[:tag_list]) + '%' ])
render :inline => "<%= auto_complete_result(@tags, 'name') %>"
@tags = ActsAsTaggableOn::Tag.find(:all)
@tag_names = []
for tag in @tags
@tag_names << tag.name
end
respond_to do |format|
format.json {render :inline => @tag_names.to_json}
end
end

def index
Expand Down
8 changes: 8 additions & 0 deletions app/helpers/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,15 @@ def icon_link_to(icon_class, content, href)
link_html.html_safe
end

def tag_auto_complete_field(id, options = {})
options[:url][:format] = 'json'
html = ""
html << render(:partial => 'shared/tag_auto_complete', :locals => {:id => id, :options => options})
html.html_safe
end

def auto_complete_field(id, options = {})
options[:url][:format] = 'json'
html = ""
html << render(:partial => 'shared/auto_complete', :locals => {:id => id, :options => options})
html.html_safe
Expand Down
2 changes: 1 addition & 1 deletion app/views/messages/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.auto_complete#message_to_auto_complete

-content_for :end_javascript do
= auto_complete_field 'message_to', {:url => auto_complete_for_username_user_messages_path(user), :tokens=>','}
= auto_complete_field 'message_to', {:url => {:controller => 'messages', :action => 'auto_complete_for_username', :user => user}, :method=>'post'}

-if @reply
= f.hidden_field :subject
Expand Down
15 changes: 13 additions & 2 deletions app/views/shared/_auto_complete.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
-content_for :end_javascript do
=javascript_include_tag 'tag-it/tag-it'
=stylesheet_include_tag 'tag-it/tag-it'
=stylesheet_link_tag 'tag-it/tag-it'
:javascript
alert('Now make it work');
var tags;
$.ajax({
type: '#{options[:method] || 'get'}',
url: '#{url_for options[:url]}',
success: function(data){
values = data;
$('##{id}').autocomplete({
source: values
});
}
});

17 changes: 17 additions & 0 deletions app/views/shared/_tag_auto_complete.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-content_for :end_javascript do
=javascript_include_tag 'tag-it/tag-it'
=stylesheet_link_tag 'tag-it/tag-it'
:javascript
var tags;
$.ajax({
type: '#{options[:method] || 'get'}',
url: '#{url_for options[:url]}',
success: function(data){
values = data;
$('##{id}').tagit({
availableTags: values,
allowSpaces: true
});
}
});

2 changes: 1 addition & 1 deletion app/views/users/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
= f.text_field :tag_list, {:autocomplete => "off"}
#tag_list_auto_complete.auto_complete
-content_for :end_javascript do
= auto_complete_field 'tag_list', {:url => { :controller => "tags", :action => 'auto_complete_for_tag_name'}, :tokens => [','] }
= auto_complete_field 'user_tag_list', {:url => { :controller => "tags", :action => 'auto_complete_for_tag_name'}, :tokens => [','] }

%label.controls
%em
Expand Down

0 comments on commit 07d252d

Please sign in to comment.