Skip to content

Commit 00c0b44

Browse files
committed
adds after_validation method to UriValidator
This is used to revert the workaround applied on fragment part of the URI to accept multiple sharp sign in the URI. This decoding of sharp sign in the fragment part allows to keep visual match with the user input.
1 parent e6f4c64 commit 00c0b44

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

app/models/bookmark.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class Bookmark < Content
3333
bookmark.link = UriValidator.before_validation(bookmark.link);
3434
end
3535

36+
after_validation do |bookmark|
37+
bookmark.link = UriValidator.after_validation(bookmark.link);
38+
end
39+
3640
def create_node(attrs={})
3741
attrs[:cc_licensed] = false
3842
super

app/models/link.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class Link < ActiveRecord::Base
3434
link.url = UriValidator.before_validation(link.url);
3535
end
3636

37+
after_validation do |link|
38+
link.url = UriValidator.after_validation(link.url);
39+
end
40+
3741
### Behaviour ###
3842

3943
def self.hit(id)

app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class User < ActiveRecord::Base
4444
user.mastodon_url = UriValidator.before_validation(user.mastodon_url)
4545
end
4646

47+
after_validation do |user|
48+
user.homesite = UriValidator.after_validation(user.homesite)
49+
user.mastodon_url = UriValidator.after_validation(user.mastodon_url)
50+
end
51+
4752
def self.collective
4853
find_by(name: "Collectif")
4954
end

app/validators/uri_validator.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,15 @@ def self.before_validation(raw, default_scheme='http://')
5454
return raw
5555
end
5656

57+
def self.after_validation(raw)
58+
# Decodes sharp signs (#) found in URI fragment to keep visual match with
59+
# the user input
60+
fragments = raw.split("#")
61+
if (fragments.length == 2)
62+
raw = fragments[0] + '#' + fragments[1].gsub('%23', '#')
63+
end
64+
65+
return raw
66+
end
67+
5768
end

0 commit comments

Comments
 (0)