code refactor(keep adding)

1.
old code

def page_group_toolbar(page_group)
  c = ""
  case page_group.class_name
  when "Preparation"
    c << li_link_to(t("Preparation"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"preparation"), true) +
      li_link_to(t("TaskCard"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"task_card")) +
      li_link_to(t("ExtraContent"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"extra_content"))
  when "TaskCard"
    c << li_link_to(t("Preparation"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"preparation"))+
      li_link_to(t("TaskCard"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"task_card"), true)+
      li_link_to(t("ExtraContent"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"extra_content"))
  when "ExtraContent"
    c << li_link_to(t("Preparation"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"preparation"))+
      li_link_to(t("TaskCard"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"task_card"))+
      li_link_to(t("ExtraContent"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"extra_content"), true)
  end
  return c
end

 
Refactor1

def page_group_toolbar(page_group)
  c = ""
  c << li_link_to(t("Preparation"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"preparation"), preparation?(page_group))
  c << li_link_to(t("TaskCard"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"task_card"), task_card?(page_group))
  c << li_link_to(t("ExtraContent"), admin_self_study_page_groups_path(:scenario_id=>page_group.scenario.id, :type=>"extra_content"), extra_content?(page_group))
  return c
end

def preparation?(page_group) 
  page_group.class_name == "preparation".camelize 
end 

def task_card?(page_group) 
  page_group.class_name == "task_card".camelize 
end 

def extra_content?(page_group) 
  page_group.class_name == "extra_content".camelize 
end 

 

Refactor2

TYPES = %w(preparation task_card extra_content)
def page_group_toolbar(page_group)
  returning "" do |c|
    TYPES.each do |type|
      c << li_link_to(t(type.camelize), admin_self_study_page_groups_path(:scenario_id => page_group.scenario.id, :type => type), send("#{type}?", page_group)) if page_group.scenario.send(type)
    end
  end
end

TYPES.each do |type| 
  define_method "#{type}?" do |page_group| 
    page_group.class_name == t.camelize 
  end 
end

你可能感兴趣的:(C++,c,C#)