Changeset 492
- Timestamp:
- 01/25/07 01:16:47 (2 years ago)
- Files:
-
- branches/form_system_263/app/controllers/application.rb (modified) (3 diffs)
- branches/form_system_263/app/controllers/form_category_controller.rb (modified) (1 diff)
- branches/form_system_263/app/models/form_category.rb (modified) (2 diffs)
- branches/form_system_263/app/views/form_category/_recursive_tree.rhtml (deleted)
- branches/form_system_263/app/views/form_category/online_help__general.rhtml (deleted)
- branches/form_system_263/app/views/form_category/view_as_tree.rhtml (deleted)
- branches/form_system_263/config/menu.rb (modified) (1 diff)
- branches/form_system_263/db/migrate/021_version_one_dot_two_tables.rb (modified) (2 diffs)
- branches/form_system_263/db/migrate/022_version_one_dot_two_data.rb (modified) (1 diff)
- branches/form_system_263/test/fixtures/form_categories.yml (modified) (1 diff)
- branches/form_system_263/test/fixtures/form_types.yml (modified) (6 diffs)
- branches/form_system_263/test/functional/form_category_controller_test.rb (modified) (8 diffs)
- branches/form_system_263/test/functional/form_type_controller_test.rb (modified) (1 diff)
- branches/form_system_263/test/test_helper.rb (modified) (1 diff)
- branches/form_system_263/test/unit/form_category_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/form_system_263/app/controllers/application.rb
r488 r492 196 196 # 197 197 # Parameters: 198 # model_class - The class object of the model whose instance is created. 199 # name_attribute - The name of the attribute of the model instance that returns 200 # the instance's name (written to log). 201 def common_create(model_class, name_attribute='name', redirect={:action => 'index'}, cancel_url_options={:action => 'index'}, partial_to_render='shared/create_or_edit') 198 # model_class - The class object of the model whose instance is created. 199 # name_attribute - The name of the attribute of the model instance that returns 200 # the instance's name (written to log). 201 # redirect - Redirection after succesful creation. 202 # cancel_url_options - Redirection after cancel. 203 # partial_to_render - Partial to render with. 204 def common_create(model_class, name_attribute='name', redirect={:action => 'list'}, cancel_url_options={:action => 'list'}, partial_to_render='shared/create_or_edit') 202 205 if request.post? 203 206 @model = model_class.create(params[:model]) … … 226 229 # name_attribute - The name of the attribute of the model instance that returns 227 230 # the instance's name. 228 def common_edit(model_class, name_attribute='name', redirect={:action => 'index'}, cancel_url_options={:action => 'index'}, partial_to_render='shared/create_or_edit') 231 # redirect - Redirection after succesful modification. 232 # cancel_url_options - Redirection after cancel. 233 # partial_to_render - Partial to render with. 234 def common_edit(model_class, name_attribute='name', redirect={:action => 'list'}, cancel_url_options={:action => 'list'}, partial_to_render='shared/create_or_edit') 229 235 @model = model_class.find(params[:id]) 230 236 … … 256 262 # name_attribute - The name of the attribute of the model instance that returns 257 263 # the instance's name (written to log). 258 def common_remove(model_class, name_attribute='name', redirect={:action => 'index'}) 264 # redirect - Passed to redirect_to. 265 def common_remove(model_class, name_attribute='name', redirect={:action => 'list'}) 259 266 redirect_to redirect 260 267 branches/form_system_263/app/controllers/form_category_controller.rb
r488 r492 13 13 14 14 def list 15 @help = 'general'16 15 @title = _('Form categories') 17 @fields = [ [_('Name'), :name], 18 [_('Parent'), :parent_name], 19 [_('Tag'), :form_tag], 16 17 @fields = [ 18 [_('Name'), :name, 'name'], 19 [_('Tag'), :form_tag, 'form_tag_id'], 20 20 [_('Form type count'), :form_type_count] 21 21 ] 22 @items = items_for_page(FormCategory, :order => 'parent_id, form_tag_id, name') 22 23 @items = items_for_page(FormCategory) 23 24 render(:partial => 'shared/list_items', :layout => 'mainlevel') 24 25 end 25 26 26 27 def create 27 @help = 'general'28 28 @title = _('Create a new form category') 29 30 form_tag_options = FormTag.find(:all, :order => 'name').map {|t| [t.full_name, t.id] } 29 31 @fields = [ 30 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 }], 31 [_('Tag'), :select, 'form_tag_id', FormTag.find(:all, :order => 'name').map {|tag| [tag.full_name, tag.id] }, { :include_blank => true } ], 32 [_('Parent'), :select, 'parent_id' ] 32 [_('Name'), :text_field, 'name', {:size => 40, :maxlength => 40}], 33 [_('Tag'), :select, 'form_tag_id', form_tag_options, {:include_blank => true}], 33 34 ] 34 update_possible_parents() 35 common_create(FormCategory , 'name', {:action => 'list'}, {:action => 'list'})35 36 common_create(FormCategory) 36 37 end 37 38 38 39 def edit 39 @ help = 'general'40 @title = _('Edit form category') 40 @title = _('Edit form category') 41 41 42 @form_category = FormCategory.find params[:id] 43 form_tag_options = FormTag.find(:all, :order => 'name').map {|t| [t.full_name, t.id] } 42 44 @fields = [ 43 45 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 }], 44 [_('Tag'), :select, 'form_tag_id', FormTag.find(:all, :order => 'name').map {|tag| [tag.full_name, tag.id] }, { :include_blank => true } ], 45 [_('Parent'), :select, 'parent_id' ] 46 [_('Tag'), :select, 'form_tag_id', form_tag_options, { :include_blank => true } ], 46 47 ] 47 update_possible_parents(@form_category) 48 common_edit(FormCategory , 'name', {:action => 'list'}, {:action => 'list'})48 49 common_edit(FormCategory) 49 50 end 50 51 51 52 def remove 52 common_remove(FormCategory , 'name', :action => 'list') {|instance|53 unless instance. children.empty? and instance.form_types.empty?53 common_remove(FormCategory) {|instance| 54 unless instance.form_types.empty? 54 55 msg_could_not_remove_because_not_empty(instance) 55 56 end 56 57 } 57 58 end 58 59 def view_as_tree60 @help = 'general'61 @title = ('Form category hierarchy')62 @form_categories = FormCategory.find :all, :readonly => true, :order => 'name'63 end64 65 private66 67 # Function: update_possible_parents68 # =================================69 #70 # Updates possible parents for the form_category.71 #72 # If no form_category is given, then the list of73 # possible parents will contain all existing categories.74 def update_possible_parents(form_category = nil)75 form_categories = FormCategory.find(:all, :order => 'name')76 possible_parents = [[_('No parent'), 0]]77 78 form_categories.each do |potential_parent|79 unless potential_parent.is_child_of?(form_category) or potential_parent == form_category80 possible_parents << [potential_parent.name, potential_parent.id]81 end82 end83 # Options for parent select84 @fields.last[3] = possible_parents85 end86 59 end branches/form_system_263/app/models/form_category.rb
r488 r492 8 8 validates_presence_of :name 9 9 validates_uniqueness_of :name 10 validates_length_of :name, :within => 4..4011 10 # Callbacks 12 11 before_destroy :check_empty 13 12 # Associations 14 acts_as_tree :order => 'name'15 13 belongs_to :form_tag 16 14 has_many :form_types, :order => 'name' 17 15 18 16 exports_automatic_field 'cmms_form_category_name', :string, :name 19 20 21 def is_child_of?(potential_parent)22 return false if potential_parent == nil23 return false if potential_parent == self24 25 current_node = self26 27 while true28 current_node = current_node.parent29 return true if current_node == potential_parent30 return false if current_node == nil31 end32 end33 34 # Returns the parent category's name, or +nil+ if the35 # category doesn't have a parent.36 def parent_name37 self.parent.name if self.parent38 end39 17 40 18 # Returns the number of form types in this form category … … 47 25 # Prevents destroing categories with children or form types 48 26 def check_empty 49 return true if self. children.empty? and self.form_types.empty?27 return true if self.form_types.empty? 50 28 return false 51 29 end branches/form_system_263/config/menu.rb
r488 r492 117 117 MenuButton.new(_('Form categories'), '22x22/menu_form_category.gif', 118 118 MenuItem.new(_('List'), 'form_category', 'list', '16x16/list.gif'), 119 MenuItem.new(_('Tree'), 'form_category', 'view_as_tree', '16x16/view_tree.gif'),120 119 MenuItem.new(_('Create'), 'form_category', 'create', '16x16/create.gif'), 121 120 Page.new('form_category', 'edit') branches/form_system_263/db/migrate/021_version_one_dot_two_tables.rb
r488 r492 144 144 add_index 'form_template_images', ['form_type_id'] 145 145 # If faster image quering is needed try indexing the name columns too! 146 147 # Form system changes 148 # Remove the tree structure of the form categories 149 remove_column 'form_categories', 'parent_id' 146 150 end 147 151 … … 170 174 171 175 rename_action_keys(true) 176 177 # FIXME: Add the form_categories column 172 178 end 173 179 branches/form_system_263/db/migrate/022_version_one_dot_two_data.rb
r488 r492 33 33 key 34 34 } 35 36 # Remove action keys 37 names_of_removed_action_keys = [ 'form_category/view_as_tree' ] 38 for name in names_of_removed_action_keys 39 begin 40 TempActionKey.find_by_name(name).destroy 41 rescue 42 # Do nothing 43 end 44 end 35 45 end 36 46 branches/form_system_263/test/fixtures/form_categories.yml
r488 r492 1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 2 3 category1: 3 # FIXME: We should get rid of this 4 5 test_form_category: 4 6 id: 1 5 name: plans 6 parent_id: 0 7 8 category2: 9 id: 2 10 name: notices 11 parent_id: 1 12 13 unauthorized: 14 id: 5 15 name: unauthorized 16 parent_id: 0 17 18 category_created_by_admin: 19 id: 3 20 name: "admin category" 21 parent_id: 0 22 23 category_created_by_other_admin: 24 id: 4 25 name: "admin category" 26 parent_id: 3 27 28 category_created_by_yet_another_admin: 29 id: 6 30 name: "another admin category" 31 parent_id: 4 32 33 invalid_forms: 34 id: 7 35 name: 'valid and invalid forms' 36 parent_id: 0 37 38 # test_is_child_of 39 # root 40 # a1 a2 41 # b1 b2 42 43 root: 44 id: 8 45 name: root 46 parent_id: 0 47 48 a1: 49 id: 9 50 name: a1 51 parent_id: 8 52 53 a2: 54 id: 10 55 name: a2 56 parent_id: 8 57 58 b1: 59 id: 11 60 name: b1 61 parent_id: 10 62 63 b2: 64 id: 12 65 name: b2 66 parent_id: 10 67 68 69 # ID SPACE 100 - 199 70 # functional/admin_controller_test.rb 71 category_with_form_types: 72 id: 100 73 name: 'I have form types' 74 parent_id: 0 75 76 # ID SPACE 200 - 299 77 # unit/form_test.rb 78 form_test_category: 79 id: 200 80 name: 'For form tests' 81 parent_id: 0 82 83 # ID SPACE 300 - 399 84 # functional/main_controller_test.rb 85 special_chars_category: 86 id: 300 87 name: 'For special form types' 88 parent_id: 0 89 90 # ID SPACE 400 - 499 91 # test/functional/form_category_controller_test.rb 92 should_get_deleted1: 93 id: 400 94 name: 'Should get deleted 1' 95 parent_id: 0 96 97 should_get_deleted2: 98 id: 401 99 name: 'Should get deleted 2' 100 parent_id: 0 7 name: "We should not use fixtures!" branches/form_system_263/test/fixtures/form_types.yml
r488 r492 14 14 removable1: 15 15 id: 101 16 form_category_id: 1 0016 form_category_id: 1 17 17 name: 'Me ready. Me no forms have.' 18 18 ready: true 19 19 removable2: 20 20 id: 102 21 form_category_id: 1 0021 form_category_id: 1 22 22 name: 'Me ready. Me no forms have.' 23 23 ready: true 24 24 removable3: 25 25 id: 103 26 form_category_id: 1 0026 form_category_id: 1 27 27 name: 'Me ready. Me no forms have.' 28 28 ready: true 29 29 ready_with_forms: 30 30 id: 104 31 form_category_id: 1 0031 form_category_id: 1 32 32 name: 'Me ready. Me many repots have.' 33 33 ready: true … … 38 38 form_type_with_all_user_field_types: 39 39 id: 200 40 form_category_id: 20040 form_category_id: 1 41 41 name: 'I have all user field types' 42 42 editable: true … … 47 47 form_type_for_special_chars_forms: 48 48 id: 300 49 form_category_id: 30049 form_category_id: 1 50 50 name: "I'm special" 51 51 form_category_id: 1 … … 57 57 invalid_and_valid_forms: 58 58 id: 400 59 form_category_id: 759 form_category_id: 1 60 60 name: 'Invalid and valid forms' 61 61 … … 74 74 # id: 2 75 75 # name: urgent notices 76 # form_category_id: 276 # form_category_id: 1 77 77 # 78 78 #odt_forms: … … 111 111 # id: 10 112 112 # name: 'Has a ready and a not ready form' 113 # form_category_id: 7113 # form_category_id: 1 114 114 # branches/form_system_263/test/functional/form_category_controller_test.rb
r488 r492 6 6 7 7 class FormCategoryControllerTest < Test::Unit::TestCase 8 fixtures :users, :keyrings, :keyrings_users, :action_keys, :action_keys_keyrings,9 :form_categories, :assets, :assets_user_groups, :user_groups,10 :user_groups_users, :form_types11 12 8 def setup 13 9 @controller = FormCategoryController.new 14 10 @request = ActionController::TestRequest.new 15 11 @response = ActionController::TestResponse.new 16 login() 12 13 simple_setup_for_functional_test 14 15 # Add keys 16 @keyring.action_keys << create_action_key('form_category/list') 17 @keyring.action_keys << create_action_key('form_category/create') 18 @keyring.action_keys << create_action_key('form_category/edit') 19 @keyring.action_keys << create_action_key('form_category/remove') 17 20 end 18 21 19 def test_index 20 user = @request.session[:user] 22 def test_list 23 FormCategory.create!(:name => 'Category 1') 24 FormCategory.create!(:name => 'Category 2') 25 FormCategory.create!(:name => 'Category 3') 21 26 22 27 get 'list' 23 28 assert_response :success 24 categories = FormCategory.find :all, :order => 'parent_id, name'25 assert_equal categories, assigns(:items)26 end27 29 28 def test_view_as_tree 29 user = @request.session[:user] 30 31 get 'view_as_tree' 32 assert_response :success 33 categories = FormCategory.find :all, :order => 'name' 34 assert_equal categories, assigns(:form_categories) 30 assert_equal false, assigns(:items).empty? 35 31 end 36 32 … … 40 36 41 37 post 'create', :model => {'name' => new_category, 'form_tag_id' => form_tag.id} 38 assert_response :redirect 39 assert_redirected_to :action => 'list' 42 40 43 assert_redirected_to :action => 'list'44 41 category = FormCategory.find_by_name new_category 45 assert_kind_of FormCategory, category46 42 assert_equal new_category, category.name 47 43 assert_equal form_tag, category.form_tag … … 53 49 # Checks that the +remove+ action does not remove anything if called via GET 54 50 def test_remove_with_get 55 category = FormCategory. find @form_categories['should_get_deleted1']['id']56 get 'remove', :delete => {category.id =>1}51 category = FormCategory.create!(:name => "Won't be removed") 52 get 'remove', :delete => {category.id => 1} 57 53 58 54 assert_response :redirect … … 79 75 # Checks that the +remove+ action removes categories if called via POST 80 76 def test_remove_post 81 category1 = FormCategory. find @form_categories['should_get_deleted1']['id']82 category2 = FormCategory. find @form_categories['should_get_deleted2']['id']77 category1 = FormCategory.create!(:name => 'should_get_deleted1') 78 category2 = FormCategory.create!(:name => 'should_get_deleted2') 83 79 84 80 post 'remove', :delete => {category1.id=>1, category2.id=>1} … … 96 92 97 93 def test_remove_category_containing_form_types 98 category1 = FormCategory.find @form_categories['category_with_form_types']['id'] 99 category2 = FormCategory.find @form_categories['should_get_deleted1']['id'] 100 category3 = FormCategory.find @form_categories['should_get_deleted2']['id'] 94 category1 = FormCategory.create!(:name => 'should_get_deleted1') 95 category2 = FormCategory.create!(:name => 'category_with_form_types') 96 category3 = FormCategory.create!(:name => 'should_get_deleted2') 97 98 category2.form_types << FormType.create!(:name => 'new form type') 101 99 102 100 post 'remove', :delete => {category1.id=>1, category2.id=>1, category3.id=>1} … … 104 102 assert_redirected_to :action => 'list' 105 103 106 # The firstcategory has subcategories, so it should not be deleted.107 assert_ equal category1, FormCategory.find(category1.id)108 assert_ raise(ActiveRecord::RecordNotFound) {FormCategory.find category2.id}104 # The second category has subcategories, so it should not be deleted. 105 assert_raise(ActiveRecord::RecordNotFound) {FormCategory.find category1.id} 106 assert_equal category2, FormCategory.find(category2.id) 109 107 assert_raise(ActiveRecord::RecordNotFound) {FormCategory.find category3.id} 110 108 … … 120 118 # Checks that we don't get an empty notice message when nothing gets removed. 121 119 def test_remove_nothing_doesnt_show_empty_notice 122 category1 = FormCategory.find @form_categories['category_with_form_types']['id'] 120 category1 = FormCategory.create!(:name => 'category_with_form_types') 121 category1.form_types << FormType.create!(:name => 'new form type') 123 122 124 123 post 'remove', :delete => {category1.id=>1} … … 138 137 139 138 def test_edit 140 category_id = @form_categories['category_created_by_admin']['id'] 139 category = FormCategory.create!(:name => 'original name') 140 new_name = 'new name' 141 141 142 # New attributes (shouldn't match the old ones) 142 new_name = "The new name for the category"143 assert_not_equal new_name, category.name 143 144 144 post 'edit', :id => category _id, :model => {"name" => new_name}145 post 'edit', :id => category.id, :model => {"name" => new_name} 145 146 146 assert_equal new_name, FormCategory.find(category _id).name147 assert_equal new_name, FormCategory.find(category.id).name 147 148 148 149 assert_not_nil flash[:notice] branches/form_system_263/test/functional/form_type_controller_test.rb
r488 r492 37 37 def test_create_form_type_successfully 38 38 login() 39 form_category_id = @form_categories['category_with_form_types']['id']39 form_category_id = FormCategory.create!(:name => 'test category').id 40 40 41 41 # GET step1 branches/form_system_263/test/test_helper.rb
r488 r492 37 37 38 38 # Add more helper methods to be used by all tests here... 39 40 # Creates an asset, a user, a user group and a keyring. 41 # Also sets the @ref_url and @request.env['HTTP_REFERER']. 42 def simple_setup_for_functional_test 43 # If the action should "redirect_to :back" then 44 # "assert_redirected_to @ref_url" can be used to check. 45 @ref_url = 'http://test.host/redirect/back' 46 @request.env['HTTP_REFERER'] = @ref_url 47 48 @user_group = create_user_group('test') 49 @keyring = create_keyring('test') 50 @user = create_user('test') 51 @user_group.users << @user 52 @keyring.users << @user 53 login('test') 54 55 # Create the root asset and give @user_group all permissions to it 56 @root = create_root_asset('Root', 'ROOT') 57 add_user_group_to_asset(@user_group, @root, true, true, true, true, true, true, true) 58 end 39 59 40 60 # In tests call login(args) or login_user() to log in as branches/form_system_263/test/unit/form_category_test.rb
r488 r492 3 3 class FormCategoryTest < Test::Unit::TestCase 4 4 5 # Checks that +FormCategory#is_child_of+ works.6 def test_is_child_of7 # Setup8 create_categories9 10 assert_equal false, @b1.is_child_of?(nil)11 assert_equal false, @b1.is_child_of?(@b1)12 assert_equal true, @b1.is_child_of?(@a2)13 assert_equal true, @b1.is_child_of?(@root)14 assert_equal false, @b1.is_child_of?(@b2)15 assert_equal false, @b1.is_child_of?(@a1)16 17 assert_equal false, @b2.is_child_of?(nil)18 assert_equal false, @b2.is_child_of?(@b2)19 assert_equal true, @b2.is_child_of?(@a2)20 assert_equal true, @b2.is_child_of?(@root)21 assert_equal false, @b2.is_child_of?(@b1)22 assert_equal false, @b2.is_child_of?(@a1)23 24 assert_equal false, @a1.is_child_of?(nil)25 assert_equal false, @a1.is_child_of?(@a1)26 assert_equal true, @a1.is_child_of?(@root)27 assert_equal false, @a1.is_child_of?(@a2)28 assert_equal false, @a1.is_child_of?(@b1)29 assert_equal false, @a1.is_child_of?(@b2)30 31 assert_equal false, @a2.is_child_of?(nil)32 assert_equal false, @a2.is_child_of?(@a2)33 assert_equal true, @a2.is_child_of?(@root)34 assert_equal false, @a2.is_child_of?(@a1)35 assert_equal false, @a2.is_child_of?(@b1)36 assert_equal false, @a2.is_child_of?(@b2)37 38 assert_equal false, @root.is_child_of?(nil)39 assert_equal false, @root.is_child_of?(@root)40 assert_equal false, @root.is_child_of?(@a1)41 assert_equal false, @root.is_child_of?(@a2)42 assert_equal false, @root.is_child_of?(@b1)43 assert_equal false, @root.is_child_of?(@b2)44 end45 46 # Checks that +FormCategory#parent_name+ works.47 def test_parent_name48 # Setup49 create_categories50 51 assert_equal nil, @root.parent_name52 assert_equal 'root', @a1.parent_name53 assert_equal 'root_a2', @b1.parent_name54 end55 56 5 # Checks that categories with children or form types can't be destroyed 57 6 def test_check_empty 58 7 category = FormCategory.create!(:name => 'test') 59 child = FormCategory.create!(:name => 'test child', :parent_id => category.id)60 61 assert_equal false, category.destroy62 assert_equal [child], category.children(true)63 8 64 9 form_type = FormType.create!(:name => 'test', :form_category_id => category.id) 65 assert_equal false, category.destroy66 assert_equal [child], category.children(true)67 assert_equal [form_type], category.form_types(true)68 69 category.children.clear70 10 assert_equal false, category.destroy 71 11 assert_equal [form_type], category.form_types(true) … … 73 13 category.form_types.clear 74 14 category.destroy 15 75 16 assert_raise(ActiveRecord::RecordNotFound) { FormCategory.find category.id } 76 17 end 77 78 private79 80 def create_categories81 @root = FormCategory.create(:name => 'root')82 @a1 = FormCategory.create!(:name => 'root_a1', :parent => @root)83 @a2 = FormCategory.create!(:name => 'root_a2', :parent => @root)84 @b1 = FormCategory.create!(:name => 'root_a2_b1', :parent => @a2)85 @b2 = FormCategory.create!(:name => 'root_a2_b2', :parent => @a2)86 end87 18 end