Module: Motor::ActiveRecordUtils

Defined in:
lib/motor/active_record_utils.rb,
lib/motor/active_record_utils/types.rb,
lib/motor/active_record_utils/fetch_methods.rb,
lib/motor/active_record_utils/defined_scopes_extension.rb,
lib/motor/active_record_utils/active_storage_blob_patch.rb,
lib/motor/active_record_utils/active_storage_links_extension.rb

Defined Under Namespace

Modules: ActiveStorageBlobPatch, ActiveStorageLinksExtension, DefinedScopesExtension, FetchMethods, Types

Class Method Summary collapse

Class Method Details

.generate_csv_for_relation(relation, reset_limit: false) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/motor/active_record_utils.rb', line 16

def generate_csv_for_relation(relation, reset_limit: false)
  relation = relation.limit(nil).offset(nil) if reset_limit

  result = load_query_for_csv(relation)

  CSV.generate do |csv|
    csv << result.columns

    result.rows.each { |row| csv << row }
  end
end

.load_query_for_csv(relation) ⇒ Object


28
29
30
31
32
33
34
# File 'lib/motor/active_record_utils.rb', line 28

def load_query_for_csv(relation)
  model_name = relation.klass.model_name.human(count: :many, default: relation.klass.name.titleize.pluralize)

  query = Motor::Query.find_by(name: "Export #{model_name}")

  relation.klass.connection.exec_query(query&.sql_body || relation.to_sql)
end

.reset_id_sequence!(model) ⇒ Object


7
8
9
10
11
12
13
14
# File 'lib/motor/active_record_utils.rb', line 7

def reset_id_sequence!(model)
  case ActiveRecord::Base.connection.class.name
  when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
    ActiveRecord::Base.connection.reset_pk_sequence!(model.table_name)
  else
    ActiveRecord::Base.connection.reset_sequence!(model.table_name, 'id')
  end
end