Skip to content

Commit

Permalink
fix rails 3 bugs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
edmagne committed Nov 1, 2012
1 parent fb1a1e7 commit c569055
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -4,7 +4,7 @@ A redmine plugin to elect issues.


== Compatibility == 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 * For redmine 1.3.* go to https://github.com/dextra/redmine_issues_poll/tree/old


== Features == Features
Expand Down
100 changes: 48 additions & 52 deletions app/controllers/polls_controller.rb
@@ -1,34 +1,32 @@
# Copyright 2011/2012 Dextra Sistemas # Copyright 2011/2012 Dextra Sistemas
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.

class PollsController < ApplicationController class PollsController < ApplicationController
unloadable unloadable

before_filter :find_project, :authorize before_filter :find_project, :authorize
before_filter :get_statuses, :only => [:index, :set_statuses] before_filter :get_statuses, :only => [:index, :set_statuses]

def get_statuses def get_statuses
@statuses = IssueStatus.find(:all, :order => 'position') @statuses = IssueStatus.find(:all, :order => 'position')
@eligible_statuses = EligibleStatus.find(:all).collect{ |status| status.status_id } @eligible_statuses = EligibleStatus.find(:all).collect{ |status| status.status_id }
end end

def index 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) : '--'}} @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' => ''} @login = {'order' => 'asc', 'class' => ''}
@name = {'order' => 'asc', 'class' => ''} @name = {'order' => 'asc', 'class' => ''}
@votes = {'order' => 'asc', 'class' => ''} @votes = {'order' => 'asc', 'class' => ''}

if params[:order_login] if params[:order_login]
if params[:order_login] == "asc" if params[:order_login] == "asc"
@login = {'order' => 'desc', 'class' => 'sort asc'} @login = {'order' => 'desc', 'class' => 'sort asc'}
Expand Down Expand Up @@ -58,7 +56,7 @@ def index
@users = @users.sort{|a, b| a['login'] <=> b['login'] } @users = @users.sort{|a, b| a['login'] <=> b['login'] }
end end
end end

def set_statuses def set_statuses
EligibleStatus.delete_all EligibleStatus.delete_all
if params[:statuses] if params[:statuses]
Expand All @@ -67,7 +65,7 @@ def set_statuses
flash[:notice] = t(:configuration_successful) flash[:notice] = t(:configuration_successful)
redirect_to :action => 'index', :tab => 'config' redirect_to :action => 'index', :tab => 'config'
end end

def set_votes def set_votes
votes = params[:votes] votes = params[:votes]
if /^\d+$/.match(votes) if /^\d+$/.match(votes)
Expand All @@ -85,71 +83,69 @@ def set_votes
end end
redirect_to :action => 'index', :tab => 'config' redirect_to :action => 'index', :tab => 'config'
end end

def update_votes def update_votes
votes = params[:votes] @votes = params[:votes]
render :update do |page| @success = false
if /^\d+$/.match(votes) if /^\d+$/.match(@votes)
user = User.find(params[:user_id]) @user = User.find(params[:user_id])
current_poll_votes = user.poll_votes.find(:first, :conditions => ["project_id = ?", @project.id]) current_poll_votes = @user.poll_votes.find(:first, :conditions => ["project_id = ?", @project.id])
if current_poll_votes if current_poll_votes
current_poll_votes.update_attribute(:votes, params[: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))
else else
page.alert(t(:invalid_votes)) @user.poll_votes << @project.poll_votes.new(:votes => @votes)
end end
@success = true
end

respond_to do |format|
format.js
end end

end end

def bet def bet
@issue = Issue.find(:first, :conditions => ["id = ? AND project_id = ?", params[:issue_id], @project.id]) @issue = Issue.find(:first, :conditions => ["id = ? AND project_id = ?", params[:issue_id], @project.id])
user = User.current user = User.current
render :update do |page| @bet = Bet.new(params[:bet])
@bet = Bet.new(params[:bet]) @bet.issue_id = @issue.id
@bet.issue_id = @issue.id @bet.user_id = user.id
@bet.user_id = user.id @added = false

@success = false
if @bet.save if @bet.save
poll_vote = user.poll_votes.find(:first, :conditions => ["project_id = ?", @issue.project_id]) poll_vote = user.poll_votes.find(:first, :conditions => ["project_id = ?", @issue.project_id])
poll_vote.votes -= @bet.votes poll_vote.votes -= @bet.votes
poll_vote.save poll_vote.save
@issue.bet_votes ||= 0 @issue.bet_votes ||= 0
@issue.bet_votes += @bet.votes @issue.bet_votes += @bet.votes
@issue.save @issue.save
page.replace_html :issues_poll_area, :partial => 'hooks/view_polls_form' @success = true
if @bet.votes > 0 if @bet.votes > 0
page.alert(t(:bet_successful_created)) @added = true
else
page.alert(t(:bet_successful_canceled))
end
else
page.alert(@bet.errors.full_messages.join('\n'))
end end
end end
respond_to do |format|
format.js
end
end end

