From 72ec6ae54faf8e48b8dee6c3a99ac89a7e4af722 Mon Sep 17 00:00:00 2001 From: Michel Felipe Date: Wed, 3 Jun 2015 17:52:33 -0300 Subject: [PATCH] Refactor in tasks.js to create a singleton 'Task', show success icon after save each tag and tasks.css changes --- app/controllers/my_profile/tasks_controller.rb | 12 +++++++++--- app/views/tasks/index.html.erb | 50 ++------------------------------------------------ public/javascripts/tasks.js | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- public/stylesheets/tasks.css | 23 ++++++++++++++++++++++- test/functional/tasks_controller_test.rb | 3 +++ 5 files changed, 140 insertions(+), 53 deletions(-) diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 4c6ca89..29fb7a3 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -94,7 +94,6 @@ class TasksController < MyProfileController end def save_tags - if request.post? && params[:tag_list] result = { success: false, @@ -103,7 +102,7 @@ class TasksController < MyProfileController ActsAsTaggableOn.remove_unused_tags = true - task = profile.tasks.find_by_id params[:task_id] + task = Task.to(profile).find_by_id params[:task_id] save = user.tag(task, with: params[:tag_list], on: :tags) if save @@ -114,15 +113,22 @@ class TasksController < MyProfileController render json: result end - #FIXME make this test + # FIXME make this test # Should not search for article tasks # Should not search for other profile tags # Should search only task tags # Should check the permissions + def search_tags + arg = params[:term].downcase + result = ActsAsTaggableOn::Tag.find(:all, :conditions => ['LOWER(name) LIKE ?', "%#{arg}%"]) + render :text => prepare_to_token_input_by_label(result).to_json, :content_type => 'application/json' + end + end + end diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index fe0ae1a..bd311b6 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -94,53 +94,7 @@ <%= javascript_include_tag 'tasks' %> diff --git a/public/javascripts/tasks.js b/public/javascripts/tasks.js index 3a2947c..597dee2 100644 --- a/public/javascripts/tasks.js +++ b/public/javascripts/tasks.js @@ -1,5 +1,109 @@ (function($) { + /** + * @class Task singleton created with module pattern + */ + Task = (function(){ + + var _showError = function(context,response){ + + var errorIcon = $('',{ + 'class':'ui-icon ui-icon-alert', + style:'float: left; margin-right: .3em;' + }); + + var content = $('

',{ + html:''+response.message+'' + }).prepend(errorIcon); + + var div = $('

',{ + 'class':'alert fg-state-error ui-state-error' + }).append(content); + + context.element.parents('.task_box').before(div); + + }; + + var _showSuccess = function(context,response){ + _addIcon(context,'ok'); + + setTimeout(function(){ + $('.ok').parent().remove(); + },1000); + }; + + var _addIcon = function(context,className){ + $('.'+className).parent().remove(); + + var item = $('
  • ',{ + 'class':'inputosaurus-input tag-saved', + html:'' + }); + + if(className == 'ok'){ + $('.loading').parent().remove(); + } + context.elements.input.parent().before(item); + } + + return { + + /** + * @see inputosaurus#_sendTags The 'this' context here is the jquery ui widget component + */ + onAddTag: function(response){ + this.element.parents('.task_box') + .prev('.fg-state-error') + .remove(); + + if(response.success){ + + _showSuccess(this,response); + }else{ + + _showError(this,response); + } + }, + showTags: function(cfg){ + + jQuery('.filter-tags').inputosaurus({ + hideInput: true + }); + + $('#filter-add-tag').change(function(){ + + if($(this).val() != ''){ + jQuery('.filter-tags').inputosaurus('addTags',$(this).children(':selected').text()); + } + }); + + jQuery('.tag-list').inputosaurus({ + autoCompleteSource: '/myprofile/'+cfg.profileIdentifier+'/tasks/search_tags', + activateFinalResult: true, + submitTags: { + url: '/myprofile/'+cfg.profileIdentifier+'/tasks/save_tags', + beforeSend: function(){ + + $('.ok').parent().remove(); + + this.element.parents('.task_box') + .prev('.fg-state-error') + .remove(); + + _addIcon(this,'loading'); + + //Add loading here! + }, + success: this.onAddTag + } + }); + + } + + }; + + })(); + $("input.task_accept_radio").click(function(){ task_id = this.getAttribute("task_id"); $('#on-accept-information-' + task_id).show('fast'); @@ -62,4 +166,3 @@ function change_task_responsible(el) { } }); } - diff --git a/public/stylesheets/tasks.css b/public/stylesheets/tasks.css index 1af513b..1acad7c 100644 --- a/public/stylesheets/tasks.css +++ b/public/stylesheets/tasks.css @@ -61,8 +61,9 @@ margin: 0 5px 0 10px; } -.formfieldline .inputosaurus-container { +.inputosaurus-container { vertical-align: middle; + margin-top: -2px; } .tag-list-fields input { @@ -73,3 +74,23 @@ background: url('/images/loading-small.gif') right center no-repeat !important; background-color: #fff !important; } + +.tag-saved { + width: 24px; + height: 24px; + background: none !important; +} + +.ok { + position: absolute; + width: 24px; + height: 24px; + background: url('/images/ok.png') right center no-repeat +} + +.loading { + position: absolute; + width: 24px; + height: 24px; + background: url('/images/loading-small.gif') right center no-repeat +} diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 0c24051..049d121 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -684,6 +684,9 @@ class TasksControllerTest < ActionController::TestCase assert_not_includes task_one.tags_from(nil), 'test' end +#region_validators_controller_test.rb: give_permission('ze', 'manage_environment_validators', environment) +#profile_editor_controller_test.rb: user2.stubs(:has_permission?).with('edit_profile', anything).returns(true) +#profile_editor_controller_test.rb: user2.expects(:has_permission?).with(:manage_friends, anything).returns(true) should 'not tag task with permission but another user' do requestor = fast_create(Person) -- libgit2 0.21.2