Created
May 23, 2012 05:30
-
-
Save richievos/2773412 to your computer and use it in GitHub Desktop.
Revisions
-
richievos revised this gist
May 23, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ def gender end private def infer_gender(name) if male_name?(name) 'male' elsif female_name?(name) -
richievos revised this gist
May 23, 2012 . 3 changed files with 57 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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 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 charactersOriginal 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' / _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, :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> 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 charactersOriginal 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 -
richievos revised this gist
May 23, 2012 . No changes.There are no files selected for viewing
-
richievos revised this gist
May 23, 2012 . 1 changed file with 22 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 -
richievos created this gist
May 23, 2012 .There are no files selected for viewing
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 charactersOriginal 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