From 1d6816419ddd9ab2f59df0dd9e6c4a980bf90766 Mon Sep 17 00:00:00 2001 From: nwb Date: Wed, 21 May 2014 19:30:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E7=94=B3?= =?UTF-8?q?=E8=AF=B7=E5=8A=A0=E5=85=A5=E9=A1=B9=E7=9B=AE=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/applied_project_controller.rb | 17 +++++++++++++++-- app/helpers/applied_project_helper.rb | 11 +++++++++-- app/helpers/watchers_helper.rb | 18 +++++++++++++----- app/views/applied_project/_set_applied.js.erb | 4 ++++ app/views/layouts/base_projects.html.erb | 6 +++--- 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 app/views/applied_project/_set_applied.js.erb diff --git a/app/controllers/applied_project_controller.rb b/app/controllers/applied_project_controller.rb index 2f887963..0bf9394a 100644 --- a/app/controllers/applied_project_controller.rb +++ b/app/controllers/applied_project_controller.rb @@ -2,15 +2,28 @@ class AppliedProjectController < ApplicationController #申请加入项目 def applied_join_project + @user_id = params[:user_id] + @project = Project.find(params[:project_id]) AppliedProject.create(:user_id => params[:user_id], :project_id => params[:project_id]) - redirect_to_referer_or {render :text => ( 'applied success.'), :layout => true} + #redirect_to project_path(params[:project_id]) + #redirect_to_referer_or {render :text => ( 'applied success.'), :layout => true} + respond_to do |format| + format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + format.js { render :partial => 'set_applied'} + end end #取消申请 def unapplied_join_project + @project = Project.find(params[:project_id]) @applied = AppliedProject.find(params[:id]) @applied.destroy - redirect_to_referer_or {render :text => ( 'unsubscribe success.'), :layout => true} + #redirect_to project_path(params[:project_id]) + #redirect_to_referer_or {render :text => ( 'unsubscribe success.'), :layout => true} + respond_to do |format| + format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + format.js { render :partial => 'set_applied' } + end end end diff --git a/app/helpers/applied_project_helper.rb b/app/helpers/applied_project_helper.rb index a766976a..eef4c9cc 100644 --- a/app/helpers/applied_project_helper.rb +++ b/app/helpers/applied_project_helper.rb @@ -1,14 +1,21 @@ module AppliedProjectHelper + def applied_css(project) + id = project.id + "#{project.class.to_s.underscore}-#{id}-applied" + end + def applied_link(project, user, options=[]) return '' unless user && user.logged? applied = project.applied_projects.find_by_user_id(user.id) text = applied ? l(:label_unapply_project) : l(:label_apply_project) + @applied_flag = project.instance_of?(Project) + css = @applied_flag ? ([applied_css(project), applied ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : + ([applied_css(project), applied ? 'icon icon-applied ' : 'icon icon-applied-off '].join(' ') << options[0].to_s) if applied appliedid = applied.id end - url = appliedproject_path( :id=>appliedid, :user_id => user.id, @@ -16,6 +23,6 @@ module AppliedProjectHelper ) method = applied ? 'delete' : 'post' - link_to text, url, :remote => true, :method => method + link_to text, url, :remote => true, :method => method ,:class=>css end end diff --git a/app/helpers/watchers_helper.rb b/app/helpers/watchers_helper.rb index 1d7e2bcf..50170ee9 100644 --- a/app/helpers/watchers_helper.rb +++ b/app/helpers/watchers_helper.rb @@ -30,7 +30,7 @@ module WatchersHelper objects = Array.wrap(objects) watched = objects.any? {|object| object.watched_by?(user)} - @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or objects.first.instance_of?(Contest) or (objects.first.instance_of?(Bid))) + @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or objects.first.instance_of?(Contest) or (objects.first.instance_of?(Bid))) css = @watch_flag ? ([watcher_css(objects), watched ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : ([watcher_css(objects), watched ? 'icon icon-fav ' : 'icon icon-fav-off '].join(' ') << options[0].to_s) @@ -188,22 +188,30 @@ module WatchersHelper end.join.html_safe end + def applied_css(project) + id = project.id + "#{project.class.to_s.underscore}-#{id}-applied" + end + def applied_link(project, user, options=[]) return '' unless user && user.logged? applied = project.applied_projects.find_by_user_id(user.id) text = applied ? l(:label_unapply_project) : l(:label_apply_project) + @applied_flag = project.instance_of?(Project) + css = @applied_flag ? ([applied_css(project), applied ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : + ([applied_css(project), applied ? 'icon icon-applied ' : 'icon icon-applied-off '].join(' ') << options[0].to_s) if applied appliedid = applied.id end - - url = appliedproject_path( + url = appliedproject_path( :id=>appliedid, :user_id => user.id, :project_id => project.id ) method = applied ? 'delete' : 'post' - link_to text, url, :remote => true, :method => method + link_to text, url, :remote => true, :method => method ,:class=>css end -end + + end diff --git a/app/views/applied_project/_set_applied.js.erb b/app/views/applied_project/_set_applied.js.erb new file mode 100644 index 00000000..e7759206 --- /dev/null +++ b/app/views/applied_project/_set_applied.js.erb @@ -0,0 +1,4 @@ +<% selector = ".#{applied_css(@project)}" %> +$("<%= selector %>").each(function(){$(this).replaceWith("<%= escape_javascript applied_link(@project, User.current) %>")}); + + diff --git a/app/views/layouts/base_projects.html.erb b/app/views/layouts/base_projects.html.erb index 8d4de3a7..653b5ae5 100644 --- a/app/views/layouts/base_projects.html.erb +++ b/app/views/layouts/base_projects.html.erb @@ -105,9 +105,9 @@
- - <% if ( !(User.current.member_of? @project) && User.current.login?) %> - <%= applied_link(@project, User.current) %> + <% if ( !(User.current.member_of? @project) && User.current.login?) %> + + <%= applied_link(@project, User.current) %> <% end %>