Skip to content

Commit 4e27f12

Browse files
author
System Administrator
committed
Reviews backend, saving, etc.
1 parent a00ed0b commit 4e27f12

File tree

12 files changed

+178
-18
lines changed

12 files changed

+178
-18
lines changed

app/controllers/drafts_controller.rb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,32 @@ def save
3434

3535
def review
3636
@drft = Draft.find_by_public_url(params[:id])
37-
raw = HTMLEntities.new.decode(@drft.content)
38-
preproc = StanfordParser::DocumentPreprocessor.new
39-
sentences = preproc.getSentencesFromString(raw)
40-
sentences.each_with_index do |sent, i|
41-
chars = ["?", ",", ".", ":", ";", "...", "''", "'"]
42-
sent = sentences[i] = sent.join(" ")
43-
sent = sentences[i] = sent.gsub(/(``[^'']+$)/) {|s| "#{$1}\""}
44-
sent = sentences[i] = sent.gsub("`` ", ' "').gsub("--", "—")
45-
chars.each {|chr| sent = sentences[i] = sent.gsub(" " + chr, chr)}
46-
sentences[i] = '<a class="sentence" href="#" id="sent-' + i.to_s + '">' + sent.strip + '</span>'
37+
if session[:review_id].nil?
38+
reviewified = @drft.reviewify
39+
rev = Review.new(:draft_id => @drft.id, :n_sentences => reviewified[:n_sentences])
40+
rev.save
41+
session[:review_id] = rev.id
42+
@content = reviewified[:content]
43+
@n_zeros = reviewified[:n_sentences]
44+
else
45+
@content = HTMLEntities.new.decode(Review.find(session[:review_id]).content)
46+
@n_zeros = Review.find(session[:review_id]).n_sentences
47+
@general_comments = Review.find(session[:review_id]).general_comments
48+
if @content.nil?
49+
reviewified = @drft.reviewify
50+
@content = reviewified[:content]
51+
@n_zeros = reviewified[:n_sentences]
52+
end
4753
end
48-
@content = sentences.join(" ")
49-
@n_zeros = '0' * sentences.length
54+
end
55+
56+
def save_review
57+
rev = Review.find(session[:review_id])
58+
rev.content = HTMLEntities.new.encode(params[:content])
59+
rev.signature = params[:signature] unless params[:signature].empty?
60+
rev.general_comments = params[:general_comments] unless params[:general_comments].empty?
61+
rev.updated_at = Time.new
62+
rev.save
63+
render :json => Time.new
5064
end
5165
end

app/models/draft.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
class Draft < ActiveRecord::Base
2+
has_many :reviews
3+
4+
def reviewify
5+
raw = HTMLEntities.new.decode(self.content)
6+
preproc = StanfordParser::DocumentPreprocessor.new
7+
sentences = preproc.getSentencesFromString(raw)
8+
sentences.each_with_index do |sent, i|
9+
strt, brk = false, false
10+
chars = ["?", ",", ".", ":", ";", "...", "''", "'"]
11+
sent = sentences[i] = sent.join(" ")
12+
sent = sentences[i] = sent.gsub(/(``[^'']+$)/) {|s| "#{$1}\""}
13+
sent = sentences[i] = sent.gsub("`` ", ' "').gsub("--", "&mdash;")
14+
chars.each {|chr| sent = sentences[i] = sent.gsub(" " + chr, chr)}
15+
if sent.index(/<\/p>\s+<p>/)
16+
sent = sentences[i] = sent.gsub(/<\/p>\s+<p>/) {|s| ""}
17+
brk = true
18+
end
19+
if sent.index("<p>")
20+
sent = sentences[i] = sent.gsub("<p>", "")
21+
strt = true
22+
end
23+
sentences[i] = '<a class="sentence" href="#" id="sent-' + i.to_s + '">' + sent.strip + '</a>'
24+
if brk then sentences[i] = sentences[i].insert(0, "</p><p>") end
25+
if strt then sentences[i] = sentences[i].insert(0, "<p>") end
26+
end
27+
return {:content => sentences.join(" "), :n_sentences => "0" * sentences.length}
28+
end
229
end

app/models/review.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Review < ActiveRecord::Base
2+
belongs_to :draft
23
end

