From 9f859b903d3665b6a7097f1eec48f5e8a3f6fb51 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Tue, 31 Jul 2012 14:25:46 +0800 Subject: [PATCH] Add custom validator and fix .html_safe with rescue --- app/models/asset.rb | 5 ++++- config/initializers/custom_validators.rb | 7 +++++++ config/locales/en.yml | 3 +++ config/locales/zh_tw.yml | 3 +++ .../built_in_modules/announcement/app/models/bulletin.rb | 2 +- .../panel/announcement/back_end/bulletins/show.html.erb | 4 ++-- .../panel/announcement/front_end/bulletins/show.html.erb | 2 +- vendor/built_in_modules/news/app/models/news_bulletin.rb | 2 +- .../views/panel/news/back_end/news_bulletins/show.html.erb | 4 ++-- .../panel/news/front_end/news_bulletins/index.html.erb | 2 +- .../panel/news/front_end/news_bulletins/show.html.erb | 2 +- .../views/panel/news/widget/news_bulletins/_index.html.erb | 2 +- .../panel/news/widget/news_bulletins/home_banner.html.erb | 2 +- .../page_content/back_end/page_contexts/show.html.erb | 2 +- .../page_content/front_end/page_contexts/index.html.erb | 4 ++-- .../built_in_modules/web_resource/app/models/web_link.rb | 2 +- 16 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 config/initializers/custom_validators.rb diff --git a/app/models/asset.rb b/app/models/asset.rb index 617f0496..b591ab9c 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -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 diff --git a/config/initializers/custom_validators.rb b/config/initializers/custom_validators.rb new file mode 100644 index 00000000..1b3a9f2c --- /dev/null +++ b/config/initializers/custom_validators.rb @@ -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 \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 23b682cb..bd24300f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 15b1c63d..160a5295 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -77,6 +77,9 @@ zh_tw: visitors_this_month: 本月造訪 visitors_this_year: 今年造訪 + errors: + at_least_one: 必須至少有一個值 + admin: access: denied: diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 0a3e3fec..8f8d14f3 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -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 diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/show.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/show.html.erb index 989c0564..8c8f2295 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/show.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/show.html.erb @@ -17,11 +17,11 @@
  • <%= t('announcement.subtitle') %> - <%= @bulletin.subtitle.html_safe %> + <%= @bulletin.subtitle.html_safe rescue '' %>
  • <%= t('announcement.text') %> - <%= @bulletin.text.html_safe %> + <%= @bulletin.text.html_safe rescue '' %>
  • diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb index 11401f13..af1ca12c 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb @@ -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 %>
    - <%= @bulletin.text.html_safe %> + <%= @bulletin.text.html_safe rescue '' %>
    <% if @bulletin.bulletin_links.size > 0 %> diff --git a/vendor/built_in_modules/news/app/models/news_bulletin.rb b/vendor/built_in_modules/news/app/models/news_bulletin.rb index 2db774cb..2ed52c6a 100644 --- a/vendor/built_in_modules/news/app/models/news_bulletin.rb +++ b/vendor/built_in_modules/news/app/models/news_bulletin.rb @@ -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 diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/show.html.erb index 5d2b4dcb..686a1ee1 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/show.html.erb @@ -17,11 +17,11 @@
  • <%= t('news.subtitle') %> - <%= @news_bulletin.subtitle.html_safe %> + <%= @news_bulletin.subtitle.html_safe rescue '' %>
  • <%= t('news.text') %> - <%= @news_bulletin.text.html_safe %> + <%= @news_bulletin.text.html_safe rescue '' %>
  • diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/index.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/index.html.erb index b8749136..5efb8003 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/index.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/index.html.erb @@ -24,7 +24,7 @@ <%= image_tag post.image rescue nil %> <%= link_to post.title, panel_news_front_end_news_bulletin_path(post), :class => 'news_title' %> - <%= post.subtitle.html_safe %> + <%= post.subtitle.html_safe rescue '' %> <%= display_date(post.postdate) %> diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb index 268825f4..edd8a9e1 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb @@ -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 %>
    - <%= @news_bulletin.text.html_safe %> + <%= @news_bulletin.text.html_safe rescue '' %>
    <% if @news_bulletin.news_bulletin_links.size > 0 %> diff --git a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/_index.html.erb b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/_index.html.erb index 205c2828..39af5c14 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/_index.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/_index.html.erb @@ -16,7 +16,7 @@ <%= image_tag post.image %> <%= link_to post.title, panel_news_front_end_news_bulletin_path(post), :class => 'news_title' %> - <%= post.subtitle.html_safe %> + <%= post.subtitle.html_safe rescue '' %> <%= display_date(post.postdate) %> diff --git a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb index db6fe07e..eb3cf591 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb @@ -21,7 +21,7 @@
  • <%= image_tag(post.image.url, :size => "290x130") if post.image.file %>

    <%= link_to post.title, panel_news_front_end_news_bulletin_path(post, :category_id => post.news_bulletin_category_id) %>

    -

    <%= post.subtitle.html_safe %>

    +

    <%= post.subtitle.html_safe rescue '' %>

  • <% end %> diff --git a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/show.html.erb b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/show.html.erb index 913be983..242dde67 100644 --- a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/show.html.erb +++ b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/show.html.erb @@ -13,7 +13,7 @@
  • <%= t('page_content.context') %> - <%= @page_context.context.html_safe %> + <%= @page_context.context.html_safe rescue '' %>
  • <%= t('page_content.張貼者') %> diff --git a/vendor/built_in_modules/page_content/app/views/panel/page_content/front_end/page_contexts/index.html.erb b/vendor/built_in_modules/page_content/app/views/panel/page_content/front_end/page_contexts/index.html.erb index 6648b46b..3e607ad4 100644 --- a/vendor/built_in_modules/page_content/app/views/panel/page_content/front_end/page_contexts/index.html.erb +++ b/vendor/built_in_modules/page_content/app/views/panel/page_content/front_end/page_contexts/index.html.erb @@ -4,8 +4,8 @@

    <%= flash_messages %>

    <%= dislpay_view_count(@page_context) %>
    -

    <%= @page_context.page.title rescue nil %>

    +

    <%= @page_context.page.title rescue '' %>

    -
    <%= @page_context.context.html_safe rescue nil %>
    +
    <%= @page_context.context.html_safe rescue '' %>
    diff --git a/vendor/built_in_modules/web_resource/app/models/web_link.rb b/vendor/built_in_modules/web_resource/app/models/web_link.rb index be1f9682..8e0216f2 100644 --- a/vendor/built_in_modules/web_resource/app/models/web_link.rb +++ b/vendor/built_in_modules/web_resource/app/models/web_link.rb @@ -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 )