Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot reload, column total votes, refactorings #18

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a29d305
unuseful spaces removed
May 9, 2019
5efcbde
migration compatiblity with rails 5
Utopism May 9, 2019
57530a2
+sum_votes_up_and_dn, and where conditions normalized
Utopism May 9, 2019
e2c913e
unuseful spaces removed
Utopism May 9, 2019
2641afa
unloadable removed (causes issues with hot reload), exception removed
Utopism May 9, 2019
335b69b
+field_sum_votes_up_and_dn
Utopism May 9, 2019
b40a4d3
+field_sum_votes_up_and_dn
Utopism May 9, 2019
f61d095
V1.0.3
Utopism May 9, 2019
c9e6466
move to standard location for model, and reflect the name of the class
Utopism May 9, 2019
7acc6dc
hot reload + new sum_votes_up_and_dn column
Utopism May 9, 2019
91c9c74
unloadable removed for hot reload, exception removed
Utopism May 9, 2019
c61cfd3
empty helper class removed
Utopism May 9, 2019
f7058be
fr locale
Utopism May 9, 2019
636bfb6
normalized issues view directory
Utopism May 9, 2019
07be66f
fixed wrong move of view
Utopism May 9, 2019
08082fa
class vote_on_issues-my-vote-opt fixed
Utopism May 9, 2019
a864a3e
moved votes to partial
Utopism May 9, 2019
36ce626
moved my votes to a partial
Utopism May 9, 2019
459087f
use standard issue_fields_rows
Utopism May 9, 2019
46d4623
use conventional view path issues/show_details_bottom
Utopism May 9, 2019
e36fcdd
V1.0.3
Utopism May 9, 2019
604b8df
V1.0.3
Utopism May 9, 2019
1a6db40
un-necessary commented code removed
Utopism May 9, 2019
1dd0e9f
correct way to do the queries no multiple query for each user
Utopism May 9, 2019
1967c79
fixed : pluck -> array of vote and login
Utopism May 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

A plugin for Redmine[www.redmine.org] which allows users to up- or down-vote issues.

Current version: 1.0.2
Current version: 1.0.3

Developed on Redmine 3.3.2

== Usage

* Install plugin

