Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Money freaks out whenever anything resembling a currency is in the field #65

Closed
skwp opened this Issue · 8 comments

5 participants

@skwp

If you have a price field and someone types in e.g. "COMING SOON" the money gem tries to parse the COM as a currency and blows up. I'm not sure if this is a problem of money-rails or money..suggestions on handling are much welcome. Adding a numericality validation does not seem to help as monetize tries to intercept the field before it hits normal validations (?)

 Failure/Error: product.update_attribute(:price, "COMING SOON")
     NoMethodError:
       undefined method `id' for "COM":String
     # ./spec/models/product_spec.rb:178:in `block (3 levels) in <top (required)>'
class Product
validate :price_cents, :numericality => true
monetize :price_cents
end
@weppos
Owner

My suggestion is to not use the same price for the price amount and the flag. It's not a good idea.
Use a boolean flag or a string field to store the "Coming Soon" or any other label and display it if present.

@skwp

The point is a user can type in anything random into the price field. It blows up before I even have a chance to strip it anywhere. Can't do before_validation, nothing...

@skwp

I guess it can be done in the controller

@skwp

I'd like to clarify this issue. We have a price field in a form. Users can type in absolutely anything. They typing in, let's say "FOO". This completely blows up money. There's no way to trap this unless we clean it in the controller which seems like the wrong place.

@haydn

Bump.

@anthonator

Is this still an issue? I'm not seeing this behavior.

@semmons99
Owner

no activity, closing

@semmons99 semmons99 closed this
@skwp

It's fine, but for anyone who comes across this problem, here is our solution:

module Reverb
  module Cleaners
    module Price

      PRICE_REGEX = /(?<price>[\d\s,.]+)/

      def self.clean(price)
        return nil unless price

        match = PRICE_REGEX.match(price)
        clean_price = match ? match[:price] : 0

        Money.parse(clean_price)
      end
    end
  end
end

In the controller

params[:product][:price] = Reverb::Cleaners::Price.clean(params[:product][:price])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.