diff --git a/app/controllers/applied_project_controller.rb b/app/controllers/applied_project_controller.rb index 2f8879636..0bf9394aa 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 a766976a4..eef4c9ccf 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 1d7e2bcf9..50170ee9e 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 000000000..e77592060 --- /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 8d4de3a7c..653b5ae5d 100644 --- a/app/views/layouts/base_projects.html.erb +++ b/app/views/layouts/base_projects.html.erb @@ -105,9 +105,9 @@