Permalink
Browse files

fix rails 3 bugs #3

  • Loading branch information...
1 parent fb1a1e7 commit c5690559e4bc68488432b2ec8fcab755d726aeea @edmagne edmagne committed Nov 1, 2012
View
@@ -4,7 +4,7 @@ A redmine plugin to elect issues.
== Compatibility
-* Compatible with redmine 2.0.3
+* Compatible with redmine 2.1.2
* For redmine 1.3.* go to https://github.com/dextra/redmine_issues_poll/tree/old
== Features
@@ -1,34 +1,32 @@
# Copyright 2011/2012 Dextra Sistemas
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-
class PollsController < ApplicationController
unloadable
-
+
before_filter :find_project, :authorize
before_filter :get_statuses, :only => [:index, :set_statuses]
-
def get_statuses
@statuses = IssueStatus.find(:all, :order => 'position')
@eligible_statuses = EligibleStatus.find(:all).collect{ |status| status.status_id }
end
-
+
def index
@users = @project.users.collect {|user| {'id' => user.id, 'login' => user.login, 'fullname' => user.name(:firstname_lastname), 'votes' => user.polls_votes_project(@project.id)? user.polls_votes_project(@project.id) : '--'}}
@login = {'order' => 'asc', 'class' => ''}
@name = {'order' => 'asc', 'class' => ''}
@votes = {'order' => 'asc', 'class' => ''}
-
+
if params[:order_login]
if params[:order_login] == "asc"
@login = {'order' => 'desc', 'class' => 'sort asc'}
@@ -58,7 +56,7 @@ def index
@users = @users.sort{|a, b| a['login'] <=> b['login'] }
end
end
-
+
def set_statuses
EligibleStatus.delete_all
if params[:statuses]
@@ -67,7 +65,7 @@ def set_statuses
flash[:notice] = t(:configuration_successful)
redirect_to :action => 'index', :tab => 'config'
end
-
+
def set_votes
votes = params[:votes]
if /^\d+$/.match(votes)
@@ -85,71 +83,69 @@ def set_votes
end
redirect_to :action => 'index', :tab => 'config'
end
-
+
def update_votes
- votes = params[:votes]
- render :update do |page|
- if /^\d+$/.match(votes)
- user = User.find(params[:user_id])
- current_poll_votes = user.poll_votes.find(:first, :conditions => ["project_id = ?", @project.id])
- if current_poll_votes
- current_poll_votes.update_attribute(:votes, params[:votes])
- else
- user.poll_votes << @project.poll_votes.new(:votes => votes)
- end
- page.replace_html "view_votes_#{user.id}", votes
- page.call('toggleForm', user.id)
- page.alert(t(:assignment_successful))
+ @votes = params[:votes]
+ @success = false
+ if /^\d+$/.match(@votes)
+ @user = User.find(params[:user_id])
+ current_poll_votes = @user.poll_votes.find(:first, :conditions => ["project_id = ?", @project.id])
+ if current_poll_votes
+ current_poll_votes.update_attribute(:votes, params[:votes])
else
- page.alert(t(:invalid_votes))
+ @user.poll_votes << @project.poll_votes.new(:votes => @votes)
end
+ @success = true
+ end
+
+ respond_to do |format|
+ format.js
end
+
end
-
+
def bet
@issue = Issue.find(:first, :conditions => ["id = ? AND project_id = ?", params[:issue_id], @project.id])
user = User.current
- render :update do |page|
- @bet = Bet.new(params[:bet])
- @bet.issue_id = @issue.id
- @bet.user_id = user.id
-
- if @bet.save
- poll_vote = user.poll_votes.find(:first, :conditions => ["project_id = ?", @issue.project_id])
- poll_vote.votes -= @bet.votes
- poll_vote.save
- @issue.bet_votes ||= 0
- @issue.bet_votes += @bet.votes
- @issue.save
- page.replace_html :issues_poll_area, :partial => 'hooks/view_polls_form'
- if @bet.votes > 0
- page.alert(t(:bet_successful_created))
- else
- page.alert(t(:bet_successful_canceled))
- end
- else
- page.alert(@bet.errors.full_messages.join('\n'))
+ @bet = Bet.new(params[:bet])
+ @bet.issue_id = @issue.id
+ @bet.user_id = user.id
+ @added = false
+ @success = false
+ if @bet.save
+ poll_vote = user.poll_votes.find(:first, :conditions => ["project_id = ?", @issue.project_id])
+ poll_vote.votes -= @bet.votes
+ poll_vote.save
+ @issue.bet_votes ||= 0
+ @issue.bet_votes += @bet.votes
+ @issue.save
+ @success = true
+ if @bet.votes > 0
+ @added = true
end
end
+ respond_to do |format|
+ format.js
+ end
end
-
+
def cancel_bet
votes = User.current.votes_bet_by_issue(params[:issue_id])
if votes <= 0
- votes = 0
+ votes = 0
else
- votes *= -1
+ votes *= -1
end
params[:bet] = {:votes => votes}
bet
end
-
+
private
def find_project
@project = Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound
render_404
- end
-
+ end
+
end
@@ -31,12 +31,12 @@ limitations under the License. -->
<% if User.current.can_bet?(@issue.project_id, 1) and @issue.can_receive_bet? %>
<%= form_for(
- :bet,
- :remote => true,
- :url => {:controller => 'polls', :action => 'bet', :issue_id => @issue.id, :project_id => @issue.project_id},
- :method => :post,
- :complete => "Form.Element.focus('relation_issue_to_id');",
- :html => {:id => 'issues-polls-form'}) do |f| %>
+ :bet,
+ :remote => true,
+ :url => {:controller => 'polls', :action => 'bet', :issue_id => @issue.id, :project_id => @issue.project_id},
+ :method => :post,
+ :complete => "Form.Element.focus('relation_issue_to_id');",
+ :html => {:id => 'issues-polls-form'}) do |f| %>
<%= f.select :votes, 1..User.current.polls_votes_project(@project.id), {} %>
@@ -62,7 +62,10 @@ limitations under the License. -->
<td> <%= bet.author.name(:firstname_lastname) %> </td>
<td> <%= bet.sum_votes %> </td>
<td> <% if bet.user_id == User.current.id and @issue.can_receive_bet? %>
- <%= link_to_remote t(:link_cancel_bet), :url => {:controller => 'polls', :action => 'cancel_bet', :issue_id => @issue.id, :project_id => @issue.project_id}, :confirm => t(:confirm_cancel_bet) %>
+ <%= link_to t(:link_cancel_bet),
+ url_for(:controller => 'polls', :action => 'cancel_bet', :issue_id => @issue.id, :project_id => @issue.project_id),
+ :confirm => t(:confirm_cancel_bet),
+ :remote => true %>
<% end %> </td>
</tr>
<% end %>
@@ -44,7 +44,7 @@ limitations under the License. -->
<td>
<span id="view_votes_<%=user['id']%>" class="view_votes_toggle" onclick="toggleForm(<%=user['id']%>);"><%= user['votes'] %></span>
<span id="view_form_<%=user['id']%>" class="form_votes_toggle" style="display:none;">
- <%= form_tag :url => {:action => 'update_votes', :user_id => user['id']}, :remote => true do %>
+ <%= form_tag url_for(:action => 'update_votes', :user_id => user['id']), :remote => true do %>
<%= text_field_tag :votes, user['votes']%>
<%= submit_tag t(:label_update_votes) %>
<%= button_to_function t(:button_cancel), "toggleForm(#{user['id']});"%>
@@ -60,20 +60,7 @@ limitations under the License. -->
<script type="text/javascript">
function toggleForm(id) {
- $$('.form_votes_toggle').without($('view_form_'+id)).each(function(e){
- e.hide();
- })
- $$('.view_votes_toggle').without($('view_votes_'+id)).each(function(e){
- e.show();
- })
- var view_votes = $('view_votes_'+id);
- var view_form = $('view_form_'+id);
- if(view_votes.style.display == 'none'){
- view_votes.show();
- view_form.hide();
- } else {
- view_form.show();
- view_votes.hide();
- }
+ $('#view_form_'+id).toggle();
+ $('#view_votes_'+id).toggle();
}
</script>
View
@@ -0,0 +1,11 @@
+$("#issues_poll_area").html("<%= escape_javascript(render :partial => 'hooks/view_polls_form') %>");
+
+<% if @success %>
+ <% if @added %>
+ alert("<%= t(:bet_successful_created) %>");
+ <% else %>
+ alert("<%= t(:bet_successful_canceled) %>");
+ <% end %>
+<% else %>
+ alert("<%= @bet.errors.full_messages.join('\n') %>");
+<% end %>
@@ -0,0 +1,11 @@
+$("#issues_poll_area").html("<%= escape_javascript(render :partial => 'hooks/view_polls_form') %>");
+
+<% if @success %>
+ <% if @added %>
+ alert("<%= t(:bet_successful_created) %>");
+ <% else %>
+ alert("<%= t(:bet_successful_canceled) %>");
+ <% end %>
+<% else %>
+ alert("<%= @bet.errors.full_messages.join('\n') %>");
+<% end %>
@@ -0,0 +1,7 @@
+$("#view_votes_<%=@user.id%>").html("<%= @votes %>");
+<% if @success %>
+ toggleForm("<%= @user.id %>");
+ alert("<%= t(:assignment_successful) %>");
+<% else %>
+ alert("<%= t(:invalid_votes) %>");
+<% end %>
View
@@ -15,8 +15,14 @@
module IssuesPoll
class Hooks < Redmine::Hook::ViewListener
- render_on :view_issues_show_description_bottom, :partial => 'hooks/view_polls_info'
+ def view_issues_show_description_bottom(context={})
+ context[:controller].send(:render_to_string, {
+ :partial => 'hooks/view_polls_info',
+ :locals => context
+ })
+ end
+
def view_layouts_base_html_head(context={})
stylesheet_link_tag 'style', :plugin => 'redmine_issues_poll'
end

0 comments on commit c569055

Please sign in to comment.