Module: ModelMixins::ImportCsvClassMethods
- Defined in:
- lib/model_mixins/import_csv_class_methods.rb
Instance Method Summary collapse
- #build_header_from_csv(row, white_list) ⇒ Object
- #build_record_from_csv(row, header, white_list) ⇒ Object
- #get_white_list(import_settings = nil) ⇒ Object
Instance Method Details
#build_header_from_csv(row, white_list) ⇒ Object
33 34 35 |
# File 'lib/model_mixins/import_csv_class_methods.rb', line 33 def build_header_from_csv(row, white_list) header = row end |
#build_record_from_csv(row, header, white_list) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/model_mixins/import_csv_class_methods.rb', line 3 def build_record_from_csv(row, header, white_list) record_hash = {} row.each_with_index do |value, index| unless value.blank? #case header[index] # when "name" # subject = AcquisitionSubject.find_by_name(value) # record_hash[:acquisition_subject_id] = subject.id if !subject.blank? && white_list.include?("acquisition_subject_id") # when "acquisition_subject_status" # status = AcquisitionSubject.status_id(value) # record_hash[header[index]] = status.to_s.force_encoding("UTF-8") if !status.blank? && white_list.include?(header[index]) # when "price", "end_price_on_square_m", "payed_in_deposit", "area" # value.gsub!(",", ".") # desetinna tecka # numbers = value.scan /[-+]?\d*\.?\d+/ # price = numbers*"" # record_hash[header[index]] = price.force_encoding("UTF-8") if !price.blank? && white_list.include?(header[index]) # when "code" # currency = Currency.find_by_code(value) # record_hash[:currency_id] = currency.id if !currency.blank? && white_list.include?("currency_id") # when "date_of_signature", "maturity_by_ks", "end_date_by_ks" # datetime = DateTime.parse(value) # record_hash[header[index]] = datetime if !datetime.blank? && white_list.include?(header[index]) # else record_hash[header[index]] = value.force_encoding("UTF-8") if white_list.include?(header[index]) #end end end record_hash end |
#get_white_list(import_settings = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/model_mixins/import_csv_class_methods.rb', line 37 def get_white_list(import_settings = nil) white_list = [] except = import_settings.blank? ? nil : import_settings[:except] only = import_settings.blank? ? nil : import_settings[:only] attr_accessible[:default].each do |v| white_list << v if !v.blank? end unless only.blank? white_list &= only # intersect end unless except.blank? white_list -= except # difference end white_list end |