From bcba8262b3136fa851f361a428ffac62d11f7036 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Tue, 21 Apr 2009 12:17:42 -0700 Subject: [PATCH] [#3942] Added a controller to handle JavaScript requests for a CustomField options. * Added OverheadTimeEntryActivityController which requires administrator access * Added a partial which will render the HTML options for a select field * Added some RSpec examples --- ...overhead_time_entry_activity_controller.rb | 34 ++++++ .../_values.html.erb | 1 + ...ead_time_entry_activity_controller_spec.rb | 107 ++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 app/controllers/overhead_time_entry_activity_controller.rb create mode 100644 app/views/overhead_time_entry_activity/_values.html.erb create mode 100644 spec/controllers/overhead_time_entry_activity_controller_spec.rb diff --git a/app/controllers/overhead_time_entry_activity_controller.rb b/app/controllers/overhead_time_entry_activity_controller.rb new file mode 100644 index 0000000..fa0111e --- /dev/null +++ b/app/controllers/overhead_time_entry_activity_controller.rb @@ -0,0 +1,34 @@ +class OverheadTimeEntryActivityController < ApplicationController + unloadable + + before_filter :require_admin + + def index + if params[:custom_field] + @field = TimeEntryActivityCustomField.find(params[:custom_field]) + @values = select_values_for_field(@field) + end + @values ||= [] + + render :partial => 'values' + end + + private + + def select_values_for_field(field) + if field.field_format == 'list' + returning [] do |r| + field.possible_values.each do |item| + if item == 'Nil' + r << [l(:label_none), item] # Nil should use the none label + else + r << [item, item] + end + end + r + end + elsif field.field_format == 'bool' + return [[true,true],[false,false]] + end + end +end diff --git a/app/views/overhead_time_entry_activity/_values.html.erb b/app/views/overhead_time_entry_activity/_values.html.erb new file mode 100644 index 0000000..78f7bf4 --- /dev/null +++ b/app/views/overhead_time_entry_activity/_values.html.erb @@ -0,0 +1 @@ +<%= options_for_select(@values) %> diff --git a/spec/controllers/overhead_time_entry_activity_controller_spec.rb b/spec/controllers/overhead_time_entry_activity_controller_spec.rb new file mode 100644 index 0000000..2764982 --- /dev/null +++ b/spec/controllers/overhead_time_entry_activity_controller_spec.rb @@ -0,0 +1,107 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe OverheadTimeEntryActivityController, "#index as non-admin" do + integrate_views + + before(:each) do + User.stub!(:current).and_return do + user = mock_model(User, + :admin? => false, + :logged? => true, + :language => :en, + :anonymous? => false, + :name => 'Test user', + :projects => Project + ) + end + end + + it 'should not be successful' do + get :index + response.should_not be_success + end + + it 'should return a 403 status code' do + get :index + response.code.should eql("403") + end +end + +describe OverheadTimeEntryActivityController, "#index as admin in js format" do + integrate_views + + before(:each) do + User.stub!(:current).and_return do + user = mock_model(User, + :admin? => true, + :logged? => true, + :language => :en, + :anonymous? => false, + :name => 'Test user', + :projects => Project + ) + end + end + + it 'should be successful' do + get :index + response.should be_success + end + + it 'should default to no values if the Billing Status field is invalid' do + get :index + assigns[:values].should be_empty + end + + it 'should render the values partial' do + get :index + response.should render_template('overhead_time_entry_activity/_values') + end + + describe 'with a list custom_field parameter' do + before(:each) do + @custom_field = mock_model(TimeEntryActivityCustomField, :possible_values => ['A','B','Nil'], :field_format => 'list') + TimeEntryActivityCustomField.stub!(:find).and_return(@custom_field) + end + + it 'should assign values to the possible values of the field' do + get :index, :custom_field => @custom_field.id + assigns[:field].should eql(@custom_field) + end + + it 'should find the Billing Status field based on the selected custom_field' do + TimeEntryActivityCustomField.should_receive(:find).with(@custom_field.id.to_s).and_return(@custom_field) + get :index, :custom_field => @custom_field.id + end + + it 'should render the possible values as a set of options' do + get :index, :custom_field => @custom_field.id + response.should have_tag('option[value=?]', 'A','A') + response.should have_tag('option[value=?]', 'B','B') + response.should have_tag('option[value=?]', 'Nil','none') + end + end + + describe 'with a boolean custom_field parameter' do + before(:each) do + @custom_field = mock_model(TimeEntryActivityCustomField, :field_format => 'bool') + TimeEntryActivityCustomField.stub!(:find).and_return(@custom_field) + end + + it 'should assign values to the possible values of the field' do + get :index, :custom_field => @custom_field.id + assigns[:field].should eql(@custom_field) + end + + it 'should find the Billing Status field based on the selected custom_field' do + TimeEntryActivityCustomField.should_receive(:find).with(@custom_field.id.to_s).and_return(@custom_field) + get :index, :custom_field => @custom_field.id + end + + it 'should render the values as true or false' do + get :index, :custom_field => @custom_field.id + response.should have_tag('option[value=?]', 'true','true') + response.should have_tag('option[value=?]', 'false','false') + end + end +end