Skip to content

Commit ef4a372

Browse files
Deprecate answers model (#11308)
* Deprecate answers model * Fixed unit and functional tests * Comments on parts that need clarification * Fixed the activity function in home controller * More fixes to functional tests * Fixed a unit test
1 parent 3994614 commit ef4a372

22 files changed

+44
-106
lines changed

app/api/srch/search.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Search < Grape::API
7777
doc_type: 'QUESTIONS',
7878
doc_url: model.path(:question),
7979
doc_title: model.title,
80-
score: model.answers.length
80+
score: model.comments.length
8181
)
8282
end
8383
DocList.new(results_list.flatten, search_request)
@@ -257,7 +257,7 @@ class Search < Grape::API
257257
doc_type: 'QUESTIONS',
258258
doc_url: model.path(:question),
259259
doc_title: model.title,
260-
score: model.answers.length
260+
score: model.comments.length
261261
)
262262
end
263263

@@ -320,7 +320,7 @@ class Search < Grape::API
320320
doc_title: model.title,
321321
doc_author: model.user.username,
322322
doc_image_url: model.images.empty? ? 0 : model.images.first.path,
323-
score: model.answers.length,
323+
score: model.comments.length,
324324
latitude: model.lat,
325325
longitude: model.lon,
326326
blurred: model.blurred?,

app/controllers/application_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def set_locale
137137
end
138138

139139
def comments_node_and_path
140-
@node = @comment.aid == 0 ? @comment.node : @comment.answer.node
140+
@node = @comment.node
141141

142142
@path = params[:type] && params[:type] == 'question' ? @node.path(:question) : @node.path
143143
end

app/controllers/home_controller.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,14 @@ def activity
145145
# group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp
146146
comments = comments.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == 'production'
147147
comments = comments.to_a # ensure it can be serialized for caching
148-
answer_comments = Comment.joins(:answer, :user)
149-
.order('timestamp DESC')
150-
.where('timestamp - answers.created_at > ?', 86_400)
151-
.limit(20)
152-
.group(['answers.id', 'comments.cid']) # ONLY_FULL_GROUP_BY, issue #3120
153-
answer_comments = answer_comments.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == 'production'
154-
answer_comments = answer_comments.to_a # ensure it can be serialized for caching
155-
activity = (notes + wikis + comments + answer_comments).sort_by(&:created_at).reverse
148+
activity = (notes + wikis + comments).sort_by(&:created_at).reverse
156149
response = [
157150
activity,
158151
blog,
159152
notes,
160153
wikis,
161154
revisions,
162-
comments,
163-
answer_comments
155+
comments
164156
]
165157
response
166158
end

app/models/answer.rb

-35
This file was deleted.

app/models/comment.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class Comment < ApplicationRecord
44

55
belongs_to :node, foreign_key: 'nid', touch: true, counter_cache: true
66
belongs_to :user, foreign_key: 'uid'
7-
belongs_to :answer, foreign_key: 'aid'
87
has_many :likes, as: :likeable
98

109
has_many :replied_comments, class_name: "Comment", foreign_key: 'reply_to', dependent: :destroy
@@ -21,7 +20,7 @@ class Comment < ApplicationRecord
2120
def self.inheritance_column
2221
'rails_type'
2322
end
24-
23+
2524
def self.search(query)
2625
Comment.where('MATCH(comment) AGAINST(?)', query)
2726
.where(status: 1)
@@ -90,7 +89,7 @@ def next_thread
9089
end
9190

9291
def parent
93-
aid.zero? ? node : answer&.node
92+
node
9493
end
9594

9695
def status_value
@@ -155,14 +154,14 @@ def notify(current_user)
155154
uids = uids_to_notify - already
156155
uids+= current_user.followers.collect(&:uid)
157156
uids.uniq!
158-
157+
159158
# Send Browser Notification Using Action Cable
160159
notify_user_ids = uids_to_notify + already
161160
notify_user_ids = notify_user_ids.uniq
162161
send_browser_notification notify_user_ids
163-
162+
164163
uids = uids.select { |i| i != 0 } # remove bad comments (some early ones lack uid)
165-
164+
166165
notify_users(uids, current_user)
167166
notify_tag_followers(already + uids)
168167
end

app/models/node.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def updated_month
9191
has_many :drupal_content_field_map_editor, foreign_key: 'nid' # , dependent: :destroy # re-enable in Rails 5
9292
has_many :images, foreign_key: :nid
9393
has_many :node_selections, foreign_key: :nid, dependent: :destroy
94-
has_many :answers, foreign_key: :nid, dependent: :destroy
9594

9695
belongs_to :user, foreign_key: 'uid'
9796

@@ -639,7 +638,7 @@ def self.for_tagname_and_type(tagname, type = 'note', options = {})
639638
def self.for_wildcard_tagname_and_type(tagname, type = 'note')
640639
search_term = tagname[0..-2] + '%'
641640
Node.where(status: 1, type: type)
642-
.includes(:revision, :tag, :answers)
641+
.includes(:revision, :tag)
643642
.references(:term_data, :node_revisions)
644643
.where('term_data.name LIKE (?) OR term_data.parent LIKE (?)', search_term, search_term)
645644
end

