Same custom method validation with different fields of same type

7 views
Skip to first unread message

Rodrigo Lueneberg

unread,
Oct 28, 2015, 4:45:49 PM10/28/15
to rubyonra...@googlegroups.com
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?

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/.

botp

unread,
Oct 29, 2015, 12:35:02 AM10/29/15
to rubyonra...@googlegroups.com
On Thu, Oct 29, 2015 at 4:45 AM, Rodrigo Lueneberg <li...@ruby-forum.com> wrote:
> def is_valid_coordinate? # searches local geo_codes table for valid
> zip
> coord = self.full_target_area_coordinate.to_str

coord = self.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)

errors.add(self.to_sym, :invalid)


> return false
> end
>
> self.target_area_coordinate =
> UtilsModule.convert_coordinate(self.full_target_area_coordinate)

just return true here.
i would suggest separating the conversion from the validation.

kind regards,
--botp

> return true
> end
>
> end
>

tamouse pontiki

unread,
Oct 29, 2015, 12:58:02 AM10/29/15
to rubyonra...@googlegroups.com
On Wed, Oct 28, 2015 at 3:45 PM, Rodrigo Lueneberg <li...@ruby-forum.com> 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
--
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 rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
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.

Reply all
Reply to author
Forward
0 new messages