Skip to content

Commit

Permalink
Add alternate comparison types to versions
Browse files Browse the repository at this point in the history
- The types are:
-- Previous: The default and the previously used type
-- Subsequent: Compares against the next version
-- Current: Compares against the current version
- Allow switching between comparison types in index and diff views
-- Have links vary depending upon current comparison type
  • Loading branch information
BrokenEagle committed Mar 17, 2020
1 parent a95e57d commit e23ee17
Show file tree
Hide file tree
Showing 41 changed files with 487 additions and 220 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def respond_with(subject, *options, &block)
super
end

def set_version_comparison
params[:type] = %w[previous subsequent current].include?(params[:type]) ? params[:type] : "previous"
end

def model_name
controller_name.classify
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/artist_commentary_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ArtistCommentaryVersionsController < ApplicationController
respond_to :html, :xml, :json

def index
set_version_comparison
@commentary_versions = ArtistCommentaryVersion.paginated_search(params)
@commentary_versions = @commentary_versions.includes(:updater, post: :uploader) if request.format.html?

Expand Down
1 change: 1 addition & 0 deletions app/controllers/artist_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ArtistVersionsController < ApplicationController
respond_to :html, :xml, :json

def index
set_version_comparison
@artist_versions = ArtistVersion.paginated_search(params)
@artist_versions = @artist_versions.includes(:updater, artist: :urls) if request.format.html?

Expand Down
1 change: 1 addition & 0 deletions app/controllers/note_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class NoteVersionsController < ApplicationController
respond_to :html, :xml, :json

def index
set_version_comparison
@note_versions = NoteVersion.paginated_search(params)
@note_versions = @note_versions.includes(:updater) if request.format.html?

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/pool_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class PoolVersionsController < ApplicationController
around_action :set_timeout

def index
set_version_comparison
@pool_versions = PoolVersion.paginated_search(params)
@pool_versions = @pool_versions.includes(:updater, :pool) if request.format.html?

Expand All @@ -19,7 +20,8 @@ def diff
if params[:other_id]
@other_version = PoolVersion.find(params[:other_id])
else
@other_version = @pool_version.previous
set_version_comparison
@other_version = @pool_version.send(params[:type])
end
end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/post_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PostVersionsController < ApplicationController
respond_to :js, only: [:undo]

def index
set_version_comparison
@post_versions = PostVersion.paginated_search(params)

if request.format.html?
Expand Down
24 changes: 15 additions & 9 deletions app/controllers/wiki_page_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class WikiPageVersionsController < ApplicationController
layout "sidebar"

def index
set_version_comparison
@wiki_page_versions = WikiPageVersion.paginated_search(params)
@wiki_page_versions = @wiki_page_versions.includes(:updater) if request.format.html?

Expand All @@ -16,15 +17,20 @@ def show

def diff
if params[:thispage].blank? || params[:otherpage].blank?
redirect_back fallback_location: wiki_pages_path, notice: "You must select two versions to diff"
return
end

@thispage = WikiPageVersion.find(params[:thispage])
@otherpage = WikiPageVersion.find(params[:otherpage])

if @thispage.id < @otherpage.id
@thispage, @otherpage = @otherpage, @thispage
page_id = params[:thispage] || params[:otherpage]
if page_id.blank?
redirect_back fallback_location: wiki_pages_path, notice: "You must select at least one version to diff"
return
end
set_version_comparison
@thispage = WikiPageVersion.find(page_id)
@otherpage = @thispage.send(params[:type])
else
@thispage = WikiPageVersion.find(params[:thispage])
@otherpage = WikiPageVersion.find(params[:otherpage])
if @thispage.id < @otherpage.id
@thispage, @otherpage = @otherpage, @thispage
end
end

respond_with([@thispage, @otherpage])
Expand Down
46 changes: 33 additions & 13 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,45 @@ def listing_type(*fields, member_check: true, types: [:revert, :standard])
(fields.reduce(false) { |acc, field| acc || params.dig(:search, field).present? } && (!member_check || CurrentUser.is_member?) ? types[0] : types[1])
end

