Skip to content

Commit

Permalink
db & cg: rapidftr#90 - re-ordering of forms functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorge authored and Dahlia Bock committed Nov 16, 2010
1 parent ea95ca4 commit 9927dcf
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 53 deletions.
11 changes: 10 additions & 1 deletion app/controllers/form_section_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ def enable
end
redirect_to formsections_url
end


def save_order
params[:form_order].each do |key, value|
form_section = FormSection.get_by_unique_id(key)
form_section.order = value.to_i
form_section.save!
end
redirect_to formsections_url
end

def new
@form_section = FormSection.new(params[:form_section])
end
Expand Down
95 changes: 56 additions & 39 deletions app/views/form_section/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,63 @@

<% form_tag enable_form_url, :id => 'enable_or_disable_form_section', :method => :post, :name => 'sections' do -%>

<table id="form_sections">
<thead>
<tr>
<th><input type="checkbox" disabled='true'/></th>
<th>Enabled</th>
<th>Name</th>
<th>Description</th>
<th>Ordering</th>
<th>Fields</th>
</tr>
</thead>
<tbody>

<% @form_sections.each do |section| %>

<tr id="<%= section.unique_id %>_row">
<td>
<% if section.editable? %>
<%= check_box_tag "sections[#{section.unique_id}]" %>
<% end %>
<table id="form_sections">
<thead>
<tr>
<th><input type="checkbox" disabled=true /></th>
<th>Enabled</th>
<th>Name</th>
<th>Description</th>
<th colspan="3">Ordering</th>
<th>Fields</th>
</tr>
</thead>
<tbody>
<% @form_sections.each do |section| %>
<tr id="<%= section.unique_id%>_row">
<td>
<% if section.editable? %>
<%= check_box_tag "sections[#{section.unique_id}]" %>
<% end %>
</td>
<td>
<span class="enabledStatus <%= section.enabled=="true" ? "tick" :"cross" %>">
<%= section.enabled? ? "Enabled" : "Disabled" %>
</span>
</td>
<td><a href="#" class="formSectionLink"><%= section.name %></a></td>
<td><%= section.description %></td>
<td>
<span class="formSectionOrder"><%= section.order %></span>
<span class="updatedFormSectionOrder">
<%= hidden_field_tag "form_order[#{section.unique_id}]", section.order %>
</span>
</td>
<% if section.editable? %>
<td class="orderingColumn">
<a id="<%= "#{section.unique_id}_up"%>" href="#" class="hidden moveUp">Up</a>
</td>
<td>
<span class="enabledStatus <%= section.enabled=="true" ? "tick" :"cross" %>">
<%= section.enabled? ? "Enabled" : "Disabled" %>
</span>
<td class="orderingColumn">
<a id="<%= "#{section.unique_id}_down"%>" href="#" class="hidden moveDown">Down</a>
</td>
<td><a href="#" class="formSectionLink"><%= section.name %></a></td>
<td><%= section.description %></td>
<td><span class="formSectionOrder"><%= section.order %></span></td>
<td>
<% if section.editable? %>
<%= link_to "Manage Fields", formsection_fields_path(section.unique_id), :class=>'manageFieldsLink' %>
<% end %>
<%= link_to "Manage Fields", formsection_fields_path(section.unique_id), :class=>'manageFieldsLink'%>
</td>
</tr>
<% end %>
</tbody>
</table>
<input type="button" name="Enable" value="Enable" id="enable_form"/>
<input type="button" name="Disable" value="Disable" id="disable_form"/>
<% end -%>

<p><%= link_to "Back", admin_path() %></p>
<% else %>
<td></td>
<td></td>
<td></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<div class="formSectionButtons">
<input type="button" name="Enable" value="Enable" id="enable_form"/>
<input type="button" name="Disable" value="Disable" id="disable_form"/>
<input type="button" name="SaveOrder" value="Save Order" id="save_order"/>
</div>
<div class="backLink">
<%= link_to "Back", admin_path() %>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<%= javascript_include_tag "jquery-1.4.3.min.js" %>
<%= javascript_include_tag "jquery-ui-1.8.6.custom.min.js" %>
<%= javascript_include_tag "application.js" %>
<%= javascript_include_tag "form_section.js" %>
<%= javascript_include_tag "utils.js" %>
<%= yield :javascript %>
</head>
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

