Skip to content

Commit e780ba9

Browse files
committed
refactor: safe_urlメソッドをモデルからヘルパーに移動
- Dojoモデルのsafe_urlメソッドをApplicationHelperのsafe_dojo_urlに移動 - app/views/shared/_dojo.html.erbでsafe_dojo_url()ヘルパーを使用
1 parent 440fac1 commit e780ba9

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

app/helpers/application_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ def sanitize_content(content)
5555
)
5656
end
5757

58+
def safe_dojo_url(dojo)
59+
return '#' if dojo.url.blank?
60+
61+
begin
62+
uri = URI.parse(dojo.url)
63+
uri.scheme&.match?(/\Ahttps?\z/) ? dojo.url : '#'
64+
rescue URI::InvalidURIError
65+
'#'
66+
end
67+
end
68+
5869
# 'inline_' プレフィックスがついたflashメッセージをビュー内で表示するヘルパー
5970
# inline_alert → alert, inline_warning → warning のように変換してBootstrapのCSSクラスを適用
6071
def render_inline_flash_messages

app/models/dojo.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ def active_at?(date)
9090
created_at <= date && (inactivated_at.nil? || inactivated_at > date)
9191
end
9292

93-
# URLの安全性をチェックして、安全なURLを返す
94-
def safe_url
95-
return '#' if url.blank?
96-
97-
# URI.parseがエラーになるか、HTTPスキームでない場合は'#'を返す
98-
begin
99-
uri = URI.parse(url)
100-
uri.scheme&.match?(/\Ahttps?\z/) ? url : '#'
101-
rescue URI::InvalidURIError
102-
'#'
103-
end
104-
end
105-
10693
# 再活性化メソッド
10794
def reactivate!
10895
if inactivated_at.present?

app/views/shared/_dojo.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<li class="dojo" id="<%= html_escape(dojo.name) %>">
22
<header>
3-
<%= link_to lazy_image_tag(dojo.logo, alt: html_escape("CoderDojo #{dojo.name}"), class: 'dojo-picture'), sanitize_url(dojo.safe_url),
3+
<%= link_to lazy_image_tag(dojo.logo, alt: html_escape("CoderDojo #{dojo.name}"), class: 'dojo-picture'), safe_dojo_url(dojo),
44
target: "_blank", rel: "external noopener noreferrer" %>
55
<span class="dojo-name">
6-
<%= link_to html_escape("#{dojo.name} (#{dojo.prefecture.name})"), sanitize_url(dojo.safe_url), target: "_blank", rel: "external noopener noreferrer" %>
6+
<%= link_to html_escape("#{dojo.name} (#{dojo.prefecture.name})"), safe_dojo_url(dojo), target: "_blank", rel: "external noopener noreferrer" %>
77
<% if not dojo.counter == 1 %>
88
<span class="dojo-counter"
99
data-original-title="道場数"

0 commit comments

Comments
 (0)