Last active
August 29, 2015 14:27
-
-
Save ch1ago/13d6428c70d1d5544a76 to your computer and use it in GitHub Desktop.
Refactoring Sample
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
module Hello | |
class Account | |
class List | |
def initialize(request) | |
@request = request | |
end | |
def list | |
gather_wanted_strings | |
gather_wanted_models | |
gather_valid_strings | |
ensure_consistency_accross_models_and_session | |
return @models | |
end | |
private | |
def gather_wanted_strings | |
@wanted_strings = @request.session['access_tokens'] ||= [] | |
end | |
def gather_wanted_models | |
@models = find_models(@wanted_strings) | |
end | |
def find_models(access_tokens) | |
case access_tokens.size | |
when 0 then return @models = [] | |
when 1 then access_tokens = access_tokens.first | |
end | |
AccessToken.where(access_token: access_tokens) | |
end | |
def gather_valid_strings | |
@valid_strings = @models.map(&:active_access_token_or_destroy).map(&:presence).compact | |
end | |
def ensure_consistency_accross_models_and_session | |
if @wanted_strings != @valid_strings | |
@request.session['access_tokens'] = @valid_strings | |
@models = @models.select { |at| @valid_strings.include?(at.access_token) } | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment