Prefer polymorphism over is_a?

This commit is contained in:
Douwe Maan 2017-08-17 19:26:45 +02:00
parent 9c22974cd1
commit 5f758aff57
5 changed files with 14 additions and 6 deletions

View file

@ -18,7 +18,7 @@ module NotesActions
@notes = prepare_notes_for_rendering(@notes)
notes_json[:notes] =
if noteable.is_a?(Issue)
if noteable.discussions_rendered_on_frontend?
note_serializer.represent(@notes)
else
@notes.map { |note| note_json(note) }
@ -87,7 +87,7 @@ module NotesActions
if note.persisted?
attrs[:valid] = true
if noteable.is_a?(Issue)
if noteable.discussions_rendered_on_frontend?
attrs.merge!(note_serializer.represent(note))
else
attrs.merge!(

View file

@ -37,7 +37,7 @@ class AwardEmoji < ActiveRecord::Base
end
def expire_etag_cache
return unless awardable.is_a?(Note)
return unless awardable.respond_to?(:expire_etag_cache)
awardable.expire_etag_cache
end

View file

@ -24,6 +24,10 @@ module Noteable
DiscussionNote::NOTEABLE_TYPES.include?(base_class_name)
end
def discussions_rendered_on_frontend?
false
end
def discussion_notes
notes
end

View file

@ -269,6 +269,10 @@ class Issue < ActiveRecord::Base
end
end
def discussions_rendered_on_frontend?
true
end
private
# Returns `true` if the given User can read the current Issue.

View file

@ -300,12 +300,12 @@ class Note < ActiveRecord::Base
end
def expire_etag_cache
return unless for_issue?
return unless noteable.discussions_rendered_on_frontend?
key = Gitlab::Routing.url_helpers.project_noteable_notes_path(
noteable.project,
project,
target_type: noteable_type.underscore,
target_id: noteable.id
target_id: noteable_id
)
Gitlab::EtagCaching::Store.new.touch(key)
end