[#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
This commit is contained in:
Eric Davis
2009-04-21 12:17:42 -07:00
parent 631a43392d
commit bcba8262b3
3 changed files with 142 additions and 0 deletions
@@ -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
@@ -0,0 +1 @@
<%= options_for_select(@values) %>
@@ -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