Changeset 371

Show
Ignore:
Timestamp:
01/03/07 23:15:51 (2 years ago)
Author:
jarmo
Message:
  • Merged non db changes from [342] to [370] to the 1.2-alpha branch
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2-alpha/app/controllers/application.rb

    r339 r371  
    99 
    1010# Class: ApplicationController 
    11 # ================================= 
     11# ============================ 
    1212# 
    1313# The filters added to this controller will be run for all controllers in the application. 
     
    204204      @model = model_class.create(params[:model]) 
    205205 
    206       unless @model.new_record? 
     206      if not @model.new_record? 
    207207        info("#{model_class} '#{@model.attributes[name_attribute]}' created") 
    208208        flash[:notice] = msg_create_successful(@model) 
    209209        redirect_to(redirect) and return 
    210       else 
     210      elsif @model.errors.empty? 
    211211        flash[:error] = msg_system_failure_in(_('Creation')) 
    212212      end 
     
    235235        flash[:notice] = msg_changes_saved 
    236236        redirect_to(redirect) and return 
    237       else 
     237      elsif @model.errors.empty? 
    238238        flash[:notice] = msg_saving_failed 
    239239      end 
  • branches/1.2-alpha/app/controllers/asset_controller.rb

    r339 r371  
    3838      unless @asset 
    3939        if params[:cmms_link] 
    40          flash[:error] = msg_invalid_link_in_markup(params[:full_code]) 
    41        else 
     40          flash[:error] = msg_invalid_link_in_markup(params[:full_code]) 
     41        else 
    4242          flash[:error] = msg_system_failure_in(_('Displaying the asset')) 
    43        end 
     43        end 
    4444        return redirect_to(:back) 
    4545      end 
     
    9696        return 
    9797      end 
    98       flash[:error] = msg_saving_failed 
     98      flash[:error] = msg_saving_failed if @model.errors.empty? 
    9999    else 
    100100      @model.update_asset_field_values 
     
    182182 
    183183      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
    185185        exipre_asset_tree_fragments 
    186186        flash[:notice] = msg_changes_saved 
    187187        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? 
    191192    else 
    192193      unless @model.lock_for_current_user(LOCK_DURATION) 
     
    218219  def change_type 
    219220    @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       return 
    224     end 
    225  
    226     @title = _('Change type') 
    227  
    228221    @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 
    252308    end 
    253309 
     
    261317 
    262318  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 
    264326  end 
    265327 
     
    277339 
    278340end 
     341 
     342# Exception that can be raised when resource is locked 
     343class ResourceLocked < StandardError 
     344end 
  • branches/1.2-alpha/app/controllers/asset_permission_controller.rb

    r339 r371  
    114114    if @selected_asset.update_attribute(:use_parents_permissions, params[:selected_asset][:use_parents_permissions]) 
    115115      flash[:notice] = msg_changes_saved 
    116     else 
     116    elsif @selected_asset.errors.empty? 
    117117      flash[:error] = msg_saving_failed 
    118118    end 
  • branches/1.2-alpha/app/controllers/data_permission_controller.rb

    r299 r371  
    9292      if @object.update_permissions(params[:permission], params[:object]) 
    9393        flash[:notice] = msg_changes_saved 
    94       else 
     94      elsif @object.errors.empty? 
    9595        flash[:error] = msg_saving_failed 
    9696      end 
  • branches/1.2-alpha/app/controllers/message_controller.rb

    r299 r371  
    3838    if @message.unread 
    3939      @message.unread = false 
    40       unless @message.save 
     40      unless @message.save and @message.errors.empty? 
    4141        flash[:error] = msg_saving_failed 
    4242      end 
  • branches/1.2-alpha/app/controllers/report_type_controller.rb

    r299 r371  
    168168    if request.post? 
    169169      @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 
    171172      if @new_field_group.valid? 
    172173        @report_type.report_field_groups << @new_field_group 
     
    286287    @groups = @report_type.report_field_groups 
    287288    @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] } 
    289291    @new_field_group = ReportFieldGroup.new 
    290292    @enum_value = EnumeratedValue.new 
  • branches/1.2-alpha/app/controllers/user_controller.rb

    r339 r371  
    7575                       [_('Description'), :description] 
    7676                      ] 
    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'} ] 
    8080               ] 
    8181    @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 
    8392 
    8493    if request.post? 
     
    8897        flash[:notice] = _('User information changed') 
    8998        info "User information changed for #{@model.login}" 
    90       else 
     99      elsif @model.errors.empty? 
    91100        flash[:error] = _('Unable to change user information') 
    92101      end 
     
    98107 
    99108    # Ignore GET requests 
    100     return if request.get? 
     109    return if request.get? or not params[:delete] or params[:delete].empty? 
    101110 
    102111    names = [] 
     
    107116    end 
    108117    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(',')) 
    109133  end 
    110134 
  • branches/1.2-alpha/app/controllers/work_order_controller.rb

    r339 r371  
    102102      return 
    103103    end 
    104     @events = @work_order.work_order_events 
    105104    @users = @work_order.asset.users(@work_order.responsible_user_id) 
    106105 
    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) 
    108107 
    109108    if request.post? 
     
    126125      end 
    127126 
     127      @work_order.add_comment(params[:work_order_comment]) if params[:work_order_comment] 
     128 
    128129      case params[:work_order_action] 
    129130      when 'leave' 
    130         # We would save the comment here 
    131         return 
     131        # Do nothing 
    132132      when 'accept' 
    133133        @work_order.accept 
     
    160160      end 
    161161    end 
     162    @events = @work_order.work_order_events 
    162163  end 
    163164 
     
    182183      @work_order.deadline = nil if not params[:deadline_enabled] 
    183184 
    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? 
    186192    else 
    187193      unless @work_order.lock_for_current_user(LOCK_DURATION) 
     
    194200        return redirect_to(:action => 'view', :id => @work_order.id) 
    195201      end 
     202 
     203      user_entered_page_group(:work_order_edit) 
    196204    end 
    197205  end 
  • branches/1.2-alpha/app/helpers/application_helper.rb

    r339 r371  
    749749 
    750750    results = [] 
    751     children = "" 
    752751    for node in nodes 
    753752      result = "" 
     
    763762 
    764763      # 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)  
    766765 
    767766      # Don't display this node if the user is unauthorized to read it and 
  • branches/1.2-alpha/app/models/asset.rb

    r339 r371  
    22# See license agreement for additional rights 
    33 
     4# Class: Asset 
     5# ============ 
     6# 
    47class Asset < ProtectorModel 
    58  # Protection 
     
    3134  include AssetConditionAnalysis 
    3235 
     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  # 
    3347  # Returns the smallest possible SELECT clause that can be used in a find call for this model. 
    3448  # Contains protected fields and +use_parents_permissions+. 
     
    3751  end 
    3852 
     53  # Function: Asset.full_code_for 
     54  # ============================= 
     55  # 
    3956  # Returns the full code for asset with the given id or nil 
     57  # 
     58  # Parameters: 
     59  # ----------- 
     60  # id - Asset id 
     61  # 
    4062  def Asset.full_code_for(id) 
    4163    begin 
     
    4668  end 
    4769 
     70  # Function: Asset.get_first_authorized_to_read 
     71  # ============================================ 
     72  # 
    4873  # Returns the first found asset user is authorized to read. 
    4974  # If user is unauthorized to read any assets raises an exception. 
     
    5479  end 
    5580 
     81  # Function: Asset.find_by_full_code 
     82  # ================================= 
     83  # 
    5684  # Finds assets user is authorized to read by the full code. 
     85  # 
     86  # Parameters: 
     87  # ----------- 
     88  # full_code - Full code of an asset 
     89  # 
    5790  def Asset.find_by_full_code(full_code) 
    5891    code = full_code.split('-')[-1] 
     
    6396  end 
    6497 
    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  # 
    68109  def Asset.find_authorized_to(operation, args) 
    69110    return records_not_found(args) unless User.current_user 
     
    89130  end 
    90131 
     132  # Function: Asset.search_from_authorized_to_read 
     133  # ============================================== 
     134  # 
    91135  # 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  # 
    92141  def Asset.search_from_authorized_to_read(keywords) 
    93142    return [] unless keywords and not keywords.empty? 
     
    119168  end 
    120169 
     170  # Function: full_code 
     171  # =================== 
     172  # 
    121173  # Returns the asset's full code. 
    122174  # 
    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  # 
    125177  def full_code 
    126178    return self.code_prefix + self.code 
    127179  end 
    128180 
     181  # Function: code_prefix 
     182  # ===================== 
     183  # 
    129184  # Returns the asset's code prefix. 
    130185  # 
     
    139194  end 
    140195 
    141   # Returns: 'code (name)' 
     196  # Function: code_and_name 
     197  # ======================= 
     198  # 
     199  # Returns code and name in from 'code (name)' 
    142200  def code_and_name 
    143201    self.code + ' (' + self.name + ')' 
    144202  end 
    145203 
     204  # Function: is_root? 
     205  # 
    146206  # Returns true if asset is the root asset. 
    147207  def is_root? 
     
    149209  end 
    150210 
    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. 
    152215  # i.e. returns asset's id and it's children's ids and their chilren's ids etc. 
    153216  def id_and_subasset_ids(levels = nil) 
     
    159222  end 
    160223 
     224  # Function: path_to_root 
     225  # ====================== 
     226  # 
    161227  # Returns assets that are in the path from this asset to the root asset. 
    162228  def path_to_root 
     
    165231  end 
    166232 
     233  # Function: users 
     234  # =============== 
     235  # 
    167236  # 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  # 
    169242  def users(exclude_user_id = nil) 
    170243    return [] if self.user_groups.empty? 
     
    175248  end 
    176249 
     250  # Function: authorized_to_read? 
     251  # ============================= 
     252  # 
    177253  # Returns true if the user is authorized to read this asset. 
    178254  def authorized_to_read? 
     
    182258  end 
    183259 
     260  # Function: authorized_to_create_report? 
     261  # ====================================== 
     262  # 
    184263  # Returns true if the user is authorized to create reports to this asset 
    185264  def authorized_to_create_report? 
     
    187266  end 
    188267 
     268  # Function: authorized_to_create_work_order? 
     269  # ========================================== 
     270  # 
    189271  # Returns true if the user is authorized to create work_orders to this asset 
    190272  def authorized_to_create_work_order? 
     
    192274  end 
    193275 
     276  # Function: authorized_to_attach_file? 
     277  # ==================================== 
     278  # 
    194279  # Returns true if the user is authorized to create attachments to this asset 
    195280  def authorized_to_attach_file? 
     
    197282  end 
    198283 
     284  # Function: authorized_to_create_subasset? 
     285  # ======================================== 
     286  # 
    199287  # Returns true if the user is authorized to create sub-assets to this asset 
    200288  def authorized_to_create_subasset? 
     
    202290  end 
    203291 
     292  # Function: authorized_to_edit_permissions? 
     293  # ========================================= 
     294  # 
    204295  # Returns true if the user is authorized to edit asset permissions for this asset 
    205296  def authorized_to_edit_permissions? 
     
    207298  end 
    208299 
     300  # Function: authorized_to_remove? 
     301  # =============================== 
     302  # 
    209303  # Returns true if the user is authorized to remove this asset 
    210304  def authorized_to_remove? 
     
    212306  end 
    213307 
     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  # 
    214319  # Returns true if the user is authorized to remove this asset and all its sub-assets 
    215320  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) 
    222322  end 
    223323 
     
    243343  end 
    244344 
     345  # Function: authorized_to_create? 
     346  # =============================== 
     347  # 
    245348  # Returns true if the user is authorized to create new instances of +datatype+ to this asset 
    246349  def authorized_to_create?(datatype) 
     
    257360  end 
    258361 
     362  # Function: has_subasset_authorized_to_read? 
     363  # ========================================== 
     364  # 
    259365  # Returns true if asset has a sub-asset user is authorized to read. 
    260366  def has_subasset_authorized_to_read? 
     
    266372  end 
    267373 
    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. 
    271379  # The same association is created also for all sub-assets using this asset's permissions. 
    272380  # 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  # 
    273388  def add_user_group(user_group, permissions = {}) 
    274389    raise("#{user_group.class} in not a UserGroup") unless user_group.is_a?(UserGroup) 
     
    320435  end 
    321436 
    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. 
    323441  # Return value contains at most one permission profile for each user group. 
    324442  # If the current user belongs to multiple user groups and through them has multiple 
     
    327445  # contains one permission profile which has all permissions the summed permission 
    328446  # 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  # 
    332453  def data_permission_profiles_for_current_user(datatype) 
    333454    return [] unless user = User.current_user 
     
    357478  end 
    358479 
     480  # Function: node_depth 
     481  # ==================== 
     482  # 
    359483  # Returns the number of nodes in the asset tree below the receiving asset. 
    360484  def node_depth 
     
    363487  end 
    364488 
     489  # Function: copy_permissions_from 
     490  # =============================== 
     491  # 
    365492  # Copies all permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) 
    366493  # from Asset +asset+ to the receiver, whose existing permissions are destroyed. 
     
    371498  end 
    372499 
     500  # Function: copy_permissions_to_subassets 
     501  # ======================================= 
     502  # 
    373503  # Copies asset's permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) to sub-assets. 
    374504  # Permissions are copied only to those assets the user is authorized to edit. 
     
    377507  # After call sub-assets will have the same permissions as the asset. 
    378508  # 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. 
    381516  def copy_permissions_to_subassets(node_levels_down = 1, assets_groups = self.assets_user_groups) 
    382517    return 0 if node_levels_down < 1 
     
    390525  end 
    391526 
     527  # Function: update_asset_field_values 
     528  # =================================== 
     529  # 
    392530  # This should be called before the asset's fields are edited and before a 
    393531  # new asset is created. It's responsible for creating the necessary 
     
    421559  end 
    422560 
    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. 
    426571  def authorized_to(operation) 
    427572    return false unless User.current_user 
     
    431576  end 
    432577 
     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  # 
    433600  # Changes the parent of this asset. The use_parents_permissions is set to false before moving. 
    434601  def reparent(new_parent) 
     
    463630  end 
    464631 
     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 
    465683  protected 
    466684 
     685  # Function: after_find 
     686  # ==================== 
     687  # 
    467688  # Saves values of protected fields and use_parents_permissions to instance variables, 
    468689  # so that we can check if they have been changed. 
     
    473694  end 
    474695 
     696  # Function: check_authorized_to_edit_parent 
     697  # ========================================= 
     698  # 
    475699  #