Changeset 441
- Timestamp:
- 01/18/07 09:53:34 (2 years ago)
- Files:
-
- branches/workorder_report_rename/app/models/assets_user_group.rb (modified) (10 diffs)
- branches/workorder_report_rename/app/models/form_permission_profile.rb (moved) (moved from branches/workorder_report_rename/app/models/report_permission_profile.rb) (2 diffs)
- branches/workorder_report_rename/app/models/form_type.rb (moved) (moved from branches/workorder_report_rename/app/models/report_type.rb) (8 diffs)
- branches/workorder_report_rename/app/models/user_group.rb (modified) (1 diff)
- branches/workorder_report_rename/config/menu.rb (modified) (1 diff)
- branches/workorder_report_rename/db/migrate/021_version_one_dot_two_tables.rb (modified) (7 diffs)
- branches/workorder_report_rename/public/images/22x22/menu_form.gif (moved) (moved from branches/workorder_report_rename/public/images/22x22/menu_report.gif)
- branches/workorder_report_rename/test/unit/form_test.rb (moved) (moved from branches/workorder_report_rename/test/unit/report_test.rb) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/workorder_report_rename/app/models/assets_user_group.rb
r329 r441 27 27 belongs_to_protector :asset, :edit_permission => :authorized_to_edit_permissions? 28 28 has_many :data_permission_profiles, :dependent => :delete_all 29 has_many : report_permission_profiles, :order => 'other', :dependent => :delete_all29 has_many :form_permission_profiles, :order => 'other', :dependent => :delete_all 30 30 has_many :work_order_permission_profiles, :order => 'other', :dependent => :delete_all 31 31 has_many :attachment_permission_profiles, :order => 'other', :dependent => :delete_all … … 43 43 return true if self.attributes == original_attributes 44 44 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') 46 46 47 47 begin … … 50 50 raise(InvalidAssetPermissions) unless self.asset.permissions_valid? 51 51 # Create default permission profiles or/and remove permission profiles. 52 # For Reports53 if original_create_ report == false and self.create_report== true54 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== false56 self. report_permission_profiles.clear52 # For Forms 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 57 57 end 58 58 # For Work orders … … 82 82 def copy_data_permission_profiles_from(object) 83 83 # If needed update attributes 84 if self.create_ report != object.create_reportor84 if self.create_form != object.create_form or 85 85 self.create_work_order != object.create_work_order or 86 86 self.attach_file != object.attach_file 87 87 88 88 self.attributes = { 89 :create_ report => object.create_report,89 :create_form => object.create_form, 90 90 :create_work_order => object.create_work_order, 91 91 :attach_file => object.attach_file … … 152 152 # are given, then new other permission profile(s) will be created with 153 153 # 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) 155 155 raise("Unauthorized to edit asset #{self.asset.full_code}'s permissions") unless authorized_to_edit? 156 156 … … 170 170 171 171 # 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| 173 173 next if attrs.nil? 174 174 tmp_attrs = attrs.symbolize_keys … … 179 179 } 180 180 } 181 ReportPermissionProfile.create(new_report_other_profile) if new_report_other_profile181 FormPermissionProfile.create(new_form_other_profile) if new_form_other_profile 182 182 WorkOrderPermissionProfile.create(new_work_order_other_profile) if new_work_order_other_profile 183 183 AttachmentPermissionProfile.create(new_attachment_other_profile) if new_attachment_other_profile … … 190 190 # Returns true if none of the permission profiles for data +data+ has permission to +operation+ true. 191 191 # +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. 193 193 # 194 # Example: object.none_can(:read, : reports)194 # Example: object.none_can(:read, :forms) 195 195 def none_can(operation, data) 196 196 attribute = case operation … … 206 206 207 207 case data 208 when : reports209 self. report_permission_profiles.each { |p|208 when :forms 209 self.form_permission_profiles.each { |p| 210 210 if p.send(attribute) 211 211 return false … … 298 298 299 299 if childs_group.update_attributes(:edit => self.edit, 300 :create_ report => self.create_report,300 :create_form => self.create_form, 301 301 :create_work_order => self.create_work_order, 302 302 :attach_file => self.attach_file, branches/workorder_report_rename/app/models/form_permission_profile.rb
r24 r441 2 2 # See license agreement for additional rights 3 3 4 class ReportPermissionProfile < DataPermissionProfile4 class FormPermissionProfile < DataPermissionProfile 5 5 # Protection 6 6 parents_protector_and_protected_fields … … 13 13 14 14 15 # Reportdoesn't have state15 # Form doesn't have state 16 16 HAS_CHANGE_STATE = false 17 17 end branches/workorder_report_rename/app/models/form_type.rb
r368 r441 2 2 # See license agreement for additional rights 3 3 4 class ReportType < ActiveRecord::Base4 class FormType < ActiveRecord::Base 5 5 # Protection 6 6 attr_protected :id, :editable, :ready … … 12 12 before_save :create_html_template_and_images 13 13 # Associations 14 has_one : report_template, :dependent => :destroy15 has_one : report_html_template, :dependent => :destroy16 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 : reports19 has_many : report_template_images, :dependent => :destroy20 belongs_to : report_category14 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 :forms 19 has_many :form_template_images, :dependent => :destroy 20 belongs_to :form_category 21 21 22 exports_automatic_fields_from : report_category23 exports_automatic_field 'cmms_ report_type_name', :string, :name22 exports_automatic_fields_from :form_category 23 exports_automatic_field 'cmms_form_type_name', :string, :name 24 24 25 # Returns the name of the category this reporttype belongs to.25 # Returns the name of the category this form type belongs to. 26 26 def category_name 27 return self. report_category.name27 return self.form_category.name 28 28 end 29 29 30 # Returns the number of reports of this reporttype31 def report_count32 return self. reports.count30 # Returns the number of forms of this form type 31 def form_count 32 return self.forms.count 33 33 end 34 34 … … 38 38 return unless declarations and declarations != [] 39 39 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) 41 41 } 42 42 end … … 46 46 return unless declarations and declarations != [] 47 47 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) 49 49 } 50 50 end 51 51 52 # Returns fields which should be displayed in the reportlist52 # Returns fields which should be displayed in the form list 53 53 # 54 54 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]) 56 56 end 57 57 58 # Creates a empty report for this reporttype58 # Creates a empty form for this form type 59 59 # 60 def create_empty_ report(asset)61 raise 'Unable to create assetless report' unless asset62 new_ report = Report.new63 new_ report.report_type = self64 new_ report.asset = asset65 self. report_fields.each { |f|66 field_value = f.create_field_value(new_ report)67 new_ report.report_field_values << field_value if field_value60 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 68 68 } 69 return new_ report69 return new_form 70 70 end 71 71 72 72 73 # Extracts keywords from all reports the user is authorized to read74 # in the report types with ids in +report_type_ids+.75 # + report_type_ids+:: List or array of ids for the reporttypes76 # from whose reports the keywords are extracted.77 # +condition+:: Condition(s) specifying from which reports the73 # 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 78 78 # keywords are extracted. 79 79 # 80 80 # Return value is an array of unique words. 81 # If no reporttype ids is given, an empty array is returned81 # If no form type ids is given, an empty array is returned 82 82 # 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? 85 85 if condition 86 86 if condition.is_a?(Array) 87 87 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) 90 90 else 91 conditions = ["(#{condition}) AND report_type_id IN (?)", report_type_ids]91 conditions = ["(#{condition}) AND form_type_id IN (?)", form_type_ids] 92 92 end 93 93 else 94 conditions = [' report_type_id IN (?)', report_type_ids]94 conditions = ['form_type_id IN (?)', form_type_ids] 95 95 end 96 96 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| 99 99 keywords += value.get_keywords 100 100 } … … 103 103 end 104 104 105 # Searchs reports matching +search_string+ from all reports in the reporttype the user is authorized to read.106 # +search_string+:: String containing words which are searched from the reports107 # +args+:: Further specifies the reports to search. Use the same arguments as for Report.find105 # 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 forms 107 # +args+:: Further specifies the forms to search. Use the same arguments as for Form.find 108 108 # 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. 111 111 # 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) 118 118 } 119 elsif reports.is_a?(Report)120 return reports.matches_search_string?(search_string) ? reports : nil119 elsif forms.is_a?(Form) 120 return forms.matches_search_string?(search_string) ? forms : nil 121 121 else 122 122 return nil … … 124 124 end 125 125 126 # Creates HTML report template for this reporttype when it doesn't have HTML template.127 # With true +update_existing+ the existing HTML template is updated to correspond to the current reporttemplate.128 # As a before_save callback creates HTML template when report type is saved for the first time after reporttemplate 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. 129 129 def create_html_template_and_images(update_existing = false) 130 return true unless self. report_template131 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) 133 133 elsif update_existing 134 html_template = self. report_html_template134 html_template = self.form_html_template 135 135 else 136 136 return true 137 137 end 138 style, body = self.convert_ report_template_to_html138 style, body = self.convert_form_template_to_html 139 139 html_template.style = style.to_s 140 140 html_template.body = body.to_s 141 141 begin 142 ReportHtmlTemplate.transaction do143 ReportTemplateImage.transaction do142 FormHtmlTemplate.transaction do 143 FormTemplateImage.transaction do 144 144 html_template.save! 145 145 self.save_template_images! … … 147 147 end 148 148 rescue ActiveRecord::RecordNotSaved 149 _error("Unable to create HTML template and template images for reporttype (id=#{self.id}) \"#{self.name}\"")149 _error("Unable to create HTML template and template images for form type (id=#{self.id}) \"#{self.name}\"") 150 150 return false 151 151 end … … 154 154 protected 155 155 156 def convert_ report_template_to_html157 raise 'Template does not exist' unless self. report_template158 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| 159 159 field.to_user_field 160 160 }.compact 161 template = self. report_template.clone161 template = self.form_template.clone 162 162 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) 164 164 end 165 165 166 # Save images in reporttemplate to database.166 # Save images in form template to database. 167 167 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)) 170 170 } 171 171 end branches/workorder_report_rename/app/models/user_group.rb
r24 r441 15 15 has_many :assets_user_groups, :dependent => :destroy 16 16 has_many :assets, :through => :assets_user_groups 17 has_many : report_permissions, :dependent => :delete_all18 # has_many : reports, :through => :report_permissions17 has_many :form_permissions, :dependent => :delete_all 18 # has_many :forms, :through => :form_permissions 19 19 has_many :work_order_permissions, :dependent => :delete_all 20 20 # has_many :work_orders, :through => :work_order_permissions branches/workorder_report_rename/config/menu.rb
r351 r441 34 34 Page.new('attachment', 'select_asset') 35 35 ), 36 MenuButton.new(_(' Reports'), '22x22/menu_report.gif',36 MenuButton.new(_('Forms'), '22x22/menu_form.gif', 37 37 MenuItem.new(_('List'), 'report', 'list', '16x16/list.gif'), 38 38 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 1 1 class VersionOneDotTwoTables < ActiveRecord::Migration 2 @@report_tables = [ 3 'reports', 4 'report_field_values' 5 ] 6 2 7 def self.up 8 rename_tables('report', 'form', @@report_tables) 9 3 10 use_limit_for_binary_columns = ['MySQL'].include?(ActiveRecord::Base.connection.adapter_name) 4 11 5 # Enumerated reportfields12 # Enumerated form fields 6 13 create_table 'enumerated_values', :force => true do |t| 7 t.column ' report_field_id', :integer, :references => :report_fields14 t.column 'form_field_id', :integer, :references => :form_fields 8 15 t.column 'value', :string, :limit => 40 9 16 end 10 add_column ' report_field_values', 'enumerated_value_id', :integer, :references => :enumerated_values17 add_column 'form_field_values', 'enumerated_value_id', :integer, :references => :enumerated_values 11 18 12 # Work order & reportjoining13 add_column ' reports', 'cause_work_order_id', :integer, :references => :work_orders14 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'] 15 22 16 add_column 'work_orders', 'cause_ report_id', :integer, :references => :reports17 add_index 'work_orders', ['cause_ report_id']23 add_column 'work_orders', 'cause_form_id', :integer, :references => :forms 24 add_index 'work_orders', ['cause_form_id'] 18 25 19 26 # Tags for data types - Constitutes causal chains for asset data. … … 23 30 t.column 'cause_tag_id', :integer, :references => :data_type_tags 24 31 end 25 add_column ' report_categories', 'report_tag_id', :integer, :references => :data_type_tags26 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'] 27 34 add_column 'work_order_types', 'work_order_tag_id', :integer, :references => :data_type_tags 28 35 add_index 'work_order_types', ['work_order_tag_id'] … … 34 41 TempAsset.set_created_at(Time.now) 35 42 36 # Join table for reports and work orders is no longer used!43 # Join table for forms and work orders is no longer used! 37 44 # 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' 39 46 40 47 # Asset fields … … 62 69 add_index 'asset_field_values', ['asset_field_id'] 63 70 64 # HTML report templates for reporttypes65 create_table ' report_html_templates', :force => true do |t|66 t.column ' report_type_id', :integer71 # HTML form templates for form types 72 create_table 'form_html_templates', :force => true do |t| 73 t.column 'form_type_id', :integer 67 74 if use_limit_for_binary_columns 68 t.column 'style', :binary, :limit => ReportHtmlTemplate::MAX_STYLE_SIZE69 t.column 'body', :binary, :limit => ReportHtmlTemplate::MAX_BODY_SIZE75 t.column 'style', :binary, :limit => FormHtmlTemplate::MAX_STYLE_SIZE 76 t.column 'body', :binary, :limit => FormHtmlTemplate::MAX_BODY_SIZE 70 77 else 71 78 t.column 'style', :binary … … 73 80 end 74 81 end 75 add_index ' report_html_templates', ['report_type_id']76 # Reporttemplate images77 create_table ' report_template_images', :force => true do |t|78 t.column ' report_type_id', :integer82 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 79 86 t.column 'content_type', :text 80 87 t.column 'name', :text … … 82 89 t.column 'is_image_field', :boolean, :default => false 83 90 if use_limit_for_binary_columns 84 t.column 'data', :binary, :limit => ReportTemplateImage::MAX_SIZE91 t.column 'data', :binary, :limit => FormTemplateImage::MAX_SIZE 85 92 else 86 93 t.column 'data', :binary 87 94 end 88 95 end 89 add_index ' report_template_images', ['report_type_id']96 add_index 'form_template_images', ['form_type_id'] 90 97 # If faster image quering is needed try indexing the name columns too! 91 98 end … … 93 100 def self.down 94 101 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' 99 106 remove_column 'work_order_types', 'work_order_tag_id' 100 107 drop_table 'data_type_tags' 101 108 remove_column 'asset_types', 'typical_maintenance_time_interval' 102 109 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 105 133 end 106 134 end branches/workorder_report_rename/test/unit/form_test.rb
r368 r441 1 1 require File.dirname(__FILE__) + '/../test_helper' 2 2 3 class ReportTest < Test::Unit::TestCase3 class FormTest < Test::Unit::TestCase 4 4 def setup 5 5 # 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. 7 7 user = create_user('test') 8 8 User.current_user = user … … 11 11 @root_asset = Asset.create!(:name => 'Test', :code => 'TST') 12 12 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) 14 14 15 # Reporttype with all user field types16 @ 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! 29 29 30 # First test report31 @ report = @report_type.create_empty_report(@root_asset)32 @ report.ready = true33 @ report.save!34 @ report.update_automatic_field_values30 # 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 35 35 36 # Second test report37 @ report2 = @report_type.create_empty_report(@root_asset)38 @ report2.ready = true39 @ report2.save!36 # Second test form 37 @form2 = @form_type.create_empty_form(@root_asset) 38 @form2.ready = true 39 @form2.save! 40 40 41 41 # 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.reload49 @ report2.reload42 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 50 50 end 51 51 52 52 # Testing instance method value_of 53 53 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) 57 57 assert_equal 'Foobar', value 58 58 end … … 60 60 # Checks that value_of returns the right default "" if the field does not exist 61 61 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) 63 63 assert_not_nil field 64 value = @ report.value_of(field)64 value = @form.value_of(field) 65 65 assert_equal "", value 66 66 end … … 68 68 def test_automatic_field_values 69 69 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'], 72 72 ['TST', 'cmms_asset_full_code'] 73 73 ] 74 74 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)).value75 assert_equal expected_value, @form.get_field_value_for(@form_type.form_fields.find_by_user_field_name(automatic_field_name)).value 76 76 end 77 77 end 78 78 79 79 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.id80 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 84 84 85 85 # 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) 88 88 end 89 89 90 90 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.reload91 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 96 96 97 97 right_keywords = ['some' , 'text', 'other', 'and', 'something', 'tst', 'type', 'test'] 98 keywords = @ report.get_keywords98 keywords = @form.get_keywords 99 99 100 100 assert_equal right_keywords.uniq.sort, keywords.uniq.sort … … 107 107 part_of_word2 = "som" 108 108 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) 113 113 # and these should not 114 114 word1 = "abc" 115 115 word2 = "foobar" 116 116 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) 120 120 end 121 121 … … 124 124 string1 = "Some text" 125 125 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) 128 128 # and these should not 129 129 string3 = "Have at you!" 130 130 string4 = "None shall pass!" 131 131 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) 135 135 end 136 136 137 def test_ Report_search137 def test_Form_search 138 138 str1 = 'fungerar intte' # not found 139 139 str2 = 'Som other' # found in id1 … … 141 141 str4 = 'o' # found in both 142 142 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) 151 151 152 152 # 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") 158 158 end 159 159 160 def test_ Report_created_at161 old_ report = Report.find TempReport.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id162 oldest = Report.created_at(:min)163 assert_equal old_ report.created_at, oldest164 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]) 165 165 166 report = create_report(@root_asset, true)167 youngest = Report.created_at(:max)168 assert_equal report.created_at, youngest169 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]) 170 170 171 Temp Report.destroy_all171 TempForm.destroy_all 172 172 # 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) 175 175 176 176 # 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).id178 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) 180 180 end 181 181 182 def test_ Report_year_span183 old_ report = Report.find TempReport.create!(:created_at => 5.years.ago, :asset_id => @root_asset.id, :ready => true).id184 assert_equal [old_ report.created_at.year, @report.created_at.year], Report.year_span182 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 185 185 end 186 186