From 116b5628fac5b99783f77b19b78c5ccfc4f1ea7a Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Mon, 22 Jul 2013 16:47:42 +0200 Subject: [PATCH] uses default attribute rendering for custom fields in wp#form --- app/helpers/work_packages_helper.rb | 11 ++++++++- app/views/work_packages/_attributes.html.erb | 3 --- .../_form_custom_fields.html.erb | 24 ------------------- .../step_definitions/custom_field_steps.rb | 12 ++++++++-- .../work_packages/editable_fields.feature | 18 ++++++++++++++ lib/instance_finder.rb | 6 ++++- spec/factories/custom_field_factory.rb | 5 ++++ spec/factories/custom_value_factory.rb | 5 ++++ spec/helpers/work_packages_helper_spec.rb | 24 +++++++++++++++++-- 9 files changed, 75 insertions(+), 33 deletions(-) delete mode 100644 app/views/work_packages/_form_custom_fields.html.erb diff --git a/app/helpers/work_packages_helper.rb b/app/helpers/work_packages_helper.rb index e73bbcf825..ff74647b48 100644 --- a/app/helpers/work_packages_helper.rb +++ b/app/helpers/work_packages_helper.rb @@ -75,7 +75,8 @@ module WorkPackagesHelper work_package_form_due_date_attribute(form, work_package, locals), work_package_form_estimated_hours_attribute(form, work_package, locals), work_package_form_done_ratio_attribute(form, work_package, locals), - ].compact + work_package_form_custom_values_attribute(form, work_package, locals) + ].flatten.compact end def work_package_form_top_attributes(form, work_package, locals = {}) @@ -345,4 +346,12 @@ module WorkPackagesHelper WorkPackageAttribute.new(:done_ratio, field) end end + + def work_package_form_custom_values_attribute(form, work_package, locals = {}) + work_package.custom_field_values.map do |value| + field = custom_field_tag_with_label :work_package, value + + WorkPackageAttribute.new(:"work_package_#{value.id}", field) + end + end end diff --git a/app/views/work_packages/_attributes.html.erb b/app/views/work_packages/_attributes.html.erb index 70f040e624..387c073149 100644 --- a/app/views/work_packages/_attributes.html.erb +++ b/app/views/work_packages/_attributes.html.erb @@ -36,7 +36,4 @@ See doc/COPYRIGHT.rdoc for more details. <% end %> - -
- <%= render :partial => 'form_custom_fields', :locals => { :work_package => work_package } %> <% end %> diff --git a/app/views/work_packages/_form_custom_fields.html.erb b/app/views/work_packages/_form_custom_fields.html.erb deleted file mode 100644 index b0353c5bfd..0000000000 --- a/app/views/work_packages/_form_custom_fields.html.erb +++ /dev/null @@ -1,24 +0,0 @@ -<%#-- copyright -OpenProject is a project management system. - -Copyright (C) 2012-2013 the OpenProject Team - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License version 3. - -See doc/COPYRIGHT.rdoc for more details. - -++#%> - -
- <% i = 0 %> - <% split_on = (work_package.custom_field_values.size / 2.0).ceil - 1 %> - <% work_package.custom_field_values.each do |value| %> -

<%= custom_field_tag_with_label :issue, value %>

- <% if i == split_on -%> -
- <% end -%> - <% i += 1 -%> - <% end -%> -
-
diff --git a/features/step_definitions/custom_field_steps.rb b/features/step_definitions/custom_field_steps.rb index aa64770bbe..f73c7ab337 100644 --- a/features/step_definitions/custom_field_steps.rb +++ b/features/step_definitions/custom_field_steps.rb @@ -14,8 +14,8 @@ RouteMap.register(const, "/custom_fields") end -Given /^the following (user|issue) custom fields are defined:$/ do |type, table| - type = (type + "_custom_field").to_sym +Given /^the following (user|issue|work package) custom fields are defined:$/ do |type, table| + type = (type.gsub(" ", "_") + "_custom_field").to_sym as_admin do table.hashes.each_with_index do |r, i| @@ -43,6 +43,14 @@ Given /^the user "(.+?)" has the user custom field "(.+?)" set to "(.+?)"$/ do | user.save! end +Given /^the work package "(.+?)" has the custom field "(.+?)" set to "(.+?)"$/ do |wp_name, field_name, value| + wp = InstanceFinder.find(WorkPackage, wp_name) + custom_field = InstanceFinder.find(WorkPackageCustomField, field_name) + + wp.custom_values.build(:custom_field => custom_field, :value => value) + wp.save! +end + Given /^the custom field "(.+)" is( not)? summable$/ do |field_name, negative| custom_field = WorkPackageCustomField.find_by_name(field_name) diff --git a/features/work_packages/editable_fields.feature b/features/work_packages/editable_fields.feature index 9d46c2ff3c..f00de92c15 100644 --- a/features/work_packages/editable_fields.feature +++ b/features/work_packages/editable_fields.feature @@ -75,3 +75,21 @@ Feature: Fields editable on work package edit | Activity | | Comment | + Scenario: Going to the page and viewing custom field fields + Given the role "manager" may have the following rights: + | edit_work_packages | + + Given the following work package custom fields are defined: + | name | type | + | cf1 | int | + + And there are the following planning elements in project "ecookbook": + | subject | + | pe1 | + + And the work package "pe1" has the custom field "cf1" set to "4" + + When I go to the edit page of the work package called "pe1" + + Then I should see the following fields: + | cf1 | 4 | diff --git a/lib/instance_finder.rb b/lib/instance_finder.rb index 1a5d5ad03b..a00e1accaa 100644 --- a/lib/instance_finder.rb +++ b/lib/instance_finder.rb @@ -17,6 +17,10 @@ class InstanceFinder end def self.find(model, identifier) - instance = @model_method_map[model].call(identifier) + if @model_method_map[model].nil? + raise "#{model} is not registerd with InstanceFinder" + end + + @model_method_map[model].call(identifier) end end diff --git a/spec/factories/custom_field_factory.rb b/spec/factories/custom_field_factory.rb index a51dc6bdc5..ee971b37c5 100644 --- a/spec/factories/custom_field_factory.rb +++ b/spec/factories/custom_field_factory.rb @@ -63,6 +63,11 @@ FactoryGirl.define do end end + factory :work_package_custom_field do + sequence(:name) { |n| "Work Package Custom Field #{n}" } + type "WorkPackageCustomField" + end + factory :issue_custom_field do sequence(:name) { |n| "Issue Custom Field #{n}" } type "WorkPackageCustomField" diff --git a/spec/factories/custom_value_factory.rb b/spec/factories/custom_value_factory.rb index 8c9d5afc22..16d44795d4 100644 --- a/spec/factories/custom_value_factory.rb +++ b/spec/factories/custom_value_factory.rb @@ -23,5 +23,10 @@ FactoryGirl.define do custom_field :factory => :issue_custom_field customized :factory => :issue end + + factory :work_package_custom_value do + custom_field :factory => :issue_custom_field + customized :factory => :issue + end end end diff --git a/spec/helpers/work_packages_helper_spec.rb b/spec/helpers/work_packages_helper_spec.rb index 48f65fb132..c78f607b2c 100644 --- a/spec/helpers/work_packages_helper_spec.rb +++ b/spec/helpers/work_packages_helper_spec.rb @@ -12,6 +12,9 @@ require 'spec_helper' describe WorkPackagesHelper do + let(:stub_work_package) { FactoryGirl.build_stubbed(:planning_element) } + let(:form) { double('form', :select => "").as_null_object } + def inside_form &block ret = '' @@ -60,9 +63,7 @@ describe WorkPackagesHelper do describe :work_package_form_issue_category_attribute do let(:stub_project) { FactoryGirl.build_stubbed(:project) } - let(:stub_work_package) { FactoryGirl.build_stubbed(:planning_element) } let(:stub_category) { FactoryGirl.build_stubbed(:issue_category) } - let(:form) { double('form', :select => "").as_null_object } before do # set sensible defaults @@ -112,4 +113,23 @@ describe WorkPackagesHelper do attribute.field.should have_selector('input#work_package_estimated_hours[@value="3.00"]') end end + + describe :work_package_form_custom_values_attribute do + let(:stub_custom_value) { FactoryGirl.build_stubbed(:work_package_custom_value) } + let(:expected) { "field contents" } + + before do + stub_work_package.stub!(:custom_field_values).and_return([stub_custom_value]) + + helper.should_receive(:custom_field_tag_with_label).with(:work_package, stub_custom_value).and_return(expected) + end + + it "should return an array for an element for every value" do + helper.work_package_form_custom_values_attribute(form, stub_work_package, {}).size.should == 1 + end + + it "should return the result inside the field" do + helper.work_package_form_custom_values_attribute(form, stub_work_package, {}).first.field.should == expected + end + end end