* Download latest release[https://github.com/ojde/redmine-vote_on_issues-plugin/releases]

* Extract into Redmine's plugin directory <tt><redmine_path>/plugins</tt>

* Rename the <tt>redmine-vote_on_issues-plugin-x.y.z</tt> directory to <tt>vote_on_issues</tt>

* Restart Redmine

* Enable for project

* Enable permissions for roles (minimum requirement is
always the user being logged in)

* "Vote on issue": Up- or down-vote on issues; change own votes or withdraw
them. See own votes.

* "View votes": See the total number of votes on an issue.

* "View voters": See who voted how on an issue.

There are Up- and down-votes columns available for the issue listing.
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/vote_on_issues_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class VoteOnIssuesController < ApplicationController
# respond_to :html, :js
unloadable

#Authorize against global permissions defined in init.rb
# ?? does prevent everythin below admin?
# TODO - find out how this works
#before_filter :authorize_global
#before_filter :authorize
#before_action :authorize_global
#before_action :authorize

def index
@project = Project.find(params[:project_id])
@votes = VoteOnIssue.all
Expand All @@ -22,16 +21,17 @@ def cast_vote
elsif 'nil' == params[:vote_val]
@iMyVote = 0;
end

begin
@vote = VoteOnIssue.find_by!("issue_id = ? AND user_id = ?", params[:issue_id], User.current.id)

@vote = VoteOnIssue.where(issue_id: params[:issue_id]).where(user_id: User.current.id).first

if @vote
if 0 != @iMyVote
@vote.vote_val = @iMyVote
@vote.save
else
@vote.destroy
end
rescue ActiveRecord::RecordNotFound
else
if 0 != @iMyVote
@vote = VoteOnIssue.new
@vote.user_id = User.current.id
Expand All @@ -40,15 +40,15 @@ def cast_vote
@vote.save
end
end

@nVotesUp = VoteOnIssue.getUpVoteCountOnIssue(params[:issue_id])
@nVotesDn = VoteOnIssue.getDnVoteCountOnIssue(params[:issue_id])

@issue = Issue.find(params[:issue_id])

# Auto loads /app/views/vote_on_issues/cast_vote.js.erb
end

def show_voters
@issue = Issue.find(params[:issue_id])
@UpVotes = VoteOnIssue.getListOfUpVotersOnIssue(params[:issue_id])
Expand Down
2 changes: 0 additions & 2 deletions app/helpers/vote_on_issues_helper.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/models/voi_query_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'query'

class VoiQueryColumn < QueryColumn
def value(object)
object.send name, object
end

def value_object(object)
object.send name, object
end
end
37 changes: 13 additions & 24 deletions app/models/vote_on_issue.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
class VoteOnIssue < ActiveRecord::Base
unloadable

# Every vote belongs to a user and an issue
belongs_to :user
belongs_to :issue


scope :up_vote, lambda { where('vote_val > 0') }
scope :down_vote, lambda { where('vote_val < 0') }

def self.getMyVote(issue)
iRet = 0
begin
@vote = VoteOnIssue.find_by!("issue_id = ? AND user_id = ?", issue.id, User.current.id)
iRet = @vote.vote_val
rescue ActiveRecord::RecordNotFound
iRet = 0
@vote = where(issue_id: issue.id).where(user_id: User.current.id).first
iRet = @vote.vote_val if @vote
end
iRet
end

def self.getUpVoteCountOnIssue(issue_id)
where("issue_id = ? AND vote_val > 0", issue_id).count
where(issue_id: issue_id).up_vote.count
end

def self.getDnVoteCountOnIssue(issue_id)
where("issue_id = ? AND vote_val < 0", issue_id).count
where(issue_id: issue_id).down_vote.count
end

def self.getListOfUpVotersOnIssue(issue_id)
# this does load the users, but costly: One query for each user
# where("issue_id = ? AND vote_val > 0", issue_id)
# this does load the users, less costly: One query for all users
# includes(:user).where("issue_id = ? AND vote_val > 0", issue_id)
# where("issue_id = ? AND vote_val > 0", issue_id).includes(:user)
# joins users successfully, but still execs one query for each user
# where("issue_id = ? AND vote_val > 0", issue_id).joins(:user)
# This does what I want, but I'd love to find out how to do this in rails...
find_by_sql( ["SELECT `vote_on_issues`.`vote_val` AS vote_val, `users`.`login` AS user_login FROM `vote_on_issues` LEFT JOIN `users` ON (`users`.`id` = `vote_on_issues`.`user_id`) WHERE (`issue_id` = ? AND `vote_val` > 0) ORDER BY user_login ASC", issue_id] )
where(issue_id: issue_id).up_vote.joins(:user).order('users.login ASC').pluck(:vote_val, 'users.login')
end

def self.getListOfDnVotersOnIssue(issue_id)
# see getListOfUpVotersOnIssue
find_by_sql( ["SELECT `vote_on_issues`.`vote_val` AS vote_val, `users`.`login` AS user_login FROM `vote_on_issues` LEFT JOIN `users` ON (`users`.`id` = `vote_on_issues`.`user_id`) WHERE (`issue_id` = ? AND `vote_val` < 0) ORDER BY user_login ASC", issue_id] )
where(issue_id: issue_id).down_vote.joins(:user).order('users.login ASC').pluck(:vote_val, 'users.login')
end

end
18 changes: 18 additions & 0 deletions app/views/issues/_my_votes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="value">
<div class="vote_on_issues-issue-votes-nowrap">
<span id="vote_on_issues_MyVoteNil">&ndash;</span>
<%= image_tag('arrow-u-r-green.png', :id => 'vote_on_issues_MyVoteUpOn', :style => 'display:none', :class => 'vote_on_issues-icon-vote', :plugin => 'vote_on_issues') %>
<%= image_tag('arrow-d-r-red.png', :id => 'vote_on_issues_MyVoteDnOn', :style => 'display:none', :class => 'vote_on_issues-icon-vote', :plugin => 'vote_on_issues') %>
</div>

<div class="vote_on_issues-issue-votes-nowrap">
<span class="vote_on_issues-my-vote-opt">&nbsp;&nbsp;<%= l(:view_issues_my_vote_opt) %>:</span>
<%= link_to(image_tag('arrow-u-r-blank.png', :id => 'vote_on_issues_MyVoteUpOff', :style => 'display:none', :class => 'vote_on_issues-icon-vote', :plugin => 'vote_on_issues'), {:controller => 'vote_on_issues', :action => 'cast_vote', :issue_id => @issue, :vote_val => 'vup' }, remote: true, :class => 'vote_on_issues-icon-vote', :title => l(:view_issues_my_vote_opt_up) ) %>
<%= link_to(image_tag('arrow-d-r-blank.png', :id => 'vote_on_issues_MyVoteDnOff', :style => 'display:none', :class => 'vote_on_issues-icon-vote', :plugin => 'vote_on_issues'), {:controller => 'vote_on_issues', :action => 'cast_vote', :issue_id => @issue, :vote_val => 'vdn' }, remote: true, :class => 'vote_on_issues-icon-vote', :title => l(:view_issues_my_vote_opt_dn) ) %>
<%= link_to(image_tag('delete-black.png', :id => 'vote_on_issues_MyVoteDelete', :style => 'display:none', :class => 'vote_on_issues-icon-vote', :plugin => 'vote_on_issues'), {:controller => 'vote_on_issues', :action => 'cast_vote', :issue_id => @issue, :vote_val => 'nil' }, remote: true, :class => 'vote_on_issues-icon-vote', :title => l(:view_issues_my_vote_opt_nil) ) %>
</div>

<script>
vote_on_issues.showMyVote(<%= VoteOnIssue.getMyVote(@issue) %>);
</script>
</div>
14 changes: 14 additions & 0 deletions app/views/issues/_show_details_bottom.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if @project.module_enabled?(:vote_on_issues) %>

<%= issue_fields_rows do |rows| %>
<%
if authorize_for('issues', 'view_votes')
rows.left l(:view_issues_label_votes), render(partial: 'issues/votes')
end

if authorize_for('issues', 'cast_vote')
rows.right l(:view_issues_my_vote), render(partial: 'issues/my_votes')
end
%>
<% end %>
<% end %>
33 changes: 33 additions & 0 deletions app/views/issues/_votes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="value">
<div class="vote_on_issues-issue-votes-nowrap">
<span id="vote_on_issues_nVotesUp"></span><%= image_tag 'arrow-u-r-green.png', :class => 'vote_on_issues-icon-votes', :plugin => 'vote_on_issues' %>
</div>
<div class="vote_on_issues-issue-votes-nowrap">
<span id="vote_on_issues_nVotesDn"></span><%= image_tag 'arrow-d-r-red.png', :class => 'vote_on_issues-icon-votes', :plugin => 'vote_on_issues' %>
</div>

<% if authorize_for('issues', 'view_voters') %>
<div class="vote_on_issues-issue-votes-nowrap">
<%= link_to(l(:view_issues_label_voters), {:controller => 'vote_on_issues', :action => 'show_voters', :issue_id => @issue }, remote: true, :id => "vote_on_issues-link-voters", :class => 'vote_on_issues-link-voters', :title => l(:view_issues_show_voters) ) %>
</div>

<div id="vote_on_issues-issue-voters" style="display:none;">
<div>
<table><tbody><tr>
<td><%= l(:view_issues_issue_voters) %></td>
<td onClick="vote_on_issues.hideListOfVoters()"><%= image_tag('delete-white.png', :plugin => 'vote_on_issues') %></td>
</tr></tbody></table>
</div>
<div>
<table><tbody id="vote_on_issues-issue-voters-list">
</tbody></table>
</div>
</div>
<% end %>

<script>
vote_on_issues.showVotesOnIssue(<%= VoteOnIssue.getUpVoteCountOnIssue(@issue.id) %>, <%= VoteOnIssue.getDnVoteCountOnIssue(@issue.id) %>);
vote_on_issues.sImgUp = '<%= image_tag('arrow-u-r-green.png', :plugin => 'vote_on_issues') %>';
vote_on_issues.sImgDn = '<%= image_tag('arrow-d-r-red.png', :plugin => 'vote_on_issues') %>';
</script>
</div>
72 changes: 0 additions & 72 deletions app/views/view_issues/_show_details_bottom.erb

This file was deleted.

8 changes: 4 additions & 4 deletions app/views/vote_on_issues/show_voters.js.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

vote_on_issues.clearListOfVoters();

<% @UpVotes.each do |vote| %>
vote_on_issues.addToListOfVoters(<%= vote['vote_val'] %>, "<%= vote['user_login'] %>");
<% @UpVotes.each do |vote, login| %>
vote_on_issues.addToListOfVoters(<%= vote %>, "<%= login %>");
<% end %>

<% @DnVotes.each do |vote| %>
vote_on_issues.addToListOfVoters(<%= vote['vote_val'] %>, "<%= vote['user_login'] %>");
<% @DnVotes.each do |vote, login| %>
vote_on_issues.addToListOfVoters(<%= vote %>, "<%= login %>");
<% end %>

vote_on_issues.showListOfVoters();
Loading