def diff_list_html(new, old, latest, ul_class: ["diff-list"], li_class: [])
diff = SetDiff.new(new, old, latest)
def diff_list_html(this_list, other_list, ul_class: ["diff-list"], li_class: [])
diff = SetDiff.new(this_list, other_list)
render "diff_list", diff: diff, ul_class: ul_class, li_class: li_class
end

def diff_name_html(this_name, prev_name)
def diff_name_html(this_name, other_name)
pattern = Regexp.new('.')
DiffBuilder.new(this_name, prev_name, pattern).build
DiffBuilder.new(this_name, other_name, pattern).build
end

def diff_body_html(record, previous, field)
return h(record[field]).gsub(/\r?\n/, '<span class="paragraph-mark">¶</span><br>').html_safe if previous.blank?
def diff_body_html(record, other, field)
if record.blank? || other.blank?
diff_record = other.presence || record
return h(diff_record[field]).gsub(/\r?\n/, '<span class="paragraph-mark">¶</span><br>').html_safe
end

pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
DiffBuilder.new(record[field], previous[field], pattern).build
DiffBuilder.new(record[field], other[field], pattern).build
end

def status_diff_html(record)
previous = record.previous
def status_diff_html(record, type)
other = record.send(type)

return "New" if previous.blank?
if other.blank?
return type == "previous" ? "New" : ""
end

statuses = []
record.class.status_fields.each do |field, status|
if record.has_attribute?(field)
statuses += [status] if record[field] != previous[field]
statuses += [status] if record[field] != other[field]
else
statuses += [status] if record.send(field)
statuses += [status] if record.send(field, type)
end
end
statuses.join("<br>").html_safe

altered = record.updater_id != other.updater_id

