Sha256: 91aca52ed48cab1c5f20e19a1b6ac4ec1198b73e3ff624953be23872d08da957

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Motor
  module ActiveRecordUtils
    module_function

    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

    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

    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
  end
end

require_relative 'active_record_utils/types'
require_relative 'active_record_utils/fetch_methods'
require_relative 'active_record_utils/defined_scopes_extension'
require_relative 'active_record_utils/active_storage_links_extension'
require_relative 'active_record_utils/active_storage_blob_patch'
require_relative 'active_record_utils/active_record_filter'
require_relative 'active_record_utils/active_record_connection_column_patch'
require_relative 'active_record_utils/action_text_attribute_patch'

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
motor-admin-cstham8-0.4.35 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.34 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.33 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.32 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.31 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.30 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.29 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.28 lib/motor/active_record_utils.rb
motor-admin-cstham8-0.4.27 lib/motor/active_record_utils.rb