Skip to content

Commit

Permalink
Submitting a draft (publiclab#2747)
Browse files Browse the repository at this point in the history
* Checkbox and JS function added

* publish_draft method, routes, mail_notify_stop

* coauthor can see and publish

* bower update

* undo and trying

* draft label on dashboard

* draft option not visible to first timer

* updating bower

* debug1

* removing debug statement

* button view

* view 2

* normal

* generate path added

* publish privil update

* code climate

* displaying comment with status1 only

* coauthor can view draft

* test correction

* button text change to save if draft

* fixture addition and 1 test

* tests , fixtures and minor changes

* test addition

* codeclimate 1

* codeclimate 2

* codeclimate 3

* minor fixes
  • Loading branch information
grvsachdeva authored and jywarren committed Jun 3, 2018
1 parent e08a352 commit 82c68f2
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,4 @@ RUBY VERSION
ruby 2.3.7p456

BUNDLED WITH
1.16.2
1.16.2
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def alert_and_redirect_moderated
flash[:warning] = "First-time poster <a href='/profile/#{@node.author.name}'>#{@node.author.name}</a> submitted this #{time_ago_in_words(@node.created_at)} ago and it has not yet been approved by a moderator. <a class='btn btn-default btn-sm' href='/moderate/publish/#{@node.id}'>Approve</a> <a class='btn btn-default btn-sm' href='/moderate/spam/#{@node.id}'>Spam</a>"
elsif @node.status == 4 && (current_user && current_user.id == @node.author.id) && !flash[:first_time_post]
flash[:warning] = "Thank you for contributing open research, and thanks for your patience while your post is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so."
elsif @node.status == 3 && (current_user && current_user.id == @node.author.id) && !flash[:first_time_post]
flash[:warning] = "This is a Draft note. Kindly complete it and publish it using 'Publish Draft' button."
elsif @node.status == 3 && (current_user && (current_user.is_coauthor(@node) || current_user.can_moderate?)) && !flash[:first_time_post]
flash[:warning] = "This is a Draft note. Kindly complete it and publish it using <a class='btn btn-success' href='/notes/publish_draft/#{@node.id}'>Publish Draft</a> button."
elsif @node.status != 1 && !(current_user && (current_user.role == 'admin' || current_user.role == 'moderator'))
# if it's spam or a draft
# no notification; don't let people easily fish for existing draft titles; we should try to 404 it
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def activity
if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
notes = notes.where('(node.status = 1 OR node.status = 4 OR node.status = 3)')
elsif current_user
notes = notes.where('(node.status = 1 OR ((node.status = 3 OR node.status = 4) AND node.uid = ?))', current_user.uid)
coauthor_nids = Node.joins(:node_tag).joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid').select('node.*, term_data.*, community_tags.*').where(type: 'note', status: 3).where('term_data.name = (?)', "with:#{current_user.username}").collect(&:nid)
notes = notes.where('(node.nid IN (?) OR node.status = 1 OR ((node.status = 3 OR node.status = 4) AND node.uid = ?))', coauthor_nids, current_user.uid)
else
notes = notes.where('node.status = 1')
end
Expand All @@ -129,6 +130,7 @@ def activity
comments = Comment.joins(:node, :drupal_user)
.order('timestamp DESC')
.where('timestamp - node.created > ?', 86_400) # don't report edits within 1 day of page creation
.where('node.status = ?', 1)
.page(params[:page])
.group('title') # group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp
# group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp
Expand Down
29 changes: 25 additions & 4 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class NotesController < ApplicationController
respond_to :html
before_filter :require_user, only: %i(create edit update delete rsvp)
before_filter :require_user, only: %i(create edit update delete rsvp publish_draft)

def index
@title = I18n.t('notes_controller.research_notes')
Expand Down Expand Up @@ -55,7 +55,7 @@ def show
flash[:warning] = "You need to login to view the page"
redirect_to '/login'
return
elsif @node.status == 3 && @node.author.user != current_user && !current_user.can_moderate?
elsif @node.status == 3 && @node.author.user != current_user && !current_user.can_moderate? && !@node.has_tag("with:#{current_user.username}")
flash[:notice] = "Only author can access the draft note"
redirect_to '/'
return
Expand Down Expand Up @@ -105,6 +105,14 @@ def create
body: params[:body],
main_image: params[:main_image])

