Designs can be split across multiple pages by setting "break before" to true on a section

diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc
index 9c0dee9..031b09f 100644
--- a/CHANGELOG.rdoc
+++ b/CHANGELOG.rdoc
@@ -5,6 +5,7 @@
   * Projects and Sites are now sorted alphabetically when creating a sheet
   * Unsaved changes while editing sheets now asks the user if they want leave the page
   * Add last_emailed_at to sheets
+  * Designs can be split across multiple pages by setting "break before" to true on a section

 == 0.6.0

diff --git a/app/assets/javascripts/designs.js.coffee b/app/assets/javascripts/designs.js.coffee
index 722d467..c7d7117 100644
--- a/app/assets/javascripts/designs.js.coffee
+++ b/app/assets/javascripts/designs.js.coffee
@@ -172,7 +172,6 @@ jQuery ->
       $('[data-object~="' + $(this).data('selector') + '"]').hide()
       $($(this).data('target')).show()
       $($(this).data('target-hide')).hide()
-      false
     )
     .on('click', '[data-object~="variable-insert-after"]', () ->
       $.post(root_url + 'designs/add_variable', $("form").serialize() + "&location=" + $(this).data('position') + "&_method=post", null, "script")
diff --git a/app/assets/javascripts/variables.js.coffee b/app/assets/javascripts/variables.js.coffee
index c5cbb8f..f322200 100644
--- a/app/assets/javascripts/variables.js.coffee
+++ b/app/assets/javascripts/variables.js.coffee
@@ -98,6 +98,9 @@ jQuery ->
   $(document)
     .on('change', '#variable_variable_type', () -> toggleOptions($(this)))

+  if $('#variable_variable_type')
+    toggleOptions($('#variable_variable_type'));
+
   $('#options[data-object~="sortable"]').sortable( placeholder: "well alert alert-block" )

   $(document).on('click', '[data-object~="form-check-before-submit"]', () ->
@@ -117,6 +120,8 @@ jQuery ->
     if checkSoftDateMinMax() == false
       return false
     window.$isDirty = false
+    if $(this).data('page')?
+      $('#current_design_page').val($(this).data('page'))
     $($(this).data('target')).submit()
     false
   )
@@ -138,6 +143,3 @@ jQuery ->
     retrieveVariable(position)
     false
   )
-
-  if $('#variable_variable_type')
-    toggleOptions($('#variable_variable_type'));
diff --git a/app/controllers/sheets_controller.rb b/app/controllers/sheets_controller.rb
index f9cee12..526bb45 100644
--- a/app/controllers/sheets_controller.rb
+++ b/app/controllers/sheets_controller.rb
@@ -190,9 +190,17 @@ class SheetsController < ApplicationController
       if @sheet.save
         update_variables!

-        format.html { redirect_to @sheet, notice: 'Sheet was successfully created.' }
-        format.json { render json: @sheet, status: :created, location: @sheet }
+        params[:current_design_page] = params[:current_design_page].to_i + 1
+
+        if params[:current_design_page] < @sheet.design.total_pages
+          format.html { render 'edit' }
+          format.json { head :no_content }
+        else
+          format.html { redirect_to @sheet, notice: 'Sheet was successfully created.' }
+          format.json { render json: @sheet, status: :created, location: @sheet }
+        end
       else
+        params[:current_design_page] = 0
         format.html { render action: "new" }
         format.json { render json: @sheet.errors, status: :unprocessable_entity }
       end
@@ -209,9 +217,17 @@ class SheetsController < ApplicationController
         if @sheet.update_attributes(post_params)
           update_variables!

-          format.html { redirect_to @sheet, notice: 'Sheet was successfully updated.' }
-          format.json { head :no_content }
+          params[:current_design_page] = params[:current_design_page].to_i + 1
+
+          if params[:current_design_page] < @sheet.design.total_pages
+            format.html { render 'edit' }
+            format.json { head :no_content }
+          else
+            format.html { redirect_to @sheet, notice: 'Sheet was successfully updated.' }
+            format.json { head :no_content }
+          end
         else
+          params[:current_design_page] = 0
           format.html { render action: "edit" }
           format.json { render json: @sheet.errors, status: :unprocessable_entity }
         end
diff --git a/app/models/design.rb b/app/models/design.rb
index 404b4f5..dd81c9e 100644
--- a/app/models/design.rb
+++ b/app/models/design.rb
@@ -46,30 +46,6 @@ class Design < ActiveRecord::Base
     self.attributes.reject{|key, val| ['id', 'user_id', 'project_id', 'deleted', 'created_at', 'updated_at'].include?(key.to_s)}
   end

