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 8312803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:16:49+00:00 2026-06-08T20:16:49+00:00

So I’m taking my first step into using Presenters for my rails app and

  • 0

So I’m taking my first step into using Presenters for my rails app and I’m just looking at a refactoring of some of my code. I have several fields which display phone numbers (i.e. phone, cell and fax) nicely formatted or show “none given”. Obviously I originally had this in the view but moved the logic into my presenter. Once there I noticed it was all the same so refactored it into a private method which uses the method name and the send function:

class CustomerPresenter < BasePresenter
  presents :customer

  def phone
    format_number(__method__)
  end

  def cell
    format_number(__method__)
  end

  def fax
    format_number(__method__)
  end

private

  def format_number(method)
    hande_none customer.send(method) do
      h.number_to_phone(customer.send(method), :area_code => true)
    end
  end
end

and yet this code still doesn’t seem DRY. Because format_number uses the method name it seems that I have to define three seperate methods. I was curious if there was something more I could do here.

p.s. hande_none simply returns the block if there is something there or returns “none given”

  • 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-06-08T20:16:51+00:00Added an answer on June 8, 2026 at 8:16 pm

    I generally avoid to mix actual getter/method/attributes names with the methods used to format them.

    That’s why I’d use *_formatted suffix: with a general suffix, you can have a simple method_missing which would lead you to something like:

    class CustomerPresenter < BasePresenter
      presents :customer
    
      private
    
      def format_number(method)
        hande_none customer.send(method) do
          h.number_to_phone(customer.send(method), :area_code => true)
        end
      end
    
      def method_missing(method_name, *args, &block)
        case method_name
        when /(.*)_formatted$/
          #here you could create the method on the fly to avoid method missing next time
          return format_number( $1 )
        end    
        super(method_name, *args, &block)
      end
    end
    

    Basically, I’ve this in my BasePresenter for *_currency_format

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to

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.