map.enable_form 'form_section/enable', :controller => 'form_section', :action => 'enable', :value => true, :conditions => {:method => :post}
map.disable_form 'form_section/disable', :controller => 'form_section', :action => 'enable', :value => false

map.resources :formsections, :controller => 'form_section' do |form_section|

map.save_order "/form_section/save_order", :controller => "form_section", :action => "save_order"

map.resources :formsections, :controller=>'form_section' do |form_section|
additional_field_actions = FieldsController::FIELD_TYPES.inject({}){|h, type| h["new_#{type}"] = :get; h }
additional_field_actions[:new] = :get
additional_field_actions[:move_up] = :post
Expand Down
16 changes: 8 additions & 8 deletions features/manage_forms.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ Feature: So that admin can see Manage Forms Page
Background:
Given I am logged in as an admin
And I follow "Admin"
And I follow "Manage Forms"

Scenario: Admins should be able view default forms
Given I am on the admin page
When I follow "Manage Forms"
Then I should see "Basic details"
Then I should see "Family details"
Then I should see "Caregiver details"
And I create a new form called "Other details"
And I follow "Manage Forms"

Scenario: Admins should see correct re-ordering links for each section
Then I should see the "basic_details" section without any ordering links
And I should see "family_details" with order of "2"
And I should see "caregiver_details" with order of "3"
And I should see "other_details" with order of "4"

Scenario: Admins should see a back button
Then I should see "Back"
28 changes: 26 additions & 2 deletions features/step_definitions/form_section_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
row.inner_html.should_not include(link)
end

Then /^I should see the "([^\"]*)" section without any ordering links$/ do |section_name|
row = Hpricot(response.body).search("tr[@id=#{section_name}_row]").first
row.search("span.moveUp").should be_empty
row.search("span.moveDown").should be_empty
end

When /^I add a new text field with "([^\"]*)" and "([^\"]*)"$/ do |name, help_text|
When 'I follow "Add Custom Field"'
And 'I follow "Text Field"'
Expand All @@ -26,6 +32,13 @@
click_button("#{field_name}_#{arrow_name}")
end

When /^I press "([^\"]*)" next to "([^\"]*)"/ do |direction, unique_id|
click_link("#{unique_id}_#{direction}")
end

Then /^I should see an order of "([^\"]*)" next to "([^\"]*)"/ do |order, unique_id|
end

Then /^the "([^\"]*)" field should be above the "([^\"]*)" field$/ do |first_field_name, second_field_name|
table_rows = Hpricot(response.body).search("table tr")
row_ids = table_rows.collect {|row| row[:id]}
Expand All @@ -36,10 +49,21 @@
index_of_first_row.should < index_of_second_row
end

Then /^the "([^"]*)" dropdown should default to "([^"]*)"$/ do |field, value|
Then /^the "([^\"]*)" dropdown should default to "([^\"]*)"$/ do |field, value|
response_body.should have_selector("select[name='child[my_blank_dropdown_test]'] option[selected][value='']")
end

Given /^I fill in options for "([^"]*)"$/ do |field_label|
Given /^I fill in options for "([^\"]*)"$/ do |field_label|
fill_in(field_label, :with => "Option 1\r\nOption 2\r\nOption 3")
end

Given /^I create a new form called "([^\"]*)"$/ do |form_name|
FormSection.create_new_custom form_name
end

Then /^I should see "([^\"]*)" with order of "([^\"]*)"$/ do |form_name, form_order|
# Convert form name into unique_id, find this row, find order, make sure matches form_order
row = Hpricot(response.body).at("tr[@id=#{form_name}_row]")
order = row.at("span[@class='formSectionOrder']").inner_html
order.should == form_order
end
63 changes: 63 additions & 0 deletions public/javascripts/form_section.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
$(document).ready(function() {
initOrderingColumns();
$("a.moveDown").click(moveDown);
$("a.moveUp").click(moveUp);
$("input#save_order").click(saveOrder);
});

