Skip to content

Commit

Permalink
Allow more protocols for news
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Jan 5, 2014
1 parent d60bb46 commit e9e22ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/assets/stylesheets/RonRonnement.css.scss
Expand Up @@ -1225,6 +1225,12 @@ div#form_links {
.link input {
width: 30%;
}
.field_with_errors {
display: inline-block;
width: 30%;
border: 1px dashed red;
input { width: 96%; }
}
label#label-link-lang {
width: 10%;
}
Expand Down
22 changes: 18 additions & 4 deletions app/models/link.rb
Expand Up @@ -17,17 +17,31 @@
# We follow the number of clicks on each of these links.
#
class Link < ActiveRecord::Base
PROTOCOLS = HTML::Pipeline::SanitizationFilter::WHITELIST[:protocols]['a']['href'] - [:relative]

belongs_to :news

attr_accessor :user
attr_accessible :user, :title, :url, :lang

validates :title, :presence => { :message => "Un lien doit obligatoirement avoir un titre" }
validates_format_of :url, :message => "L'URL d'un lien n'est pas valide", :with => URI::regexp(%w(http https))
validate :authorized_protocol

def url=(raw)
return if raw.blank?
uri = URI.parse(raw)
if uri.scheme.blank?
raw = "http://#{raw}"
uri = URI.parse(raw)
end
write_attribute :url, uri.to_s
end

def url=(url)
url = "http://#{url}" if url.present? && url.not.start_with?('http')
write_attribute :url, url
def authorized_protocol
uri = URI.parse(url)
unless PROTOCOLS.include?(uri.scheme)
errors.add(:url, "L'URL d'un lien n'est pas valide")
end
end

### Behaviour ###
Expand Down
2 changes: 1 addition & 1 deletion app/models/news.rb
Expand Up @@ -31,7 +31,7 @@ class News < Content
belongs_to :moderator, :class_name => "User"
has_many :links, :dependent => :destroy, :inverse_of => :news
has_many :paragraphs, :dependent => :destroy, :inverse_of => :news
accepts_nested_attributes_for :links, :allow_destroy => true
accepts_nested_attributes_for :links, :allow_destroy => true, :reject_if => proc { |attrs| attrs['url'].blank? }
accepts_nested_attributes_for :paragraphs, :allow_destroy => true
has_many :versions, :class_name => 'NewsVersion',
:dependent => :destroy,
Expand Down

0 comments on commit e9e22ff

Please sign in to comment.