Changeset 441

Show
Ignore:
Timestamp:
01/18/07 09:53:34 (2 years ago)
Author:
timo
Message:

Committed some unfinished changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/workorder_report_rename/app/models/assets_user_group.rb

    r329 r441  
    2727  belongs_to_protector :asset, :edit_permission => :authorized_to_edit_permissions? 
    2828  has_many :data_permission_profiles, :dependent => :delete_all 
    29   has_many :report_permission_profiles, :order => 'other', :dependent => :delete_all 
     29  has_many :form_permission_profiles, :order => 'other', :dependent => :delete_all 
    3030  has_many :work_order_permission_profiles, :order => 'other', :dependent => :delete_all 
    3131  has_many :attachment_permission_profiles, :order => 'other', :dependent => :delete_all 
     
    4343    return true if self.attributes == original_attributes 
    4444 
    45     original_create_report, original_create_work_order, original_attach_file = original_attributes.values_at('create_report', 'create_work_order', 'attach_file') 
     45    original_create_form, original_create_work_order, original_attach_file = original_attributes.values_at('create_form', 'create_work_order', 'attach_file') 
    4646 
    4747    begin 
     
    5050          raise(InvalidAssetPermissions) unless self.asset.permissions_valid? 
    5151          # Create default permission profiles or/and remove permission profiles. 
    52           # For Report
    53           if original_create_report == false and self.create_report == true 
    54             self.report_permission_profiles.create(:user_group_id => self.user_group_id, :group_read => true) 
    55           elsif original_create_report == true and self.create_report == false 
    56             self.report_permission_profiles.clear 
     52          # For Form
     53          if original_create_form == false and self.create_form == true 
     54            self.form_permission_profiles.create(:user_group_id => self.user_group_id, :group_read => true) 
     55          elsif original_create_form == true and self.create_form == false 
     56            self.form_permission_profiles.clear 
    5757          end 
    5858          # For Work orders 
     
    8282  def copy_data_permission_profiles_from(object) 
    8383    # If needed update attributes 
    84     if self.create_report != object.create_report or 
     84    if self.create_form != object.create_form or 
    8585        self.create_work_order != object.create_work_order or 
    8686        self.attach_file != object.attach_file 
    8787 
    8888      self.attributes = { 
    89         :create_report => object.create_report
     89        :create_form => object.create_form
    9090        :create_work_order => object.create_work_order, 
    9191        :attach_file => object.attach_file 
     
    152152  # are given, then new other permission profile(s) will be created with 
    153153  # attributes given in hashes +new_*_other_profile+. 
    154   def update_permission_profiles(profiles, new_report_other_profile = nil, new_work_order_other_profile = nil, new_attachment_other_profile = nil) 
     154  def update_permission_profiles(profiles, new_form_other_profile = nil, new_work_order_other_profile = nil, new_attachment_other_profile = nil) 
    155155    raise("Unauthorized to edit asset #{self.asset.full_code}'s permissions") unless authorized_to_edit? 
    156156 
     
    170170 
    171171      # Create new other profiles if needed. Only use group_* attributes from new_*_other_profile. 
    172       [new_report_other_profile, new_work_order_other_profile, new_attachment_other_profile].each { |attrs| 
     172      [new_form_other_profile, new_work_order_other_profile, new_attachment_other_profile].each { |attrs| 
    173173        next if attrs.nil? 
    174174        tmp_attrs = attrs.symbolize_keys 
     
    179179        } 
    180180      } 
    181       ReportPermissionProfile.create(new_report_other_profile) if new_report_other_profile 
     181      FormPermissionProfile.create(new_form_other_profile) if new_form_other_profile 
    182182      WorkOrderPermissionProfile.create(new_work_order_other_profile) if new_work_order_other_profile 
    183183      AttachmentPermissionProfile.create(new_attachment_other_profile) if new_attachment_other_profile 
     
    190190  # Returns true if none of the permission profiles for data +data+ has permission to +operation+ true. 
    191191  # +operation+:: defines which permission is checked. Choices are :read, :change_state and :edit. 
    192   # +data+:: defines data type of profiles. Choices are :reports, :work_orders and :attachments. 
     192  # +data+:: defines data type of profiles. Choices are :forms, :work_orders and :attachments. 
    193193  # 
    194   # Example: object.none_can(:read, :reports) 
     194  # Example: object.none_can(:read, :forms) 
    195195  def none_can(operation, data) 
    196196    attribute = case operation 
     
    206206 
    207207    case data 
    208     when :report
    209       self.report_permission_profiles.each { |p| 
     208    when :form
     209      self.form_permission_profiles.each { |p| 
    210210        if p.send(attribute) 
    211211          return false 
     
    298298 
    299299      if childs_group.update_attributes(:edit => self.edit, 
    300                                            :create_report => self.create_report
     300                                           :create_form => self.create_form
    301301                                           :create_work_order => self.create_work_order, 
    302302                                           :attach_file => self.attach_file, 
  • branches/workorder_report_rename/app/models/form_permission_profile.rb

    r24 r441  
    22# See license agreement for additional rights 
    33 
    4 class ReportPermissionProfile < DataPermissionProfile 
     4class FormPermissionProfile < DataPermissionProfile 
    55  # Protection 
    66  parents_protector_and_protected_fields 
     
    1313 
    1414 
    15   # Report doesn't have state 
     15  # Form doesn't have state 
    1616  HAS_CHANGE_STATE = false 
    1717end 
  • branches/workorder_report_rename/app/models/form_type.rb

    r368 r441  
    22# See license agreement for additional rights 
    33 
    4 class ReportType < ActiveRecord::Base 
     4class FormType < ActiveRecord::Base 
    55  # Protection 
    66  attr_protected :id, :editable, :ready 
     
    1212  before_save :create_html_template_and_images 
    1313  # Associations 
    14   has_one :report_template, :dependent => :destroy 
    15   has_one :report_html_template, :dependent => :destroy 
    16   has_many :report_fields, :dependent => :delete_all, :order => 'report_field_group_id, place' 
    17   has_many :report_field_groups, :dependent => :destroy, :order => 'place' 
    18   has_many :report
    19   has_many :report_template_images, :dependent => :destroy 
    20   belongs_to :report_category 
     14  has_one :form_template, :dependent => :destroy 
     15  has_one :form_html_template, :dependent => :destroy 
     16  has_many :form_fields, :dependent => :delete_all, :order => 'form_field_group_id, place' 
     17  has_many :form_field_groups, :dependent => :destroy, :order => 'place' 
     18  has_many :form
     19  has_many :form_template_images, :dependent => :destroy 
     20  belongs_to :form_category 
    2121 
    22   exports_automatic_fields_from :report_category 
    23   exports_automatic_field 'cmms_report_type_name', :string, :name 
     22  exports_automatic_fields_from :form_category 
     23  exports_automatic_field 'cmms_form_type_name', :string, :name 
    2424 
    25   # Returns the name of the category this report type belongs to. 
     25  # Returns the name of the category this form type belongs to. 
    2626  def category_name 
    27     return self.report_category.name 
     27    return self.form_category.name 
    2828  end 
    2929 
    30   # Returns the number of reports of this report type 
    31   def report_count 
    32     return self.reports.count 
     30  # Returns the number of forms of this form type 
     31  def form_count 
     32    return self.forms.count 
    3333  end 
    3434 
     
    3838    return unless declarations and declarations != [] 
    3939    declarations.each_element { |e| 
    40       self.report_fields << ReportField.create_from_user_field_declaration(e) 
     40      self.form_fields << FormField.create_from_user_field_declaration(e) 
    4141    } 
    4242  end 
     
    4646    return unless declarations and declarations != [] 
    4747    declarations.each { |e| 
    48       self.report_fields << ReportField.create_from_image_field_declaration(e) 
     48      self.form_fields << FormField.create_from_image_field_declaration(e) 
    4949    } 
    5050  end 
    5151 
    52   # Returns fields which should be displayed in the report list 
     52  # Returns fields which should be displayed in the form list 
    5353  # 
    5454  def list_columns 
    55     return self.report_fields.find(:all, :conditions => ["list_column = ?", true]) 
     55    return self.form_fields.find(:all, :conditions => ["list_column = ?", true]) 
    5656  end 
    5757 
    58   # Creates a empty report for this report type 
     58  # Creates a empty form for this form type 
    5959  # 
    60   def create_empty_report(asset) 
    61     raise 'Unable to create assetless report' unless asset 
    62     new_report = Report.new 
    63     new_report.report_type = self 
    64     new_report.asset = asset 
    65     self.report_fields.each { |f| 
    66       field_value = f.create_field_value(new_report
    67       new_report.report_field_values << field_value if field_value 
     60  def create_empty_form(asset) 
     61    raise 'Unable to create assetless form' unless asset 
     62    new_form = Form.new 
     63    new_form.form_type = self 
     64    new_form.asset = asset 
     65    self.form_fields.each { |f| 
     66      field_value = f.create_field_value(new_form
     67      new_form.form_field_values << field_value if field_value 
    6868    } 
    69     return new_report 
     69    return new_form 
    7070  end 
    7171 
    7272 
    73   # Extracts keywords from all reports the user is authorized to read 
    74   # in the report types with ids in +report_type_ids+. 
    75   # +report_type_ids+:: List or array of ids for the report types 
    76   #                     from whose reports the keywords are extracted. 
    77   # +condition+::       Condition(s) specifying from which reports the 
     73  # Extracts keywords from all forms the user is authorized to read 
     74  # in the form types with ids in +form_type_ids+. 
     75  # +form_type_ids+:: List or array of ids for the form types 
     76  #                     from whose forms the keywords are extracted. 
     77  # +condition+::       Condition(s) specifying from which forms the 
    7878  #                     keywords are extracted. 
    7979  # 
    8080  # Return value is an array of unique words. 
    81   # If no report type ids is given, an empty array is returned 
     81  # If no form type ids is given, an empty array is returned 
    8282  # 
    83   def ReportType.get_keywords(report_type_ids = [], condition = nil) 
    84     return [] if report_type_ids.empty? 
     83  def FormType.get_keywords(form_type_ids = [], condition = nil) 
     84    return [] if form_type_ids.empty? 
    8585    if condition 
    8686      if condition.is_a?(Array) 
    8787        conditions = condition.dup 
    88         conditions[0] = "(#{condition[0]}) AND report_type_id IN (?)" 
    89         conditions.push(report_type_ids) 
     88        conditions[0] = "(#{condition[0]}) AND form_type_id IN (?)" 
     89        conditions.push(form_type_ids) 
    9090      else 
    91         conditions = ["(#{condition}) AND report_type_id IN (?)", report_type_ids] 
     91        conditions = ["(#{condition}) AND form_type_id IN (?)", form_type_ids] 
    9292      end 
    9393    else 
    94       conditions = ['report_type_id IN (?)', report_type_ids] 
     94      conditions = ['form_type_id IN (?)', form_type_ids] 
    9595    end 
    9696    keywords = [] 
    97     Report.find_authorized_to_read(:all, :select => Report.minimal_select, :conditions => conditions, :include => :report_field_values, :readonly => true).each { |report
    98       report.report_field_values.each { |value| 
     97    Form.find_authorized_to_read(:all, :select => Form.minimal_select, :conditions => conditions, :include => :form_field_values, :readonly => true).each { |form
     98      form.form_field_values.each { |value| 
    9999        keywords += value.get_keywords 
    100100      } 
     
    103103  end 
    104104 
    105   # Searchs reports matching +search_string+ from all reports in the report type the user is authorized to read. 
    106   # +search_string+:: String containing words which are searched from the report
    107   # +args+:: Further specifies the reports to search. Use the same arguments as for Report.find 
     105  # Searchs forms matching +search_string+ from all forms in the form type the user is authorized to read. 
     106  # +search_string+:: String containing words which are searched from the form
     107  # +args+:: Further specifies the forms to search. Use the same arguments as for Form.find 
    108108  # 
    109   # Returns reports matching the search string. 
    110   # If no reports are found nil or an empty array is returned depending on (find) arguments. 
     109  # Returns forms matching the search string. 
     110  # If no forms are found nil or an empty array is returned depending on (find) arguments. 
    111111  # 
    112   def search_reports(search_string, *args) 
    113     reports = self.reports.find_authorized_to_read(*args) 
    114     return reports if search_string.empty? 
    115     if reports.is_a?(Array) 
    116       return reports.select { |report
    117         report.matches_search_string?(search_string) 
     112  def search_forms(search_string, *args) 
     113    forms = self.forms.find_authorized_to_read(*args) 
     114    return forms if search_string.empty? 
     115    if forms.is_a?(Array) 
     116      return forms.select { |form
     117        form.matches_search_string?(search_string) 
    118118      } 
    119     elsif reports.is_a?(Report
    120       return reports.matches_search_string?(search_string) ? reports : nil 
     119    elsif forms.is_a?(Form
     120      return forms.matches_search_string?(search_string) ? forms : nil 
    121121    else 
    122122      return nil 
     
    124124  end 
    125125 
    126   # Creates HTML report template for this report type when it doesn't have HTML template. 
    127   # With true +update_existing+ the existing HTML template is updated to correspond to the current report template. 
    128   # As a before_save callback creates HTML template when report type is saved for the first time after report template has been created. 
     126  # Creates HTML form template for this form type when it doesn't have HTML template. 
     127  # With true +update_existing+ the existing HTML template is updated to correspond to the current form template. 
     128  # As a before_save callback creates HTML template when form type is saved for the first time after form template has been created. 
    129129  def create_html_template_and_images(update_existing = false) 
    130     return true unless self.report_template 
    131     if self.report_html_template.nil? 
    132       html_template = ReportHtmlTemplate.new(:report_type_id => self.id) 
     130    return true unless self.form_template 
     131    if self.form_html_template.nil? 
     132      html_template = FormHtmlTemplate.new(:form_type_id => self.id) 
    133133    elsif update_existing 
    134       html_template = self.report_html_template 
     134      html_template = self.form_html_template 
    135135    else 
    136136      return true 
    137137    end 
    138     style, body = self.convert_report_template_to_html 
     138    style, body = self.convert_form_template_to_html 
    139139    html_template.style = style.to_s 
    140140    html_template.body = body.to_s 
    141141    begin 
    142       ReportHtmlTemplate.transaction do 
    143         ReportTemplateImage.transaction do 
     142      FormHtmlTemplate.transaction do 
     143        FormTemplateImage.transaction do 
    144144          html_template.save! 
    145145          self.save_template_images! 
     
    147147      end 
    148148    rescue ActiveRecord::RecordNotSaved 
    149       _error("Unable to create HTML template and template images for report type (id=#{self.id}) \"#{self.name}\"") 
     149      _error("Unable to create HTML template and template images for form type (id=#{self.id}) \"#{self.name}\"") 
    150150      return false 
    151151    end 
     
    154154protected 
    155155 
    156   def convert_report_template_to_html 
    157     raise 'Template does not exist' unless self.report_template 
    158     user_fields = self.report_fields.map { |field| 
     156  def convert_form_template_to_html 
     157    raise 'Template does not exist' unless self.form_template 
     158    user_fields = self.form_fields.map { |field| 
    159159      field.to_user_field 
    160160    }.compact 
    161     template = self.report_template.clone 
     161    template = self.form_template.clone 
    162162    template.set_user_fields(user_fields, true) 
    163     return template.convert_to_html(ReportHtmlTemplate::IMAGE_BASE_URL_HTML_TAG) 
     163    return template.convert_to_html(FormHtmlTemplate::IMAGE_BASE_URL_HTML_TAG) 
    164164  end 
    165165 
    166   # Save images in report template to database. 
     166  # Save images in form template to database. 
    167167  def save_template_images! 
    168     self.report_template.get_all_images.each { |image_attributes| 
    169       ReportTemplateImage.create!(image_attributes.merge(:report_type_id => self.id)) 
     168    self.form_template.get_all_images.each { |image_attributes| 
     169      FormTemplateImage.create!(image_attributes.merge(:form_type_id => self.id)) 
    170170    } 
    171171  end 
  • branches/workorder_report_rename/app/models/user_group.rb

    r24 r441  
    1515  has_many :assets_user_groups, :dependent => :destroy 
    1616  has_many :assets, :through => :assets_user_groups 
    17   has_many :report_permissions, :dependent => :delete_all 
    18 #  has_many :reports, :through => :report_permissions 
     17  has_many :form_permissions, :dependent => :delete_all 
     18#  has_many :forms, :through => :form_permissions 
    1919  has_many :work_order_permissions, :dependent => :delete_all 
    2020#  has_many :work_orders, :through => :work_order_permissions 
  • branches/workorder_report_rename/config/menu.rb

    r351 r441  
    3434                    Page.new('attachment', 'select_asset') 
    3535                  ), 
    36                   MenuButton.new(_('Reports'), '22x22/menu_report.gif', 
     36                  MenuButton.new(_('Forms'), '22x22/menu_form.gif', 
    3737                    MenuItem.new(_('List'), 'report', 'list', '16x16/list.gif'), 
    3838                    MenuItem.new(_('Create'), 'report', 'select_report_type', '16x16/create.gif'), 
  • branches/workorder_report_rename/db/migrate/021_version_one_dot_two_tables.rb

    r266 r441  
    11class VersionOneDotTwoTables < ActiveRecord::Migration 
     2  @@report_tables = [ 
     3    'reports', 
     4    'report_field_values' 
     5  ] 
     6 
    27  def self.up 
     8    rename_tables('report', 'form', @@report_tables) 
     9 
    310    use_limit_for_binary_columns = ['MySQL'].include?(ActiveRecord::Base.connection.adapter_name) 
    411 
    5     # Enumerated report fields 
     12    # Enumerated form fields 
    613    create_table 'enumerated_values', :force => true do |t| 
    7       t.column 'report_field_id', :integer, :references => :report_fields 
     14      t.column 'form_field_id', :integer, :references => :form_fields 
    815      t.column 'value', :string, :limit => 40 
    916    end 
    10     add_column 'report_field_values', 'enumerated_value_id', :integer, :references => :enumerated_values 
     17    add_column 'form_field_values', 'enumerated_value_id', :integer, :references => :enumerated_values 
    1118 
    12     # Work order & report joining 
    13     add_column 'reports', 'cause_work_order_id', :integer, :references => :work_orders 
    14     add_index 'reports', ['cause_work_order_id'] 
     19    # Work order & form joining 
     20    add_column 'forms', 'cause_work_order_id', :integer, :references => :work_orders 
     21    add_index 'forms', ['cause_work_order_id'] 
    1522 
    16     add_column 'work_orders', 'cause_report_id', :integer, :references => :report
    17     add_index 'work_orders', ['cause_report_id'] 
     23    add_column 'work_orders', 'cause_form_id', :integer, :references => :form
     24    add_index 'work_orders', ['cause_form_id'] 
    1825 
    1926    # Tags for data types - Constitutes causal chains for asset data. 
     
    2330      t.column 'cause_tag_id', :integer, :references => :data_type_tags 
    2431    end 
    25     add_column 'report_categories', 'report_tag_id', :integer, :references => :data_type_tags 
    26     add_index 'report_categories', ['report_tag_id'] 
     32    add_column 'form_categories', 'form_tag_id', :integer, :references => :data_type_tags 
     33    add_index 'form_categories', ['form_tag_id'] 
    2734    add_column 'work_order_types', 'work_order_tag_id', :integer, :references => :data_type_tags 
    2835    add_index 'work_order_types', ['work_order_tag_id'] 
     
    3441    TempAsset.set_created_at(Time.now) 
    3542 
    36     # Join table for reports and work orders is no longer used! 
     43    # Join table for forms and work orders is no longer used! 
    3744    # Propably best to remove it from 001_initial.rb unless some version still uses it. 
    38     drop_table 'reports_work_orders' 
     45    drop_table 'forms_work_orders' 
    3946 
    4047    # Asset fields 
     
    6269    add_index 'asset_field_values', ['asset_field_id'] 
    6370 
    64     # HTML report templates for report types 
    65     create_table 'report_html_templates', :force => true do |t| 
    66       t.column 'report_type_id', :integer 
     71    # HTML form templates for form types 
     72    create_table 'form_html_templates', :force => true do |t| 
     73      t.column 'form_type_id', :integer 
    6774      if use_limit_for_binary_columns 
    68         t.column 'style', :binary, :limit => ReportHtmlTemplate::MAX_STYLE_SIZE 
    69         t.column 'body', :binary, :limit => ReportHtmlTemplate::MAX_BODY_SIZE 
     75        t.column 'style', :binary, :limit => FormHtmlTemplate::MAX_STYLE_SIZE 
     76        t.column 'body', :binary, :limit => FormHtmlTemplate::MAX_BODY_SIZE 
    7077      else 
    7178        t.column 'style', :binary 
     
    7380      end 
    7481    end 
    75     add_index 'report_html_templates', ['report_type_id'] 
    76     # Report template images 
    77     create_table 'report_template_images', :force => true do |t| 
    78       t.column 'report_type_id', :integer 
     82    add_index 'form_html_templates', ['form_type_id'] 
     83    # Form template images 
     84    create_table 'form_template_images', :force => true do |t| 
     85      t.column 'form_type_id', :integer 
    7986      t.column 'content_type', :text 
    8087      t.column 'name', :text 
     
    8289      t.column 'is_image_field', :boolean, :default => false 
    8390      if use_limit_for_binary_columns 
    84         t.column 'data', :binary, :limit => ReportTemplateImage::MAX_SIZE 
     91        t.column 'data', :binary, :limit => FormTemplateImage::MAX_SIZE 
    8592      else 
    8693        t.column 'data', :binary 
    8794      end 
    8895    end 
    89     add_index 'report_template_images', ['report_type_id'] 
     96    add_index 'form_template_images', ['form_type_id'] 
    9097    # If faster image quering is needed try indexing the name columns too! 
    9198  end 
     
    93100  def self.down 
    94101    drop_table 'enumerated_values' 
    95     remove_column 'report_field_values', 'enumerated_value_id' 
    96     remove_column 'reports', 'cause_work_order_id' 
    97     remove_column 'work_orders', 'cause_report_id' 
    98     remove_column 'report_categories', 'report_tag_id' 
     102    remove_column 'form_field_values', 'enumerated_value_id' 
     103    remove_column 'forms', 'cause_work_order_id' 
     104    remove_column 'work_orders', 'cause_form_id' 
     105    remove_column 'form_categories', 'form_tag_id' 
    99106    remove_column 'work_order_types', 'work_order_tag_id' 
    100107    drop_table 'data_type_tags' 
    101108    remove_column 'asset_types', 'typical_maintenance_time_interval' 
    102109    remove_column 'assets', 'created_at' 
    103     drop_table 'report_html_templates' 
    104     drop_table 'report_template_images' 
     110    drop_table 'form_html_templates' 
     111    drop_table 'form_template_images' 
     112     
     113    rename_tables('report', 'form', @@report_tables, true) 
     114  end 
     115 
     116  # Function: rename_tables 
     117  # ----------------------- 
     118  # Renames the tables listed in `names` by replacing `from` in the name with 
     119  # `to`. 
     120  # 
     121  # If `revert` is set to `true`, the process is reverted, and the tables are 
     122  # renamed by replacing `to` with `from`. 
     123  def rename_tables(from, to, names, revert=false) 
     124    for name in names 
     125      if revert 
     126        new_name = name.sub(to, from) 
     127      else 
     128        new_name = name.sub(from, to) 
     129      end 
     130 
     131      rename_table(name, new_name) 
     132    end 
    105133  end 
    106134end 
  • branches/workorder_report_rename/test/unit/form_test.rb

    r368 r441  
    11require File.dirname(__FILE__) + '/../test_helper' 
    22 
    3 class ReportTest < Test::Unit::TestCase 
     3class FormTest < Test::Unit::TestCase 
    44  def setup 
    55    # Create the root asset, a test user and a user group that has 
    6     # permissions to create reports to the root asset. 
     6    # permissions to create forms to the root asset. 
    77    user = create_user('test') 
    88    User.current_user = user 
     
    1111    @root_asset = Asset.create!(:name => 'Test', :code => 'TST') 
    1212    aug = add_user_group_to_asset(user_group, @root_asset, true, true) 
    13     aug.report_permission_profiles.create!(:other => true, :group_read => true, :group_edit => true) 
     13    aug.form_permission_profiles.create!(:other => true, :group_read => true, :group_edit => true) 
    1414 
    15     # Report type with all user field types 
    16     @report_type = ReportType.create!(:name => 'Test type') 
    17     @report_type.report_fields << ReportField.create!(:name => 'Integer', :user_field_name => 'int', :type_code => ReportField::INTEGER) 
    18     @report_type.report_fields << ReportField.create!(:name => 'String', :user_field_name => 'string', :type_code => ReportField::STRING) 
    19     @report_type.report_fields << ReportField.create!(:name => 'Boolean', :user_field_name => 'bool', :type_code => ReportField::BOOLEAN) 
    20     @report_type.report_fields << ReportField.create!(:name => 'Float', :user_field_name => 'float', :type_code => ReportField::FLOAT) 
    21     @report_type.report_fields << ReportField.create!(:name => 'Long string', :user_field_name => 'lstring', :type_code => ReportField::LONG_STRING) 
    22     @report_type.report_fields << ReportField.create!(:name => 'Date', :user_field_name => 'date', :type_code => ReportField::DATE) 
    23     @report_type.report_fields << ReportField.create!(:name => 'Time', :user_field_name => 'time', :type_code => ReportField::TIME) 
    24     @report_type.report_fields << ReportField.create!(:name => 'Datetime', :user_field_name => 'datetime', :type_code => ReportField::DATETIME) 
    25     @report_type.report_fields << ReportField.create!(:name => 'Automatic 1', :user_field_name => 'cmms_report_id', :type_code => ReportField::AUTOMATIC) 
    26     @report_type.report_fields << ReportField.create!(:name => 'Automatic 2', :user_field_name => 'cmms_report_type_name', :type_code => ReportField::AUTOMATIC) 
    27     @report_type.report_fields << ReportField.create!(:name => 'Automatic 3', :user_field_name => 'cmms_asset_full_code', :type_code => ReportField::AUTOMATIC) 
    28     @report_type.save! 
     15    # Form type with all user field types 
     16    @form_type = FormType.create!(:name => 'Test type') 
     17    @form_type.form_fields << FormField.create!(:name => 'Integer', :user_field_name => 'int', :type_code => FormField::INTEGER) 
     18    @form_type.form_fields << FormField.create!(:name => 'String', :user_field_name => 'string', :type_code => FormField::STRING) 
     19    @form_type.form_fields << FormField.create!(:name => 'Boolean', :user_field_name => 'bool', :type_code => FormField::BOOLEAN) 
     20    @form_type.form_fields << FormField.create!(:name => 'Float', :user_field_name => 'float', :type_code => FormField::FLOAT) 
     21    @form_type.form_fields << FormField.create!(:name => 'Long string', :user_field_name => 'lstring', :type_code => FormField::LONG_STRING) 
     22    @form_type.form_fields << FormField.create!(:name => 'Date', :user_field_name => 'date', :type_code => FormField::DATE) 
     23    @form_type.form_fields << FormField.create!(:name => 'Time', :user_field_name => 'time', :type_code => FormField::TIME) 
     24    @form_type.form_fields << FormField.create!(:name => 'Datetime', :user_field_name => 'datetime', :type_code => FormField::DATETIME) 
     25    @form_type.form_fields << FormField.create!(:name => 'Automatic 1', :user_field_name => 'cmms_form_id', :type_code => FormField::AUTOMATIC) 
     26    @form_type.form_fields << FormField.create!(:name => 'Automatic 2', :user_field_name => 'cmms_form_type_name', :type_code => FormField::AUTOMATIC) 
     27    @form_type.form_fields << FormField.create!(:name => 'Automatic 3', :user_field_name => 'cmms_asset_full_code', :type_code => FormField::AUTOMATIC) 
     28    @form_type.save! 
    2929 
    30     # First test report 
    31     @report = @report_type.create_empty_report(@root_asset) 
    32     @report.ready = true 
    33     @report.save! 
    34     @report.update_automatic_field_values 
     30    # First test form 
     31    @form = @form_type.create_empty_form(@root_asset) 
     32    @form.ready = true 
     33    @form.save! 
     34    @form.update_automatic_field_values 
    3535 
    36     # Second test report 
    37     @report2 = @report_type.create_empty_report(@root_asset) 
    38     @report2.ready = true 
    39     @report2.save! 
     36    # Second test form 
     37    @form2 = @form_type.create_empty_form(@root_asset) 
     38    @form2.ready = true 
     39    @form2.save! 
    4040 
    4141    # Set some string variables for testing the keyword and search methods 
    42     field = @report_type.report_fields.find_by_type_code(ReportField::STRING) 
    43     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Some text') 
    44     @report2.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Foo bar Baz') 
    45     field = @report_type.report_fields.find_by_type_code(ReportField::LONG_STRING) 
    46     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Some other text and something') 
    47     @report2.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Doh Dah Duh') 
    48     @report.reload 
    49     @report2.reload 
     42    field = @form_type.form_fields.find_by_type_code(FormField::STRING) 
     43    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Some text') 
     44    @form2.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Foo bar Baz') 
     45    field = @form_type.form_fields.find_by_type_code(FormField::LONG_STRING) 
     46    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Some other text and something') 
     47    @form2.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Doh Dah Duh') 
     48    @form.reload 
     49    @form2.reload 
    5050  end 
    5151 
    5252  # Testing instance method value_of 
    5353  def test_value_of 
    54     field = @report_type.report_fields.find_by_type_code(ReportField::STRING) 
    55     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Foobar') 
    56     value = @report.value_of(field) 
     54    field = @form_type.form_fields.find_by_type_code(FormField::STRING) 
     55    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Foobar') 
     56    value = @form.value_of(field) 
    5757    assert_equal 'Foobar', value 
    5858  end 
     
    6060  # Checks that value_of returns the right default "" if the field does not exist 
    6161  def test_value_of_nonexisting_field 
    62     field = ReportField::create!(:name => 'Test', :user_field_name => 'test', :type_code => ReportField::STRING) 
     62    field = FormField::create!(:name => 'Test', :user_field_name => 'test', :type_code => FormField::STRING) 
    6363    assert_not_nil field 
    64     value = @report.value_of(field) 
     64    value = @form.value_of(field) 
    6565    assert_equal "", value 
    6666  end 
     
    6868  def test_automatic_field_values 
    6969    test_values = [  
    70                     [@report.id, 'cmms_report_id'], 
    71                     [@report.report_type.name, 'cmms_report_type_name'], 
     70                    [@form.id, 'cmms_form_id'], 
     71                    [@form.form_type.name, 'cmms_form_type_name'], 
    7272                    ['TST', 'cmms_asset_full_code'] 
    7373                  ] 
    7474    for expected_value, automatic_field_name in test_values 
    75       assert_equal expected_value, @report.get_field_value_for(@report_type.report_fields.find_by_user_field_name(automatic_field_name)).value 
     75      assert_equal expected_value, @form.get_field_value_for(@form_type.form_fields.find_by_user_field_name(automatic_field_name)).value 
    7676    end 
    7777  end 
    7878 
    7979  def test_get_field_value_for 
    80     field = @report_type.report_fields.find_by_type_code(ReportField::STRING) 
    81     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Foobar') 
    82     value = @report.get_field_value_for(field) 
    83     assert_equal field.id, value.report_field.id 
     80    field = @form_type.form_fields.find_by_type_code(FormField::STRING) 
     81    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Foobar') 
     82    value = @form.get_field_value_for(field) 
     83    assert_equal field.id, value.form_field.id 
    8484 
    8585    # No value for this field 
    86     field = ReportField.create!(:name => 'Test', :user_field_name => 'test', :type_code => ReportField::STRING) 
    87     assert_nil @report.get_field_value_for(field) 
     86    field = FormField.create!(:name => 'Test', :user_field_name => 'test', :type_code => FormField::STRING) 
     87    assert_nil @form.get_field_value_for(field) 
    8888  end 
    8989 
    9090  def test_get_keywords 
    91     field = @report_type.report_fields.find_by_type_code(ReportField::STRING) 
    92     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Some text') 
    93     field = @report_type.report_fields.find_by_type_code(ReportField::LONG_STRING) 
    94     @report.report_field_values.find_by_report_field_id(field.id).update_attributes(:string_value => 'Some other text and something') 
    95     @report.reload 
     91    field = @form_type.form_fields.find_by_type_code(FormField::STRING) 
     92    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Some text') 
     93    field = @form_type.form_fields.find_by_type_code(FormField::LONG_STRING) 
     94    @form.form_field_values.find_by_form_field_id(field.id).update_attributes(:string_value => 'Some other text and something') 
     95    @form.reload 
    9696 
    9797    right_keywords = ['some' , 'text', 'other', 'and', 'something', 'tst', 'type', 'test'] 
    98     keywords = @report.get_keywords 
     98    keywords = @form.get_keywords 
    9999 
    100100    assert_equal right_keywords.uniq.sort, keywords.uniq.sort 
     
    107107    part_of_word2 = "som" 
    108108    part_of_word3 = "ing" 
    109     assert @report.matches_word_or_part_of_word?(word) 
    110     assert @report.matches_word_or_part_of_word?(part_of_word1) 
    111     assert @report.matches_word_or_part_of_word?(part_of_word2) 
    112     assert @report.matches_word_or_part_of_word?(part_of_word3) 
     109    assert @form.matches_word_or_part_of_word?(word) 
     110    assert @form.matches_word_or_part_of_word?(part_of_word1) 
     111    assert @form.matches_word_or_part_of_word?(part_of_word2) 
     112    assert @form.matches_word_or_part_of_word?(part_of_word3) 
    113113    # and these should not 
    114114    word1 = "abc" 
    115115    word2 = "foobar" 
    116116    word3 = "more" 
    117     assert !@report.matches_word_or_part_of_word?(word1) 
    118     assert !@report.matches_word_or_part_of_word?(word2) 
    119     assert !@report.matches_word_or_part_of_word?(word3) 
     117    assert !@form.matches_word_or_part_of_word?(word1) 
     118    assert !@form.matches_word_or_part_of_word?(word2) 
     119    assert !@form.matches_word_or_part_of_word?(word3) 
    120120  end 
    121121 
     
    124124    string1 = "Some text" 
    125125    string2 = "other tex" 
    126     assert @report.matches_search_string?(string1) 
    127     assert @report.matches_search_string?(string2) 
     126    assert @form.matches_search_string?(string1) 
     127    assert @form.matches_search_string?(string2) 
    128128    # and these should not 
    129129    string3 = "Have at you!" 
    130130    string4 = "None shall pass!" 
    131131    string5 = "I've had worse" 
    132     assert !@report.matches_search_string?(string3) 
    133     assert !@report.matches_search_string?(string4) 
    134     assert !@report.matches_search_string?(string5) 
     132    assert !@form.matches_search_string?(string3) 
     133    assert !@form.matches_search_string?(string4) 
     134    assert !@form.matches_search_string?(string5) 
    135135  end 
    136136 
    137   def test_Report_search 
     137  def test_Form_search 
    138138    str1 = 'fungerar intte' # not found 
    139139    str2 = 'Som other'     # found in id1 
     
    141141    str4 = 'o'              # found in both 
    142142 
    143     assert Report.search(str1, @report.id, @report2.id).empty? 
    144     assert_nil Report.search(str1, @report.id) 
    145     assert_nil Report.search(str1, @report2.id) 
    146     assert_equal [@report], Report.search(str2, @report.id, @report2.id) 
    147     assert_equal [@report2], Report.search(str3, @report.id, @report2.id) 
    148     should_include_both = Report.search(str4, @report.id, @report2.id) 
    149     assert  should_include_both.include?(@report
    150     assert  should_include_both.include?(@report2) 
     143    assert Form.search(str1, @form.id, @form2.id).empty? 
     144    assert_nil Form.search(str1, @form.id) 
     145    assert_nil Form.search(str1, @form2.id) 
     146    assert_equal [@form], Form.search(str2, @form.id, @form2.id) 
     147    assert_equal [@form2], Form.search(str3, @form.id, @form2.id) 
     148    should_include_both = Form.search(str4, @form.id, @form2.id) 
     149    assert  should_include_both.include?(@form
     150    assert  should_include_both.include?(@form2) 
    151151 
    152152    # Empty search string 
    153     all_reports = Report.find(:all) 
    154     reports = [] 
    155     all_reports.each {|r| reports << r if r.asset and r.authorized_to_read?} 
    156     assert_equal reports, Report.search("", :all) 
    157     assert_equal Report.find(@report.id, @report2.id, :order => "created_at DESC"), Report.search("", @report.id, @report2.id, :order => "created_at DESC") 
     153    all_forms = Form.find(:all) 
     154    forms = [] 
     155    all_forms.each {|r| forms << r if r.asset and r.authorized_to_read?} 
     156    assert_equal forms, Form.search("", :all) 
     157    assert_equal Form.find(@form.id, @form2.id, :order => "created_at DESC"), Form.search("", @form.id, @form2.id, :order => "created_at DESC") 
    158158  end 
    159159 
    160   def test_Report_created_at 
    161     old_report = Report.find TempReport.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id 
    162     oldest = Report.created_at(:min) 
    163     assert_equal old_report.created_at, oldest 
    164     assert_equal oldest, Report.created_at(:min, ['created_at < ?', 4.years.ago]) 
     160  def test_Form_created_at 
     161    old_form = Form.find TempForm.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id 
     162    oldest = Form.created_at(:min) 
     163    assert_equal old_form.created_at, oldest 
     164    assert_equal oldest, Form.created_at(:min, ['created_at < ?', 4.years.ago]) 
    165165 
    166     report = create_report(@root_asset, true) 
    167     youngest = Report.created_at(:max) 
    168     assert_equal report.created_at, youngest 
    169     assert_equal youngest, Report.created_at(:max, ['created_at > ?', 1.hour.ago]) 
     166    form = create_form(@root_asset, true) 
     167    youngest = Form.created_at(:max) 
     168    assert_equal form.created_at, youngest 
     169    assert_equal youngest, Form.created_at(:max, ['created_at > ?', 1.hour.ago]) 
    170170 
    171     TempReport.destroy_all 
     171    TempForm.destroy_all 
    172172    # When no records exist 
    173     assert_nil Report.created_at(:min) 
    174     assert_nil Report.created_at(:max) 
     173    assert_nil Form.created_at(:min) 
     174    assert_nil Form.created_at(:max) 
    175175 
    176176    # When created_at is in the future, which should never happen though 
    177     future_report = Report.find TempReport.create!(:created_at => 1.hour.from_now, :asset_id => @root_asset.id, :ready => true).id 
    178     assert_equal future_report.created_at, Report.created_at(:min) 
    179     assert_equal future_report.created_at, Report.created_at(:max) 
     177    future_form = Form.find TempForm.create!(:created_at => 1.hour.from_now, :asset_id => @root_asset.id, :ready => true).id 
     178    assert_equal future_form.created_at, Form.created_at(:min) 
     179    assert_equal future_form.created_at, Form.created_at(:max) 
    180180  end 
    181181 
    182   def test_Report_year_span 
    183     old_report = Report.find TempReport.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id 
    184     assert_equal [old_report.created_at.year, @report.created_at.year], Report.year_span 
     182  def test_Form_year_span 
     183    old_form = Form.find TempForm.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id 
     184    assert_equal [old_form.created_at.year, @form.created_at.year], Form.year_span 
    185185  end 
    186186 

© 2004-2007 Norfello Oy All Rights Reserved