Module: ModelToGooglesheet::Export::ClassMethods

Defined in:
lib/model_to_googlesheet/export.rb

Constant Summary collapse

BATCH_SIZE =

google drive has trouble with saving about 2000 of rows right away, so we’re saving them in batches. we may raise batch size up to about 1000.

500

Instance Method Summary collapse

Instance Method Details

#export_to_googlesheet(permethod_options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/model_to_googlesheet/export.rb', line 78

def export_to_googlesheet permethod_options={}
	options = Configuration.merge_configs permethod_options, model_to_googlesheet_configuration
	
	session = GoogleDrive::Session.new_for_gs({
		client_id: options[:client_id], 
		client_secret: options[:client_secret], 
		refresh_token: options[:refresh_token]
	})
	ss = session.get_or_create_ss options[:spreadsheet]
	ws = ss.create_or_recreate_ws options[:worksheet]

	amount_of_batches_to_skip = self.count/BATCH_SIZE
	(0..amount_of_batches_to_skip).each do |skip_n_batches|
		records = limit(BATCH_SIZE).offset(skip_n_batches*BATCH_SIZE)

		-> {
			records.each do |record|
				record_hash = record.get_exportable_hash options[:convert_with]
				ws.export_hash record_hash, update: false, find_by: false
			end
			ws.save
		}.rescue(2)


	end


end