%(<div class="version-statuses" data-altered="#{altered}">#{statuses.join("<br>")}</div>).html_safe
end

def wordbreakify(string)
Expand All @@ -44,6 +52,18 @@ def wordbreakify(string)
raw(wordbreaked_string)
end

def version_type_links(params)
html = []
%w[previous subsequent current].each do |type|
if type == params[:type]
html << %(<span>#{type}</span>)
else
html << tag.li(link_to(type, params.except(:controller, :action).merge(type: type).permit!))
end
end
html.join(" | ").html_safe
end

def nav_link_to(text, url, **options)
klass = options.delete(:class)

Expand Down
10 changes: 10 additions & 0 deletions app/helpers/artist_commentary_versions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ArtistCommentaryVersionsHelper
def commentary_version_field_diff(commentary_version, type, field)
other = commentary_version.send(params[:type])
if type == "previous"
diff_body_html(commentary_version, other, field)
else
diff_body_html(other, commentary_version, field)
end
end
end
65 changes: 47 additions & 18 deletions app/helpers/artist_versions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,63 @@
module ArtistVersionsHelper
def artist_version_other_names_diff(artist_version)
new_names = artist_version.other_names
old_names = artist_version.previous.try(:other_names)
latest_names = artist_version.artist.other_names
def artist_version_other_names_diff(artist_version, type)
other = artist_version.send(type)
this_names = artist_version.other_names
if other.present?
other_names = other.other_names
elsif type == "subsequent"
other_names = this_names
else
other_names = []
end

diff_list_html(new_names, old_names, latest_names)
if type == "previous"
diff_list_html(this_names, other_names)
else
diff_list_html(other_names, this_names)
end
end

def artist_version_urls_diff(artist_version)
new_urls = artist_version.urls
old_urls = artist_version.previous.try(:urls)
latest_urls = artist_version.artist.urls.map(&:to_s)
def artist_version_urls_diff(artist_version, type)
other = artist_version.send(type)
this_urls = artist_version.urls
if other.present?
other_urls = other.urls
elsif type == "subsequent"
other_urls = this_urls
else
other_urls = []
end

diff_list_html(new_urls, old_urls, latest_urls)
if type == "previous"
diff_list_html(this_urls, other_urls)
else
diff_list_html(other_urls, this_urls)
end
end

def artist_version_name_diff(artist_version)
previous = artist_version.previous
if previous.present? && (artist_version.name != previous.name)
name_diff = diff_name_html(artist_version.name, previous.name)
def artist_version_name_diff(artist_version, type)
other = artist_version.send(type)
if other.present? && (artist_version.name != other.name)
if type == "previous"
name_diff = diff_name_html(artist_version.name, other.name)
else
name_diff = diff_name_html(other.name, artist_version.name)
end
%(<br><br><b>Rename:</b><br>&ensp;#{name_diff}</p>).html_safe
else
""
end
end

def artist_version_group_name_diff(artist_version)
previous = artist_version.previous
if artist_version.group_name.present? || (previous.present? && previous.group_name.present?)
group_name_diff = diff_name_html(artist_version.group_name, previous.group_name)
def artist_version_group_name_diff(artist_version, type)
other = artist_version.send(type)
if artist_version.group_name.present? || (other.present? && other.group_name.present?)
other_group_name = (other.present? ? other.group_name : artist_version.group_name)
if type == "previous"
group_name_diff = diff_name_html(artist_version.group_name, other_group_name)
else
group_name_diff = diff_name_html(other_group_name, artist_version.group_name)
end
%(<b>Group:</b><br>&ensp;#{group_name_diff}<br><br>).html_safe
else
""
Expand Down
29 changes: 21 additions & 8 deletions app/helpers/note_versions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
module NoteVersionsHelper
def note_version_position_diff(note_version)
previous = note_version.previous
def note_version_position_diff(note_version, type)
other = note_version.send(type)

html = "#{note_version.x},#{note_version.y}"
if previous.nil? || (note_version.x == previous.x && note_version.y == previous.y)
if other.nil? || (note_version.x == other.x && note_version.y == other.y)
html
elsif type == "previous"
"#{other.x},#{other.y} -> " + html
else
"#{previous.x},#{previous.y} -> " + html
html + " -> #{other.x},#{other.y}"
end
end

def note_version_size_diff(note_version)
previous = note_version.previous
def note_version_size_diff(note_version, type)
other = note_version.send(type)

html = "#{note_version.width}x#{note_version.height}"
if previous.nil? || (note_version.width == previous.width && note_version.height == previous.height)
if other.nil? || (note_version.width == other.width && note_version.height == other.height)
html
elsif type == "previous"
"#{other.width}x#{other.height} -> " + html
else
"#{previous.width}x#{previous.height} -> " + html
html + " -> #{other.width}x#{other.height}"
end
end

def note_version_body_diff(note_version, type)
other = note_version.send(params[:type])
if type == "previous"
diff_body_html(note_version, other, :body)
else
diff_body_html(other, note_version, :body)
end
end
end
35 changes: 22 additions & 13 deletions app/helpers/pool_versions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
module PoolVersionsHelper
def pool_version_show_diff(pool_version)
previous = pool_version.previous
previous.present? && pool_version.description != previous.description
def pool_version_show_diff(pool_version, type)
other = pool_version.send(type)
other.present? && pool_version.description != other.description
end

def pool_version_name_diff(pool_version)
previous = pool_version.previous
if previous.present? && (pool_version.name != previous.name)
name_diff = diff_name_html(pool_version.pretty_name, previous.pretty_name)
def pool_version_name_diff(pool_version, type)
other = pool_version.send(type)
if other.present? && (pool_version.name != other.name)
if type == "previous"
name_diff = diff_name_html(pool_version.name, other.name)
else
name_diff = diff_name_html(other.name, pool_version.name)
end
%(<br><br><b>Rename:</b><br>&ensp;#{name_diff}</p>).html_safe
else
""
end
end

def pool_version_post_diff(pool_version)
previous = pool_version.previous
def pool_version_post_diff(pool_version, type)
other = pool_version.send(type)
diff = {}

if previous.present?
diff[:added_post_ids] = pool_version.post_ids - previous.post_ids
diff[:removed_post_ids] = previous.post_ids - pool_version.post_ids
else
if other.present? && type == "previous"
diff[:added_post_ids] = pool_version.post_ids - other.post_ids
diff[:removed_post_ids] = other.post_ids - pool_version.post_ids
elsif other.present?
diff[:added_post_ids] = other.post_ids - pool_version.post_ids
diff[:removed_post_ids] = pool_version.post_ids - other.post_ids
elsif type == "previous"
diff[:added_post_ids] = pool_version.added_post_ids
diff[:removed_post_ids] = pool_version.removed_post_ids
else
return ""
end

render "pool_versions/diff", diff: diff
Expand Down

0 comments on commit e23ee17

Please sign in to comment.