Commit ee2d7246f0bb9ae8e96237bcd2d8e3d4b1018f0e

Authored by Antonio Terceiro
1 parent 5f0ea83e

Fix translation of custom AR validation messages

app/models/category.rb
1 class Category < ActiveRecord::Base 1 class Category < ActiveRecord::Base
2 2
3 - validates_exclusion_of :slug, :in => [ 'index' ], :message => N_('%{fn} cannot be like that.') 3 + validates_exclusion_of :slug, :in => [ 'index' ], :message => N_('%{fn} cannot be like that.').fix_i18n
4 validates_presence_of :name, :environment_id 4 validates_presence_of :name, :environment_id
5 - validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('%{fn} is already being used by another category.') 5 + validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('%{fn} is already being used by another category.').fix_i18n
6 belongs_to :environment 6 belongs_to :environment
7 7
8 validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] 8 validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ]
9 - validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') 9 + validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.').fix_i18n
10 10
11 # Finds all top level categories for a given environment. 11 # Finds all top level categories for a given environment.
12 named_scope :top_level_for, lambda { |environment| 12 named_scope :top_level_for, lambda { |environment|
app/models/change_password.rb
@@ -30,10 +30,10 @@ class ChangePassword &lt; Task @@ -30,10 +30,10 @@ class ChangePassword &lt; Task
30 unless data.login.blank? || data.email.blank? 30 unless data.login.blank? || data.email.blank?
31 user = User.find_by_login_and_environment_id(data.login, data.environment_id) 31 user = User.find_by_login_and_environment_id(data.login, data.environment_id)
32 if user.nil? 32 if user.nil?
33 - data.errors.add(:login, _('%{fn} is not a valid username.')) 33 + data.errors.add(:login, _('%{fn} is not a valid username.').fix_i18n)
34 else 34 else
35 if user.email != data.email 35 if user.email != data.email
36 - data.errors.add(:email, _('%{fn} is invalid.')) 36 + data.errors.add(:email)
37 end 37 end
38 end 38 end
39 end 39 end
app/models/comment.rb
@@ -17,7 +17,7 @@ class Comment &lt; ActiveRecord::Base @@ -17,7 +17,7 @@ class Comment &lt; ActiveRecord::Base
17 validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? }) 17 validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? })
18 validates_each :name do |rec,attribute,value| 18 validates_each :name do |rec,attribute,value|
19 if rec.author_id && (!rec.name.blank? || !rec.email.blank?) 19 if rec.author_id && (!rec.name.blank? || !rec.email.blank?)
20 - rec.errors.add(:name, _('%{fn} can only be informed for unauthenticated authors')) 20 + rec.errors.add(:name, _('%{fn} can only be informed for unauthenticated authors').fix_i18n)
21 end 21 end
22 end 22 end
23 23
app/models/community.rb
@@ -34,7 +34,7 @@ class Community &lt; Organization @@ -34,7 +34,7 @@ class Community &lt; Organization
34 super 34 super
35 self.required_fields.each do |field| 35 self.required_fields.each do |field|
36 if self.send(field).blank? 36 if self.send(field).blank?
37 - self.errors.add(field, _('%{fn} can\'t be blank')) 37 + self.errors.add_on_blank(field)
38 end 38 end
39 end 39 end
40 end 40 end
app/models/create_community.rb
@@ -16,7 +16,7 @@ class CreateCommunity &lt; Task @@ -16,7 +16,7 @@ class CreateCommunity &lt; Task
16 def validate 16 def validate
17 self.environment.required_community_fields.each do |field| 17 self.environment.required_community_fields.each do |field|
18 if self.send(field).blank? 18 if self.send(field).blank?
19 - self.errors.add(field, _('%{fn} can\'t be blank')) 19 + self.errors.add_on_blank(field)
20 end 20 end
21 end 21 end
22 end 22 end
app/models/create_enterprise.rb
@@ -40,12 +40,12 @@ class CreateEnterprise &lt; Task @@ -40,12 +40,12 @@ class CreateEnterprise &lt; Task
40 40
41 if self.region && self.target 41 if self.region && self.target
42 unless self.region.validators.include?(self.target) || self.target_type == "Environment" 42 unless self.region.validators.include?(self.target) || self.target_type == "Environment"
43 - self.errors.add(:target, '%{fn} is not a validator for the chosen region') 43 + self.errors.add(:target, _('%{fn} is not a validator for the chosen region').fix_i18n)
44 end 44 end
45 end 45 end
46 46
47 if self.status != Task::Status::CANCELLED && self.identifier && Profile.exists?(:identifier => self.identifier) 47 if self.status != Task::Status::CANCELLED && self.identifier && Profile.exists?(:identifier => self.identifier)
48 - self.errors.add(:identifier, '%{fn} is already being as identifier by another enterprise, organization or person.') 48 + self.errors.add(:identifier, _('%{fn} is already being as identifier by another enterprise, organization or person.').fix_i18n)
49 end 49 end
50 end 50 end
51 51
app/models/domain.rb
@@ -10,14 +10,14 @@ class Domain &lt; ActiveRecord::Base @@ -10,14 +10,14 @@ class Domain &lt; ActiveRecord::Base
10 10
11 # <tt>name</tt> must be a sequence of word characters (a to z, plus 0 to 9, 11 # <tt>name</tt> must be a sequence of word characters (a to z, plus 0 to 9,
12 # plus '_'). Letters must be lowercase 12 # plus '_'). Letters must be lowercase
13 - validates_format_of :name, :with => /^([a-z0-9_-]+\.)+[a-z0-9_-]+$/, :message => N_('%{fn} must be composed only of lowercase latters (a to z), numbers (0 to 9), "_" and "-"') 13 + validates_format_of :name, :with => /^([a-z0-9_-]+\.)+[a-z0-9_-]+$/, :message => N_('%{fn} must be composed only of lowercase latters (a to z), numbers (0 to 9), "_" and "-"').fix_i18n
14 14
15 # checks validations that could not be expressed using Rails' predefined 15 # checks validations that could not be expressed using Rails' predefined
16 # validations. In particular: 16 # validations. In particular:
17 # * <tt>name</tt> must not start with 'www.' 17 # * <tt>name</tt> must not start with 'www.'
18 def validate 18 def validate
19 if self.name =~ /^www\./ 19 if self.name =~ /^www\./
20 - self.errors.add(:name, _('%{fn} must not start with www.')) 20 + self.errors.add(:name, _('%{fn} must not start with www.').fix_i18n)
21 end 21 end
22 end 22 end
23 23
app/models/enterprise.rb
@@ -46,7 +46,7 @@ class Enterprise &lt; Organization @@ -46,7 +46,7 @@ class Enterprise &lt; Organization
46 super 46 super
47 self.required_fields.each do |field| 47 self.required_fields.each do |field|
48 if self.send(field).blank? 48 if self.send(field).blank?
49 - self.errors.add(field, _("%{fn} can't be blank")) 49 + self.errors.add_on_blank(field)
50 end 50 end
51 end 51 end
52 end 52 end
app/models/event.rb
@@ -22,7 +22,7 @@ class Event &lt; Article @@ -22,7 +22,7 @@ class Event &lt; Article
22 22
23 validates_each :start_date do |event,field,value| 23 validates_each :start_date do |event,field,value|
24 if event.end_date && event.start_date && event.start_date > event.end_date 24 if event.end_date && event.start_date && event.start_date > event.end_date
25 - event.errors.add(:start_date, _('%{fn} cannot come before end date.')) 25 + event.errors.add(:start_date, _('%{fn} cannot come before end date.').fix_i18n)
26 end 26 end
27 end 27 end
28 28
app/models/image.rb
@@ -17,7 +17,7 @@ class Image &lt; ActiveRecord::Base @@ -17,7 +17,7 @@ class Image &lt; ActiveRecord::Base
17 :icon => '20x20!' }, 17 :icon => '20x20!' },
18 :max_size => 5.megabytes # remember to update validate message below 18 :max_size => 5.megabytes # remember to update validate message below
19 19
20 - validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB") 20 + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB").fix_i18n
21 21
22 delay_attachment_fu_thumbnails 22 delay_attachment_fu_thumbnails
23 23
app/models/person.rb
@@ -197,7 +197,7 @@ class Person &lt; Profile @@ -197,7 +197,7 @@ class Person &lt; Profile
197 197
198 validates_each :email, :on => :update do |record,attr,value| 198 validates_each :email, :on => :update do |record,attr,value|
199 if User.find(:first, :conditions => ['email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id]) 199 if User.find(:first, :conditions => ['email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id])
200 - record.errors.add(attr, _('%{fn} is already used by other user')) 200 + record.errors.add(attr, _('%{fn} is already used by other user').fix_i18n)
201 end 201 end
202 end 202 end
203 203
app/models/uploaded_file.rb
@@ -50,7 +50,7 @@ class UploadedFile &lt; Article @@ -50,7 +50,7 @@ class UploadedFile &lt; Article
50 :thumbnail_class => Thumbnail, 50 :thumbnail_class => Thumbnail,
51 :max_size => 5.megabytes # remember to update validate message below 51 :max_size => 5.megabytes # remember to update validate message below
52 52
53 - validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB") 53 + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB").fix_i18n
54 54
55 delay_attachment_fu_thumbnails 55 delay_attachment_fu_thumbnails
56 56
app/models/user.rb
@@ -100,7 +100,7 @@ class User &lt; ActiveRecord::Base @@ -100,7 +100,7 @@ class User &lt; ActiveRecord::Base
100 before_save :encrypt_password 100 before_save :encrypt_password
101 validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) 101 validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
102 102
103 - validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.') 103 + validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.').fix_i18n
104 104
105 # Authenticates a user by their login name and unencrypted password. Returns the user or nil. 105 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
106 def self.authenticate(login, password, environment = nil) 106 def self.authenticate(login, password, environment = nil)
lib/noosfero/core_ext/string.rb
@@ -40,4 +40,8 @@ class String @@ -40,4 +40,8 @@ class String
40 transliterate.downcase.gsub(/[^\w~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s 40 transliterate.downcase.gsub(/[^\w~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s
41 end 41 end
42 42
  43 + def fix_i18n
  44 + self.sub('{fn} ', '')
  45 + end
  46 +
43 end 47 end
test/unit/comment_test.rb
@@ -60,6 +60,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -60,6 +60,7 @@ class CommentTest &lt; ActiveSupport::TestCase
60 c1.name = 'my name' 60 c1.name = 'my name'
61 c1.valid? 61 c1.valid?
62 assert c1.errors.invalid?(:name) 62 assert c1.errors.invalid?(:name)
  63 + assert_no_match /\{fn\}/, c1.errors.on(:name)
63 end 64 end
64 65
65 should 'update counter cache in article' do 66 should 'update counter cache in article' do
test/unit/person_test.rb
@@ -133,6 +133,7 @@ class PersonTest &lt; ActiveSupport::TestCase @@ -133,6 +133,7 @@ class PersonTest &lt; ActiveSupport::TestCase
133 other.email = 'user@domain.com' 133 other.email = 'user@domain.com'
134 other.valid? 134 other.valid?
135 assert other.errors.invalid?(:email) 135 assert other.errors.invalid?(:email)
  136 + assert_no_match /\{fn\}/, other.errors.on(:email)
136 end 137 end
137 138
138 should 'be able to use an e-mail already used in other environment' do 139 should 'be able to use an e-mail already used in other environment' do
vendor/plugins/validates_as_cnpj/lib/validates_as_cnpj.rb
@@ -62,14 +62,14 @@ module ActiveRecord @@ -62,14 +62,14 @@ module ActiveRecord
62 module Validations 62 module Validations
63 module ClassMethods 63 module ClassMethods
64 def validates_as_cnpj(*attr_names) 64 def validates_as_cnpj(*attr_names)
65 - configuration = { :message => "%{fn} is invalid" } 65 + configuration = { }
66 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 66 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
67 67
68 validates_each(attr_names, configuration) do |record, attr_name, value| 68 validates_each(attr_names, configuration) do |record, attr_name, value|
69 next if value.blank? 69 next if value.blank?
70 70
71 unless ValidaCNPJ::valida_cnpj(value) 71 unless ValidaCNPJ::valida_cnpj(value)
72 - record.errors.add(attr_name, configuration[:message]) 72 + record.errors.add(attr_name)
73 end 73 end
74 end 74 end
75 end 75 end