-  # # Check that user has selected an editable project  OR
-  # #            user is a librarian and project_id is blank
-  # def saveable?(current_user, params)
-  #   result = (current_user.all_projects.pluck(:id).include?(params[:project_id].to_i) or (current_user.librarian? and params[:project_id].blank?))
-  #   self.errors.add(:project_id, "can't be blank" ) unless result
-  #   result = (valid_option_tokens?(current_user, params) and result)
-  #   result
-  # end
-
-  # def valid_option_tokens?(current_user, params)
-  #   result = true
-  #   option_variable_ids = (params[:option_tokens] || {}).select{|key, hash| not hash.symbolize_keys[:variable_id].to_s.strip.blank?}.collect{|key, hash| hash.symbolize_keys[:variable_id]}
-  #   if option_variable_ids.uniq.size < option_variable_ids.size
-  #     self.errors.add(:variables, "can only be added once")
-  #     result = false
-  #   end
-  #   section_names = (params[:option_tokens] || {}).select{|key, hash| not hash.symbolize_keys[:section_name].to_s.strip.blank?}.collect{|key, hash| hash.symbolize_keys[:section_name]}
-  #   if section_names.uniq.size < section_names.size
-  #     self.errors.add(:section_names, "must be unique")
-  #     result = false
-  #   end
-  #   result
-  # end
-
   def check_option_validations
     result_a = check_project_id
@@ -103,6 +79,22 @@ class Design < ActiveRecord::Base
     result
   end

+  def options_page(page_number = 0)
+    current_page = 0
+    options_subset = []
+    self.options.each do |option|
+      current_page += 1 if option[:break_before] == '1'
+      options_subset << option if current_page == page_number
+    end
+    options_subset
+  end
+
+  def total_pages
+    @total_pages ||= begin
+      self.options.select{|option| option[:break_before] == '1'}.count + 1
+    end
+  end
+
   def option_tokens=(tokens)
     self.options = []
     tokens.each_pair do |key, option_hash|
@@ -118,7 +110,8 @@ class Design < ActiveRecord::Base
                           section_name: option_hash[:section_name].strip,
                           section_id: "_" + option_hash[:section_name].strip.gsub(/[^\w]/,'_').downcase,
                           section_description: option_hash[:section_description].to_s.strip,
-                          branching_logic: option_hash[:branching_logic].to_s.strip
+                          branching_logic: option_hash[:branching_logic].to_s.strip,
+                          break_before: option_hash[:break_before]
                         }
       end
     end
diff --git a/app/views/designs/_edit_section.html.erb b/app/views/designs/_edit_section.html.erb
index 1195f02..1d0525b 100644
--- a/app/views/designs/_edit_section.html.erb
+++ b/app/views/designs/_edit_section.html.erb
@@ -35,6 +35,12 @@
       </div>
     </div>

+    <div class="control-group">
+      <div class="controls checkbox">
+        <%= check_box_tag "design[option_tokens][#{position}][break_before]", '1', @option[:break_before] == '1' %> <%= label_tag "design_option_tokens_#{position}_break_before", 'Break Before' %>
+      </div>
+    </div>
+
     <div class="control-group" style="margin-bottom:0px">
       <div class="controls">
         <%= link_to 'Add Variable After', '#', class: 'btn btn-mini', data: { object: 'variable-insert-after', position: position } %>
diff --git a/app/views/designs/_section_and_variables.html.erb b/app/views/designs/_section_and_variables.html.erb
index 7e4c083..8d8acc2 100644
--- a/app/views/designs/_section_and_variables.html.erb
+++ b/app/views/designs/_section_and_variables.html.erb
@@ -7,7 +7,9 @@

 <%= render partial: 'designs/navigation' %>

-<% @design.options.each do |option| %>
+<%= hidden_field_tag :current_design_page, params[:current_design_page].to_i %>
+
+<% @design.options_page(params[:current_design_page].to_i).each do |option| %>
   <% @option = option %>
   <% if @option[:variable_id].blank? %>
     <%= render partial: 'designs/section' %>
@@ -15,3 +17,28 @@
     <%= render partial: 'designs/variable' %>
   <% end %>
 <% end %>
