Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated gemspec and README re: price; bumped version

  • Loading branch information...
commit f86a32cf86ee0a4ce12a68064d911776772bb31e 1 parent c2127b2
Alex Batko authored
28 README.rdoc
Source Rendered
@@ -11,8 +11,9 @@ Recursively traverse a hash or array *ha*, searching for all occurrences of hash
11 11 The point of inspiration for this gem was to sanitize the params hash,
12 12 so that no matter how deeply nested a *price* or *quantity* might be, one can among
13 13 other things, remove the currency sign and commas from a form input
14   -(i.e. convert '$1,000.00' to '1000.00').
15   -Though modifying price was the initial motivation,
  14 +(i.e. convert '$1,000.00' to '1000.00'); however there is a much better way of handling this (see below).
  15 +
  16 +Though modifying price and quantity was the initial motivation for this gem,
16 17 you may use this gem to do any regex substitution to any hash key's value.
17 18
18 19 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
56 57 end
57 58 end
58 59
  60 += Note on handling price, quantity, etc.
  61 +
  62 +The following is about *price*, but it similarly applies to *quantity*, etc.
  63 +
  64 +Price may be a decimal attribute with `:precision => 8, :scale => 2`, and it must also pass validation:
  65 +
  66 + validates :price, numericality: {greater_than_or_equal_to: 0.01}
  67 +
  68 +One way to format user input is through a virtual attribute:
  69 +
  70 + def price_with_currency_and_commas=(new_price)
  71 + self.price = new_price.gsub(/[\$,]/,'') # note: this will bite you with non-US and non-CA users!
  72 + end
  73 +
  74 + def price_with_currency_and_commas
  75 + price # handle symmetry in products/_form.html.erb with number_to_currency
  76 + end
  77 +
  78 + <div class="field">
  79 + <%= f.label :price_with_currency_and_commas, "Price" %><span class="required">*</span><br />
  80 + <span class="description">(e.g. 1000 or $1,000.00)</span><br />
  81 + <%= f.text_field :price_with_currency_and_commas, size: 9, maxlength: 9, value: number_to_currency(@product.price) %>
  82 + </div>
BIN  rsubhak-0.1.1.gem → rsubhak-0.1.3.gem
Binary file not shown
4 rsubhak.gemspec
... ... @@ -1,9 +1,9 @@
1 1 Gem::Specification.new do |s|
2 2 s.name = 'rsubhak'
3   - s.version = '0.1.2'
  3 + s.version = '0.1.3'
4 4
5 5 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)."
6   - 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}"
  6 + 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."
7 7
8 8 s.authors = ['Alex Batko']
9 9 s.email = ['alexbatko@gmail.com']

0 comments on commit f86a32c

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