Skip to content

Instantly share code, notes, and snippets.

@jonny-gates
jonny-gates / rails_quiz_correction.rb
Created February 23, 2020 10:34
Rails Quiz Correction
# Q1 - How do you create a Rails app?
# TODO: rails?
rails new project-name --database=postgresql
# Q2 - How do you start coding a Rails project? Give the right sequence.
# '1. Coding the views?'
# '2. Coding the controllers?'
# '3. Coding the models?'
# TODO: 1, 2, 3?
3,2,1
@jonny-gates
jonny-gates / task.rb
Created January 31, 2020 18:17
SQL-CRUD Livecode
require 'pry-byebug'
class Task
attr_reader :id
attr_accessor :title, :description, :done
def initialize(attributes = {})
@id = attributes[:id]
@title = attributes[:title]
@description = attributes[:description]
@done = attributes[:done] || 0
@jonny-gates
jonny-gates / examples.rb
Created January 31, 2020 09:50
CRUD SQL
# http://airbnb.com/flats/42
SELECT * FROM flats
WHERE id = 42
# https://github.com/users/jonnygates
SELECT * FROM users
WHERE username = 'jonnygates'
@jonny-gates
jonny-gates / controller.rb
Created January 24, 2020 19:34
Cookbook Recap
require_relative 'view'
require_relative 'recipe'
class Controller
def initialize(cookbook)
@cookbook = cookbook
@view = View.new
end
def list
@jonny-gates
jonny-gates / app.rb
Created January 24, 2020 10:45
ToDo Manager
require_relative 'view'
require_relative 'repo'
require_relative 'controller'
require_relative 'router'
view = View.new
repo = Repo.new
controller = Controller.new(view, repo)
router = Router.new(controller)
require_relative 'app/repositories/meal_repository'
require_relative 'app/controllers/meals_controller'
require_relative 'router'
csv_file = 'data/meals.csv'
meal_repository = MealRepository.new(csv_file)
meals_controller = MealsController.new(meal_repository)
router = Router.new(meals_controller)
require_relative 'patient'
class Room
attr_accessor :id
class RoomCapacityError < Exception; end
# STATE
# capacity - integer
# clean - boolean
# patients - array
@jonny-gates
jonny-gates / app.rb
Created October 20, 2019 16:49
cookbook-day-1-recap
require_relative 'cookbook' # You need to create this file!
require_relative 'controller' # You need to create this file!
require_relative 'router'
csv_file = File.join(__dir__, 'recipes.csv')
cookbook = Cookbook.new(csv_file)
controller = Controller.new(cookbook)
router = Router.new(controller)
@jonny-gates
jonny-gates / repository.rb
Created October 18, 2019 09:40
to-do-manager
class Repository
def initialize
@tasks = []
end
def add(task)
@tasks << task
end
def all
require "sqlite3"
db = SQLite3::Database.new("chinook.sqlite")
# List all customers (name + email), ordered alphabetically (no extra information)
def query_1(db)
query = <<-SQL
SELECT first_name, last_name, email
FROM customers
ORDER BY first_name asc
SQL