app/models/user.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ module Frequency
5757
has_many :following_users, through: :active_relationships, source: :followed
5858
has_many :followers, through: :passive_relationships, source: :follower
5959
has_many :likes
60-
has_many :answers, foreign_key: :uid
6160
has_many :answer_selections, foreign_key: :user_id
6261
has_many :revisions, through: :node
6362
has_many :comments, foreign_key: :uid
@@ -436,11 +435,10 @@ def find_by_username_case_insensitive(username)
436435
# all users who've posted a node, comment, or answer in the given period
437436
def contributor_count_for(start_time, end_time)
438437
notes = Node.where(type: 'note', status: 1, created: start_time.to_i..end_time.to_i).pluck(:uid)
439-
answers = Answer.where(created_at: start_time..end_time).pluck(:uid)
440438
questions = Node.questions.where(status: 1, created: start_time.to_i..end_time.to_i).pluck(:uid)
441439
comments = Comment.where(timestamp: start_time.to_i..end_time.to_i).pluck(:uid)
442440
revisions = Revision.where(status: 1, timestamp: start_time.to_i..end_time.to_i).pluck(:uid)
443-
contributors = (notes + answers + questions + comments + revisions).compact.uniq.size
441+
contributors = (notes + questions + comments + revisions).compact.uniq.size
444442
contributors
445443
end
446444

@@ -467,12 +465,11 @@ def create_with_omniauth(auth)
467465

468466
def count_all_time_contributor
469467
notes = Node.where(type: 'note', status: 1).pluck(:uid)
470-
answers = Answer.pluck(:uid)
471468
questions = Node.questions.where(status: 1).pluck(:uid)
472469
comments = Comment.pluck(:uid)
473470
revisions = Revision.where(status: 1).pluck(:uid)
474471

475-
(notes + answers + questions + comments + revisions).compact.uniq.size
472+
(notes + questions + comments + revisions).compact.uniq.size
476473
end
477474

478475
def watching_location(nwlat, selat, nwlng, selng)

app/views/home/home.html.erb

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<% cache('feature_home-intro', skip_digest: true) do %>
22
<%= feature("home-intro") %>
33
<% end %>
4-
54
<div class="container">
65

76
<div class="projects">

app/views/questions/_questions_shadow.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div>
1616
<div class="answer">
1717
<% if params[:controller] == 'questions' %>
18-
<a class="peopleResponse" href="<%= node.path(:question) %>#answers" title="Answers"><%= node.answers.length %> <%=translation('questions._questions_shadow.people_responded')%></a>
18+
<a class="peopleResponse" href="<%= node.path(:question) %>#answers" title="Answers"><%= node.comments.length %> <%=translation('questions._questions_shadow.people_responded')%></a>
1919
<% end %>
2020
</div>
2121
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DropAnswers < ActiveRecord::Migration[5.2]
2+
def change
3+
drop_table :answers
4+
end
5+
end

test/fixtures/answers.yml

-19
This file was deleted.

test/fixtures/comments.yml

+2-8
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ moderator:
5050

5151
answer_comment_one:
5252
uid: 1
53-
nid: 0
54-
aid: 1
53+
nid: 56
5554
status: 1
5655
comment: Answer comment one
5756
timestamp: <%= Time.now.to_i + 7 %>
5857

5958
answer_comment_two:
6059
uid: 2
61-
nid: 0
62-
aid: 1
60+
nid: 56
6361
status: 1
6462
comment: Answer comment two
6563
timestamp: <%= Time.now.to_i + 8 %>
@@ -75,7 +73,6 @@ wiki_comment:
7573
question:
7674
uid: 1
7775
nid: 8
78-
aid: 0
7976
status: 1
8077
comment: Question comment
8178
timestamp: <%= Time.now.to_i + 9 %>
@@ -104,7 +101,6 @@ comment_status_4:
104101
question_one:
105102
uid: 2
106103
nid: 8
107-
aid: 0
108104
status: 1
109105
comment: Question comment one
110106
timestamp: <%= Time.now.to_i + 11 %>
@@ -113,7 +109,6 @@ question_one:
113109
question_callout:
114110
uid: 2
115111
nid: 8
116-
aid: 0
117112
status: 1
118113
comment: Hey, @bob nice question
119114
timestamp: <%= Time.now.to_i + 12 %>
@@ -122,7 +117,6 @@ question_callout:
122117
question_tag:
123118
uid: 2
124119
nid: 8
125-
aid: 0
126120
status: 1
127121
comment: 'Question #everything'
128122
timestamp: <%= Time.now.to_i + 13 %>

test/fixtures/nodes.yml

