Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:35:53+00:00 2026-05-14T20:35:53+00:00

Having a bit of difficulty finding out the proper way to mix in code

  • 0

Having a bit of difficulty finding out the proper way to mix in code that I put into the lib/ directory for Rails 2.3.5.

I have several models that require phone validation. I had at least three models that used the same code, so I wanted to keep things DRY and moved it out to the lib/ directory. I used to have code like this in each model:

validate :phone_is_valid

Then I’d have a phone_is_valid method in the model:

protected
def phone_is_valid
  # process a bunch of logic
  errors.add_to_base("invalid phone") if validation failed
end 

I moved this code out into lib/phones/ and in lib/phones I have lib/phones/phone_validation.rb, and in there I copy pasted the phone_is_valid method.

My question is, how do I mix this into all of my models now? And does my validate :phone_is_valid method remain the same or does that change? I want to make sure that the errors.add_to_base method continues to function as it did before while keeping everything DRY. obviously the following won’t work:

validate :Phones::PhoneValidation::phone_is_valid(number)

I also created another file in lib/phones/ called lib/phones/phone_normalize.rb. Again, many models need the value input by the user to be normalized. Meaning turn (555) 222-1212 to 5552221212 or something similar. Can I invoke that simply by invoking Phones::Phone_Normalize::normalize_method(number)?

I suppose I’m confused on the following:

  • How to use the lib directory for validation of multiple models that need access to a particular validation method
  • How to use the lib directory for commonly shared methods that return values
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:35:54+00:00Added an answer on May 14, 2026 at 8:35 pm

    A bit of metaprogramming magick?

    module Phones
    
      def self.included(base)
        base.send :extend, ClassMethods
      end
    
      module ClassMethods
        def validate_phone(*attr_names)
          #setup the config array eg. configuration = atrr.extract_options!
          validates_each(attr_names, configuration) do |record, attr_name, value|
              record.errors.add(attr_name, configuration[:message]) unless #validation
           end
           #setup the phone normalization
           unless configuration[:normalize]
             before_save do
               # normalization code here
             end
           end
        end
      end
    end
    
    ActiveRecord::Base.send :include, Phones
    

    Then in your model:

    validate_phone :main_phone, :cellphone, :message => "not a valid telephone number"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.