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

[in-progress]Fix/change drupal comments to comment. Part of #956 #978

Merged
merged 1 commit into from Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions app/controllers/comment_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CommentController < ApplicationController
before_filter :require_user, :only => [:create, :update, :delete]

def index
@comments = DrupalComment.paginate(page: params[:page], per_page: 30)
@comments = Comment.paginate(page: params[:page], per_page: 30)
.order('timestamp DESC')
render template: 'comments/index'
end
Expand Down Expand Up @@ -40,7 +40,7 @@ def create
# create answer comments
def answer_create
@answer_id = params[:aid]
@comment = DrupalComment.new(
@comment = Comment.new(
uid: current_user.uid,
aid: params[:aid],
comment: params[:body],
Expand All @@ -58,7 +58,7 @@ def answer_create
end

def update
@comment = DrupalComment.find params[:id]
@comment = Comment.find params[:id]

comments_node_and_path

Expand All @@ -79,7 +79,7 @@ def update
end

def delete
@comment = DrupalComment.find params[:id]
@comment = Comment.find params[:id]

comments_node_and_path

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def dashboard
revisions = revisions.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == "production"
@wikis = @wikis + revisions
@wikis = @wikis.sort_by { |a| a.created_at }.reverse
@comments = DrupalComment.joins(:drupal_node, :drupal_users)
@comments = Comment.joins(:drupal_node, :drupal_users)
.order('timestamp DESC')
.where('timestamp - node.created > ?', 86400) # don't report edits within 1 day of page creation
.limit(20)
.group('title') # group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp
# .where('comments.status = (?)', 1)
# group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp
@comments = @comments.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == "production"
@answer_comments = DrupalComment.joins(:answer, :drupal_users)
@answer_comments = Comment.joins(:answer, :drupal_users)
.order('timestamp DESC')
.where('timestamp - answers.created_at > ?', 86400)
.limit(20)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def stats

@graph_notes = DrupalNode.weekly_tallies('note', 52, @time).to_a.sort.to_json
@graph_wikis = DrupalNode.weekly_tallies('page', 52, @time).to_a.sort.to_json
@graph_comments = DrupalComment.comment_weekly_tallies(52, @time).to_a.sort.to_json
@graph_comments = Comment.comment_weekly_tallies(52, @time).to_a.sort.to_json

users = []
nids = []
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def profile
def likes
@user = DrupalUsers.find_by_name(params[:id])
@title = "Liked by "+@user.name
@notes = @user.liked_notes.includes([:drupal_tag, :drupal_comments])
@notes = @user.liked_notes.includes([:drupal_tag, :comments])
.paginate(page: params[:page], per_page: 20)
@wikis = @user.liked_pages
@tagnames = []
Expand Down Expand Up @@ -213,7 +213,7 @@ def reset
end

def comments
@comments = DrupalComment.limit(20)
@comments = Comment.limit(20)
.order("timestamp DESC")
.where(status: 0, uid: params[:id])
.paginate(page: params[:page], per_page: 30)
Expand Down
2 changes: 1 addition & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Answer < ActiveRecord::Base
belongs_to :drupal_node, foreign_key: 'nid', dependent: :destroy
belongs_to :drupal_users, foreign_key: 'uid'
has_many :answer_selections, foreign_key: 'aid'
has_many :drupal_comments, foreign_key: 'aid'
has_many :comments, foreign_key: 'aid'

validates :content, presence: true

Expand Down
4 changes: 2 additions & 2 deletions app/models/drupal_comment.rb → app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class DrupalComment < ActiveRecord::Base
class Comment < ActiveRecord::Base
include CommentsShared # common methods for comment-like models

attr_accessible :pid, :nid, :uid, :aid,
Expand All @@ -23,7 +23,7 @@ def self.inheritance_column
def self.comment_weekly_tallies(span = 52, time = Time.now)
weeks = {}
(0..span).each do |week|
weeks[span-week] = DrupalComment.select(:timestamp)
weeks[span-week] = Comment.select(:timestamp)
.where(timestamp: time.to_i - week.weeks.to_i..time.to_i - (week-1).weeks.to_i)
.count
end
Expand Down
5 changes: 0 additions & 5 deletions app/models/concerns/node_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ def likes
def liked_by(uid)
self.likers.collect(&:uid).include?(uid)
end

def comments(direction = 'DESC')
self.drupal_comments
.order("timestamp #{direction}")
end
end
8 changes: 4 additions & 4 deletions app/models/drupal_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DrupalNode < ActiveRecord::Base
string :status
string :updated_month
text :comments do
drupal_comments.map { |comment| comment.comment }
comments.map { |comment| comment.comment }
end

string :user_name do
Expand Down Expand Up @@ -72,7 +72,7 @@ def friendly_id_string
has_many :drupal_tag, :through => :drupal_node_community_tag
# these override the above... have to do it manually:
# has_many :drupal_tag, :through => :drupal_node_tag
has_many :drupal_comments, :foreign_key => 'nid', :dependent => :destroy
has_many :comments, :foreign_key => 'nid', :dependent => :destroy
has_many :drupal_content_type_map, :foreign_key => 'nid', :dependent => :destroy
has_many :drupal_content_field_mappers, :foreign_key => 'nid', :dependent => :destroy
has_many :drupal_content_field_map_editor, :foreign_key => 'nid', :dependent => :destroy
Expand Down Expand Up @@ -232,7 +232,7 @@ def revision_count
end

def comment_count
self.drupal_comments
self.comments
.count
end

Expand Down Expand Up @@ -552,7 +552,7 @@ def add_comment(params = {})
else
thread = "01/"
end
c = DrupalComment.new({
c = Comment.new({
:pid => 0,
:nid => self.nid,
:uid => params[:uid],
Expand Down
2 changes: 1 addition & 1 deletion app/models/drupal_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DrupalUsers < ActiveRecord::Base
has_many :node_selections, :foreign_key => :user_id
has_many :answers, :foreign_key => :uid
has_many :answer_selections, :foreign_key => :user_id
has_many :drupal_comments, :foreign_key => 'uid'
has_many :comments, :foreign_key => 'uid'
has_one :location_tag, :foreign_key => 'uid', :dependent => :destroy

searchable :if => proc { |user| user.status == 1 } do
Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def weekly_note_tally(span = 52)
def weekly_comment_tally(span = 52)
weeks = {}
(0..span).each do |week|
weeks[span-week] = DrupalComment.count :all, :select => :timestamp, :conditions => {:uid => self.drupal_user.uid, :status => 1, :timestamp => Time.now.to_i-week.weeks.to_i..Time.now.to_i-(week-1).weeks.to_i}
weeks[span-week] = Comment.count :all, :select => :timestamp, :conditions => {:uid => self.drupal_user.uid, :status => 1, :timestamp => Time.now.to_i-week.weeks.to_i..Time.now.to_i-(week-1).weeks.to_i}
end
weeks
end
Expand Down Expand Up @@ -192,7 +192,7 @@ def comment_streak(span = 365)
streak = 0
comment_count = 0
(0..span).each do |day|
days[day] = DrupalComment.count :all, :select => :timestamp, :conditions => {:uid => self.drupal_user.uid, :status => 1, :timestamp => Time.now.midnight.to_i-day.days.to_i..Time.now.midnight.to_i-(day-1).days.to_i}
days[day] = Comment.count :all, :select => :timestamp, :conditions => {:uid => self.drupal_user.uid, :status => 1, :timestamp => Time.now.midnight.to_i-day.days.to_i..Time.now.midnight.to_i-(day-1).days.to_i}
break if days[day] == 0
streak+=1
comment_count+=days[day]
Expand Down
2 changes: 1 addition & 1 deletion app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_tags(input, limit=5)
end

def find_comments(input, limit=5)
DrupalComment.limit(limit)
Comment.limit(limit)
.order('nid DESC')
.where('status = 1 AND comment LIKE ?', '%' + input + '%')
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/typeahead_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def find_tags(input, limit=5)
end

def find_comments(input, limit=5)
DrupalComment.limit(limit)
Comment.limit(limit)
.order('nid DESC')
.where('status = 1 AND comment LIKE ?', '%' + input + '%')
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/_activity.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<% activity.each_with_index do |node, i| %>
<% if node.is_a?(DrupalNodeRevision) || node.type == 'page' %>
<%= render partial: "dashboard/node_wiki", locals: { node: node, index: i } %>
<% elsif node.is_a?(DrupalComment) %>
<% elsif node.is_a?(Comment) %>
<%= render partial: "dashboard/node_comment", locals: { node: node, index: i } %>
<% elsif node.has_power_tag('question') %>
<%= render partial: "dashboard/node_question", locals: { node: node, index: i } %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/_node_meta.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% unless node.is_a?(DrupalComment) || node.is_a?(DrupalNodeRevision) || node.type == 'page' %>
<% unless node.is_a?(Comment) || node.is_a?(DrupalNodeRevision) || node.type == 'page' %>
<%= t('dashboard.dashboard.by') %> <a href="/profile/<%= node.author.name %>"><%= node.author.name %></a>
<% end %>
<%= distance_of_time_in_words(node.created_at, Time.current, false, :scope => :'datetime.time_ago_in_words') %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/notes/_comments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<div id="comments" class="col-md-10 comments">

<h3><span id="comment-count"><%= @node.drupal_comments.length %></span> <%= t('notes._comments.comments') %></h3>
<h3><span id="comment-count"><%= @node.comments.length %></span> <%= t('notes._comments.comments') %></h3>

<div id="comments-container">
<% @node.drupal_comments.each do |comment| %>
<% if comment.cid == @node.drupal_comments.last.cid %><a id="last" name="last"></a><% end %>
<% @node.comments.order('timestamp ASC').each do |comment| %>
<% if comment.cid == @node.comments.first.cid %><a id="last" name="last"></a><% end %>

<%= render :partial => "notes/comment", :locals => {:comment => comment} %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/stats.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<hr />

<h4><%= raw t('notes.stats.comments_posted', :count => DrupalComment.count) %></h4>
<h4><%= raw t('notes.stats.comments_posted', :count => Comment.count) %></h4>

<p><i><%= t('notes.stats.comments_posted_52_weeks') %></i></p>

Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<h4><span id="answer-0-comment-count"><%= @node.comments.length %></span> Comments</h4>

<% @node.comments('ASC').each do |comment| %>
<% @node.comments.order("timestamp ASC").each do |comment| %>
<%= render partial: "questions/comment", locals: { comment: comment, answer_id: 0 } %>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/searches/_node_result.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<% if node.author.name.present? %>
| by <a href="/profile/<%= node.author.name %>"> <%= node.author.name %> </a>
<% end %>
| <%= pluralize node.drupal_comments.count, 'comment' %>
| <%= pluralize node.comments.count, 'comment' %>
</div>
<div class="panel-body">
<%= node.drupal_node_revision.try(:first).try(:body).try(:first, 350) %>...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class AddCommentsCountToDrupalNode < ActiveRecord::Migration
def up
add_column :node, :drupal_comments_count, :integer, default: 0
add_column :node, :comments_count, :integer, default: 0
DrupalNode.reset_column_information
DrupalNode.all.each do |node|
DrupalNode.reset_counters(node.id, :drupal_comments)
DrupalNode.reset_counters(node.id, :comments)
end
end

def down
remove_column :node, :drupal_comments_count
remove_column :node, :comments_count
end
end
2 changes: 1 addition & 1 deletion db/schema.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ActiveRecord::Schema.define(:version => 20161122154603) do
t.integer "tnid", :default => 0, :null => false
t.integer "translate", :default => 0, :null => false
t.integer "cached_likes", :default => 0
t.integer "drupal_comments_count", :default => 0
t.integer "comments_count", :default => 0
t.integer "drupal_node_revisions_count", :default => 0
t.string "path"
t.integer "main_image_id"
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
blog_post_tag = DrupalTag.create! "name"=>"blog", "description"=>"", "weight"=>0
blog_post_community_tag = DrupalNodeCommunityTag.create! "tid"=>blog_post_tag.id,
"nid"=>blog_post.id, "uid"=>admin.id
blog_post_comment = DrupalComment.create! "nid"=>blog_post.id, "uid"=>admin.id,
blog_post_comment = Comment.create! "nid"=>blog_post.id, "uid"=>admin.id,
"subject"=>"", "comment"=>"Example Comment\r\n", "hostname"=>"", "status"=>0,
"format"=>1, "thread"=>"01/"

Expand Down
22 changes: 11 additions & 11 deletions test/functional/comment_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setup

test "should create note comments" do
UserSession.create(rusers(:bob))
assert_difference 'DrupalComment.count' do
assert_difference 'Comment.count' do
xhr :post, :create,
id: node(:one).nid,
body: "Notes comment"
Expand All @@ -26,7 +26,7 @@ def setup

test "should create question comments" do
UserSession.create(rusers(:bob))
assert_difference 'DrupalComment.count' do
assert_difference 'Comment.count' do
xhr :post, :create,
id: node(:question).nid,
body: "Questions comment",
Expand All @@ -39,7 +39,7 @@ def setup

test "should show error if node comment not saved" do
UserSession.create(rusers(:bob))
assert_no_difference 'DrupalComment.count' do
assert_no_difference 'Comment.count' do
xhr :post, :create,
id: node(:one).nid
end
Expand All @@ -49,7 +49,7 @@ def setup

test "should create answer comments" do
UserSession.create(rusers(:bob))
assert_difference 'DrupalComment.count' do
assert_difference 'Comment.count' do
xhr :post, :answer_create,
aid: answers(:one).id,
body: "Answers comment"
Expand All @@ -61,7 +61,7 @@ def setup

test "should show error if answer comment not saved" do
UserSession.create(rusers(:bob))
assert_no_difference 'DrupalComment.count' do
assert_no_difference 'Comment.count' do
xhr :post, :answer_create,
aid: answers(:one).id
end
Expand Down Expand Up @@ -137,7 +137,7 @@ def setup
test "should delete note comment if user is comment author" do
UserSession.create(rusers(:bob))
comment = comments(:first)
assert_difference 'DrupalComment.count', -1 do
assert_difference 'Comment.count', -1 do
xhr :get, :delete,
id: comment.id
end
Expand All @@ -148,7 +148,7 @@ def setup
test "should delete note comment if user is note author" do
UserSession.create(rusers(:bob))
comment = comments(:second)
assert_difference 'DrupalComment.count', -1 do
assert_difference 'Comment.count', -1 do
xhr :get, :delete,
id: comment.id
end
Expand All @@ -159,7 +159,7 @@ def setup
test "should delete note comment if user is admin" do
UserSession.create(rusers(:admin))
comment = comments(:first)
assert_difference 'DrupalComment.count', -1 do
assert_difference 'Comment.count', -1 do
xhr :get, :delete,
id: comment.id
end
Expand All @@ -170,7 +170,7 @@ def setup
test "should delete note comment if user is comment moderator" do
UserSession.create(rusers(:moderator))
comment = comments(:first)
assert_difference 'DrupalComment.count', -1 do
assert_difference 'Comment.count', -1 do
xhr :get, :delete,
id: comment.id
end
Expand All @@ -181,7 +181,7 @@ def setup
test "should not delete note comment if user is neither of the above" do
UserSession.create(rusers(:newcomer))
comment = comments(:first)
assert_no_difference 'DrupalComment.count' do
assert_no_difference 'Comment.count' do
get :delete,
id: comment.id
end
Expand All @@ -192,7 +192,7 @@ def setup
test "should delete question/answer comment if user is comment author" do
UserSession.create(rusers(:bob))
comment = comments(:first)
assert_difference 'DrupalComment.count', -1 do
assert_difference 'Comment.count', -1 do
xhr :get, :delete,
id: comment.id,
type: 'question'
Expand Down
Loading