phony_rails 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,11 +27,11 @@ In your model add:
27
27
 
28
28
  class SomeModel < ActiveRecord::Base
29
29
 
30
- # Normalizes the attribute itself before validation
30
+ # Normalizes the attribute itself before validation
31
31
  phony_normalize :phone_number, :default_country_code => 'US'
32
32
 
33
33
  # Normalizes attribute before validation and saves into other attribute
34
- phony_normalize :phone_number, :as => :phone_number_normalized_version, :default_country_code => 'US'
34
+ phony_normalize :phone_number, :as => :phone_number_normalized_version, :default_country_code => 'US'
35
35
 
36
36
  # Creates method normalized_fax_number that returns the normalized version of fax_number
37
37
  phony_normalized_method :fax_number
@@ -78,11 +78,11 @@ In your views use:
78
78
 
79
79
  <%= "311012341234".phony_formatted(:format => :international, :spaces => '-') %>
80
80
  <%= "+31-10-12341234".phony_formatted(:format => :international, :spaces => '-') %>
81
- <%= "+31(0)1012341234".phony_formatted(:format => :international, :spaces => '-') %>
81
+ <%= "+31(0)1012341234".phony_formatted(:format => :international, :spaces => '-') %>
82
82
 
83
83
  To first normalize the String to a certain country use:
84
84
 
85
- <%= "010-12341234".phony_formatted(normalize => :NL, :format => :international, :spaces => '-') %>
85
+ <%= "010-12341234".phony_formatted(normalize => :NL, :format => :international, :spaces => '-') %>
86
86
 
87
87
  You can also use the bang method (phony_formatted!):
88
88
 
@@ -98,28 +98,31 @@ Say you want to find a record by a phone number. Best is to normalize user input
98
98
 
99
99
  ## Changelog
100
100
 
101
+ 0.2.1
102
+ * Better error handling by @k4nar
103
+
101
104
  0.1.12
102
105
  * Further loosened gemspec dependencies.
103
106
 
104
107
  0.1.11
105
- * Better gemspec dependency versions by rjhaveri.
108
+ * Better gemspec dependency versions by @rjhaveri.
106
109
 
107
110
  0.1.10
108
111
  * Changes from henning-koch.
109
112
  * Some pending fixes.
110
113
 
111
114
  0.1.8
112
- * Improved validation methods by ddidier.
115
+ * Improved validation methods by @ddidier.
113
116
 
114
117
  0.1.6
115
118
  * Added :as option to phony_normalize.
116
119
 
117
120
  0.1.5
118
- * some tests and a helper method by ddidier.
121
+ * some tests and a helper method by @ddidier.
119
122
 
120
123
  0.1.2
121
- * Using countries gem as suggested by brutuscat.
122
- * Fixes bugs mentioned by ddidier.
124
+ * Using countries gem as suggested by @brutuscat.
125
+ * Fixes bugs mentioned by @ddidier.
123
126
 
124
127
  0.1.0
125
128
  * Added specs.
@@ -49,6 +49,7 @@ module PhonyRails
49
49
  options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
50
50
  attributes.each do |attribute|
51
51
  attribute_name = options[:as] || attribute
52
+ raise RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)" if not self.attribute_method?(attribute_name)
52
53
  write_attribute(attribute_name, PhonyRails.normalize_number(read_attribute(attribute), options))
53
54
  end
54
55
  end
@@ -69,12 +70,9 @@ module PhonyRails
69
70
  raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1
70
71
  raise ArgumentError, "'#{options[:as]}' is not an attribute on #{self.name}. You might want to use 'phony_normalized_method :#{attributes.first}' (PhonyRails)" if not self.attribute_method?(options[:as])
71
72
  end
72
- attributes.each do |attribute|
73
- raise ArgumentError, "No attribute #{attribute} found on #{self.name} (PhonyRails)" if not self.attribute_method?(attribute)
74
- # Add before validation that saves a normalized version of the phone number
75
- self.before_validation do
76
- set_phony_normalized_numbers(attributes, options)
77
- end
73
+ # Add before validation that saves a normalized version of the phone number
74
+ self.before_validation do
75
+ set_phony_normalized_numbers(attributes, options)
78
76
  end
79
77
  end
80
78
 
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -14,7 +14,7 @@ describe PhonyRails do
14
14
  s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
15
15
  s.should eql("+31 10 1234123")
16
16
  end
17
-
17
+
18
18
  end
19
19
 
20
20
  describe 'with normalize option' do
@@ -143,6 +143,12 @@ describe PhonyRails do
143
143
  Home.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
144
144
  }.should_not raise_error(ArgumentError)
145
145
  end
146
+
147
+ it "should accept a non existing attribute name" do
148
+ lambda {
149
+ Dummy.phony_normalize(:non_existing_attribute)
150
+ }.should_not raise_error
151
+ end
146
152
  end
147
153
 
148
154
  describe 'using ActiveRecord#phony_normalized_method' do
@@ -219,5 +225,14 @@ describe PhonyRails do
219
225
  home.valid?.should be_true
220
226
  home.phone_number_as_normalized.should eql('31101234123')
221
227
  end
228
+
229
+ it "should raise a RuntimeError at validation if the attribute doesn't exist" do
230
+ Dummy.phony_normalize :non_existing_attribute
231
+
232
+ dummy = Dummy.new
233
+ lambda {
234
+ dummy.valid?
235
+ }.should raise_error(RuntimeError)
236
+ end
222
237
  end
223
238
  end
@@ -26,6 +26,9 @@ class Home < ActiveRecord::Base
26
26
  phony_normalize :phone_number # normalized on validation
27
27
  end
28
28
 
29
+ class Dummy < Home
30
+ end
31
+
29
32
  RSpec.configure do |config|
30
- # some (optional) config here
33
+ # some (optional) config here
31
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-13 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: phony
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 1.8.24
108
+ rubygems_version: 1.8.25
109
109
  signing_key:
110
110
  specification_version: 3
111
111
  summary: This Gem adds useful methods to your Rails app to validate, display and save