Skip to content

Commit

Permalink
correct memoization in successor and predecessor of Journal
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 13, 2025
1 parent b269952 commit fdf0fe7
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions app/models/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ def previous
end

def successor
@successor ||= self.class
.where(journable_type:, journable_id:)
.where("#{self.class.table_name}.version > ?", version)
.order(version: :asc)
.first
return @successor if defined?(@successor)

@successor = self.class
.where(journable_type:, journable_id:)
.where("#{self.class.table_name}.version > ?", version)
.order(version: :asc)
.first
end

def noop?
Expand Down Expand Up @@ -194,14 +196,16 @@ def has_file_links?
end

def predecessor
@predecessor ||= if initial?
nil
else
self.class
.where(journable_type:, journable_id:)
.where("#{self.class.table_name}.version < ?", version)
.order(version: :desc)
.first
end
return @predecessor if defined?(@predecessor)

@predecessor = if initial?
nil
else
self.class
.where(journable_type:, journable_id:)
.where("#{self.class.table_name}.version < ?", version)
.order(version: :desc)
.first
end
end
end

0 comments on commit fdf0fe7

Please sign in to comment.