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

  • SEARCH
  • Home
  • 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 6357167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:06:47+00:00 2026-05-24T23:06:47+00:00

Today, trying to DRY up some code, I extracted some duplicate File.exists? code used

  • 0

Today, trying to DRY up some code, I extracted some duplicate File.exists? code used by several helper methods into a private method

def template_exists?(*template) and accidently monkey patched this already existing Rails helper method. This was a pretty clear indicator of a code smell, I didn’t need need any the inherited methods, yet I inherit them. Besides, what was my refactored method doing in this helper at all?

So, this helper does way too much and hence violates SRP (Single Responsibility Principle). I feel that rails helpers are inherently difficult to keep within SRP. The helper I’m looking at is a superclass helper of other helpers, counting 300+ lines itself. It is part of a very complex form using javascript to master the interaction flow. The methods in the fat helper are short and neat, so it’s not that awful, but without a doubt, it needs separation of concerns.

How should I go forth?

  1. Separate the methods into many helpers?
  2. Extracting the code inside the helpers methods into classes and delegate to them? Would you scope these classes (i.e. Mydomain::TemplateFinder)?
  3. Separate the logic into modules and list them as includes at the top?
  4. other approaches?

As I see, no 2 is safer wrt accidential monkeypatching. Maybe a combination?

Code examples and strong opinions appreciated!

  • 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-24T23:06:47+00:00Added an answer on May 24, 2026 at 11:06 pm

    Extracting helpers methods into classes (solution n°2)

    ok to address this specific way to clean up helpers 1st I dug up this old railscast :

    http://railscasts.com/episodes/101-refactoring-out-helper-object

    At the time it inspired me to create a little tabbing system (working in one of my apps in conjunction with a state machine) :

    module WorkflowHelper
    
      # takes the block  
      def workflow_for(model, opts={}, &block)
        yield Workflow.new(model, opts[:match], self)
        return false
      end
    
      class Workflow
        def initialize(model, current_url, view)
          @view = view
          @current_url = current_url
          @model = model
          @links = []
        end
    
        def link_to(text, url, opts = {})
          @links << url
          url = @model.new_record? ? "" : @view.url_for(url)
          @view.raw "<li class='#{active_class(url)}'>#{@view.link_to(text, url)}</li>"
        end
    
      private
        def active_class(url)
          'active' if @current_url.gsub(/(edit|new)/, "") == url.gsub(/(edit|new)/, "") ||
                     ( @model.new_record? && @links.size == 1 )
        end
    
      end #class Workflow
    end
    

    And my views go like this :

      -workflow_for @order, :match => request.path do |w|
        = w.link_to "✎ Create/Edit an Order", [:edit, :admin, @order]
        = w.link_to "√ Decide for Approval/Price", [:approve, :admin, @order]
        = w.link_to "✉ Notify User of Approval/Price", [:email, :admin, @order]
        = w.link_to "€ Create/Edit Order's Invoice", [:edit, :admin, @order, :invoice] 
    

    As you see it’s a nice way to encapsulate the logic in a class and have only one method in the helper/view space

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

Sidebar

Related Questions

I got an error today while trying to do some formatting to existing code.
I ran into a problem today when trying to set a field using FieldInfo.SetValue()
I'm currently trying to DRY up this initial verbose code: def planting_dates_not_nil? !plant_out_week_min.blank? ||
Actually i had been practicing some of the java programs today(trying to learn it
Today I'm trying to make a cron job for some forum login to check
Today, while I was trying to write code to just add and subtract the
Hey everyone, today I'm trying to get a link to an XML file passed
Today I have been trying to get an image to save into a database,
I just ran into LINQPad today and was trying to add a connection using
I was trying to follow some instructions today, and it starts with the comment

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.