Create model for votes with secure_id, title, and description

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent 5e8cda6fc0
commit f7f1807839
9 changed files with 106 additions and 10 deletions

View file

@ -1,4 +1,9 @@
source "http://rubygems.org"
gem "sinatra"
gem "bcrypt"
gem 'sinatra'
gem 'sinatra-activerecord'
gem 'rake'
gem 'rackup'
gem 'puma'
gem 'sqlite3'
gem 'bcrypt'

2
Rakefile Normal file
View file

@ -0,0 +1,2 @@
require './vote'
require 'sinatra/activerecord/rake'

3
config/database.yml Normal file
View file

@ -0,0 +1,3 @@
development:
adapter: sqlite3
database: db/vote.sqlite3

View file

@ -0,0 +1,11 @@
class CreateVotes < ActiveRecord::Migration[7.2]
def change
create_table :votes do |t|
t.string :secure_id
t.string :title
t.text :description
t.datetime :expire_on
t.timestamps null: false
end
end
end

22
db/schema.rb Normal file
View file

@ -0,0 +1,22 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_03_21_213011) do
create_table "votes", force: :cascade do |t|
t.string "secure_id"
t.string "title"
t.text "description"
t.datetime "expire_on"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View file

@ -1,5 +1,19 @@
<h1>Home</h1>
<p>Hello, <%= current_user.email %>.</p>
<form action="/logout" method="POST">
<input type="submit" value="Log out" />
<h2>Votes</h2>
<% @votes.each do |vote| %>
<li>
<a href="/votes/<%= vote.secure_id %>"><%= vote.title %></a>
</li>
<% end %>
<form action="/votes/new" method="get">
<button type="submit">Create new vote</button>
</form>
<form action="/logout" method="post">
<button type="submit">Log out</button>
</form>

13
views/votes_new.erb Normal file
View file

@ -0,0 +1,13 @@
<h1>New vote</h1>
<form action="/votes" method="post">
<p>
<label for="title">Title</label>
<input type="text" name="title">
</p>
<p>
<label for="description">Description</label>
<input type="text" name="description">
</p>
<button type="submit">Create new vote</button>
</form>

3
views/votes_show.erb Normal file
View file

@ -0,0 +1,3 @@
<h1><%= @vote.title %></h1>
<p><%= @vote.description %></p>

35
vote.rb
View file

@ -1,5 +1,14 @@
require 'bundler/setup'
require 'sinatra'
require 'sinatra/activerecord'
require 'bcrypt'
require 'securerandom'
#set :database, 'sqlite3:db/vote.sqlite3'
class Vote < ActiveRecord::Base
self.primary_key = "secure_id"
end
def hash_password(password)
BCrypt::Password.create(password).to_s
@ -19,11 +28,9 @@ USERS = [
enable :sessions
get '/' do
if current_user
erb :home
else
redirect '/login'
end
redirect '/login' unless current_user
@votes = Vote.all
erb :home
end
get '/login' do
@ -31,7 +38,6 @@ get '/login' do
end
post '/login' do
puts params
user = USERS.find { |u| u.email == params[:email] }
if user && verify_password(params[:password], user.password_hash)
session.clear
@ -48,6 +54,23 @@ post '/logout' do
redirect '/login'
end
get '/votes/new' do
erb :votes_new
end
get '/votes/:id' do
@vote = Vote.find(params[:id])
erb :votes_show
end
post '/votes' do
secure_id = SecureRandom.hex(8)
@vote = Vote.create(secure_id: secure_id,
title: params[:title],
description: params[:description])
redirect '/votes/' + secure_id
end
helpers do
def current_user
if session[:user_id]