Skip to content

Commit

Permalink
Build framework, Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jongha committed Jan 7, 2014
1 parent aa8b906 commit c882b41
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 4 deletions.
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

3 changes: 3 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= redmine_vote

Description goes here
31 changes: 31 additions & 0 deletions app/controllers/vote_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class VoteController < ApplicationController
unloadable

before_filter :require_login
before_filter :find_user, :find_board_and_topic#, :authorize

def submit
votes = Votes.new()
votes.submit(@message.id, @user.id, params[:point])
end

def point
votes = Votes.new()
@point = votes.point(@message.id)
end

private
def find_user
@user = User.current
end

def find_board_and_topic
begin
@board = Board.find(params[:board_id])
@message = @board.topics.find(params[:message_id])

rescue ActiveRecord::RecordNotFound
render_404
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/vote_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module VoteHelper
end
18 changes: 18 additions & 0 deletions app/models/votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Votes < ActiveRecord::Base
unloadable

def submit(message_id, user_id, point = 0)
votes = Votes.find(:first, :conditions => ['message_id = ? and user_id = ?', message_id, user_id])
unless votes
votes = Votes.new
votes.message_id = message_id
votes.user_id = user_id
votes.point = point.nil? ? 0 : point
votes.save!
end
end

def point(message_id)
return Votes.sum(:point, :conditions => ['message_id = ?', message_id])
end
end
11 changes: 11 additions & 0 deletions app/views/vote/_view_layouts_base_content.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%
unless User.current.login == ''
%>

<div id="vote">
<a class="vote-point" id="vote-point1" href="#" data-point="1"></a>
<a class="vote-point" id="vote-point-1" href="#" data-point="-1"></a>
<span>Vote</span>
</div>

<% end %>
3 changes: 3 additions & 0 deletions app/views/vote/point.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>VoteController#count</h2>

<%= @point %>
1 change: 1 addition & 0 deletions app/views/vote/submit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>VoteController#vote</h2>
17 changes: 17 additions & 0 deletions assets/javascripts/vote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$(document).ready(function() {
if($(".controller-messages").length) {
//console.log(document.URL);
$("#vote").show();
$(".vote-point").bind("click", function(event) {
event.preventDefault();
var point = $(this).data("point");
$.ajax({
type: "POST",
url: document.URL + "/vote?p=1",
success: function(data, textStatus, jqXHR) {
console.log(data);
}
});
});
}
});
5 changes: 5 additions & 0 deletions assets/stylesheets/vote.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#vote {
display:none;
margin: 20px auto;
text-align:center;
}
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# English strings go here for Rails i18n
en:
# my_label: "My label"
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Plugin's routes
# See: http://guides.rubyonrails.org/routing.html

Rails.application.routes.draw do
get 'boards/:board_id/topics/:message_id/vote', :to => 'vote#point'
post 'boards/:board_id/topics/:message_id/vote', :to => 'vote#submit'
end
9 changes: 9 additions & 0 deletions db/migrate/001_create_votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateVotes < ActiveRecord::Migration
def change
create_table :votes do |t|
t.integer :message_id
t.integer :user_id
t.integer :point
end
end
end
11 changes: 11 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'redmine'
require 'vote_application_hooks'

Redmine::Plugin.register :redmine_vote do
name 'Redmine Vote plugin'
author 'Jong-Ha Ahn'
description 'This is a plugin for Redmine'
version '0.0.1'
url 'http://github.com/jongha/redmine_vote'
author_url 'http://www.mrlatte.net'
end
12 changes: 12 additions & 0 deletions lib/vote_application_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class VoteHeaderHooks < Redmine::Hook::ViewListener

def view_layouts_base_html_head(context = {})
o = stylesheet_link_tag('vote', :plugin => 'redmine_vote')
o << javascript_include_tag('vote', :plugin => 'redmine_vote')
return o
end
end

class VoteLayoutBaseContentHooks < Redmine::Hook::ViewListener
render_on :view_layouts_base_content, :partial => 'vote/view_layouts_base_content'
end
8 changes: 8 additions & 0 deletions test/functional/vote_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require File.expand_path('../../test_helper', __FILE__)

class VoteControllerTest < ActionController::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Load the Redmine helper
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
9 changes: 9 additions & 0 deletions test/unit/votes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require File.expand_path('../../test_helper', __FILE__)

class VotesTest < ActiveSupport::TestCase

# Replace this with your real tests.
def test_truth
assert true
end
end

0 comments on commit c882b41

Please sign in to comment.