if params[:draft] == "true" && current_user.first_time_poster
flash[:notice] = "First-time users are not eligible to create a draft."
redirect_to '/'
return
elsif params[:draft] == "true"
@node.draft
end

if saved
params[:tags]&.tr(' ', ',').split(',').each do |tagname|
@node.add_tag(tagname.strip, current_user)
Expand All @@ -114,7 +122,7 @@ def create
@node.add_tag('event:rsvp', current_user)
@node.add_tag('date:' + params[:date], current_user) if params[:date]
end
if params[:draft] != true
if params[:draft] != "true"
if current_user.first_time_poster
flash[:first_time_post] = true
if @node.has_power_tag('question')
Expand All @@ -130,7 +138,6 @@ def create
end
end
else
@node.draft
flash[:notice] = I18n.t('notes_controller.saved_as_draft').html_safe
end
# Notice: Temporary redirect.Remove this condition after questions show page is complete.
Expand Down Expand Up @@ -378,4 +385,18 @@ def update_title
node.update(title: params[:title])
redirect_to URI.parse(node.path).path + "#comments"
end

def publish_draft
@node = Node.find(params[:id])
if current_user && current_user.uid == @node.uid || current_user.can_moderate? || @node.has_tag("with:#{current_user.username}")
@node.path = @node.generate_path
@node.publish
SubscriptionMailer.notify_node_creation(@node).deliver_now
flash[:notice] = "Thanks for your contribution. Research note published! Now, it's visible publically."
redirect_to @node.path
else
flash[:warning] = "You are not author or moderator so you can't publish a draft!"
redirect_to '/'
end
end
end
6 changes: 4 additions & 2 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ def self.new_note(params)
img.save
end
node.save!
node.notify
if node.status != 3
node.notify
end
else
saved = false
node.destroy
Expand Down Expand Up @@ -728,7 +730,7 @@ def add_tag(tagname, user)
nid: id)
if node_tag.save
saved = true
SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now unless tag.subscriptions.empty?
SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now unless tag.subscriptions.empty? || self.status == 3
else
saved = false
tag.destroy
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def can_moderate?
admin? || moderator?
end

def is_coauthor(node)
self.id == node.author.id || node.has_tag("with:#{self.username}")
end

def tags(limit = 10)
Tag.where('name in (?)', tagnames).limit(limit)
end
Expand Down
10 changes: 5 additions & 5 deletions app/views/dashboard/_node_default.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div class="col-md-6 note-container-note" style="overflow: hidden;">
<div class="note note-default<% if node.status == 4 %> moderated<% end %>">
<div class="note note-default<% if node.status == 4 || node.status == 3 %> moderated<% end %>">

<%= render partial: 'dashboard/node_moderate', locals: { node: node } %>

<% if node.main_image %>
<a class="img" href="<%= node.path %>"><img src="<%= node.main_image.path(:default) %>" style="width:100%;" /></a>
<a class="img" href="<%= node.path %>"><img src="<%= node.main_image.path(:default) %>" style="width:100%;" /></a>
<% end %>
<h4><a href="<%= node.path %>"><%= node.title %></a></h4>

<h4><a href="<%= node.path %>"><%= node.title %></a> <% if node.status == 3 %><span style="font-size: small;" class="label label-success">Draft</span><% end %></h4>

<p class="meta"><%= render partial: "dashboard/node_meta", locals: { node: node } %></p>

<hr style="display:none;" class="bottom" />
Expand Down
32 changes: 27 additions & 5 deletions app/views/editor/rich.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,22 @@
<option selected value="">recent edits</option>
</div>
-->
</div>
</div>

<button class="btn btn-lg btn-default btn-more">...</button>

<span> &nbsp; By publishing, you agree to <a href="https://publiclab.org/licenses">open source your work</a><span class="hidden-xs"> so that others may use it.</span></span>

<button class="ple-publish btn btn-lg btn-primary pull-right disabled">Publish</button>
<button class="ple-publish btn btn-lg btn-primary pull-right disabled"><% if controller.action_name == "edit" && @node.status == 3 %>Save<% else %>Publish<% end %></button>
<span class="ple-help pull-right">
<span class="ple-steps-left">2</span>
<span class="hidden-xs"> steps left</span>
<% if controller.action_name != "edit" && !current_user.first_time_poster %>
<span id="newOpts" style="display: inline-block;">
<span id="text" style="display: none; color:red;">Others won't be able to see this until you're ready <a href="#" >Learn more</a>&nbsp; &nbsp;</span>
<span><input type="checkbox" id="myCheck" onclick="draftCheck()">Save as draft&nbsp; | &nbsp;</span>
<span class="ple-steps-left">2</span>
<span class="hidden-xs"> steps left</span>
</span>
<% end %>
</span>

</div>
Expand Down Expand Up @@ -309,6 +315,22 @@
$('.legacy-button').click(function onLegacyClick(event) {
event.preventDefault();
window.location = "/post?legacy=true&body=" + editor.richTextModule.value() + "&tags=" + editor.tagsModule.value() + "&title=" + editor.titleModule.value();
});
});

function draftCheck() {
var checkBox = document.getElementById("myCheck");
var text = document.getElementById("text");
var button = document.getElementsByClassName("ple-publish")[0];
if (checkBox.checked === true){
editor.data.draft = true;
text.style.display = "inline-block";
button.innerHTML = "Save";
} else {
editor.data.draft = false;
text.style.display = "none";
button.innerHTML = "Publish";
}
console.log("Draft:", editor.data.draft);
}

</script>
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"leaflet": "0.7.3",
"moment": "~2.10.6",
"junction": "theleagueof/junction",
"publiclab-editor": "publiclab/PublicLab.Editor#~1.2.0",
"publiclab-editor": "publiclab/PublicLab.Editor#~1.3.0",
"inline-markdown-editor": "~0.3.0",
"leaflet-d3": "^0.3.8",
"woofmark": "~4.2.0",
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
get 'notes/popular' => 'notes#popular'
get 'notes/liked' => 'notes#liked'
post 'notes/create' => 'notes#create'
get 'notes/publish_draft/:id' => 'notes#publish_draft'

get 'places' => 'notes#places'
get 'tools' => 'notes#tools'
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/node_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ test_lego:
uid: 2
nid: 13
date: <%= DateTime.now.to_i %>

co-author:
tid: 19
uid: 2
nid: 21
date: <%= DateTime.now.to_i %>
2 changes: 1 addition & 1 deletion test/fixtures/nodes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ draft:
nid: 21
uid: 2
title: "Draft note"
path: "/notes/test/<%= Time.now.strftime("%m-%d-%Y") %>/draft-note"
path: "/notes/jeff/<%= Time.now.strftime("%m-%d-%Y") %>/draft-note"
created: <%= Time.now.to_i %>
changed: <%= Time.now.to_i %>
status: 3
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/revisions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,9 @@ method:
uid: 1
title: "Method page"
body: "[activities:spectrometer]"

draft:
nid: 21
uid: 2
title: "Draft note"
body: This is draft note.
4 changes: 4 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ test_lego:
blog-featured:
tid: 18
name: blog-featured

co-author:
tid: 19
name: with:testuser
Loading

0 comments on commit 82c68f2

Please sign in to comment.