-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #967 from hennevogel/refactor-comment-save
Refactor comments
- Loading branch information
Showing
21 changed files
with
80 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,21 @@ | ||
module Webui::HasComments | ||
|
||
# This is a nice but useless reimplementation of nested objects. Good work but nahhhhh | ||
def save_comment | ||
require_login || return | ||
|
||
required_parameters :body | ||
|
||
comment = main_object.comments.build(body: params[:body], parent_id: params[:parent_id]) | ||
comment.user = User.current | ||
comment.save! | ||
|
||
respond_to do |format| | ||
format.js do | ||
render json: 'ok' | ||
end | ||
format.html do | ||
flash[:notice] = 'Comment added successfully' | ||
end | ||
end | ||
redirect_to :back | ||
end | ||
|
||
protected | ||
|
||
def sort_comments(comments) | ||
@all_comments = Hash.new | ||
@all_comments[:parents] = [] | ||
@all_comments[:children] = [] | ||
|
||
# separate parents from children. How cruel, I know. | ||
comments.each do |com| | ||
# No valid parent | ||
if !com.parent_id.nil? && Comment.exists?(com.parent_id) | ||
@all_comments[:children] << [com.body, com.user.login, com.parent_id, com.id, com.created_at] | ||
if comment.save | ||
format.html { redirect_to :back , notice: 'Comment was successfully created.' } | ||
format.json { render json: 'ok' } | ||
else | ||
@all_comments[:parents] << [com.body, com.id, com.user.login, com.created_at] | ||
format.html { redirect_to :back, error: "Comment can't be saved: #{comment.errors.full_messages.to_sentence}." } | ||
format.json { render json: comment.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
|
||
@all_comments[:parents].sort_by! { |c| c[3] } # sorting by created_at | ||
@all_comments[:children].sort_by! { |c| c[3] } # sorting by created_at | ||
|
||
thread_comments | ||
end | ||
|
||
def thread_comments | ||
@comments = [] | ||
# now pushing sorted and final list of first/top/parent level comments into to a hash to | ||
@all_comments[:parents].each do |first_level| | ||
@comments << { | ||
created_at: first_level[3], | ||
id: first_level[1], | ||
body: first_level[0], | ||
parent_id: nil, | ||
user: first_level[2], | ||
children: find_children(first_level[1]) | ||
} | ||
end | ||
end | ||
|
||
def find_children(parent_id = nil) | ||
return [] unless parent_id | ||
current_children = [] | ||
|
||
# get children of current top comment | ||
child_comments = @all_comments[:children].select do |c| | ||
c[2] == parent_id | ||
end | ||
|
||
# pushing children coments into hash | ||
|
||
child_comments.each do |child| | ||
current_children << { | ||
created_at: child[4], | ||
id: child[3], | ||
body: child[0], | ||
parent_id: child[2], | ||
user: child[1], | ||
children: find_children(child[3]) | ||
} | ||
end | ||
return current_children | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
<%= possibly_empty_ul class: 'horizontal-list comment-actions' do %> | ||
<% unless User.current.is_nobody? %> | ||
<li class="togglable_comment" data-toggle="reply_form_of_<%= comment[:id] %>"> | ||
<a id="reply_link_id_<%= comment[:id] %>"> | ||
<li class="togglable_comment" data-toggle="reply_form_of_<%= comment.id %>"> | ||
<a id="reply_link_id_<%= comment.id %>"> | ||
<%= sprited_text('comment_add', 'Reply') %> | ||
</a> | ||
</li> | ||
<% end %> | ||
<% if User.current.login == comment[:user] || User.current.is_admin? %> | ||
<% if User.current == comment.user || User.current.is_admin? %> | ||
<!-- editing is missing and as such broken | ||
<li class="togglable_comment" data-toggle="edit_form_of_<%= comment[:id] %>"> | ||
<li class="togglable_comment" data-toggle="edit_form_of_<%= comment.id %>"> | ||
<%= link_to(sprited_text('comment_edit', 'Edit'), '#') %> | ||
</li> | ||
--> | ||
<li> | ||
<%= link_to(sprited_text('comment_delete', 'Delete'), | ||
{controller: :comments, action: :destroy, | ||
id: comment[:id]}, method: :delete) %> | ||
id: comment.id}, method: :delete) %> | ||
</li> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
<% cid = comment[:id] %> | ||
<div id="<%="reply_form_of_#{cid}"%>" style="display: none; <%= 'padding-bottom: 20px;' if level >= 1 %>"> | ||
<div id="<%="reply_form_of_#{comment.id}"%>" style="display: none;"> | ||
<%= save_comment_form do %> | ||
<p> | ||
<%= text_area_tag 'body', nil, size: '80x1', :onkeyup => 'sz(this);', :onclick => 'sz(this);', id: "reply_body_#{cid}", :placeholder => 'Comment Text', :required => 'required' %> | ||
<%= hidden_field_tag 'parent_id', comment[:id], id: "reply_parent_id_#{cid}" %> | ||
<%= text_area_tag 'body', nil, size: '80x1', :onkeyup => 'sz(this);', :onclick => 'sz(this);', id: "reply_body_#{comment.id}", :placeholder => 'Comment Text', :required => 'required' %> | ||
<%= hidden_field_tag 'parent_id', comment.id, id: "reply_parent_id_#{comment.id}" %> | ||
</p> | ||
<p><%= submit_tag 'Add reply', id: "add_reply_#{cid}"%></p> | ||
<p><%= submit_tag 'Add reply', id: "add_reply_#{comment.id}"%></p> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,19 @@ | ||
<div class="comments"> | ||
<% @comments.each_with_index do |comment, index| %> | ||
<%= javascript_tag do %> | ||
<%= render partial: 'webui/comment/expand_collapse.js.erb', locals: { comment_id: comment[:id] } %> | ||
<% end %> | ||
<div class="comment_thread"> | ||
<div class="comment comment_<%= index %>"> | ||
<%= user_with_realname_and_icon(comment[:user], no_icon: true, short: true) %> | ||
wrote <span class="comment_time"><%= fuzzy_time(comment[:created_at]) %></span> | ||
|
||
<%= comment_body(comment) %> | ||
<%= render :partial => 'webui/comment/links', :locals => { comment: comment } %> | ||
<%= render :partial => 'webui/comment/reply', :locals => { comment: comment, level: 0 } %> | ||
</div> | ||
<% unless comment[:children].blank? %> | ||
<div id="reply_of_comment_<%= comment[:id] %>"> | ||
<%= render partial: 'webui/comment/child', locals: { children: comment[:children] } %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% @comments.walk_tree do |comment, level| %> | ||
<div class="comment thread_level_<%= level >= 4 ? 4 : level %>"> | ||
<%= user_with_realname_and_icon(comment.user, no_icon: true, short: true) %> | ||
wrote <span class="comment_time"><%= fuzzy_time(comment.created_at) %></span> | ||
<%= comment_body(comment) %> | ||
<%= render :partial => 'webui/comment/links', :locals => { comment: comment } %> | ||
<%= render :partial => 'webui/comment/reply', :locals => { comment: comment, level: 0 } %> | ||
</div> | ||
<% end %> | ||
|
||
<div class="comment_new grid_16 alpha omega"> | ||
<%= render partial: 'webui/comment/new' %> | ||
</div> | ||
|
||
</div> | ||
|
||
<% content_for :ready_function do %> | ||
setup_comment_toggles(); | ||
setup_comment_toggles(); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.