Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created February 15, 2021 13:58
Show Gist options
  • Save pdbradley/a83ba64c131017525a15de6562cb55c6 to your computer and use it in GitHub Desktop.
Save pdbradley/a83ba64c131017525a15de6562cb55c6 to your computer and use it in GitHub Desktop.
Song Sheet wrapper ideas
# dinuska this is untested, just typed in, but it may give you some idea
# of how you might encapsulate a google sheet or link it to records
# in your database. I'm sure i'm getting some details wrong
# so feel free of course to reject all or part of this ha ha
require "google_drive"
class SongSheet
KEY = 'not sure how you auth to get the google sheet data?'
def self.all
# returns all rows from sheet as an array of record objects with accessors
sheet_rows.map{|row| SongSheetRecord.new(row) }
end
def self.find_by_name(name)
# returns one record from the sheet
# could just be something like:
all.find{|s| s.name == name}
end
def self.find_by_id(id)
# returns one record from the sheet
# could just be something like:
all.find{|s| s.id == id}
end
def self.session
GoogleDrive::Session.from_config("config.json")
end
def self.sheet
session.spreadsheet_by_key(KEY).worksheets[0]
end
def self.sheet_rows
sheet.rows
end
end
class SongSheetRecord
attr_reader :name, :id, :language
def initialize(row)
@row = row
fields_from_row
end
def fields_from_row
@id = @row[0]
@name = @row[1]
@language = @row[2]
end
end
class Song < ApplicationRecord
# add methods like this to your song model
def sheet_song # to find the corresponding sheet row
SongSheet.find_by_id(self.id)
end
end
@pdbradley
Copy link
Author

Then if you want to pull stuff from the google sheet into your view, you can do stuff like

<%= @song.sheet_song.language %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment