8f5553306c2cf7f4b14153f6117f8e9b

I can't seem to figure out a graceful way to do this....

# TODO: Defangle this (dep).
  # This is gross, but can't think of a classier way at the moment.
  def show_authentication_action_as_link
    html ||= ""

    if not respondent_signed_in?
      link_to "Sign up as User", new_respondent_registration_path, :class => "iframe"
      link_to 'Sign in as User', new_respondent_session_path 
    else
      link_to 'Take a survey', respondent_surveys_path
      link_to 'Logout', destroy_respondent_session_path
    end 
    if not admin_signed_in?
      link_to 'Sign in as Admin', new_admin_session_path
    else
      link_to 'Logout', destroy_admin_session_path
    end 
    if client_account_manager_signed_in?
      link_to "Client Manager Account Logout", destroy_client_account_manager_session_path
    else
      link_to "Client Manager Account Login", new_client_account_manager_session_path
    end 
  end 
  def show_authentication_action_as_button
    html ||= ""

    if not respondent_signed_in?                                                                                                                             
      button "Sign up as User", :category => "confirm", :onclick => "$('a.registration_link').trigger('click');", :class => "iframe"
      button 'Sign in as User', :category => "confirm", :url => new_respondent_session_path 
    else
      button "Take a survey", :category => "confirm", :url => respondent_surveys_path
      button 'Logout', :category => "confirm", :url => destroy_respondent_session_path
    end 
    if not admin_signed_in?
      button 'Sign in as Admin', :category => "confirm", :url => new_admin_session_path
    else
      button 'Logout', :category => "confirm", :url => destroy_admin_session_path 
    end 
    if client_account_manager_signed_in?
      button "Client Manager Account Logout", :category => "confirm", :url => destroy_client_account_manager_session_path
    else
      button "Client Manager Account Login", :category => "confirm", :url => new_client_account_manager_session_path
    end 
  end

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

February 16, 2011, February 16, 2011 07:00, permalink

No rating. Login to rate!

Sorry for the duplicate posts. The site was acting up when I tried submitting them.

A8d3f35baafdaea851914b17dae9e1fc

Adam

February 16, 2011, February 16, 2011 07:01, permalink

No rating. Login to rate!

...

A8d3f35baafdaea851914b17dae9e1fc

Adam

February 16, 2011, February 16, 2011 07:02, permalink

No rating. Login to rate!

Could probably use some further refactoring, but it's a start:

def authentication_actions
  presenter = ButtonPresenter.new(self)
  
  presenter.with_options(:category => 'confirm') do |confirmed|
    unless respondent_signed_in?
      confirmed.add('Sign up as User', :url => new_respondent_registration_path,
        :onclick => "$('a.registration_link').trigger('click');", :class => 'iframe')
      confirmed.add('Sign in as User', :url => new_respondent_session_path)
    else
      confirmed.add('Take a survey', :url => respondent_surveys_path)
      confirmed.add('Logout', :url => destroy_respondent_session_path)
    end
  end
  
  presenter
end

def show_authentication_action_as_link
  authentication_actions.to_links
end

def show_authentication_action_as_button
  authentication_actions.to_buttons
end
class ButtonPresenter
  def initialize(context)
    @context, @buttons = context, []
  end
  
  def add(text, options = {})
    @buttons << [ text, options ]
  end
  
  def to_links
    @buttons.map do |text,options|
      options = options.dup
      options.delete(:onclick)
      options.delete(:category)
      
      @context.link_to(text, options.delete(:url), options)
    end
  end
  
  def to_buttons
    @buttons.map do |text,options|
      options = options.dup
      options.delete(:url) if options.has_key?(:onclick)
      
      @context.button(text, options)
    end
  end
end

Your refactoring





Format Copy from initial code

or Cancel