Skip to content
Browse files

[Completes #41361065] Admin can order form fields

  • Loading branch information...
1 parent 3f2963b commit 644aabc7672f523a054d75fc89fcf40865bdda44 @greggersh greggersh committed
View
4 app/models/form.rb
@@ -1,5 +1,5 @@
class Form < ActiveRecord::Base
- has_many :form_fields
+ has_many :form_fields, :order => '-position DESC, id ASC'
has_one :pdf
has_many :submissions
attr_accessible :number, :title
@@ -9,7 +9,7 @@ class Form < ActiveRecord::Base
accepts_nested_attributes_for :form_fields, :reject_if => :all_blank, :allow_destroy => true
def as_json(options = {})
- super(options.merge(:only => [:id, :number, :title]))
+ super(options.merge(:only => [:number, :title]))
end
def to_param
View
6 app/models/form_field.rb
@@ -1,12 +1,12 @@
class FormField < ActiveRecord::Base
belongs_to :form
has_one :pdf_field
- attr_accessible :field_type, :name, :label, :description, :is_required, :options, :multiple
+ attr_accessible :field_type, :name, :label, :description, :is_required, :options, :multiple, :position
validates_presence_of :field_type, :name
serialize :options
def as_json(options = {})
- super_options = options.merge(:only => [:name, :field_type, :label, :description, :is_required, :multiple])
+ super_options = options.merge(:only => [:name, :field_type, :label, :description, :is_required, :multiple, :position])
super_options.merge!(:options => self.options.each{|k,v| {k: v}}) if self.options.present?
super(super_options)
end
@@ -22,5 +22,5 @@ def formtastic_field_type
else
self.field_type
end
- end
+ end
end
View
6 db/migrate/20121217231204_add_order_to_form_fields.rb
@@ -0,0 +1,6 @@
+class AddOrderToFormFields < ActiveRecord::Migration
+ def change
+ add_column :form_fields, :position, :integer
+ add_index :form_fields, [:form_id, :position]
+ end
+end
View
4 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121122023232) do
+ActiveRecord::Schema.define(:version => 20121217231204) do
create_table "agencies", :force => true do |t|
t.string "name"
@@ -31,8 +31,10 @@
t.boolean "is_required", :default => false
t.text "options"
t.boolean "multiple", :default => false
+ t.integer "position"
end
+ add_index "form_fields", ["form_id", "position"], :name => "index_form_fields_on_form_id_and_position"
add_index "form_fields", ["form_id"], :name => "index_form_fields_on_form_id"
create_table "forms", :force => true do |t|
View
4 lib/tasks/import.rake
@@ -11,8 +11,8 @@ namespace :mygov do
form = Form.find_or_create_by_number(parsed_json["form"]["number"], :title => parsed_json["form"]["title"])
pdf = form.build_pdf(parsed_json["form"]["pdf"]) if parsed_json["form"]["pdf"]
form.form_fields.destroy_all
- parsed_json["form"]["form_fields"].each do |form_field|
- new_form_field = form.form_fields.create!(form_field.reject{|k,v| k == "pdf_field"})
+ parsed_json["form"]["form_fields"].each_with_index do |form_field, index|
+ new_form_field = form.form_fields.create!(form_field.reject{|k,v| k == "pdf_field"}.merge(:position => index + 1))
if form_field["pdf_field"]
pdf_field = pdf.pdf_fields.create!(form_field["pdf_field"].first) if form_field["pdf_field"].is_a?(Array)
pdf_field = pdf.pdf_fields.create!(form_field["pdf_field"]) unless form_field["pdf_field"].is_a?(Array)
View
4 spec/features/api_spec.rb
@@ -15,8 +15,8 @@
get "/api/forms"
response.code.should == "200"
parsed_json = JSON.parse(response.body)
- parsed_json.first.should == {"id" => 1, "title" => "Sample Form 1", "number" => 'S-1'}
- parsed_json.last.should == {"id" => 2, "title" => "Sample Form 2", "number" => 'S-2'}
+ parsed_json.first.should == {"title" => "Sample Form 1", "number" => 'S-1'}
+ parsed_json.last.should == {"title" => "Sample Form 2", "number" => 'S-2'}
end
end
View
13 spec/models/form_field_spec.rb
@@ -1,9 +1,22 @@
require 'spec_helper'
describe FormField do
+ before do
+ @form = Form.create!(:title => "Form 1", :number => "F-1")
+ @valid_attributes = {
+ :field_type => "string",
+ :name => "Field 1"
+ }
+ end
it { should validate_presence_of :field_type }
it { should validate_presence_of :name }
it { should belong_to :form }
it { should have_one :pdf_field }
+
+ it "should create a new instance given valid attributes" do
+ form_field = FormField.new(@valid_attributes)
+ form_field.form = @form
+ form_field.save!
+ end
end
View
32 spec/models/form_spec.rb
@@ -12,4 +12,36 @@
it { should validate_presence_of :number }
it { should have_many :form_fields }
it { should have_one :pdf }
+
+ it "should create a new object with valid attributes" do
+ Form.create!(@valid_attributes)
+ end
+
+ context "when the form has form fields, so with field orders, some without" do
+ before do
+ @form = Form.create!(@valid_attributes)
+ @form.form_fields.create!(:field_type => "string", :name => "Unordered field 1")
+ @form.form_fields.create!(:field_type => "string", :name => "Unordered field 2")
+ @form.form_fields.create!(:field_type => "string", :name => "Field 2", :position => 2)
+ @form.form_fields.create!(:field_type => "string", :name => "Field 1", :position => 1)
+ end
+
+ it "should return the fields in order, with unordered fields last, sorted by id asc" do
+ fields = @form.form_fields
+ fields.first.name.should == "Field 1"
+ fields[1].name.should == "Field 2"
+ fields[2].name.should == "Unordered field 1"
+ fields.last.name.should == "Unordered field 2"
+ end
+ end
+
+ describe "#to_json" do
+ before do
+ @form = Form.create!(@valid_attributes)
+ end
+
+ it "should output JSON with the number and title" do
+ JSON.parse(@form.to_json).should == {"title" => 'Form Title', "number" => "F-1"}
+ end
+ end
end

0 comments on commit 644aabc

Please sign in to comment.
Something went wrong with that request. Please try again.