Skip to content

Commit

Permalink
Fix multiple phone attributes rendering error.
Browse files Browse the repository at this point in the history
  • Loading branch information
btelles committed Feb 2, 2010
1 parent 053f6ba commit 604628d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -23,7 +23,7 @@ PKG_FILES = FileList[


spec = Gem::Specification.new do |s| spec = Gem::Specification.new do |s|
s.name = "validates_and_formats_phones" s.name = "validates_and_formats_phones"
s.version = "0.0.2" s.version = "0.0.3"


s.author = "Bernie Telles" s.author = "Bernie Telles"
s.email = "btelles@gmail.com" s.email = "btelles@gmail.com"
Expand Down
10 changes: 2 additions & 8 deletions lib/validates_and_formats_phones/validates_and_formats_phones.rb
Expand Up @@ -30,23 +30,17 @@ def validates_and_formats_phones(*args)
record.errors.add attr, "must have #{size_options[0]} digits." record.errors.add attr, "must have #{size_options[0]} digits."
end end
else else
record.format_phone_fields(fields, formats) record.format_phone_field(attr, formats)
end end
end end
end end
end end


module InstanceMethods module InstanceMethods


def format_phone_fields(fields = [:phone], formats = [])
formats << DEFAULT_FORMAT if formats.empty?
fields.each do |field_name|
format_phone_field(field_name, formats) unless send(field_name).blank?
end
end
def format_phone_field(field_name, formats = []) def format_phone_field(field_name, formats = [])
formats << DEFAULT_FORMAT if formats.empty? formats << DEFAULT_FORMAT if formats.empty?
self.send("#{field_name}=", self.send(field_name).to_s.to_phone(formats)) self.send("#{field_name}=", self.send(field_name).to_s.to_phone(formats)) unless send(field_name).blank?
end end
end end
end end
Expand Down
1 change: 1 addition & 0 deletions spec/db/schema.rb
Expand Up @@ -4,6 +4,7 @@
end end
create_table :options_phones, :force => true do |t| create_table :options_phones, :force => true do |t|
t.string :other_phone t.string :other_phone
t.string :fax
end end


end end
2 changes: 1 addition & 1 deletion spec/validates_and_formats_phones_spec.rb
Expand Up @@ -4,7 +4,7 @@ class Phone < ActiveRecord::Base
validates_and_formats_phones validates_and_formats_phones
end end
class OptionsPhone < ActiveRecord::Base class OptionsPhone < ActiveRecord::Base
validates_and_formats_phones :other_phone, validates_and_formats_phones :other_phone, :fax,
'####-####', '####-####',
'(###) ###-####' '(###) ###-####'
end end
Expand Down

0 comments on commit 604628d

Please sign in to comment.