Redmine Refactor #134: Extract Method in WikiController#special to utility method

I’m still working on refactoring that case statement in WikiController#special but before I can extract the next actions (page_index,date_index), I need to refactor some of it’s inner logic.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class WikiController  "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
                                      :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
                                      :order => 'title'
      @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
      @pages_by_parent_id = @pages.group_by(&:parent_id)
    when 'export'
      redirect_to :action => 'export', :id => @project # Compatibility stub while refactoring
      return
    else
      # requested special page doesn't exist, redirect to default page
      redirect_to :action => 'index', :id => @project, :page => nil
      return
    end
    render :action => "special_#{page_title}"
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class WikiController  'export', :id => @project # Compatibility stub while refactoring
      return
    else
      # requested special page doesn't exist, redirect to default page
      redirect_to :action => 'index', :id => @project, :page => nil
      return
    end
    render :action => "special_#{page_title}"
  end
 
  # eager load information about last updates, without loading text
  def load_pages_grouped_by_date_without_content
    @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
                                    :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
                                    :order => 'title'
    @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
    @pages_by_parent_id = @pages.group_by(&:parent_id)
  end
 
end

Now that I’ve extracted the find from #special to a utility method, I can now refactor the page_index and date_index conditions out of the case statement. Then (probably next week), I can work on removing the case statement and maybe even the #special method completely.

Reference commit