+
+<% if @design.total_pages > 1 %>
+  <% form_name = @sheet.new_record? ? 'new_sheet' : "edit_sheet_#{@sheet.id}" %>
+  <nav class="pagination pagination-centered">
+    <ul>
+      <% (1..@design.total_pages).each do |page| %>
+        <% if page == params[:current_design_page].to_i + 1 %>
+          <li class="page active"><%= link_to page, '#' %></li>
+        <% else %>
+          <li class="page">
+            <%= link_to page, '#', data: { target: "##{form_name}", object: "variable-check-before-submit", page: page - 2 }  %>
+          </li>
+        <% end %>
+      <% end %>
+    </ul>
+  </nav>
+<% end %>
+
+<% if false %>
+  <% if params[:current_design_page].to_i + 1 < @design.total_pages %>
+    <%= link_to "Save and Next Page", '#', class: 'btn btn-primary' %>
+  <% else %>
+    Last Page reached
+  <% end %>
+<% end %>
diff --git a/app/views/designs/selection.js.erb b/app/views/designs/selection.js.erb
index bedc12c..cf83629 100644
--- a/app/views/designs/selection.js.erb
+++ b/app/views/designs/selection.js.erb
@@ -9,7 +9,16 @@ $(".datepicker").datepicker({
 });
 $("#ui-datepicker-div").hide();

+<% if @design and params[:current_design_page].to_i + 1 < @design.total_pages %>
+  $('#sheet_submit_btn').val('Next Page');
+<% elsif @sheet.new_record? %>
+  $('#sheet_submit_btn').val('Create Sheet');
+<% else %>
+  $('#sheet_submit_btn').val('Update Sheet');
+<% end %>
+
 <% if @design %>
+
   <% @design.options.each do |option| %>
     <% @option = option %>
     <% if @option[:variable_id].blank? %>
diff --git a/app/views/designs/show.html.erb b/app/views/designs/show.html.erb
index 4a4aa8b..d3b5143 100644
--- a/app/views/designs/show.html.erb
+++ b/app/views/designs/show.html.erb
@@ -62,4 +62,6 @@
   <center><%= image_tag 'contour/ajax-loader.gif' %></center>
 </form>

-<%= form_tag selection_designs_path(sheet: { design_id: @design.id }), remote: true, method: :post, data: { object: 'form-load' } %>
+<%= form_tag selection_designs_path(sheet: { design_id: @design.id }), remote: true, method: :post, data: { object: 'form-load' } do %>
+  <%= hidden_field_tag :current_design_page, params[:current_design_page].to_i %>
+<% end %>
diff --git a/app/views/sheets/_form.html.erb b/app/views/sheets/_form.html.erb
index 367e6b0..bebd427 100644
--- a/app/views/sheets/_form.html.erb
+++ b/app/views/sheets/_form.html.erb
@@ -72,12 +72,17 @@
   <% end %>

   <div class="form-actions">
+    <% link_name = "Next Page" if @sheet.design and params[:current_design_page].to_i + 1 < @sheet.design.total_pages %>
     <% form_name = @sheet.new_record? ? 'new_sheet' : "edit_sheet_#{@sheet.id}" %>
-    <%= f.submit nil, class: 'btn btn-primary', data: { target: "##{form_name}", object: "variable-check-before-submit" }  %>
+    <% if link_name.blank? %>
+      <%= f.submit nil,       class: 'btn btn-primary', data: { target: "##{form_name}", object: "variable-check-before-submit" }, id: 'sheet_submit_btn'  %>
+    <% else %>
+      <%= f.submit link_name, class: 'btn btn-primary', data: { target: "##{form_name}", object: "variable-check-before-submit" }  %>
+    <% end %>
     <%= cancel %>
   </div>
 <% end %>

 <% unless @sheet.new_record? %>
-  <%= form_tag selection_designs_path(sheet_id: @sheet.id, sheet: { design_id: @sheet.design_id }), remote: true, method: :post, data: { object: 'form-load' } %>
+  <%= form_tag selection_designs_path(sheet_id: @sheet.id, sheet: { design_id: @sheet.design_id }, current_design_page: params[:current_design_page].to_i), remote: true, method: :post, data: { object: 'form-load' } %>
 <% end %>
diff --git a/app/views/sheets/show.html.erb b/app/views/sheets/show.html.erb
index a3a1b50..aa263bc 100644
--- a/app/views/sheets/show.html.erb
+++ b/app/views/sheets/show.html.erb
@@ -8,7 +8,7 @@
       <%= link_to "Send Email", '#', class: 'btn btn-primary btn-mini', id: 'email_popup' %>
     <% end %>
     <%= link_to "View Sheets", sheets_path, class: 'btn btn-mini' %>
-    <%= link_to "Print", print_sheet_path(@sheet, format: 'pdf'), target: '_blank', class: 'btn btn-mini' %>
+    <%= link_to "PDF", print_sheet_path(@sheet, format: 'pdf'), target: '_blank', class: 'btn btn-mini' %>
   </h1>
 </div>