Skip to content

Commit

Permalink
Enforce required fields at form layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Terrell committed Aug 14, 2015
1 parent a37a6b6 commit e705476
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
16 changes: 15 additions & 1 deletion app/forms/generic_asset_form.rb
Expand Up @@ -26,12 +26,22 @@ def template
end

def template_terms
template.visible_property_names
template.visible_property_names.map(&:to_sym)
end

def hidden_terms
self.class.terms - template_terms.map(&:to_sym)
end

def required_fields
self.class.required_fields | template_required_property_names
end

private

def template_required_property_names
template.required_property_names.map(&:to_sym)
end
end

# Provides a compatible API for using forms that have no template
Expand All @@ -45,4 +55,8 @@ def id
def visible_property_names
property_names
end

def required_property_names
[]
end
end
4 changes: 4 additions & 0 deletions app/models/form_template.rb
Expand Up @@ -21,6 +21,10 @@ def visible_property_names
properties.select {|property| property.visible?}.collect(&:name)
end

def required_property_names
properties.select {|property| property.required?}.collect(&:name)
end

def property_map
@property_map || rebuild_property_map
end
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/form_templates.rb
Expand Up @@ -8,6 +8,12 @@
end
end

trait :with_required_title do
after(:build) do |obj|
obj.properties << build(:form_template_property, :name => "title", :required => true)
end
end

trait :with_desc do
after(:build) do |obj|
obj.properties << build(:form_template_property, :name => "description")
Expand Down
27 changes: 25 additions & 2 deletions spec/forms/generic_asset_form_spec.rb
Expand Up @@ -42,7 +42,7 @@
end
end
context "when there is a template" do
let(:template) { instance_double("FormTemplate") }
let(:template) { instance_double(FormTemplate) }
before do
generic_form.template = template
allow(described_class).to receive(:terms).and_return([:foo, :banana])
Expand All @@ -63,7 +63,7 @@
end

context "when there is a template" do
let(:template) { instance_double("FormTemplate") }
let(:template) { instance_double(FormTemplate) }
before do
generic_form.template = template
allow(template).to receive(:visible_property_names).and_return(["foo", "bar"])
Expand All @@ -74,4 +74,27 @@
end
end
end

describe "#required_fields" do
context "when there is no template" do
it "should fall back to class-level required fields" do
expect(generic_form.required_fields).to eq GenericAssetForm.required_fields
end
end
context "when there is a template" do
let(:template) { instance_double(FormTemplate) }
before do
generic_form.template = template
allow(template).to receive(:required_property_names).and_return(["foo"])
end
it "should return the template's required properties" do
expect(generic_form.required_fields).to eq [:foo]
expect(generic_form.required?(:foo)).to eq true
end
it "should not lose the class' required fields" do
allow(generic_form.class).to receive(:required_fields).and_return([:bar])
expect(generic_form.required_fields).to eq [:bar, :foo]
end
end
end
end
7 changes: 7 additions & 0 deletions spec/models/form_template_spec.rb
Expand Up @@ -24,6 +24,13 @@
end
end

describe "#required_property_names" do
it "should return names of properties which are required" do
template = FactoryGirl.build(:form_template, :with_required_title, :with_desc)
expect(template.required_property_names).to contain_exactly("title")
end
end

describe "#property_names=" do
it "should build hidden properties" do
template.property_names = ["foo", "bar"]
Expand Down

0 comments on commit e705476

Please sign in to comment.