Stream entry specs and refactor to use delegate (#2827)
* Add coverage for stream entry delegated methods * Use delegate with allow_nil to clean up stream entry
This commit is contained in:
		
				
					committed by
					
						 Eugen Rochko
						Eugen Rochko
					
				
			
			
				
	
			
			
			
						parent
						
							dacdfec973
						
					
				
				
					commit
					3f5b994ff0
				
			| @@ -1,4 +1,5 @@ | |||||||
| # frozen_string_literal: true | # frozen_string_literal: true | ||||||
|  |  | ||||||
| # == Schema Information | # == Schema Information | ||||||
| # | # | ||||||
| # Table name: stream_entries | # Table name: stream_entries | ||||||
| @@ -26,6 +27,10 @@ class StreamEntry < ApplicationRecord | |||||||
|   default_scope { where(activity_type: 'Status') } |   default_scope { where(activity_type: 'Status') } | ||||||
|   scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) } |   scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) } | ||||||
|  |  | ||||||
|  |   delegate :target, :title, :content, :thread, | ||||||
|  |            to: :status, | ||||||
|  |            allow_nil: true | ||||||
|  |  | ||||||
|   def object_type |   def object_type | ||||||
|     orphaned? || targeted? ? :activity : status.object_type |     orphaned? || targeted? ? :activity : status.object_type | ||||||
|   end |   end | ||||||
| @@ -38,26 +43,10 @@ class StreamEntry < ApplicationRecord | |||||||
|     [:follow, :request_friend, :authorize, :reject, :unfollow, :block, :unblock, :share, :favorite].include? verb |     [:follow, :request_friend, :authorize, :reject, :unfollow, :block, :unblock, :share, :favorite].include? verb | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def target |  | ||||||
|     orphaned? ? nil : status.target |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   def title |  | ||||||
|     orphaned? ? nil : status.title |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   def content |  | ||||||
|     orphaned? ? nil : status.content |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   def threaded? |   def threaded? | ||||||
|     (verb == :favorite || object_type == :comment) && !thread.nil? |     (verb == :favorite || object_type == :comment) && !thread.nil? | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def thread |  | ||||||
|     orphaned? ? nil : status.thread |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   def mentions |   def mentions | ||||||
|     orphaned? ? [] : status.mentions.map(&:account) |     orphaned? ? [] : status.mentions.map(&:account) | ||||||
|   end |   end | ||||||
|   | |||||||
| @@ -26,4 +26,52 @@ RSpec.describe StreamEntry, type: :model do | |||||||
|       expect(status.stream_entry.threaded?).to be false |       expect(status.stream_entry.threaded?).to be false | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   describe 'delegated methods' do | ||||||
|  |     context 'with a nil status' do | ||||||
|  |       subject { described_class.new(status: nil) } | ||||||
|  |  | ||||||
|  |       it 'returns nil for target' do | ||||||
|  |         expect(subject.target).to be_nil | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'returns nil for title' do | ||||||
|  |         expect(subject.title).to be_nil | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'returns nil for content' do | ||||||
|  |         expect(subject.content).to be_nil | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'returns nil for thread' do | ||||||
|  |         expect(subject.thread).to be_nil | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     context 'with a real status' do | ||||||
|  |       let(:original) { Fabricate(:status, text: 'Test status') } | ||||||
|  |       let(:status) { Fabricate(:status, reblog: original, thread: original) } | ||||||
|  |       subject { described_class.new(status: status) } | ||||||
|  |  | ||||||
|  |       it 'delegates target' do | ||||||
|  |         expect(status.target).not_to be_nil | ||||||
|  |         expect(subject.target).to eq(status.target) | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'delegates title' do | ||||||
|  |         expect(status.title).not_to be_nil | ||||||
|  |         expect(subject.title).to eq(status.title) | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'delegates content' do | ||||||
|  |         expect(status.content).not_to be_nil | ||||||
|  |         expect(subject.content).to eq(status.content) | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'delegates thread' do | ||||||
|  |         expect(status.thread).not_to be_nil | ||||||
|  |         expect(subject.thread).to eq(status.thread) | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |   end | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user