Changeset 371
- Timestamp:
- 01/03/07 23:15:51 (2 years ago)
- Files:
-
- branches/1.2-alpha/app/controllers/application.rb (modified) (3 diffs)
- branches/1.2-alpha/app/controllers/asset_controller.rb (modified) (6 diffs)
- branches/1.2-alpha/app/controllers/asset_permission_controller.rb (modified) (1 diff)
- branches/1.2-alpha/app/controllers/data_permission_controller.rb (modified) (1 diff)
- branches/1.2-alpha/app/controllers/message_controller.rb (modified) (1 diff)
- branches/1.2-alpha/app/controllers/report_type_controller.rb (modified) (2 diffs)
- branches/1.2-alpha/app/controllers/user_controller.rb (modified) (4 diffs)
- branches/1.2-alpha/app/controllers/work_order_controller.rb (modified) (5 diffs)
- branches/1.2-alpha/app/helpers/application_helper.rb (modified) (2 diffs)
- branches/1.2-alpha/app/models/asset.rb (modified) (43 diffs)
- branches/1.2-alpha/app/models/asset_type.rb (modified) (1 diff)
- branches/1.2-alpha/app/models/report.rb (modified) (1 diff)
- branches/1.2-alpha/app/models/report_category.rb (modified) (1 diff)
- branches/1.2-alpha/app/models/report_field.rb (modified) (5 diffs)
- branches/1.2-alpha/app/models/report_field_value.rb (modified) (2 diffs)
- branches/1.2-alpha/app/models/report_type.rb (modified) (2 diffs)
- branches/1.2-alpha/app/models/user.rb (modified) (1 diff)
- branches/1.2-alpha/app/models/work_order.rb (modified) (6 diffs)
- branches/1.2-alpha/app/models/work_order_event.rb (modified) (1 diff)
- branches/1.2-alpha/app/models/work_order_template.rb (modified) (2 diffs)
- branches/1.2-alpha/app/models/work_order_type.rb (modified) (1 diff)
- branches/1.2-alpha/app/views/layouts/mainlevel.rhtml (modified) (2 diffs)
- branches/1.2-alpha/app/views/report_type/create_step4.rhtml (modified) (2 diffs)
- branches/1.2-alpha/app/views/shared/_list_items.rhtml (modified) (5 diffs)
- branches/1.2-alpha/app/views/timeline/_display_event.rhtml (modified) (4 diffs)
- branches/1.2-alpha/app/views/user/edit.rhtml (modified) (1 diff)
- branches/1.2-alpha/app/views/user/login.rhtml (modified) (1 diff)
- branches/1.2-alpha/app/views/work_order/_display_work_order_event.rhtml (modified) (1 diff)
- branches/1.2-alpha/app/views/work_order/_form.rhtml (modified) (2 diffs)
- branches/1.2-alpha/app/views/work_order/view.rhtml (modified) (2 diffs)
- branches/1.2-alpha/config/menu.rb (modified) (4 diffs)
- branches/1.2-alpha/config/norfello_extensions.rb (modified) (1 diff)
- branches/1.2-alpha/example_documents/work_order.odt (modified) (previous)
- branches/1.2-alpha/lib/menu_helper.rb (modified) (1 diff)
- branches/1.2-alpha/po/fi_FI/norfello_cmms.po (modified) (114 diffs)
- branches/1.2-alpha/public/stylesheets/mainlevel.css (modified) (1 diff)
- branches/1.2-alpha/test/fixtures/action_keys.yml (modified) (1 diff)
- branches/1.2-alpha/test/fixtures/action_keys_keyrings.yml (modified) (1 diff)
- branches/1.2-alpha/test/fixtures/open_document_files/test_document_with_work_order_automatic_fields.odt (copied) (copied from trunk/test/fixtures/open_document_files/test_document_with_work_order_automatic_fields.odt)
- branches/1.2-alpha/test/functional/action_key_controller_test.rb (modified) (7 diffs)
- branches/1.2-alpha/test/functional/asset_controller_test.rb (modified) (3 diffs)
- branches/1.2-alpha/test/functional/asset_field_controller_test.rb (modified) (1 diff)
- branches/1.2-alpha/test/functional/asset_permission_controller_test.rb (modified) (1 diff)
- branches/1.2-alpha/test/functional/keyring_controller_test.rb (modified) (4 diffs)
- branches/1.2-alpha/test/functional/user_controller_test.rb (modified) (3 diffs)
- branches/1.2-alpha/test/functional/user_group_controller_test.rb (modified) (1 diff)
- branches/1.2-alpha/test/functional/work_order_type_controller_test.rb (modified) (1 diff)
- branches/1.2-alpha/test/test_helper.rb (modified) (1 diff)
- branches/1.2-alpha/test/unit/report_field_test.rb (modified) (2 diffs)
- branches/1.2-alpha/test/unit/report_template_test.rb (modified) (4 diffs)
- branches/1.2-alpha/test/unit/report_test.rb (modified) (4 diffs)
- branches/1.2-alpha/test/unit/report_type_test.rb (modified) (2 diffs)
- branches/1.2-alpha/test/unit/work_order_template_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.2-alpha/app/controllers/application.rb
r339 r371 9 9 10 10 # Class: ApplicationController 11 # ============================ =====11 # ============================ 12 12 # 13 13 # The filters added to this controller will be run for all controllers in the application. … … 204 204 @model = model_class.create(params[:model]) 205 205 206 unless@model.new_record?206 if not @model.new_record? 207 207 info("#{model_class} '#{@model.attributes[name_attribute]}' created") 208 208 flash[:notice] = msg_create_successful(@model) 209 209 redirect_to(redirect) and return 210 els e210 elsif @model.errors.empty? 211 211 flash[:error] = msg_system_failure_in(_('Creation')) 212 212 end … … 235 235 flash[:notice] = msg_changes_saved 236 236 redirect_to(redirect) and return 237 els e237 elsif @model.errors.empty? 238 238 flash[:notice] = msg_saving_failed 239 239 end branches/1.2-alpha/app/controllers/asset_controller.rb
r339 r371 38 38 unless @asset 39 39 if params[:cmms_link] 40 flash[:error] = msg_invalid_link_in_markup(params[:full_code])41 else40 flash[:error] = msg_invalid_link_in_markup(params[:full_code]) 41 else 42 42 flash[:error] = msg_system_failure_in(_('Displaying the asset')) 43 end43 end 44 44 return redirect_to(:back) 45 45 end … … 96 96 return 97 97 end 98 flash[:error] = msg_saving_failed 98 flash[:error] = msg_saving_failed if @model.errors.empty? 99 99 else 100 100 @model.update_asset_field_values … … 182 182 183 183 if (params[:field_value].nil? or AssetFieldValue.update(params[:field_value].keys, params[:field_value].values)) and 184 (@model.attributes == original_attributes or @model. update_attributes(params[:model]))184 (@model.attributes == original_attributes or @model.save) 185 185 exipre_asset_tree_fragments 186 186 flash[:notice] = msg_changes_saved 187 187 redirect_to(:action => 'view') 188 return 189 end 190 flash[:error] = msg_saving_failed 188 user_left_page_group(:asset_edit) 189 return 190 end 191 flash[:error] = msg_saving_failed if @model.errors.empty? 191 192 else 192 193 unless @model.lock_for_current_user(LOCK_DURATION) … … 218 219 def change_type 219 220 @help = 'change_type' 220 221 unless @selected_asset.authorized_to_edit?222 redirect_with_message(msg_unauthorized_operation(_('edit'), @selected_asset.code_and_name), :action => 'view')223 return224 end225 226 @title = _('Change type')227 228 221 @model = @selected_asset 229 if request.post? 230 @model.asset_type_id = params[:model][:asset_type_id] 231 if @model.save 232 exipre_asset_tree_fragments 233 flash[:notice] = msg_changes_saved 234 redirect_to(:action => 'view') 235 return 236 end 237 flash[:error] = msg_saving_failed 238 else 239 unless @model.lock_for_current_user(LOCK_DURATION) 240 flash[:error] = msg_resource_locked(@model) 241 redirect_to(:action => 'view') 242 return 243 end 244 245 unless @model.save 246 flash[:error] = msg_locking_failed_for(@model) 247 redirect_to(:action => 'view') 248 return 249 end 250 251 user_entered_page_group(:asset_change_type) 222 223 if session[:select_branches] and not @selected_asset.children.empty? 224 # Change type of all selected assets 225 find_selected_assets 226 @selected_assets.each { |asset| 227 unless asset.authorized_to_edit? 228 redirect_with_message(msg_unauthorized_operation(_('edit'), _('selected assets in branch %s', @selected_asset.full_code)), :action => 'view') 229 return 230 end 231 } 232 @title = _('Change type of all %d selected assets', @selected_assets.size) 233 234 if request.post? 235 begin 236 Asset.transaction do 237 for selected_asset in @selected_assets 238 asset = selected_asset 239 asset.asset_type_id = params[:model][:asset_type_id] 240 asset.save! 241 end 242 end 243 exipre_asset_tree_fragments 244 flash[:notice] = msg_changes_saved 245 redirect_to(:action => 'view') 246 return 247 rescue ActiveRecord::RecordNotSaved 248 if asset.errors.empty? 249 flash[:error] = msg_saving_failed 250 else 251 # show errors from the asset whose saving failed 252 @model.errors = asset.errors 253 end 254 end 255 else 256 begin 257 Asset.transaction do 258 for selected_asset in @selected_assets 259 asset = selected_asset 260 raise ResourceLocked unless asset.lock_for_current_user(LOCK_DURATION) 261 asset.save! 262 end 263 end 264 rescue ResourceLocked 265 flash[:error] = msg_resource_locked(asset) 266 redirect_to(:action => 'view') 267 return 268 rescue ActiveRecord::RecordNotSaved 269 flash[:error] = msg_locking_failed_for(asset) 270 redirect_to(:action => 'view') 271 return 272 end 273 user_entered_page_group(:asset_change_type) 274 end 275 else 276 # Change type of the selected asset 277 unless @selected_asset.authorized_to_edit? 278 redirect_with_message(msg_unauthorized_operation(_('edit'), @selected_asset.code_and_name), :action => 'view') 279 return 280 end 281 282 @title = _('Change type') 283 284 if request.post? 285 @model.asset_type_id = params[:model][:asset_type_id] 286 if @model.save 287 exipre_asset_tree_fragments 288 flash[:notice] = msg_changes_saved 289 redirect_to(:action => 'view') 290 return 291 end 292 flash[:error] = msg_saving_failed if @model.errors.empty? 293 else 294 unless @model.lock_for_current_user(LOCK_DURATION) 295 flash[:error] = msg_resource_locked(@model) 296 redirect_to(:action => 'view') 297 return 298 end 299 300 unless @model.save 301 flash[:error] = msg_locking_failed_for(@model) 302 redirect_to(:action => 'view') 303 return 304 end 305 306 user_entered_page_group(:asset_change_type) 307 end 252 308 end 253 309 … … 261 317 262 318 def remove_asset_lock 263 @selected_asset.save if @selected_asset.remove_lock 319 if @selected_assets 320 @selected_assets.each { |asset| 321 asset.save if asset.remove_lock? 322 } 323 else 324 @selected_asset.save if @selected_asset.remove_lock 325 end 264 326 end 265 327 … … 277 339 278 340 end 341 342 # Exception that can be raised when resource is locked 343 class ResourceLocked < StandardError 344 end branches/1.2-alpha/app/controllers/asset_permission_controller.rb
r339 r371 114 114 if @selected_asset.update_attribute(:use_parents_permissions, params[:selected_asset][:use_parents_permissions]) 115 115 flash[:notice] = msg_changes_saved 116 els e116 elsif @selected_asset.errors.empty? 117 117 flash[:error] = msg_saving_failed 118 118 end branches/1.2-alpha/app/controllers/data_permission_controller.rb
r299 r371 92 92 if @object.update_permissions(params[:permission], params[:object]) 93 93 flash[:notice] = msg_changes_saved 94 els e94 elsif @object.errors.empty? 95 95 flash[:error] = msg_saving_failed 96 96 end branches/1.2-alpha/app/controllers/message_controller.rb
r299 r371 38 38 if @message.unread 39 39 @message.unread = false 40 unless @message.save 40 unless @message.save and @message.errors.empty? 41 41 flash[:error] = msg_saving_failed 42 42 end branches/1.2-alpha/app/controllers/report_type_controller.rb
r299 r371 168 168 if request.post? 169 169 @new_field_group = ReportFieldGroup.new params['new_field_group'] 170 @new_field_group.report_type 170 @new_field_group.report_type = @report_type 171 @new_field_group.place = @new_field_group.report_type.report_field_groups.size + 1 171 172 if @new_field_group.valid? 172 173 @report_type.report_field_groups << @new_field_group … … 286 287 @groups = @report_type.report_field_groups 287 288 @available_field_groups = [ [_('Not in a group'), nil] ] + @groups.collect {|g| [g.name, g.id]} 288 @place_options = Array.new(@fields.size) { |i| i += 1; [ "#{i}.", i ] } 289 # Make sure that all current places are found in the options 290 @group_place_options = (@groups.collect { |g| g.place } + (1..@groups.size).to_a).sort.uniq.collect { |i| ["#{i}.", i] } 289 291 @new_field_group = ReportFieldGroup.new 290 292 @enum_value = EnumeratedValue.new branches/1.2-alpha/app/controllers/user_controller.rb
r339 r371 75 75 [_('Description'), :description] 76 76 ] 77 @ actions = [78 [_(' Permissions'), {:controller => 'keyring', :action => 'edit_keys', :image => '16x16/permissions'} ],79 [_(' Users'), {:controller => 'keyring', :action => 'edit_owners', :image => '16x16/user_group'} ]77 @keyring_actions = [ 78 [_('Keys'), {:controller => 'keyring', :action => 'edit_keys', :image => '16x16/keys'} ], 79 [_('Owners'), {:controller => 'keyring', :action => 'edit_owners', :image => '16x16/user_group'} ] 80 80 ] 81 81 @keyring_post_params = { :action => 'remove_from_keyrings', :id => @model.id } 82 @items = @model.keyrings 82 @keyring_items = @model.keyrings 83 84 # User group list 85 @user_group_fields = [ 86 [_('Name'), :name], 87 [_('Description'), :description] 88 ] 89 @user_group_actions = [ [_('Users'), {:controller => 'user_group', :action => 'users', :image => '16x16/user_group' }] ] 90 @user_group_post_params = { :action => 'remove_from_user_groups', :id => @model.id } 91 @user_group_items = @model.user_groups 83 92 84 93 if request.post? … … 88 97 flash[:notice] = _('User information changed') 89 98 info "User information changed for #{@model.login}" 90 els e99 elsif @model.errors.empty? 91 100 flash[:error] = _('Unable to change user information') 92 101 end … … 98 107 99 108 # Ignore GET requests 100 return if request.get? 109 return if request.get? or not params[:delete] or params[:delete].empty? 101 110 102 111 names = [] … … 107 116 end 108 117 flash[:notice] = _('User %s removed from keyrings: %s', @user.login_and_name, names.join(',')) 118 end 119 120 def remove_from_user_groups 121 redirect_to :back 122 123 # Ignore GET requests 124 return if request.get? or not params[:delete] or params[:delete].empty? 125 126 names = [] 127 @user = User.find(params[:id]) 128 for user_group in @user.user_groups.find(params[:delete].keys) 129 user_group.users.delete(@user) 130 names.push user_group.name 131 end 132 flash[:notice] = _('User %s removed from user groups: %s', @user.login_and_name, names.join(',')) 109 133 end 110 134 branches/1.2-alpha/app/controllers/work_order_controller.rb
r339 r371 102 102 return 103 103 end 104 @events = @work_order.work_order_events105 104 @users = @work_order.asset.users(@work_order.responsible_user_id) 106 105 107 @ title = _('Work order %s (%s)', @work_order.to_s, @work_order.work_order_type.name)106 @wo_title = _('Work order %s (%s)', @work_order.to_s, @work_order.work_order_type.name) 108 107 109 108 if request.post? … … 126 125 end 127 126 127 @work_order.add_comment(params[:work_order_comment]) if params[:work_order_comment] 128 128 129 case params[:work_order_action] 129 130 when 'leave' 130 # We would save the comment here 131 return 131 # Do nothing 132 132 when 'accept' 133 133 @work_order.accept … … 160 160 end 161 161 end 162 @events = @work_order.work_order_events 162 163 end 163 164 … … 182 183 @work_order.deadline = nil if not params[:deadline_enabled] 183 184 184 flash[:notice] = _('Changes saved.') if @work_order.save 185 redirect_to :action => 'view', :id => @work_order.id 185 if @work_order.save 186 flash[:notice] = msg_changes_saved 187 redirect_to(:action => 'view', :id => @work_order.id) 188 user_left_page_group(:work_order_edit) 189 return 190 end 191 flash[:error] = msg_saving_failed if @work_order.errors.empty? 186 192 else 187 193 unless @work_order.lock_for_current_user(LOCK_DURATION) … … 194 200 return redirect_to(:action => 'view', :id => @work_order.id) 195 201 end 202 203 user_entered_page_group(:work_order_edit) 196 204 end 197 205 end branches/1.2-alpha/app/helpers/application_helper.rb
r339 r371 749 749 750 750 results = [] 751 children = ""752 751 for node in nodes 753 752 result = "" … … 763 762 764 763 # Recurse if node has children 765 children = find_asset_tree_nodes(group_ids, node[0], is_open, depth ? depth + 1 : nil) if node[3] != '0'764 children = node[3] == '0' ? '' : find_asset_tree_nodes(group_ids, node[0], is_open, depth ? depth + 1 : nil) 766 765 767 766 # Don't display this node if the user is unauthorized to read it and branches/1.2-alpha/app/models/asset.rb
r339 r371 2 2 # See license agreement for additional rights 3 3 4 # Class: Asset 5 # ============ 6 # 4 7 class Asset < ProtectorModel 5 8 # Protection … … 31 34 include AssetConditionAnalysis 32 35 36 exports_automatic_fields_from :asset_type 37 exports_automatic_field 'cmms_asset_code', :string, :code 38 exports_automatic_field 'cmms_asset_full_code', :string, :full_code 39 exports_automatic_field 'cmms_asset_name', :string, :name 40 exports_automatic_field 'cmms_asset', :wildcard, :automatic_value_from_asset_field_value, :automatic_value_from_asset_field_value_type_resolver 41 exports_automatic_field 'cmms_grandparent_asset', :wildcard, :grandparent_asset_automatic_field, :grandparent_asset_automatic_field_type_resolver 42 exports_automatic_field 'cmms_parent_asset', :wildcard, :parent_asset_automatic_field, :parent_asset_automatic_field_type_resolver 43 44 # Function: Asset.minimal_select 45 # ============================== 46 # 33 47 # Returns the smallest possible SELECT clause that can be used in a find call for this model. 34 48 # Contains protected fields and +use_parents_permissions+. … … 37 51 end 38 52 53 # Function: Asset.full_code_for 54 # ============================= 55 # 39 56 # Returns the full code for asset with the given id or nil 57 # 58 # Parameters: 59 # ----------- 60 # id - Asset id 61 # 40 62 def Asset.full_code_for(id) 41 63 begin … … 46 68 end 47 69 70 # Function: Asset.get_first_authorized_to_read 71 # ============================================ 72 # 48 73 # Returns the first found asset user is authorized to read. 49 74 # If user is unauthorized to read any assets raises an exception. … … 54 79 end 55 80 81 # Function: Asset.find_by_full_code 82 # ================================= 83 # 56 84 # Finds assets user is authorized to read by the full code. 85 # 86 # Parameters: 87 # ----------- 88 # full_code - Full code of an asset 89 # 57 90 def Asset.find_by_full_code(full_code) 58 91 code = full_code.split('-')[-1] … … 63 96 end 64 97 65 # Version of find which only returns assets the user is authorized to perform +operation+. 66 # +operation+:: Symbol defining which permission is checked. One of :read, :edit, :create_subasset, 67 # :remove, :edit_permissions, :create_report, :create_work_order, :attach_file. 98 # Function: Asset.find_authorized_to 99 # ================================== 100 # 101 # Version of find which only returns assets the user is authorized to perform the given operation. 102 # 103 # Parameters: 104 # ----------- 105 # operation - Symbol defining which permission is checked. Possible operations 106 # are :edit, :create_report, :create_work_order, :attach_file, 107 # :create_subasset, :edit_permissions, :remove. 108 # 68 109 def Asset.find_authorized_to(operation, args) 69 110 return records_not_found(args) unless User.current_user … … 89 130 end 90 131 132 # Function: Asset.search_from_authorized_to_read 133 # ============================================== 134 # 91 135 # Returns assets matching the keywords that the current user is authorized to read 136 # 137 # Parameters: 138 # ----------- 139 # keywords - Keywords to search from assets user is authorized to read. 140 # 92 141 def Asset.search_from_authorized_to_read(keywords) 93 142 return [] unless keywords and not keywords.empty? … … 119 168 end 120 169 170 # Function: full_code 171 # =================== 172 # 121 173 # Returns the asset's full code. 122 174 # 123 # The full code is the asset's code prefixed with 124 # +Asset#code_prefix()+.175 # The full code is the asset's code prefixed with Asset#code_prefix(). 176 # 125 177 def full_code 126 178 return self.code_prefix + self.code 127 179 end 128 180 181 # Function: code_prefix 182 # ===================== 183 # 129 184 # Returns the asset's code prefix. 130 185 # … … 139 194 end 140 195 141 # Returns: 'code (name)' 196 # Function: code_and_name 197 # ======================= 198 # 199 # Returns code and name in from 'code (name)' 142 200 def code_and_name 143 201 self.code + ' (' + self.name + ')' 144 202 end 145 203 204 # Function: is_root? 205 # 146 206 # Returns true if asset is the root asset. 147 207 def is_root? … … 149 209 end 150 210 151 # Returns asset's id and ids of the sub-assets +levels+ levels down. 211 # Function: id_and_subasset_ids 212 # ============================= 213 # 214 # Returns asset's id and ids of the sub-assets given number of levels down. 152 215 # i.e. returns asset's id and it's children's ids and their chilren's ids etc. 153 216 def id_and_subasset_ids(levels = nil) … … 159 222 end 160 223 224 # Function: path_to_root 225 # ====================== 226 # 161 227 # Returns assets that are in the path from this asset to the root asset. 162 228 def path_to_root … … 165 231 end 166 232 233 # Function: users 234 # =============== 235 # 167 236 # Returns all users who belong to assets user groups. 168 # If +exclude_user_id+ is given, user with given id is excluded from returned users. 237 # 238 # Parameters: 239 # ----------- 240 # exclude_user_id - user with this id is excluded from returned users. Is optional. 241 # 169 242 def users(exclude_user_id = nil) 170 243 return [] if self.user_groups.empty? … … 175 248 end 176 249 250 # Function: authorized_to_read? 251 # ============================= 252 # 177 253 # Returns true if the user is authorized to read this asset. 178 254 def authorized_to_read? … … 182 258 end 183 259 260 # Function: authorized_to_create_report? 261 # ====================================== 262 # 184 263 # Returns true if the user is authorized to create reports to this asset 185 264 def authorized_to_create_report? … … 187 266 end 188 267 268 # Function: authorized_to_create_work_order? 269 # ========================================== 270 # 189 271 # Returns true if the user is authorized to create work_orders to this asset 190 272 def authorized_to_create_work_order? … … 192 274 end 193 275 276 # Function: authorized_to_attach_file? 277 # ==================================== 278 # 194 279 # Returns true if the user is authorized to create attachments to this asset 195 280 def authorized_to_attach_file? … … 197 282 end 198 283 284 # Function: authorized_to_create_subasset? 285 # ======================================== 286 # 199 287 # Returns true if the user is authorized to create sub-assets to this asset 200 288 def authorized_to_create_subasset? … … 202 290 end 203 291 292 # Function: authorized_to_edit_permissions? 293 # ========================================= 294 # 204 295 # Returns true if the user is authorized to edit asset permissions for this asset 205 296 def authorized_to_edit_permissions? … … 207 298 end 208 299 300 # Function: authorized_to_remove? 301 # =============================== 302 # 209 303 # Returns true if the user is authorized to remove this asset 210 304 def authorized_to_remove? … … 212 306 end 213 307 308 # Function: authorized_to_edit_branch? 309 # ====================================== 310 # 311 # Returns true if the user is authorized to edit this asset and all its sub-assets 312 def authorized_to_edit_branch? 313 branch_authorized_to(:edit) 314 end 315 316 # Function: authorized_to_remove_branch? 317 # ====================================== 318 # 214 319 # Returns true if the user is authorized to remove this asset and all its sub-assets 215 320 def authorized_to_remove_branch? 216 return false unless self.authorized_to_remove? 217 # We can't use self.children here, because it will be cached and hence self.destroy will fail, even if children have been deleted from db! 218 for asset in Asset.find(:all, :conditions => ['parent_id = ?', self.id]) 219 return false unless asset.authorized_to_remove_branch? 220 end 221 return true 321 branch_authorized_to(:remove) 222 322 end 223 323 … … 243 343 end 244 344 345 # Function: authorized_to_create? 346 # =============================== 347 # 245 348 # Returns true if the user is authorized to create new instances of +datatype+ to this asset 246 349 def authorized_to_create?(datatype) … … 257 360 end 258 361 362 # Function: has_subasset_authorized_to_read? 363 # ========================================== 364 # 259 365 # Returns true if asset has a sub-asset user is authorized to read. 260 366 def has_subasset_authorized_to_read? … … 266 372 end 267 373 268 # Creates new Asset <-> UserGroup associattion between this asset and user group 269 # +user_group+ i.e. gives user group +user_group+ permission to read this asset. 270 # If the second (optional) parameter +edit+ is true, also editing permission will be given. 374 # Function: add_user_group 375 # ======================== 376 # 377 # Creates new Asset <-> UserGroup associattion between this asset and the given 378 # user group i.e. gives the user group permission to read this asset. 271 379 # The same association is created also for all sub-assets using this asset's permissions. 272 380 # Returns true if the creation is successfull and returns false if it fails. 381 # 382 # Parameters: 383 # ----------- 384 # user_group - User group to add to this asset. 385 # permissions - Optional hash containing additional permissions to the user group, 386 # e.g. :edit => true. Default {}. 387 # 273 388 def add_user_group(user_group, permissions = {}) 274 389 raise("#{user_group.class} in not a UserGroup") unless user_group.is_a?(UserGroup) … … 320 435 end 321 436 322 # Returns current user's data permission profiles for data type +datatype+ associated to this asset. 437 # Function: data_permission_profiles_for_current_user 438 # =================================================== 439 # 440 # Returns current user's data permission profiles in this asset for the given data type. 323 441 # Return value contains at most one permission profile for each user group. 324 442 # If the current user belongs to multiple user groups and through them has multiple … … 327 445 # contains one permission profile which has all permissions the summed permission 328 446 # profiles have. Thus returned permission profiles contain all permissions there 329 # are in the current user's permission profiles for data type +datatype+. 330 # +datatype+:: String specifying which type of data permission profiles is returned. 331 # e.g. +datatype+ 'Report' gives report permission profiles etc. 447 # are in the current user's permission profiles for the given data type. 448 # 449 # Parameters: 450 # ----------- 451 # datatype - Specifies which type of data permission profiles is returned. 452 # 332 453 def data_permission_profiles_for_current_user(datatype) 333 454 return [] unless user = User.current_user … … 357 478 end 358 479 480 # Function: node_depth 481 # ==================== 482 # 359 483 # Returns the number of nodes in the asset tree below the receiving asset. 360 484 def node_depth … … 363 487 end 364 488 489 # Function: copy_permissions_from 490 # =============================== 491 # 365 492 # Copies all permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) 366 493 # from Asset +asset+ to the receiver, whose existing permissions are destroyed. … … 371 498 end 372 499 500 # Function: copy_permissions_to_subassets 501 # ======================================= 502 # 373 503 # Copies asset's permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) to sub-assets. 374 504 # Permissions are copied only to those assets the user is authorized to edit. … … 377 507 # After call sub-assets will have the same permissions as the asset. 378 508 # Returns the number of assets the permissions where copied to. 379 # +node_levels_down+:: Integer that defines how many nodes down the copying is done. Default is one node level down. 380 # e.g. If +node_levels_down+ is 2 then permissions are copied to asset's children and to their children. 509 # 510 # Parameters: 511 # ----------- 512 # node_levels_down - Integer that defines how many nodes down the copying is done. 513 # Default is one node level down. 514 # E.g. If node_levels_down is 2 then permissions are copied to 515 # asset's children and to their children. 381 516 def copy_permissions_to_subassets(node_levels_down = 1, assets_groups = self.assets_user_groups) 382 517 return 0 if node_levels_down < 1 … … 390 525 end 391 526 527 # Function: update_asset_field_values 528 # =================================== 529 # 392 530 # This should be called before the asset's fields are edited and before a 393 531 # new asset is created. It's responsible for creating the necessary … … 421 559 end 422 560 423 # Returns true if the user is authorized to perform operation +operation+ to this asset. 424 # +operation+:: Symbol defining which permission is checked. One of :edit, :create_report, 425 # :create_work_order, :attach_file, :create_subasset, :edit_permissions, :remove. 561 # Function: authorized_to 562 # ======================= 563 # 564 # Returns true if the user is authorized to perform the given operation to this asset. 565 # 566 # Parameters: 567 # ----------- 568 # operation - Symbol defining which permission is checked. Possible operations 569 # are :edit, :create_report, :create_work_order, :attach_file, 570 # :create_subasset, :edit_permissions, :remove. 426 571 def authorized_to(operation) 427 572 return false unless User.current_user … … 431 576 end 432 577 578 # Function: branch_authorized_to 579 # ============================== 580 # 581 # Returns true if the user is authorized to perform the given operation to this asset and to all its sub-assets. 582 # 583 # Parameters: 584 # ----------- 585 # operation - Symbol defining which permission is checked. Possible operations 586 # are :edit, :create_report, :create_work_order, :attach_file, 587 # :create_subasset, :edit_permissions, :remove. 588 def branch_authorized_to(operation) 589 return false unless self.authorized_to(operation) 590 # We can't use self.children here, because it will be cached and hence self.destroy will fail, even if children have been deleted from db! 591 for asset in Asset.find(:all, :conditions => ['parent_id = ?', self.id]) 592 return false unless asset.branch_authorized_to(operation) 593 end 594 return true 595 end 596 597 # Function: reparent 598 # ================== 599 # 433 600 # Changes the parent of this asset. The use_parents_permissions is set to false before moving. 434 601 def reparent(new_parent) … … 463 630 end 464 631 632 def automatic_value_from_asset_field_value(name) 633 return nil unless self.asset_type 634 asset_field = self.asset_type.asset_fields.find_by_user_field_name(name.gsub(/^cmms_asset_/, '')) 635 return nil unless asset_field 636 return self.asset_field_values.find_by_asset_field_id(asset_field.id).value 637 end 638 639 def automatic_value_from_asset_field_value_type_resolver(name) 640 return nil unless self.asset_type 641 asset_field = self.asset_type.asset_fields.find_by_user_field_name(name.gsub(/^cmms_asset_/, '')) 642 return nil unless asset_field 643 case asset_field.type_code 644 when AssetField::INTEGER 645 return :integer 646 when AssetField::STRING 647 return :string 648 when AssetField::BOOLEAN 649 return :boolean 650 when AssetField::FLOAT 651 return :float 652 when AssetField::DATE 653 return :date 654 when AssetField::TIME 655 return :time 656 when AssetField::DATETIME 657 return :datetime 658 else 659 return nil 660 end 661 end 662 663 def grandparent_asset_automatic_field(name) 664 return nil unless self.parent 665 return self.parent.value_for_automatic_field(name.gsub(/^cmms_grandparent_asset/, 'cmms_parent_asset')) 666 end 667 668 def grandparent_asset_automatic_field_type_resolver(name) 669 return nil unless self.parent 670 return self.parent.resolve_type_for_automatic_field(name.gsub(/^cmms_grandparent_asset/, 'cmms_parent_asset')) 671 end 672 673 def parent_asset_automatic_field(name) 674 return nil unless self.parent 675 return self.parent.value_for_automatic_field(name.gsub(/^cmms_parent_asset/, 'cmms_asset')) 676 end 677 678 def parent_asset_automatic_field_type_resolver(name) 679 return nil unless self.parent 680 return self.parent.resolve_type_for_automatic_field(name.gsub(/^cmms_parent_asset/, 'cmms_asset')) 681 end 682 465 683 protected 466 684 685 # Function: after_find 686 # ==================== 687 # 467 688 # Saves values of protected fields and use_parents_permissions to instance variables, 468 689 # so that we can check if they have been changed. … … 473 694 end 474 695 696 # Function: check_authorized_to_edit_parent 697 # ========================================= 698 # 475 699 #