Merge branch 'pages' of https://github.com/hackclub/highway into pages

This commit is contained in:
acon 2025-05-07 21:27:13 -04:00
commit b04962bb04
15 changed files with 162 additions and 3 deletions

View file

@ -0,0 +1,10 @@
# app/controllers/prizes_controller.rb
class PrizesController < ApplicationController
before_action :require_authentication
def add_to_box
prize = Prize.find(params[:id])
current_user.user_prizes.create!(prize: prize)
redirect_to prizes_path, notice: "Added to your prize box!"
end
end

View file

@ -0,0 +1,9 @@
class UserPrizesController < ApplicationController
before_action :require_authentication
def claim
user_prize = current_user.user_prizes.find(params[:id])
user_prize.claim!
redirect_to prize_box_path, notice: "Prize claimed! Tracking: #{user_prize.tracking_number}"
end
end

View file

@ -1,7 +1,7 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update ]
before_action :require_authentication, only: [:show, :edit, :update]
before_action :require_authentication, only: [:show, :edit, :update, :prize_box]
# def index
@ -34,6 +34,10 @@ class UsersController < ApplicationController
render :edit, status: :unprocessable_entity
end
end
def prize_box
# No need to set anything special - the view will use current_user
end
private

5
site/app/models/prize.rb Normal file
View file

@ -0,0 +1,5 @@
class Prize < ApplicationRecord
has_one_attached :image # Stores prize images
has_many :user_prizes
has_many :users, through: :user_prizes
end

View file

@ -3,10 +3,15 @@ class User < ApplicationRecord
validates :email, uniqueness: true, presence: true, allow_nil: false
has_many :posts, dependent: :destroy
has_many :projects, dependent: :destroy
has_many :user_prizes, dependent: :destroy
has_many :prizes, through: :user_prizes
def name
"#{first_name} #{last_name}"
end
def prize_box
user_prizes.where(claimed: false)
end
end

View file

@ -0,0 +1,16 @@
class UserPrize < ApplicationRecord
belongs_to :user
belongs_to :prize
scope :unclaimed, -> { where(claimed: false) }
scope :claimed, -> { where(claimed: true) }
def claim!
update!(claimed: true, claimed_at: Time.current, tracking_number: generate_tracking_number)
end
private
def generate_tracking_number
"TN-#{SecureRandom.alphanumeric(10).upcase}"
end
end

View file

@ -0,0 +1,9 @@
<%# app/views/users/prize_box.html.erb %>
<h1>Your Prize Box</h1>
<% current_user.prize_box.each do |user_prize| %>
<div class="prize">
<%= image_tag user_prize.prize.image %>
<%= button_to "Claim", claim_prize_path(user_prize), method: :patch %>
</div>
<% end %>

View file

@ -38,5 +38,20 @@ Rails.application.routes.draw do
mount LetterOpenerWeb::Engine, at: "/letter_opener"
end
## prize shit
resources :prizes do
member do
post :add_to_box
end
end
resources :user_prizes, only: [] do
member do
patch :claim
end
end
get '/prize_box', to: 'users#prize_box', as: :prize_box
end

View file

@ -0,0 +1,11 @@
class CreatePrizes < ActiveRecord::Migration[8.0]
def change
create_table :prizes do |t|
t.string :name
t.string :sku
t.boolean :claimable
t.timestamps
end
end
end

View file

@ -0,0 +1,13 @@
class CreateUserPrizes < ActiveRecord::Migration[8.0]
def change
create_table :user_prizes do |t|
t.references :user, null: false, foreign_key: true
t.references :prize, null: false, foreign_key: true
t.boolean :claimed
t.datetime :claimed_at
t.string :tracking_number
t.timestamps
end
end
end

24
site/db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_05_05_203646) do
ActiveRecord::Schema[8.0].define(version: 2025_05_06_165025) do
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
@ -60,6 +60,14 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_05_203646) do
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "prizes", force: :cascade do |t|
t.string "name"
t.string "sku"
t.boolean "claimable"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "projects", force: :cascade do |t|
t.string "kitted_guide"
t.string "github_repo"
@ -73,6 +81,18 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_05_203646) do
t.index ["user_id"], name: "index_projects_on_user_id"
end
create_table "user_prizes", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "prize_id", null: false
t.boolean "claimed"
t.datetime "claimed_at"
t.string "tracking_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["prize_id"], name: "index_user_prizes_on_prize_id"
t.index ["user_id"], name: "index_user_prizes_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email"
t.string "login_code"
@ -91,4 +111,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_05_203646) do
add_foreign_key "posts", "projects"
add_foreign_key "posts", "users"
add_foreign_key "projects", "users"
add_foreign_key "user_prizes", "prizes"
add_foreign_key "user_prizes", "users"
end

11
site/test/fixtures/prizes.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
sku: MyString
claimable: false
two:
name: MyString
sku: MyString
claimable: false

15
site/test/fixtures/user_prizes.yml vendored Normal file
View file

@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user: one
prize: one
claimed: false
claimed_at: 2025-05-06 12:50:25
tracking_number: MyString
two:
user: two
prize: two
claimed: false
claimed_at: 2025-05-06 12:50:25
tracking_number: MyString

View file

@ -0,0 +1,7 @@
require "test_helper"
class PrizeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class UserPrizeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end