amount_field 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +12 -0
- data/Manifest +1 -0
- data/README.rdoc +3 -3
- data/Rakefile +1 -1
- data/amount_field.gemspec +4 -4
- data/lib/amount_field/form_helper.rb +12 -7
- data/test/form_helper_test.rb +68 -0
- data/test/validations_test.rb +1 -1
- metadata +14 -6
data/History.txt
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
== 1.4.1 2010-04-23
|
2
|
+
|
3
|
+
* Method amount_field considers given option "name" and "id":
|
4
|
+
<% form_for(@manager) do |f| %>
|
5
|
+
<%= f.amount_field :salary, :name => :salary, :id => "salary_id" %>
|
6
|
+
...
|
7
|
+
=>
|
8
|
+
<input name="salary" id="salary_id" ... /> instead of
|
9
|
+
<input name="manager[amount_field_value]" id="manager_salary"... />
|
10
|
+
* fix: display invalid value in current format (e.g. in german format: "-0,1" or "12,34"
|
11
|
+
and not "-0.1" and "12.34").
|
12
|
+
|
1
13
|
== 1.4.0 2009-10-06
|
2
14
|
|
3
15
|
* fix: negative values like '-1,00', '-1,0', '-1,' or '-1' are considered valid
|
data/Manifest
CHANGED
data/README.rdoc
CHANGED
@@ -9,12 +9,12 @@ Drop me a line if you encounter problems.
|
|
9
9
|
|
10
10
|
As Gem:
|
11
11
|
|
12
|
-
$ gem sources -a http://
|
13
|
-
$ sudo gem install
|
12
|
+
$ gem sources -a http://gemcutter.org/ (you only have to do this once)
|
13
|
+
$ sudo gem install amount_field
|
14
14
|
|
15
15
|
As plugin:
|
16
16
|
|
17
|
-
$ script/plugin install git://github.com/thomasbaustert/amount_field.git
|
17
|
+
$ script/plugin install git://github.com/thomasbaustert/amount_field.git -r 2.3
|
18
18
|
|
19
19
|
== Example
|
20
20
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('amount_field', '1.4.
|
5
|
+
Echoe.new('amount_field', '1.4.1') do |p|
|
6
6
|
p.description = "Rails gem/plugin that accepts (amount) values in german or us format like 1.234,56 or 1,234.56"
|
7
7
|
p.url = "http://github.com/thomasbaustert/amount_field"
|
8
8
|
p.author = "Thomas Baustert"
|
data/amount_field.gemspec
CHANGED
@@ -2,20 +2,20 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{amount_field}
|
5
|
-
s.version = "1.4.
|
5
|
+
s.version = "1.4.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Thomas Baustert"]
|
9
|
-
s.date = %q{
|
9
|
+
s.date = %q{2010-04-23}
|
10
10
|
s.description = %q{Rails gem/plugin that accepts (amount) values in german or us format like 1.234,56 or 1,234.56}
|
11
11
|
s.email = %q{ business@thomasbaustert.de}
|
12
12
|
s.extra_rdoc_files = ["README.rdoc", "lib/amount_field.rb", "lib/amount_field/configuration.rb", "lib/amount_field/form_helper.rb", "lib/amount_field/form_tag_helper.rb", "lib/amount_field/validations.rb"]
|
13
|
-
s.files = ["History.txt", "MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "init.rb", "install.rb", "lib/amount_field.rb", "lib/amount_field/configuration.rb", "lib/amount_field/form_helper.rb", "lib/amount_field/form_tag_helper.rb", "lib/amount_field/validations.rb", "locale/de.yml", "locale/en.yml", "test/form_helper_test.rb", "test/form_tag_helper_test.rb", "test/models/test_product.rb", "test/test_helper.rb", "test/validations_test.rb", "uninstall.rb"
|
13
|
+
s.files = ["History.txt", "MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "amount_field.gemspec", "init.rb", "install.rb", "lib/amount_field.rb", "lib/amount_field/configuration.rb", "lib/amount_field/form_helper.rb", "lib/amount_field/form_tag_helper.rb", "lib/amount_field/validations.rb", "locale/de.yml", "locale/en.yml", "test/form_helper_test.rb", "test/form_tag_helper_test.rb", "test/models/test_product.rb", "test/test_helper.rb", "test/validations_test.rb", "uninstall.rb"]
|
14
14
|
s.homepage = %q{http://github.com/thomasbaustert/amount_field}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Amount_field", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{amount_field}
|
18
|
-
s.rubygems_version = %q{1.3.
|
18
|
+
s.rubygems_version = %q{1.3.6}
|
19
19
|
s.summary = %q{Rails gem/plugin that accepts (amount) values in german or us format like 1.234,56 or 1,234.56}
|
20
20
|
s.test_files = ["test/form_helper_test.rb", "test/form_tag_helper_test.rb", "test/models/test_product.rb", "test/test_helper.rb", "test/validations_test.rb"]
|
21
21
|
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module AmountField #:nodoc:
|
2
2
|
module Helpers #:nodoc:
|
3
3
|
|
4
|
+
#TODO/2010-04-23/tb remove redundant code
|
5
|
+
# using @template.amount_field(object, method, options) !?
|
6
|
+
|
4
7
|
module FormHelper #:nodoc:
|
5
8
|
|
6
9
|
include ActionView::Helpers::NumberHelper
|
@@ -11,15 +14,16 @@ module AmountField #:nodoc:
|
|
11
14
|
format_options.merge!(options.delete(:format) || {})
|
12
15
|
|
13
16
|
object = options.delete(:object) || instance_variable_get("@#{object_name}")
|
14
|
-
|
17
|
+
|
15
18
|
# if no explicit value is given, we set a formatted one. In case of an error we take the
|
16
19
|
# original value inserted by the user.
|
17
20
|
unless object.errors.on(method)
|
18
21
|
options[:value] ||= number_with_precision(object.send(method), format_options)
|
19
22
|
else
|
20
|
-
options[:value] ||= object.send("#{method}_before_type_cast")
|
23
|
+
options[:value] ||= object.send("#{AmountField::Configuration.prefix}_#{method}") || object.send("#{method}_before_type_cast")
|
21
24
|
end
|
22
|
-
|
25
|
+
|
26
|
+
options[:name] ||= "#{object_name}[#{AmountField::Configuration.prefix}_#{method}]"
|
23
27
|
options[:class] = "#{options[:class]} #{AmountField::Configuration.css_class}"
|
24
28
|
options[:id] ||= "#{object_name}_#{method}"
|
25
29
|
|
@@ -33,7 +37,6 @@ module AmountField #:nodoc:
|
|
33
37
|
include ActionView::Helpers::NumberHelper
|
34
38
|
|
35
39
|
def amount_field(method, options = {})
|
36
|
-
#todo try to remove redundant code by using: @template.amount_field(object, method, options)
|
37
40
|
format_options = I18n.t(:'number.amount_field.format', :raise => true) rescue {}
|
38
41
|
format_options = format_options.merge(AmountField::ActiveRecord::Validations.configuration)
|
39
42
|
format_options.merge!(options.delete(:format) || {})
|
@@ -41,16 +44,18 @@ module AmountField #:nodoc:
|
|
41
44
|
if (explicit_object = options.delete(:object))
|
42
45
|
self.object = explicit_object
|
43
46
|
end
|
44
|
-
|
47
|
+
|
45
48
|
# if no explicit value is given, we set a formatted one. In case of an error we take the
|
46
49
|
# original value inserted by the user.
|
47
50
|
unless object.errors.on(method)
|
48
51
|
options[:value] ||= number_with_precision(object.send(method), format_options)
|
49
52
|
else
|
50
|
-
options[:value] ||= object.send("#{method}_before_type_cast")
|
53
|
+
options[:value] ||= object.send("#{AmountField::Configuration.prefix}_#{method}") || object.send("#{method}_before_type_cast")
|
51
54
|
end
|
52
|
-
|
55
|
+
|
56
|
+
options[:name] ||= "#{object_name}[#{AmountField::Configuration.prefix}_#{method}]"
|
53
57
|
options[:class] = "#{options[:class]} #{AmountField::Configuration.css_class}"
|
58
|
+
options[:id] ||= "#{object_name}_#{method}"
|
54
59
|
|
55
60
|
text_field(method, options)
|
56
61
|
end
|
data/test/form_helper_test.rb
CHANGED
@@ -197,6 +197,74 @@ class FormHelperTest < ActionView::TestCase
|
|
197
197
|
assert_dom_equal expected_form, output_buffer
|
198
198
|
end
|
199
199
|
|
200
|
+
test "consider option name if given" do
|
201
|
+
@test_product = TestProduct.new(:amount_field_price => "47.11")
|
202
|
+
form_for(:test_product, @test_product, :builder => MyFormBuilder) do |f|
|
203
|
+
concat f.amount_field(:price, :name => 'article')
|
204
|
+
end
|
205
|
+
|
206
|
+
expected_input =
|
207
|
+
"<input name='article' size='30'" +
|
208
|
+
" class=' amount_field' type='text' id='test_product_price' value='47.11' />"
|
209
|
+
expected_form =
|
210
|
+
"<form action='http://www.example.com' method='post'>#{expected_input}</form>"
|
211
|
+
|
212
|
+
assert_dom_equal expected_input, amount_field(:test_product, :price, :name => 'article', :value => 47.11)
|
213
|
+
assert_dom_equal expected_form, output_buffer
|
214
|
+
end
|
215
|
+
|
216
|
+
test "consider option id if given" do
|
217
|
+
@test_product = TestProduct.new(:amount_field_price => "63.41")
|
218
|
+
form_for(:test_product, @test_product, :builder => MyFormBuilder) do |f|
|
219
|
+
concat f.amount_field(:price, :name => 'article', :id => 'my_id')
|
220
|
+
end
|
221
|
+
|
222
|
+
expected_input =
|
223
|
+
"<input name='article' size='30'" +
|
224
|
+
" class=' amount_field' type='text' id='my_id' value='63.41' />"
|
225
|
+
expected_form =
|
226
|
+
"<form action='http://www.example.com' method='post'>#{expected_input}</form>"
|
227
|
+
|
228
|
+
assert_dom_equal expected_input, amount_field(:test_product, :price, :name => 'article', :id => 'my_id', :value => 63.41)
|
229
|
+
assert_dom_equal expected_form, output_buffer
|
230
|
+
end
|
231
|
+
|
232
|
+
test "invalid negative value is displayed with current format options" do
|
233
|
+
with_locale('de') do
|
234
|
+
class MyTestProduct < ActiveRecord::Base
|
235
|
+
set_table_name 'test_products'
|
236
|
+
validates_amount_format_of :price
|
237
|
+
validates_numericality_of :price, :greater_than_or_equal_to => 0.0
|
238
|
+
end
|
239
|
+
@test_product = MyTestProduct.new(:amount_field_price => "-0,1")
|
240
|
+
@test_product.valid?
|
241
|
+
|
242
|
+
form_for(:test_product, @test_product, :builder => MyFormBuilder) do |f|
|
243
|
+
concat f.amount_field(:price)
|
244
|
+
end
|
245
|
+
|
246
|
+
assert_match /value="-0,1"/, output_buffer
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
test "invalid positive value is displayed with current format options" do
|
251
|
+
with_locale('de') do
|
252
|
+
class MyTestProduct < ActiveRecord::Base
|
253
|
+
set_table_name 'test_products'
|
254
|
+
validates_amount_format_of :price
|
255
|
+
validates_numericality_of :price, :less_than_or_equal_to => 10.0
|
256
|
+
end
|
257
|
+
@test_product = MyTestProduct.new(:amount_field_price => "12,34")
|
258
|
+
@test_product.valid?
|
259
|
+
|
260
|
+
form_for(:test_product, @test_product, :builder => MyFormBuilder) do |f|
|
261
|
+
concat f.amount_field(:price)
|
262
|
+
end
|
263
|
+
|
264
|
+
assert_match /value="12,34"/, output_buffer
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
200
268
|
protected
|
201
269
|
|
202
270
|
def protect_against_forgery?
|
data/test/validations_test.rb
CHANGED
@@ -371,7 +371,7 @@ class ValidationsTest < ActiveSupport::TestCase
|
|
371
371
|
end
|
372
372
|
end
|
373
373
|
|
374
|
-
test "
|
374
|
+
test "another validation macro validates the converted value" do
|
375
375
|
class TestProductWithOtherValidationMacros < ActiveRecord::Base
|
376
376
|
set_table_name 'test_products'
|
377
377
|
validates_amount_format_of :price
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amount_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 4
|
8
|
+
- 1
|
9
|
+
version: 1.4.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Thomas Baustert
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-04-23 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -32,6 +37,7 @@ files:
|
|
32
37
|
- Manifest
|
33
38
|
- README.rdoc
|
34
39
|
- Rakefile
|
40
|
+
- amount_field.gemspec
|
35
41
|
- init.rb
|
36
42
|
- install.rb
|
37
43
|
- lib/amount_field.rb
|
@@ -47,7 +53,6 @@ files:
|
|
47
53
|
- test/test_helper.rb
|
48
54
|
- test/validations_test.rb
|
49
55
|
- uninstall.rb
|
50
|
-
- amount_field.gemspec
|
51
56
|
has_rdoc: true
|
52
57
|
homepage: http://github.com/thomasbaustert/amount_field
|
53
58
|
licenses: []
|
@@ -66,18 +71,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
71
|
requirements:
|
67
72
|
- - ">="
|
68
73
|
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
69
76
|
version: "0"
|
70
|
-
version:
|
71
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
78
|
requirements:
|
73
79
|
- - ">="
|
74
80
|
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 1
|
83
|
+
- 2
|
75
84
|
version: "1.2"
|
76
|
-
version:
|
77
85
|
requirements: []
|
78
86
|
|
79
87
|
rubyforge_project: amount_field
|
80
|
-
rubygems_version: 1.3.
|
88
|
+
rubygems_version: 1.3.6
|
81
89
|
signing_key:
|
82
90
|
specification_version: 3
|
83
91
|
summary: Rails gem/plugin that accepts (amount) values in german or us format like 1.234,56 or 1,234.56
|