class Imports::JoinRecords
Attributes
Public Class Methods
Source
# File app/lib/imports/join_records.rb, line 55 def self.call(...) = new(...).call def call return [] if records.blank? join_table_class.import( ["#{records_type.underscore}_id", "#{import_type.underscore}_id"], records.map(&:id).product([import.id]).uniq, on_duplicate_key_ignore: true ) end private # Resolve (or generate) the ActiveRecord model representing the join table. # # @return [Class<ApplicationRecord>] join model class def join_table_class join_table_name = "#{import_type.pluralize}#{records_type}" outer_import_type = @import_type outer_records_type = @records_type join_table_name.safe_constantize || Class.new(ApplicationRecord) do @import_type = outer_import_type @records_type = outer_records_type self.table_name = "#{@import_type.tableize}_#{@records_type.tableize}" def self.model_name ActiveModel::Name.new( self, nil, [@import_type, @records_type].sort.join ) end end end end
Source
# File app/lib/imports/join_records.rb, line 84 def self.model_name ActiveModel::Name.new( self, nil, [@import_type, @records_type].sort.join ) end
Source
# File app/lib/imports/join_records.rb, line 38 def initialize(import, records, records_type: nil) @import = import @records = records # e.g. ClassImport, CohortImport, ImmunisationImport @import_type = import.class.name # We don't do this sooner because it may be useful for debugging to # populate what we can in instances where there are no records to actually # operate on. return if records.blank? # e.g. Patient, Parent, ParentRelationship, VaccinationRecord @records_type = records_type&.classify || records.map(&:class).uniq.sole.name end
Public Instance Methods
Source
# File app/lib/imports/join_records.rb, line 57 def call return [] if records.blank? join_table_class.import( ["#{records_type.underscore}_id", "#{import_type.underscore}_id"], records.map(&:id).product([import.id]).uniq, on_duplicate_key_ignore: true ) end
Source
# File app/lib/imports/join_records.rb, line 72 def join_table_class join_table_name = "#{import_type.pluralize}#{records_type}" outer_import_type = @import_type outer_records_type = @records_type join_table_name.safe_constantize || Class.new(ApplicationRecord) do @import_type = outer_import_type @records_type = outer_records_type self.table_name = "#{@import_type.tableize}_#{@records_type.tableize}" def self.model_name ActiveModel::Name.new( self, nil, [@import_type, @records_type].sort.join ) end end end
Resolve (or generate) the ActiveRecord model representing the join table.
@return [Class<ApplicationRecord>] join model class