From: Jean-Philippe Lang Date: Tue, 7 Aug 2012 16:58:51 +0000 (+0000) Subject: Update the new custom field form with remotely. X-Git-Tag: 2.1.0~164 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=599736aca7b9b5bd47d39fdf72c9fc1127730497;p=redmine.git Update the new custom field form with remotely. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10163 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 83e0fa254..115333581 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -261,6 +261,10 @@ class CustomField < ActiveRecord::Base validate_field_value(value).empty? end + def format_in?(*args) + args.include?(field_format) + end + protected # Returns the error message for the given value regarding its format diff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erb index 7ffbbaedc..dcfcc32ec 100644 --- a/app/views/custom_fields/_form.html.erb +++ b/app/views/custom_fields/_form.html.erb @@ -1,88 +1,31 @@ <%= error_messages_for 'custom_field' %> - -

<%= f.text_field :name, :required => true %>

-

<%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :onchange => "toggle_custom_field_format();", - :disabled => !@custom_field.new_record? %>

+

<%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :disabled => !@custom_field.new_record? %>

+ +<% if @custom_field.format_in? 'list', 'user', 'version' %>

<%= f.check_box :multiple, :disabled => @custom_field.multiple && !@custom_field.new_record? %>

+<% end %> + +<% unless @custom_field.format_in? 'list', 'bool', 'date', 'user', 'version' %>

<%= f.text_field :min_length, :size => 5, :no_label => true %> - <%= f.text_field :max_length, :size => 5, :no_label => true %>
(<%=l(:text_min_max_length_info)%>)

<%= f.text_field :regexp, :size => 50 %>
(<%=l(:text_regexp_info)%>)

+<% end %> + +<% if @custom_field.format_in? 'list' %>

<%= f.text_area :possible_values, :value => @custom_field.possible_values.to_a.join("\n"), :rows => 15 %> <%= l(:text_custom_field_possible_values_info) %>

+<% end %> + +<% unless @custom_field.format_in? 'user', 'version' %>

<%= @custom_field.field_format == 'bool' ? f.check_box(:default_value) : f.text_field(:default_value) %>

+<% end %> + <%= call_hook(:view_custom_fields_form_upper_box, :custom_field => @custom_field, :form => f) %>
@@ -127,4 +70,3 @@ when "IssueCustomField" %> <% end %> <%= call_hook(:"view_custom_fields_form_#{@custom_field.type.to_s.underscore}", :custom_field => @custom_field, :form => f) %> -<%= javascript_tag "toggle_custom_field_format();" %> diff --git a/app/views/custom_fields/edit.html.erb b/app/views/custom_fields/edit.html.erb index dce34736f..43a8ebf30 100644 --- a/app/views/custom_fields/edit.html.erb +++ b/app/views/custom_fields/edit.html.erb @@ -2,7 +2,7 @@ » <%= link_to l(@custom_field.type_name), :controller => 'custom_fields', :action => 'index', :tab => @custom_field.class.name %> » <%=h @custom_field.name %> -<%= labelled_form_for :custom_field, @custom_field, :url => custom_field_path(@custom_field), :html => {:method => :put} do |f| %> +<%= labelled_form_for :custom_field, @custom_field, :url => custom_field_path(@custom_field), :html => {:method => :put, :id => 'custom_field_form'} do |f| %> <%= render :partial => 'form', :locals => { :f => f } %> <%= submit_tag l(:button_save) %> <% end %> diff --git a/app/views/custom_fields/new.html.erb b/app/views/custom_fields/new.html.erb index cde4fab58..71e7197ec 100644 --- a/app/views/custom_fields/new.html.erb +++ b/app/views/custom_fields/new.html.erb @@ -2,8 +2,18 @@ » <%= link_to l(@custom_field.type_name), :controller => 'custom_fields', :action => 'index', :tab => @custom_field.class.name %> » <%= l(:label_custom_field_new) %> -<%= labelled_form_for :custom_field, @custom_field, :url => custom_fields_path do |f| %> +<%= labelled_form_for :custom_field, @custom_field, :url => custom_fields_path, :html => {:id => 'custom_field_form'} do |f| %> <%= render :partial => 'form', :locals => { :f => f } %> <%= hidden_field_tag 'type', @custom_field.type %> <%= submit_tag l(:button_save) %> <% end %> + +<%= javascript_tag do %> +$('#custom_field_field_format').change(function(){ + $.ajax({ + url: '<%= new_custom_field_path(:format => 'js') %>', + type: 'get', + data: $('#custom_field_form').serialize() + }); +}); +<% end %> diff --git a/app/views/custom_fields/new.js.erb b/app/views/custom_fields/new.js.erb new file mode 100644 index 000000000..d77c97f07 --- /dev/null +++ b/app/views/custom_fields/new.js.erb @@ -0,0 +1 @@ +$('#content').html('<%= escape_javascript(render :template => 'custom_fields/new', :layout => nil, :formats => [:html]) %>') diff --git a/test/functional/custom_fields_controller_test.rb b/test/functional/custom_fields_controller_test.rb index 3e3506c1b..816a12b03 100644 --- a/test/functional/custom_fields_controller_test.rb +++ b/test/functional/custom_fields_controller_test.rb @@ -16,18 +16,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require File.expand_path('../../test_helper', __FILE__) -require 'custom_fields_controller' - -# Re-raise errors caught by the controller. -class CustomFieldsController; def rescue_action(e) raise e end; end class CustomFieldsControllerTest < ActionController::TestCase fixtures :custom_fields, :custom_values, :trackers, :users def setup - @controller = CustomFieldsController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new @request.session[:user_id] = 1 end @@ -43,7 +36,10 @@ class CustomFieldsControllerTest < ActionController::TestCase assert_response :success assert_template 'new' assert_kind_of klass, assigns(:custom_field) - assert_tag :select, :attributes => {:name => 'custom_field[field_format]'} + assert_select 'form#custom_field_form' do + assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' + assert_select 'input[type=hidden][name=type][value=?]', klass.name + end end end @@ -51,22 +47,23 @@ class CustomFieldsControllerTest < ActionController::TestCase get :new, :type => 'IssueCustomField' assert_response :success assert_template 'new' - assert_tag :input, :attributes => {:name => 'custom_field[name]'} - assert_tag :select, - :attributes => {:name => 'custom_field[field_format]'}, - :child => { - :tag => 'option', - :attributes => {:value => 'user'}, - :content => 'User' - } - assert_tag :select, - :attributes => {:name => 'custom_field[field_format]'}, - :child => { - :tag => 'option', - :attributes => {:value => 'version'}, - :content => 'Version' - } - assert_tag :input, :attributes => {:name => 'type', :value => 'IssueCustomField'} + assert_select 'form#custom_field_form' do + assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do + assert_select 'option[value=user]', :text => 'User' + assert_select 'option[value=version]', :text => 'Version' + end + assert_select 'input[type=hidden][name=type][value=IssueCustomField]' + end + end + + def test_new_js + get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js' + assert_response :success + assert_template 'new' + assert_equal 'text/javascript', response.content_type + + field = assigns(:custom_field) + assert_equal 'list', field.field_format end def test_new_with_invalid_custom_field_class_should_render_404