def cancel_bet def cancel_bet
votes = User.current.votes_bet_by_issue(params[:issue_id]) votes = User.current.votes_bet_by_issue(params[:issue_id])
if votes <= 0 if votes <= 0
votes = 0 votes = 0
else else
votes *= -1 votes *= -1
end end
params[:bet] = {:votes => votes} params[:bet] = {:votes => votes}
bet bet
end end

private private


def find_project def find_project
@project = Project.find(params[:project_id]) @project = Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render_404 render_404
end end

end end
17 changes: 10 additions & 7 deletions app/views/hooks/_view_polls_form.html.erb
Expand Up @@ -31,12 +31,12 @@ limitations under the License. -->
<% if User.current.can_bet?(@issue.project_id, 1) and @issue.can_receive_bet? %> <% if User.current.can_bet?(@issue.project_id, 1) and @issue.can_receive_bet? %>
<%= form_for( <%= form_for(
:bet, :bet,
:remote => true, :remote => true,
:url => {:controller => 'polls', :action => 'bet', :issue_id => @issue.id, :project_id => @issue.project_id}, :url => {:controller => 'polls', :action => 'bet', :issue_id => @issue.id, :project_id => @issue.project_id},
:method => :post, :method => :post,
:complete => "Form.Element.focus('relation_issue_to_id');", :complete => "Form.Element.focus('relation_issue_to_id');",
:html => {:id => 'issues-polls-form'}) do |f| %> :html => {:id => 'issues-polls-form'}) do |f| %>
<%= f.select :votes, 1..User.current.polls_votes_project(@project.id), {} %> <%= f.select :votes, 1..User.current.polls_votes_project(@project.id), {} %>
Expand All @@ -62,7 +62,10 @@ limitations under the License. -->
<td> <%= bet.author.name(:firstname_lastname) %> </td> <td> <%= bet.author.name(:firstname_lastname) %> </td>
<td> <%= bet.sum_votes %> </td> <td> <%= bet.sum_votes %> </td>
<td> <% if bet.user_id == User.current.id and @issue.can_receive_bet? %> <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> <% end %> </td>
</tr> </tr>
<% end %> <% end %>
Expand Down
19 changes: 3 additions & 16 deletions app/views/polls/_members.html.erb
Expand Up @@ -44,7 +44,7 @@ limitations under the License. -->
<td> <td>
<span id="view_votes_<%=user['id']%>" class="view_votes_toggle" onclick="toggleForm(<%=user['id']%>);"><%= user['votes'] %></span> <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;"> <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']%> <%= text_field_tag :votes, user['votes']%>
<%= submit_tag t(:label_update_votes) %> <%= submit_tag t(:label_update_votes) %>
<%= button_to_function t(:button_cancel), "toggleForm(#{user['id']});"%> <%= button_to_function t(:button_cancel), "toggleForm(#{user['id']});"%>
Expand All @@ -60,20 +60,7 @@ limitations under the License. -->


<script type="text/javascript"> <script type="text/javascript">
function toggleForm(id) { function toggleForm(id) {
$$('.form_votes_toggle').without($('view_form_'+id)).each(function(e){ $('#view_form_'+id).toggle();
e.hide(); $('#view_votes_'+id).toggle();
})
$$('.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();
}
} }
</script> </script>
11 changes: 11 additions & 0 deletions app/views/polls/bet.js
@@ -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 %>
11 changes: 11 additions & 0 deletions app/views/polls/cancel_bet.js
@@ -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 %>
7 changes: 7 additions & 0 deletions app/views/polls/update_votes.js
@@ -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 %>
8 changes: 7 additions & 1 deletion lib/issues_poll_hook.rb
Expand Up @@ -15,8 +15,14 @@
module IssuesPoll module IssuesPoll


class Hooks < Redmine::Hook::ViewListener 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={}) def view_layouts_base_html_head(context={})
stylesheet_link_tag 'style', :plugin => 'redmine_issues_poll' stylesheet_link_tag 'style', :plugin => 'redmine_issues_poll'
end end
Expand Down

0 comments on commit c569055

Please sign in to comment.