Skip to content

Instantly share code, notes, and snippets.

@richievos
Created May 23, 2012 05:30
Show Gist options
  • Save richievos/2773412 to your computer and use it in GitHub Desktop.
Save richievos/2773412 to your computer and use it in GitHub Desktop.

Revisions

  1. richievos revised this gist May 23, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion public_state_manipulation.rb
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ def gender
    end

    private
    def infer_gender(@name)
    def infer_gender(name)
    if male_name?(name)
    'male'
    elsif female_name?(name)
  2. richievos revised this gist May 23, 2012. 3 changed files with 57 additions and 2 deletions.
    5 changes: 5 additions & 0 deletions public_state_manipulation.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # Proposed benefits:
    # * more verbose, but you can see more clearly all the "stuff" your method is using
    # * easier to re-use methods, since they're not coupled to instance var names
    # * less room for concurrency issues, since all state transforms would be in the top methods

    class User
    def gender
    infer_gender
    17 changes: 15 additions & 2 deletions same_thing_in_partials.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,18 @@
    Proposed benefits:
    / * more verbose, but you can see more clearly all the "stuff" your method is using
    / * easier to re-use partials/helpers, since they're not coupled to instance var names
    / * easy to tell what data you need loaded to be able to use a helper/partial
    / top_file.html.haml
    render :partial => 'user'
    = render :partial => 'user'

    / _user.html.haml
    = user_full_name_markup
    = render :partial => 'user_friend'

    / _user_friend.html.haml
    = @friend.name

    def user_full_name_markup
    <em>@user.last_name, @user.first_name</em>
    @@ -11,11 +21,14 @@ end
    vs

    / top_file.html.haml
    render :partial => 'user', :locals => { :user => @user }
    render :partial => 'user', :locals => { :user => @user, :friend => @friend }

    / _user.html.haml
    = user_full_name_markup(user)
    = render :partial => 'user_friend', :locals => { :friend => friend }

    / _user_friend.html.haml
    = friend.name

    def user_full_name_markup(user)
    <em>user.last_name, user.first_name</em>
    37 changes: 37 additions & 0 deletions simple_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Proposed benefits:
    # * more verbose, but you can see more clearly all the "stuff" your method is using
    # * easier to re-use methods, since they're not coupled to instance var names

    class UsersController < ApplicationController
    def update
    load_user
    check_if_user_now_active
    end

    private
    def load_user
    @user = User.find(params[:user_id])
    end

    def check_if_user_now_active
    flash.add(:info, "active") if @user.active?
    end
    end

    vs

    class UsersController < ApplicationController
    def update
    @user = load_user
    check_if_user_now_active(@user, flash)
    end

    private
    def load_user(params)
    User.find(params[:user_id])
    end

    def check_if_user_now_active(user, flash)
    flash.add(:info, "active") if user.active?
    end
    end
  3. richievos revised this gist May 23, 2012. No changes.
  4. richievos revised this gist May 23, 2012. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions same_thing_in_partials.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    / top_file.html.haml
    render :partial => 'user'

    / _user.html.haml
    = user_full_name_markup

    def user_full_name_markup
    <em>@user.last_name, @user.first_name</em>
    end

    vs

    / top_file.html.haml
    render :partial => 'user', :locals => { :user => @user }

    / _user.html.haml
    = user_full_name_markup(user)


    def user_full_name_markup(user)
    <em>user.last_name, user.first_name</em>
    end
  5. richievos created this gist May 23, 2012.
    52 changes: 52 additions & 0 deletions public_state_manipulation.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    class User
    def gender
    infer_gender
    @gender
    end

    private
    def infer_gender
    @gender ||= if male_name?
    'male'
    elsif female_name?
    'female'
    else
    'unknown'
    end
    end

    def male_name?
    @name == 'bob'
    end

    def female_name?
    @name == 'jane'
    end
    end

    vs

    class User
    def gender
    @gender ||= infer_gender(@name)
    end

    private
    def infer_gender(@name)
    if male_name?(name)
    'male'
    elsif female_name?(name)
    'female'
    else
    'unknown'
    end
    end

    def male_name?(name)
    name == 'bob'
    end

    def female_name?(name)
    name == 'jane'
    end
    end