function initOrderingColumns() {
$("#form_sections tbody tr").each(function(index, element){
$("a.moveDown", element).show();
$("a.moveUp", element).show();
});

$("#form_sections tbody tr:nth-child(2)").each(function(index, element){
$("a.moveDown", element).show();
$("a.moveUp", element).hide();
});

$("#form_sections tbody tr:last").each(function(index, element){
$("a.moveDown", element).hide();
$("a.moveUp", element).show();
});

$("#form_sections tbody tr").each(function(index, element){ $(element).find(".updatedFormSectionOrder :input").val(index + 1); });
}

function moveUp()
{
var row = $(this).parents("tr");
var prevRow = row.prev("tr");
prevRow.before(row);
initOrderingColumns();
}

function moveDown()
{
var row = $(this).parents("tr");
var prevRow = row.next("tr");
prevRow.after(row);
initOrderingColumns();
}

function saveOrder(event) {
var form_order = {};
var updatedOrderings = $('.updatedFormSectionOrder :input');
$.each(updatedOrderings, function() {
var name = $(this).attr("name");
var id = /form_order\[(.*)\]/.exec(name)[1]
form_order[id] = $(this).attr("value");
});

$.ajax({
type: "POST",
data: {"form_order" : form_order},
url: '/form_section/save_order',
success: function(data) {
$("#form_sections").html($(data).find("#form_sections"));
$("a.moveDown").bind("click", moveDown);
$("a.moveUp").bind("click", moveUp);
initOrderingColumns();
}
});
}
11 changes: 11 additions & 0 deletions public/stylesheets/core.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ th { text-align: left;}
td, th { padding: .5em 10px; border-bottom: 1px solid #fff; }
td a { font-weight: bold; }

/* default classes */
.hidden { display:none; }

/* default: forms */
.default-form fieldset { padding:0.5em 10px; color: #666; background-color: #ddd; border: 1px solid #678; margin-bottom: 1em; overflow: hidden; clear: both; }
.default-form fieldset legend { font-weight: bold; font-size: 1.1em; }
Expand Down Expand Up @@ -190,6 +193,14 @@ td.checkbox { text-align: center;}
.new-form-section .submitButton { float:right; text-align:right;}
.new-form-section .cancelButton { float:left;}

/* form section index page */
span.moveUp { cursor:pointer; color: #3399FF; }
span.moveDown { cursor:pointer; color: #3399FF;}
div input#enable_form, div input#disable_form { float: left; margin-right: 5px; }
div input#save_order { float: right; }
div.formSectionButtons { overflow:hidden; }
div.formSectionButtons input { font-size: .75em; margin-top: 5px;}
div.backLink { font-size: 1em; margin-left: 5px; margin-top: 5px;}

/* fields list page */
.direction-button { display:none }
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/form_section_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ def valid?
assigns[:form_section].should == expected_form_section
end
end

describe "post save_order" do
it "should save the order of the forms" do
form_one = FormSection.create(:unique_id => "first_form", :name => "first form", :order => 1)
form_two = FormSection.create(:unique_id => "second_form", :name => "second form", :order => 2)
form_three = FormSection.create(:unique_id => "third_form", :name => "third form", :order => 3)
post :save_order, :form_order => {form_one.unique_id.to_s => "3", form_two.unique_id.to_s => "1", form_three.unique_id.to_s => "2"}
FormSection.get_by_unique_id(form_one.unique_id).order.should == 3
FormSection.get_by_unique_id(form_two.unique_id).order.should == 1
FormSection.get_by_unique_id(form_three.unique_id).order.should == 2
end
end

describe "post enable" do
it "when called with value false disables only the selected form sections" do
form_section1 = {:name=>"name1", :description=>"desc", :enabled=>"true", :unique_id=>"form_1"}
Expand Down Expand Up @@ -89,4 +102,16 @@ def valid?
post :enable, :value => true, :sections => {"form_1" => 1, "form_2" => 1}, :controller => "form_section"
end
end

describe "post save_order" do
it "should save the order of the forms" do
form_one = FormSection.create_new_custom("first form")
form_two = FormSection.create_new_custom("second form")
form_three = FormSection.create_new_custom("third form")
post :save_order, :form_order => {form_one.unique_id.to_s => "3", form_two.unique_id.to_s => "1", form_three.unique_id.to_s => "2"}
FormSection.get(form_one.id).order.should == 3
FormSection.get(form_three.id).order.should == 2
FormSection.get(form_two.id).order.should == 1
end
end
end

0 comments on commit 9927dcf

Please sign in to comment.