module ModelToGooglesheet::Export

Public Class Methods

included(base) click to toggle source
# File lib/model_to_googlesheet/export.rb, line 13
def self.included(base)
        base.extend ClassMethods
end

Public Instance Methods

delete_from_googlesheet(permethod_options={}) click to toggle source
# File lib/model_to_googlesheet/export.rb, line 37
def delete_from_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.exact_ss options[:spreadsheet]
        return unless ss
        ws = ss.worksheet_by_title options[:worksheet]
        return unless ws

        record_hash = get_exportable_hash options[:convert_with]

        row = ws.row_with_hash record_hash, find_by: options[:find_by]
        return unless row

        row.clear 
        ws.save
end
export_to_googlesheet(permethod_options={}) click to toggle source
# File lib/model_to_googlesheet/export.rb, line 18
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.get_or_create_ws options[:worksheet]

        record_hash = self.get_exportable_hash options[:convert_with]

        ws.export_hash record_hash, 
                update: options[:update], find_by: options[:find_by]

        ws.save
end
get_exportable_hash(convert_with) click to toggle source
# File lib/model_to_googlesheet/export.rb, line 59
def get_exportable_hash convert_with
        ActiveSupport::HashWithIndifferentAccess.new(
                case convert_with 
                when nil 
                        attributes
                when Symbol 
                        self.send convert_with
                when Proc
                        convert_with.call self
                end
        )
end