app/views/drafts/review.html.erb

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
border-top: 1px solid #777;
1212
border-bottom: 1px solid #777;
1313
background-color: white;
14+
min-height: 400px;
1415
float: left;
1516
}
1617
li {
@@ -66,17 +67,35 @@ a.sentence.down {
6667
.note {
6768
padding: 10px;
6869
font-family: Courier;
69-
70+
height: 90px;
7071
}
7172
</style>
7273

7374
<script type="text/javascript">
7475
var times = '<%= @n_zeros %>'.split('');
76+
var t = null;
77+
78+
var save_review = function(continuous) {
79+
var content = $("#draft_content").html();
80+
$.post("/drafts/save_review", {authenticity_token: AUTH_TOKEN, content: content, signature: $("#signature").attr("value"), general_comments: $("#general_comments").val()},
81+
function(ret) {
82+
var ret = JSON.parse(ret);
83+
var now = new Date();
84+
document.title = "(saved at " + now.format("h:MM:sstt") + " on " + now.format("dddd dS, yyyy") + ")";
85+
}
86+
)
87+
if (continuous) {
88+
setTimeout("save_review(true)", 1000 * 30) // Save every thirty seconds.
89+
} else {
90+
return false;
91+
}
92+
};
7593

7694
var hide_notes = function(affect) {
7795
$(".note:visible[affect=" + affect + "]").each(function(e) {
7896
if ($(this).val() == "") {
7997
$(this).hide();
98+
$("#sent-" + $(this).attr("id").split("-")[1]).css("text-decoration", "none");
8099
};
81100
});
82101
};
@@ -85,8 +104,16 @@ $(document).ready(function() {
85104
$(".note").live("keydown", function(e) {
86105
if (e.keyCode == 13) {
87106
$(this).hide();
107+
if ($(this).val() != "") {
108+
$("#sent-" + $(this).attr("id").split("-")[1]).css("text-decoration", "underline");
109+
} else {
110+
$("#sent-" + $(this).attr("id").split("-")[1]).css("text-decoration", "none");
111+
}
88112
};
89113
});
114+
115+
setTimeout("save_review(true)", 3000);
116+
90117
var pop_note = function(id, action, ev) {
91118
var $note = $("#note-" + id);
92119
if ($note.length == 0) {
@@ -110,13 +137,17 @@ $(document).ready(function() {
110137
} else {
111138
$note.attr("affect", "neutral");
112139
$note.hide();
140+
if ($note.val() == "") {
141+
$("#sent-" + $note.attr("id").split("-")[1]).css("text-decoration", "none");
142+
};
113143
};
114144
$note.focus();
115-
setTimeout("hide_notes('" + $note.attr("affect") + "')", 2000);
145+
t = setTimeout("hide_notes('" + $note.attr("affect") + "')", 2000);
116146
return false;
117147
}
118148

119149
$(".sentence").click(function(ev) {
150+
clearTimeout(t);
120151
$(".note").each(function(e) {
121152
$(this).hide();
122153
});
@@ -147,12 +178,22 @@ $(document).ready(function() {
147178
<li>Click a sentence <strong>once</strong> if you like it and <strong>twice</strong> if you don't.</li>
148179
<li>A window for <strong>quick notes</strong> will pop up in either case. <strong>Notes disappear</strong> if you don't type anything.</li>
149180
<li>If you do leave comments, <strong>hit enter</strong> to close the note window. <strong>Everything will be saved automatically</strong>.</li>
181+
</ol>
150182
</div>
151183
<div id="draft">
152184
<h1 style="font-family: Arial; font-size: 22px; margin-top: 20px;"><%= @drft.title %></h1>
153-
<%= @content %>
185+
<div id="draft_content">
186+
<% if @content.nil? %>
187+
Whoops, it looks like something's gone horribly wrong and we've lost your work.
188+
<% else %>
189+
<%= @content %>
190+
<% end %>
191+
</div>
154192
</div>
155193
</a>
194+
<script language="javascript" src="/javascripts/simpleswap.js"></script>
156195
<div class="instructions">
157-
<h3 style="margin: 2px 0px 5px 0px;">All done? Sign your name: <input type="text" style="font-family: Verdana; width: 180px; height: 25px; vertical-align: middle; padding-top: 2px; font-size: 13px; text-align: center; font-weight: bold; color: #555"/> and <a href="#">click here to send your feedback.</a></h3>
158-
</div>
196+
<h3 style="margin: 2px 0px 5px 0px;">General Comments:</h3><textarea id="general_comments" style="width: 100%; height: 90px; padding: 10px; font-family: Courier"><%= @general_comments %></textarea>
197+
<hr>
198+
<div style="float: left; font-size: 12px; padding-top: 5px;"><h3 style="margin: 2px 0px 5px 0px;">All done? Sign your name: <input type="text" style="font-family: Verdana; width: 180px; height: 25px; vertical-align: middle; padding-top: 2px; font-size: 13px; text-align: center; font-weight: bold; color: #555" id="signature" value=""/> and</h3></div> <div style="float: right"><a href="#" id="submit-review"><img src="/images/button.png" oversrc="/images/button-hover.png"/></a></div>
199+
</div>

db/development.sqlite3

8 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class ReviewsBackend < ActiveRecord::Migration
2+
def self.up
3+
add_column :reviews, :draft_id, :integer
4+
add_column :reviews, :content, :text
5+
add_column :reviews, :signature, :string
6+
add_column :drafts, :email, :string
7+
end
8+
9+
def self.down
10+
remove_column :reviews, :draft_id
11+
remove_column :reviews, :content
12+
remove_column :reviews, :signature
13+
remove_column :drafts, :email
14+
end
15+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddNSentences < ActiveRecord::Migration
2+
def self.up
3+
add_column :reviews, :n_sentences, :string
4+
end
5+
6+
def self.down
7+
remove_column :reviews, :n_sentences
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddGeneralComments < ActiveRecord::Migration
2+
def self.up
3+
add_column :reviews, :general_comments, :text
4+
end
5+
6+
def self.down
7+
remove_column :reviews, :general_comments
8+
end
9+
end

db/schema.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# It's strongly recommended to check this file into your version control system.
1111

12-
ActiveRecord::Schema.define(:version => 20091025225533) do
12+
ActiveRecord::Schema.define(:version => 20091027064650) do
1313

1414
create_table "drafts", :force => true do |t|
1515
t.datetime "created_at"
@@ -18,11 +18,17 @@
1818
t.text "title", :default => ""
1919
t.text "content", :default => ""
2020
t.string "public_url"
21+
t.string "email"
2122
end
2223

2324
create_table "reviews", :force => true do |t|
2425
t.datetime "created_at"
2526
t.datetime "updated_at"
27+
t.integer "draft_id"
28+
t.text "content"
29+
t.string "signature"
30+
t.string "n_sentences"
31+
t.text "general_comments"
2632
end
2733

2834
end

public/images/button-hover.png

4.75 KB
Loading

public/images/button.png

4.88 KB
Loading

public/javascripts/simpleswap.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This is the implementation of SimpleSwap
2+
// by Jehiah Czebotar
3+
// Version 1.1 - June 10, 2005
4+
// Distributed under Creative Commons
5+
//
6+
// Include this script on your page
7+
// then make image rollovers simple like:
8+
// <img src="/images/ss_img.gif" oversrc="/images/ss_img_over.gif">
9+
//
10+
// http://jehiah.com/archive/simple-swap
11+
//
12+
13+
14+
function SimpleSwap(el,which){
15+
el.src=el.getAttribute(which || "origsrc");
16+
}
17+
18+
function SimpleSwapSetup(){
19+
var x = document.getElementsByTagName("img");
20+
for (var i=0;i<x.length;i++){
21+
var oversrc = x[i].getAttribute("oversrc");
22+
if (!oversrc) continue;
23+
24+
// preload image
25+
// comment the next two lines to disable image pre-loading
26+
x[i].oversrc_img = new Image();
27+
x[i].oversrc_img.src=oversrc;
28+
// set event handlers
29+
x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
30+
x[i].onmouseout = new Function("SimpleSwap(this);");
31+
// save original src
32+
x[i].setAttribute("origsrc",x[i].src);
33+
}
34+
}
35+
36+
var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
37+
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}
38+

0 commit comments

Comments
 (0)