+13
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,16 @@ email:
686686
type: "note"
687687
cached_likes: 0
688688
slug: "note-email-links"
689+
690+
# Node to link comments with no parent nodes, so that other tests don't fail
691+
dummy_node:
692+
nid: 56
693+
uid: 2
694+
title: "Dummy Node"
695+
path: "/notes/jeff/12-23-2020/dummy-node"
696+
created: <%= DateTime.new(2020,12,23).to_i %>
697+
changed: <%= DateTime.new(2020,12,23).to_i %>
698+
status: 8 # Intentional status so that other tests do not fail.
699+
type: "note"
700+
cached_likes: 0
701+
slug: "note-dummy-node"

test/functional/api/search_api_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def app
128128
end
129129

130130
test 'search Tag Nearby Nodes functionality with a valid query' do
131-
get '/api/srch/taglocations?nwlat=180.0&selat=0.0&nwlng=0.0&selng=176.5'
131+
get "/api/srch/taglocations?nwlat=180.0&selat=0.0&nwlng=0.0&selng=176.5"
132132
assert last_response.ok?
133133

134134
# Expected search pattern
@@ -178,7 +178,7 @@ def app
178178
end
179179

180180
test 'search Tag Nearby Nodes functionality with a valid query and specific period' do
181-
get '/api/srch/taglocations?nwlat=171.0&selat=0.0&nwlng=0.0&selng=174.8&sort_by=recent&order_direction=ASC&to=2018-08-16'
181+
get "/api/srch/taglocations?nwlat=171.0&selat=0.0&nwlng=0.0&selng=174.8&sort_by=recent&order_direction=ASC&to=2018-08-16" # Very weird Test failing here
182182
assert last_response.ok?
183183

184184
# Expected search pattern
@@ -204,7 +204,7 @@ def app
204204
end
205205

206206
test 'search Tag Nearby Nodes functionality with a valid query and from date greater then to date' do
207-
get '/api/srch/taglocations?nwlat=180.0&selat=0.0&nwlng=0.0&selng=180.0&sort_by=recent&order_direction=ASC&from=2018-08-16&to=2018-07-31'
207+
get "/api/srch/taglocations?nwlat=180.0&selat=0.0&nwlng=0.0&selng=180.0&sort_by=recent&order_direction=ASC&from=2018-08-16&to=2018-07-31"
208208
assert last_response.ok?
209209

210210
# Expected search pattern

test/functional/comment_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def teardown
174174
post :update, params: { id: comment.id, body: new_comment_body, type: 'question' }
175175
comment.reload
176176
assert_equal new_comment_body, comment.comment
177-
assert_redirected_to comment.answer.node.path(:question) + '?_=' + Time.now.to_i.to_s
177+
assert_redirected_to comment.node.path(:question) + '?_=' + Time.now.to_i.to_s
178178
assert_equal flash[:notice], 'Comment updated.'
179179
end
180180

test/functional/home_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def setup
1111

1212
get :home
1313
assert_response :success
14-
assert_select "title", Sanitize.clean('&#127880;') + (" Public Lab: #{title}")
14+
assert_select "title", Sanitize.clean('&#127880;') + (" Public Lab: #{title}") # Test failing here
1515
end
1616

1717
test 'home should redirect to dashboard if logged in' do

test/functional/notes_controller_test.rb

-5
Original file line numberDiff line numberDiff line change
@@ -803,17 +803,12 @@ def test_get_rss_feed
803803
UserSession.create(users(:moderator))
804804
node = nodes(:question)
805805
node.save
806-
answer1 = answers(:one)
807-
answer1.save
808-
answer2 = answers(:two)
809-
answer2.save
810806
n_count = Node.count
811807

812808
post :delete, params: { id: node.id }, xhr: true
813809

814810
assert_response :success
815811
assert_equal Node.count, n_count - 1
816-
assert_equal Answer.count, 0
817812
end
818813

819814
test 'moderator can publish the draft' do

test/functional/questions_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def setup
9696
assert_response :success
9797
assert_equal assigns(:title), 'Unanswered questions'
9898
assert_not_nil assigns(:questions)
99-
assert_equal assigns(:questions).first.answers.length, 0
99+
assert_equal assigns(:questions).first.comments.length, 0
100100
assert_template :index
101101
end
102102

test/functional/wiki_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def teardown
484484

485485
assert_response :success
486486
assert_template :index
487-
assert_select "title", Sanitize.clean('&#127880;') + (" Public Lab: Popular wiki pages")
487+
assert_select "title", Sanitize.clean('&#127880;') + (" Public Lab: Popular wiki pages") # Test failing here
488488
end
489489

490490
test 'should display well liked wiki pages' do

test/unit/comment_test.rb

-2
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ def setup
155155
end
156156

157157
test 'should create comments for answers' do
158-
answer = answers(:one)
159158
comment = Comment.new(
160159
uid: users(:bob).id,
161-
aid: answer.id,
162160
comment: 'Test comment'
163161
)
164162
assert comment.save

0 commit comments

Comments
 (0)