On Wed, Oct 28, 2015 at 3:45 PM, Rodrigo Lueneberg <[email protected]>
wrote:
> How can I refactor this coordinate validation routine so that I can use
> this method with other fields of the same type, for instance, with
> "full_last_position" and also maintain the cleanliness of the code and
> avoiding repeating method code?
>
Maybe what you're looking for is ActiveModel::Validator ?
>
> class Provider < ActiveRecord::Base
> include UtilsModule
> attr_accessor :full_target_area_coordinate, :full_last_position # this
> field validates and saves coordinate
>
> validate :full_target_area_coordinate, :is_valid_coordinate?
>
> def is_valid_coordinate? # searches local geo_codes table for valid
> zip
> coord = self.full_target_area_coordinate.to_str
> match = coord.match(/^(-?\d{1,2}\.\d{6}),\s*(-?\d{1,2}\.\d{6})$/)
> if match.nil? # invalid format
> errors.add(:target_area_coordinate, :invalid)
> return false
> end
>
> self.target_area_coordinate =
> UtilsModule.convert_coordinate(self.full_target_area_coordinate)
> return true
> end
>
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/b1d068a6f9732cbadddf00e3a4bc1295%40ruby-forum.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/CAHUC_t8nbNXyzeyPzsVZjYFc7_h5OOPXTLn6sgWTLhMunGS27Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.