Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to refactor methods like this one?
def activity_status
if currently_working?
'working'
elsif worked_recently?
'active'
elsif has_activity?
'inactive'
elsif signed_in_at_least_once?
'no_activity'
else
'never_sign_in'
end
end
@kossnocorp

This comment has been minimized.

Show comment
Hide comment
@kossnocorp

kossnocorp Sep 9, 2013

def activity_status
  case true
  when currently_working?
    'working'
  when worked_recently?
    'active'
  when has_activity?
    'inactive'
  when signed_in_at_least_once?
    'no_activity'
  else
    'never_sign_in'
  end
end
Owner

kossnocorp commented Sep 9, 2013

def activity_status
  case true
  when currently_working?
    'working'
  when worked_recently?
    'active'
  when has_activity?
    'inactive'
  when signed_in_at_least_once?
    'no_activity'
  else
    'never_sign_in'
  end
end
@vrybas

This comment has been minimized.

Show comment
Hide comment
@vrybas

vrybas Sep 9, 2013

@kossnocorp,

def activity_status
  return 'working'      if currently_working?
  return 'active'       if worked_recently?
  return 'inactive'     if has_activity?
  return 'no_activity'  if signed_in_at_least_once?
  'never_sign_in'
end

vrybas commented Sep 9, 2013

@kossnocorp,

def activity_status
  return 'working'      if currently_working?
  return 'active'       if worked_recently?
  return 'inactive'     if has_activity?
  return 'no_activity'  if signed_in_at_least_once?
  'never_sign_in'
end
@p0deje

This comment has been minimized.

Show comment
Hide comment
@p0deje

p0deje Sep 9, 2013

def activity_status
  case 
  when currently_working?       then 'working'
  when worked_recently?         then 'active'
  when has_activity?            then 'inactive'
  when signed_in_at_least_once? then 'no_activity'
  else 'never_sign_in'
  end
end

p0deje commented Sep 9, 2013

def activity_status
  case 
  when currently_working?       then 'working'
  when worked_recently?         then 'active'
  when has_activity?            then 'inactive'
  when signed_in_at_least_once? then 'no_activity'
  else 'never_sign_in'
  end
end
@dmitryn

This comment has been minimized.

Show comment
Hide comment
@dmitryn

dmitryn Sep 9, 2013

Although above solutions are good, however i add new style for variability:

def activity_status
  currently_working? && 'working' or
  worked_recently? && 'active' or 
  has_activity? && 'inactive' or
  signed_in_at_least_once? &&'no_activity' or
  'never_sign_in'
end

dmitryn commented Sep 9, 2013

Although above solutions are good, however i add new style for variability:

def activity_status
  currently_working? && 'working' or
  worked_recently? && 'active' or 
  has_activity? && 'inactive' or
  signed_in_at_least_once? &&'no_activity' or
  'never_sign_in'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment