Add custom validator and fix .html_safe with rescue

This commit is contained in:
Christophe Vilayphiou 2012-07-31 14:25:46 +08:00
parent ecb75fd085
commit 9f859b903d
16 changed files with 32 additions and 16 deletions

View File

@ -9,7 +9,10 @@ class Asset
field :description, localize: true
field :title, localize: true
validates_presence_of :title, :data, :description
validates :title, :at_least_one => true
validates :description, :at_least_one => true
validates_presence_of :data
belongs_to :asset_category
belongs_to :assetable, polymorphic: true

View File

@ -0,0 +1,7 @@
class AtLeastOneValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("errors.at_least_one")) if (value.blank? || value.values.join.eql?(''))
end
end

View File

@ -79,6 +79,9 @@ en:
visitors_this_month: This month's visitors
visitors_this_year: This year's visitors
errors:
at_least_one: must at least have one value
admin:
access:
denied:

View File

@ -77,6 +77,9 @@ zh_tw:
visitors_this_month: 本月造訪
visitors_this_year: 今年造訪
errors:
at_least_one: 必須至少有一個值
admin:
access:
denied:

View File

@ -57,7 +57,7 @@ class Bulletin
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
validates :title, :presence => true
validates :title, :at_least_one => true
before_save :check_deadline,:update_status

View File

@ -17,11 +17,11 @@
</li>
<li>
<b><%= t('announcement.subtitle') %></b>
<%= @bulletin.subtitle.html_safe %>
<%= @bulletin.subtitle.html_safe rescue '' %>
</li>
<li>
<b><%= t('announcement.text') %></b>
<%= @bulletin.text.html_safe %>
<%= @bulletin.text.html_safe rescue '' %>
</li>
<li>
<li>

View File

@ -12,7 +12,7 @@
<%= link_to image_tag(@bulletin.image.url, :size => "320x240"), @bulletin.image.url, {:target => '_blank', :title => @bulletin.image_identifier} if @bulletin.image.file %>
</div>
<div class="news_paragraph">
<%= @bulletin.text.html_safe %>
<%= @bulletin.text.html_safe rescue '' %>
</div>
<div class="linkAndFile">
<% if @bulletin.bulletin_links.size > 0 %>

View File

@ -58,7 +58,7 @@ class NewsBulletin
accepts_nested_attributes_for :news_bulletin_files, :allow_destroy => true
accepts_nested_attributes_for :news_bulletin_links, :allow_destroy => true
validates_presence_of :title
validates :title, :at_least_one => true
before_save :update_status

View File

@ -17,11 +17,11 @@
</li>
<li>
<b><%= t('news.subtitle') %></b>
<%= @news_bulletin.subtitle.html_safe %>
<%= @news_bulletin.subtitle.html_safe rescue '' %>
</li>
<li>
<b><%= t('news.text') %></b>
<%= @news_bulletin.text.html_safe %>
<%= @news_bulletin.text.html_safe rescue '' %>
</li>
<li>
<li>

View File

@ -24,7 +24,7 @@
<td><%= image_tag post.image rescue nil %></td>
<td>
<%= link_to post.title, panel_news_front_end_news_bulletin_path(post), :class => 'news_title' %>
<%= post.subtitle.html_safe %>
<%= post.subtitle.html_safe rescue '' %>
</td>
<td><%= display_date(post.postdate) %></td>
</tr>

View File

@ -14,7 +14,7 @@
<%= link_to image_tag(@news_bulletin.image.url, :size => "320x240"), @news_bulletin.image.url, {:target => '_blank', :title => @news_bulletin.image_identifier} if @news_bulletin.image.file %>
</div>
<div class="news_paragraph">
<%= @news_bulletin.text.html_safe %>
<%= @news_bulletin.text.html_safe rescue '' %>
</div>
<div class="linkAndFile">
<% if @news_bulletin.news_bulletin_links.size > 0 %>

View File

@ -16,7 +16,7 @@
<td><%= image_tag post.image %></td>
<td>
<%= link_to post.title, panel_news_front_end_news_bulletin_path(post), :class => 'news_title' %>
<%= post.subtitle.html_safe %>
<%= post.subtitle.html_safe rescue '' %>
</td>
<td><%= display_date(post.postdate) %></td>
</tr>

View File

@ -21,7 +21,7 @@
<li>
<div class="news_img"><%= image_tag(post.image.url, :size => "290x130") if post.image.file %></div>
<h3 class="h3 news_title"><%= link_to post.title, panel_news_front_end_news_bulletin_path(post, :category_id => post.news_bulletin_category_id) %></h3>
<p class="news_wrap"><%= post.subtitle.html_safe %></p>
<p class="news_wrap"><%= post.subtitle.html_safe rescue '' %></p>
</li>
<% end %>
</ul>

View File

@ -13,7 +13,7 @@
</li>
<li>
<b><%= t('page_content.context') %></b>
<%= @page_context.context.html_safe %>
<%= @page_context.context.html_safe rescue '' %>
</li>
<li>
<b><%= t('page_content.張貼者') %></b>

View File

@ -4,8 +4,8 @@
<p id="notice"><%= flash_messages %></p>
<div class="view_count pull-right"><%= dislpay_view_count(@page_context) %></div>
<h1 class="h1"><%= @page_context.page.title rescue nil %></h1>
<h1 class="h1"><%= @page_context.page.title rescue '' %></h1>
<div class="page_content"><%= @page_context.context.html_safe rescue nil %></div>
<div class="page_content"><%= @page_context.context.html_safe rescue '' %></div>

View File

@ -21,7 +21,7 @@ class WebLink
belongs_to :web_link_category
validates_presence_of :title
validates :title, :at_least_one => true
def self.search( category_id = nil )