0

Rails 4.1 Ruby 2.0 Windows 8.1

I have a three different models in my application where I need to "sanitize" the phone numbers and email prior to saving. Can I do something like this in each of the models:

 before_save :sanitize_phones_and_email

and in helpers/application_helper.rb:

def sanitize_phones_and_email
  (self.email = email.downcase) if attribute_present?("email")
  (self.work_phone = phony_normalize work_phone, :default_country_code => 'US') if   attribute_present?("work_phone")
  (self.mobile_phone = phony_normalize mobile_phone, :default_country_code => 'US') if attribute_present?("mobile_phone")
  (self.fax_phone = phony_normalize fax_phone, :default_country_code => 'US') if attribute_present?("fax_phone")
  (self.other_phone = phony_normalize other_phone, :default_country_code => 'US') if attribute_present?("other_phone")
end

Would "self" be processed properly by Rails? (since I can't pass it as an argument to a method)

EastsideDev
  • 5,291
  • 6
  • 50
  • 87
  • Example right here in TFM: http://guides.rubyonrails.org/active_record_callbacks.html#callback-registration But I don't think you meant application_helper.rb. – Dave Schweisguth Jun 10 '14 at 17:11
  • I DO mean application_helper.rb. I'm already doing what your link points to. I am interested in DRY'ing it, as in I don't want to repeat myself in every model. I guess there is no clean way to do it – EastsideDev Jun 10 '14 at 21:26

1 Answers1

1

Helpers should only be used for methods that will be used in your views.

To answer your question, no, this will not work. You cannot use view helpers within your models.

If "sanitize_phones_and_email" were defined in every model where you are using it, it would work just fine (and "self" would refer to the instance of that model).

If you are interested in DRY'ing out your models, a simple and effective way (but not necessarily the best object oriented practice) is to use a mixin. When you include a mixin, the methods in that module automatically become instance methods on the class. "self" will refer to the object that it was included in.

For example, in Rails 4 you could put something like this in your "concerns" folder:

app/models/concerns/sanitzable.rb:

module Sanitizable
  extend ActiveSupport::Concern

  included do
    before_save :sanitize_phones_and_email
  end

  def sanitize_phones_and_email
    (self.email = email.downcase) if attribute_present?("email")
    (self.work_phone = phony_normalize work_phone, :default_country_code => 'US') if      attribute_present?("work_phone")
    (self.mobile_phone = phony_normalize mobile_phone, :default_country_code => 'US') if attribute_present?("mobile_phone")
    (self.fax_phone = phony_normalize fax_phone, :default_country_code => 'US') if attribute_present?("fax_phone")
    (self.other_phone = phony_normalize other_phone, :default_country_code => 'US') if attribute_present?("other_phone")
  end
end

app/models/my_model.rb

class MyModel < ActiveRecord::Base
  include Sanitizable    
end
Community
  • 1
  • 1
Joe Edgar
  • 794
  • 4
  • 13