Added new autocomplete for task assignments.

Introduced the cool jquery token plugin.
This commit is contained in:
benni 2011-05-14 19:41:46 +02:00
parent 2e56bfe895
commit 5d028ccf3a
10 changed files with 880 additions and 13 deletions

View file

@ -0,0 +1,11 @@
class UsersController < ApplicationController
# Currently used to display users nick and ids for autocomplete
def index
@users = User.where("nick LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @users.map { |u| {:id => u.id, :name => u.nick} } }
end
end
end

View file

@ -0,0 +1,2 @@
module UsersHelper
end

View file

@ -27,13 +27,13 @@ class Task < ActiveRecord::Base
def enough_users_assigned?
assignments.find_all_by_accepted(true).size >= required_users ? true : false
end
# extracts nicknames from a comma seperated string
# Get users from comma seperated ids
# and makes the users responsible for the task
def user_list=(string)
@user_list = string.split(%r{,\s*})
new_users = @user_list - users.collect(&:nick)
old_users = users.reject { |user| @user_list.include?(user.nick) }
def user_list=(ids)
list = ids.split(",")
new_users = list - users.collect(&:id)
old_users = users.reject { |user| list.include?(user.id) }
logger.debug "New users: #{new_users}"
logger.debug "Old users: #{old_users}"
@ -44,8 +44,8 @@ class Task < ActiveRecord::Base
assignments.find(:all, :conditions => ["user_id IN (?)", old_users.collect(&:id)]).each(&:destroy)
end
# create new assignments
new_users.each do |nick|
user = User.find_by_nick(nick)
new_users.each do |id|
user = User.find(id)
if user.blank?
errors.add(:user_list)
else
@ -62,7 +62,7 @@ class Task < ActiveRecord::Base
end
def user_list
@user_list ||= users.collect(&:nick).join(", ")
@user_list ||= users.collect(&:id).join(", ")
end
private

View file

@ -3,12 +3,12 @@
%head
%meta{"http-equiv" => "content-type", :content => "text/html;charset=UTF-8"}
%title= "FoodSoft - " + (yield(:title) or controller.controller_name)
= stylesheet_link_tag 'main', 'rails_messages', 'nav', 'simple_form', :cache => "all_cached"
= stylesheet_link_tag 'main', 'rails_messages', 'nav', 'simple_form', 'token-input', :cache => "all_cached"
= stylesheet_link_tag "print", :media => "print"
<!--[if lte IE 7]>
= stylesheet_link_tag 'ie_hacks'
<![endif]-->
= javascript_include_tag 'jquery.min', 'jquery-ui.min', 'jquery_ujs', 'application', 'ordering', :cache => "all_cached"
= javascript_include_tag 'jquery.min', 'jquery-ui.min', 'jquery_ujs', 'jquery.tokeninput', 'application', 'ordering', :cache => "all_cached"
= yield(:head)
%body
#logininfo= render :partial => 'shared/loginInfo'

View file

@ -1,8 +1,17 @@
- content_for :head do
:javascript
$(function() {
$("#task_user_list").tokenInput("#{users_path(:format => :json)}", {
crossDomain: false,
prePopulate: $("#task_user_list").data("pre")
});
});
= simple_form_for @task do |f|
= f.input :name
= f.input :description
= f.input :duration, :as => :select, :collection => 1..3
= f.input :user_list, :as => :string
= f.input :user_list, :as => :string, :input_html => { 'data-pre' => @task.users.map { |u| {:id => u.id, :name => u.nick} }.to_json }
= f.input :required_users
= f.association :workgroup
= f.input :due_date, :include_blank => true