Dernière tentative pour un diff unifié propre

This commit is contained in:
echarp 2014-10-26 18:32:46 +01:00
parent ca688e0786
commit e97c5bc044
4 changed files with 42 additions and 10 deletions

View File

@ -67,7 +67,7 @@ gem 'email_validator'
# Email address obfuscation
gem 'actionview-encoded_mail_to'
# To display a patched diff for event descriptions
gem 'differ', github: 'genuitytech/differ', branch: 'add_patch_format'
gem 'differ'
# A generic library to administrate the tool
gem 'activeadmin', github: 'gregbell/active_admin'

View File

@ -6,13 +6,6 @@ GIT
jquery-rails
rails (>= 3.2.2)
GIT
remote: git://github.com/genuitytech/differ.git
revision: ff1a29ab2fa87620149e001d07aa48bef92de850
branch: add_patch_format
specs:
differ (0.1.2)
GIT
remote: git://github.com/gregbell/active_admin.git
revision: 944af76ace89f941a7baa92775854d2e1d0e6dba
@ -135,6 +128,7 @@ GEM
thread_safe (~> 0.1)
warden (~> 1.2.3)
devise-i18n (0.11.2)
differ (0.1.2)
docile (1.1.5)
em-websocket (0.5.1)
eventmachine (>= 0.12.9)
@ -387,7 +381,7 @@ DEPENDENCIES
compass-rails
devise
devise-i18n
differ!
differ
email_validator
font-awesome-rails
geocoder

View File

@ -10,7 +10,9 @@
- prev = render file: '/events/show'
- @event = former
= Differ.diff(new, prev).format_as(:patch).gsub(/\[\d{1,2}m/, '')
- require 'differ/format/patch'
- Differ.format = Differ::Format::Patch
= Differ.diff new, prev
\
- if @current_user
= edit_moderation_url @event

View File

@ -0,0 +1,36 @@
# Formatter for diff output
module Differ
module Format
# Try to format as unified diff, using - and +
module Patch
class << self
def format(change)
case
when change.change?
as_change change
when change.delete?
as_delete change
when change.insert?
as_insert change
else
''
end
end
private
def as_insert(change)
"+ #{change.insert}"
end
def as_delete(change)
"- #{change.delete}"
end
def as_change(change)
[as_delete(change), as_insert(change)].join '\n'
end
end
end
end
end