Skip to content
Browse files

updated gemspec and README re: price; bumped version

  • Loading branch information...
1 parent c2127b2 commit f86a32cf86ee0a4ce12a68064d911776772bb31e @abatko committed Apr 4, 2012
Showing with 28 additions and 4 deletions.
  1. +26 −2 README.rdoc
  2. BIN rsubhak-0.1.1.gem → rsubhak-0.1.3.gem
  3. +2 −2 rsubhak.gemspec
View
28 README.rdoc
@@ -11,8 +11,9 @@ Recursively traverse a hash or array *ha*, searching for all occurrences of hash
The point of inspiration for this gem was to sanitize the params hash,
so that no matter how deeply nested a *price* or *quantity* might be, one can among
other things, remove the currency sign and commas from a form input
-(i.e. convert '$1,000.00' to '1000.00').
-Though modifying price was the initial motivation,
+(i.e. convert '$1,000.00' to '1000.00'); however there is a much better way of handling this (see below).
+
+Though modifying price and quantity was the initial motivation for this gem,
you may use this gem to do any regex substitution to any hash key's value.
As this method is recursive, it can handle deeply nested hashes and arrays, starting from either (see test/).
@@ -56,3 +57,26 @@ And then in your code, do something like the following. Note that in the Rails a
end
end
+= Note on handling price, quantity, etc.
+
+The following is about *price*, but it similarly applies to *quantity*, etc.
+
+Price may be a decimal attribute with `:precision => 8, :scale => 2`, and it must also pass validation:
+
+ validates :price, numericality: {greater_than_or_equal_to: 0.01}
+
+One way to format user input is through a virtual attribute:
+
+ def price_with_currency_and_commas=(new_price)
+ self.price = new_price.gsub(/[\$,]/,'') # note: this will bite you with non-US and non-CA users!
+ end
+
+ def price_with_currency_and_commas
+ price # handle symmetry in products/_form.html.erb with number_to_currency
+ end
+
+ <div class="field">
+ <%= f.label :price_with_currency_and_commas, "Price" %><span class="required">*</span><br />
+ <span class="description">(e.g. 1000 or $1,000.00)</span><br />
+ <%= f.text_field :price_with_currency_and_commas, size: 9, maxlength: 9, value: number_to_currency(@product.price) %>
+ </div>
View
BIN rsubhak-0.1.1.gem → rsubhak-0.1.3.gem
Binary file not shown.
View
4 rsubhak.gemspec
@@ -1,9 +1,9 @@
Gem::Specification.new do |s|
s.name = 'rsubhak'
- s.version = '0.1.2'
+ s.version = '0.1.3'
s.summary = "Recursively traverse a hash or array (ha), searching for all occurrences of hash key (k), and substituting (within their values) all occurrences of pattern (p) for replacement (r)."
- s.description = "Rsubhak can be used to sanitize a params hash. For instance, remove currency symbols and/or commas from decimal fields (e.g. price, cost, quantity), thereby converting '$1,500.00' to '1500.00'. But really, it's quite generic: #{s.summary}"
+ s.description = "#{s.summary}. Though the initial intent was to sanitize the params hash as far as removing currency symbols and/or commas from attributes such as price and quantity, there is a better approach: see the note on the homepage."
s.authors = ['Alex Batko']
s.email = ['alexbatko@gmail.com']

0 comments on commit f86a32c

Please sign in to comment.
Something went wrong with that request. Please try again.