Module: PostJson::Copyable::ClassMethods

Defined in:
lib/post_json/concerns/copyable.rb

Instance Method Summary collapse

Instance Method Details

#copy(destination_collection_name) ⇒ Object


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
32
33
# File 'lib/post_json/concerns/copyable.rb', line 6

def copy(destination_collection_name)
  destination = Collection[destination_collection_name]
  if exists?
    src_id = persisted_settings.id

    if destination.persisted?
      dest_id = destination.persisted_settings.id
      query = original_all
      query = query.joins("INNER JOIN #{table_name} as dest ON dest.id = #{table_name}.id")
      query = query.where("dest.__doc__model_settings_id = '#{dest_id}'")
      query = query.where("\"#{table_name}\".__doc__model_settings_id = '#{src_id}'")
      query = query.where("dest.id = \"#{table_name}\".id")
      conflicting_ids = query.pluck("dest.id").join(", ")
      if conflicting_ids.present?
        error_message = "Following primary keys (#{primary_key}) already exists in collection \"#{destination.collection_name}\": #{conflicting_ids}."
        raise ActiveRecord::RecordNotUnique, error_message
      end
    end

    dest_id = destination.persisted_settings.id
    selectors = PostJson::Base.column_names.map { |s| s == "__doc__model_settings_id" ? "'#{dest_id}' as #{s}" : s }.join(", ")
    condition = "__doc__model_settings_id = '#{src_id}'"
    destination.transaction do
      destination.connection.execute("INSERT INTO #{table_name} (SELECT #{selectors} FROM #{table_name} WHERE #{condition})")
    end
  end
  destination
end