优化用户申请加入项目界面刷新

This commit is contained in:
nwb 2014-05-21 19:30:49 +08:00
parent 104ee13ac7
commit 1d6816419d
5 changed files with 44 additions and 12 deletions

View File

@ -2,15 +2,28 @@ class AppliedProjectController < ApplicationController
#申请加入项目 #申请加入项目
def applied_join_project 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]) 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 end
#取消申请 #取消申请
def unapplied_join_project def unapplied_join_project
@project = Project.find(params[:project_id])
@applied = AppliedProject.find(params[:id]) @applied = AppliedProject.find(params[:id])
@applied.destroy @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
end end

View File

@ -1,14 +1,21 @@
module AppliedProjectHelper module AppliedProjectHelper
def applied_css(project)
id = project.id
"#{project.class.to_s.underscore}-#{id}-applied"
end
def applied_link(project, user, options=[]) def applied_link(project, user, options=[])
return '' unless user && user.logged? return '' unless user && user.logged?
applied = project.applied_projects.find_by_user_id(user.id) applied = project.applied_projects.find_by_user_id(user.id)
text = applied ? l(:label_unapply_project) : l(:label_apply_project) 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 if applied
appliedid = applied.id appliedid = applied.id
end end
url = appliedproject_path( url = appliedproject_path(
:id=>appliedid, :id=>appliedid,
:user_id => user.id, :user_id => user.id,
@ -16,6 +23,6 @@ module AppliedProjectHelper
) )
method = applied ? 'delete' : 'post' 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 end

View File

@ -30,7 +30,7 @@ module WatchersHelper
objects = Array.wrap(objects) objects = Array.wrap(objects)
watched = objects.any? {|object| object.watched_by?(user)} 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) : 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) ([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.join.html_safe
end end
def applied_css(project)
id = project.id
"#{project.class.to_s.underscore}-#{id}-applied"
end
def applied_link(project, user, options=[]) def applied_link(project, user, options=[])
return '' unless user && user.logged? return '' unless user && user.logged?
applied = project.applied_projects.find_by_user_id(user.id) applied = project.applied_projects.find_by_user_id(user.id)
text = applied ? l(:label_unapply_project) : l(:label_apply_project) 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 if applied
appliedid = applied.id appliedid = applied.id
end end
url = appliedproject_path(
url = appliedproject_path(
:id=>appliedid, :id=>appliedid,
:user_id => user.id, :user_id => user.id,
:project_id => project.id :project_id => project.id
) )
method = applied ? 'delete' : 'post' 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
end

View File

@ -0,0 +1,4 @@
<% selector = ".#{applied_css(@project)}" %>
$("<%= selector %>").each(function(){$(this).replaceWith("<%= escape_javascript applied_link(@project, User.current) %>")});

View File

@ -105,9 +105,9 @@
</div> </div>
<!--添加项目申请--> <!--添加项目申请-->
<div style="margin-left: 20px;"> <div style="margin-left: 20px;">
<% if ( !(User.current.member_of? @project) && User.current.login?) %>
<% if ( !(User.current.member_of? @project) && User.current.login?) %> <!--added by linchun--> <span class="icon-fav icon"></span>
<span class="icon-fav icon"></span><%= applied_link(@project, User.current) %> <%= applied_link(@project, User.current) %>
<% end %> <% end %>
</div> </div>