Last active
December 11, 2015 14:09
-
-
Save edavis10/4612523 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FooController < ApplicationController | |
# | |
# Each "------ N" is a step of refactoring. I start with 0 and go as far as I need to | |
# depending on the code smell. Each step makes the code easier to reuse outside the view, | |
# controller, etc. | |
# ------ 0. Code smell, ivar | |
def some_action | |
@calculated_data = Something.funky.that('isnt').abstracted('in a lower level') | |
end | |
# View: <%= h @calculated_data %> | |
# ------ 1. helper_method | |
def some_action | |
end | |
private # << I'm not sure if private/protected for helper_method. Double check. | |
def calculated_data | |
Something.funky.that('isnt').abstracted('in a lower level') | |
end | |
helper_method :calculated_data | |
# View: <%= h calculated_data %> | |
# ------ 2. Helper | |
def some_action | |
end | |
# In app/helpers/ | |
module FooHelper | |
def calculated_data | |
Something.funky.that('isnt').abstracted('in a lower level') | |
end | |
end | |
# View: <%= h calculated_data %> | |
# ------ 3. Class | |
def some_action | |
end | |
# in app/models or lib/ or someplace in load path | |
class CalculatedSomething | |
def self.calculate | |
Something.funky.that('isnt').abstracted('in a lower level') | |
end | |
end | |
# View: <%= h CalculatedSomething.calculate %> | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment