Hi,
The code below works perfectly but is so repetitive I feel there must be
a standard way to refactor it in this situation.  I am learning on my
own so am wondering how more experienced programmers would do this.  Any
examples would be appreciated.


def reset_to_previous
      contents_history = Profile.find(:first, conditions: [ "user_id =
?", current_user.id])

      if contents_history[:last_minisection_id].present?
        session[:minisection_id]    =
contents_history[:last_minisection_id]
        session[:subsection_id]     =
contents_history[:last_subsection_id]
        session[:section_id]        = contents_history[:last_section_id]
        session[:chapter_id]        = contents_history[:last_chapter_id]
        session[:book_id]           = contents_history[:last_book_id]
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        session[:book_title]        = Book.find(session[:book_id]).title
        session[:chapter_title]     =
Chapter.find(session[:chapter_id]).title
        session[:section_title]     =
Section.find(session[:section_id]).title
        session[:subsection_title]  =
Subsection.find(session[:subsection_id]).title
        session[:minisection_title] =
Minisection.find(session[:minisection_id]).title
        redirect_to(:controller => "contents", :action =>
"index_minisections", :subsection_id => session[:subsection_id])
      elsif contents_history[:last_subsection_id].present?
        session[:subsection_id]     =
contents_history[:last_subsection_id]
        session[:section_id]        = contents_history[:last_section_id]
        session[:chapter_id]        = contents_history[:last_chapter_id]
        session[:book_id]           = contents_history[:last_book_id]
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        session[:book_title]        = Book.find(session[:book_id]).title
        session[:chapter_title]     =
Chapter.find(session[:chapter_id]).title
        session[:section_title]     =
Section.find(session[:section_id]).title
        session[:subsection_title]  =
Subsection.find(session[:subsection_id]).title
        redirect_to(:controller => "contents", :action =>
"index_subsections", :section_id => session[:section_id])
      elsif contents_history[:last_section_id].present?
        session[:section_id]        = contents_history[:last_section_id]
        session[:chapter_id]        = contents_history[:last_chapter_id]
        session[:book_id]           = contents_history[:last_book_id]
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        session[:book_title]        = Book.find(session[:book_id]).title
        session[:chapter_title]     =
Chapter.find(session[:chapter_id]).title
        session[:section_title]     =
Section.find(session[:section_id]).title
        redirect_to(:controller => "contents", :action =>
"index_sections", :chapter_id => session[:chapter_id])
      elsif contents_history[:last_chapter_id].present?
        session[:chapter_id]        = contents_history[:last_chapter_id]
        session[:book_id]           = contents_history[:last_book_id]
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        session[:book_title]        = Book.find(session[:book_id]).title
        session[:chapter_title]     =
Chapter.find(session[:chapter_id]).title
        redirect_to(:controller => "contents", :action =>
"index_chapters", :book_id => session[:book_id])
      elsif contents_history[:last_book_id].present?
        session[:book_id]           = contents_history[:last_book_id]
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        session[:book_title]        = Book.find(session[:book_id]).title
        redirect_to(:controller => "contents", :action => "index_books",
:subject_id => session[:subject_id])
      elsif contents_history[:last_subject_id].present?
        session[:subject_id]        = contents_history[:last_subject_id]
        session[:subject_title]     =
Subject.find(session[:subject_id]).title
        redirect_to(:controller => "subjects", :action => "index")
      end
    end


Thanks,

Dave

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/9c160498fa6e3d322c0479cee918ecf5%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to