Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require:
plugins:
- rubocop-performance

AllCops:
TargetRubyVersion: 3.3.3
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'decanter', '~> 4.0'
gem 'factory_bot_rails', '~> 6.4'
gem 'faker', '~> 3.3'
gem 'figaro', '~> 1.2'
gem 'jbuilder'
gem 'jsonapi-serializer', '~> 2.2'
gem 'lp_token_auth', '~> 2.0'
gem 'paper_trail', '~> 15.1'
Expand Down Expand Up @@ -38,6 +39,7 @@ group :development, :test do
gem 'pry', '~> 0.14.2'
gem 'rspec-rails', '~> 6.0.0'

gem 'byebug'
gem 'debug', platforms: %i[mri mingw x64_mingw]
end

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ GEM
bullet (7.2.0)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
case_transform (0.2)
activesupport
childprocess (5.1.0)
Expand Down Expand Up @@ -159,6 +160,9 @@ GEM
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.10.2)
jsonapi-renderer (0.2.2)
jsonapi-serializer (2.2.0)
Expand Down Expand Up @@ -365,6 +369,7 @@ DEPENDENCIES
binding_of_caller
bootsnap
bullet (~> 7.1)
byebug
cssbundling-rails
database_cleaner-active_record
database_cleaner-redis
Expand All @@ -373,6 +378,7 @@ DEPENDENCIES
factory_bot_rails (~> 6.4)
faker (~> 3.3)
figaro (~> 1.2)
jbuilder
jsonapi-serializer (~> 2.2)
letter_opener
lp_token_auth (~> 2.0)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::API

end
23 changes: 23 additions & 0 deletions app/controllers/attendees_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AttendeesController < ApplicationController
def create
@room = Room.find_by(invite_code: create_attendee_room_params[:invite_code], status: :open)
@attendee = Attendee.new(create_attendee_params.merge(room: @room))

if @attendee.save
render :create, status: :created
else
@errors = @attendee.errors.full_messages
render 'shared/errors', status: :unprocessable_entity
end
end

def create_attendee_params
params.require(:attendee)
.permit(:name)
end

def create_attendee_room_params
params.require(:room)
.permit(:invite_code)
end
end
12 changes: 12 additions & 0 deletions app/controllers/concerns/room_invite_codifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module RoomInviteCodifier
extend ActiveSupport::Concern

def new_room_invite_code
code = nil
while code.nil?
code = SecureRandom.hex(3) # Generate a random 8-character hex string
# Check if the generated code already exists in the database
return code unless Room.where(invite_code: code, status: :open).any?
end
end
end
18 changes: 18 additions & 0 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RoomsController < ApplicationController
include RoomInviteCodifier
def create
@room = Room.new(create_room_params.merge(invite_code: new_room_invite_code, status: :open))
if @room.save
render :create, status: :created
else
@errors = @room.errors.full_messages
render 'shared/errors', status: :unprocessable_entity
end
end

def create_room_params
params
.require(:room)
.permit(:name, venue_ids: [])
end
end
5 changes: 5 additions & 0 deletions app/controllers/venues_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class VenuesController < ApplicationController
def index
@venues = Venue.all
end
end
3 changes: 3 additions & 0 deletions app/models/attendee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Attendee < ApplicationRecord
belongs_to :room
end
8 changes: 8 additions & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Room < ApplicationRecord
has_and_belongs_to_many :venues
has_many :attendees

validates_presence_of :name

enum status: { open: 0, closed: 1 }
end
6 changes: 6 additions & 0 deletions app/models/room_venue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RoomVenue < ApplicationRecord
belongs_to :room
belongs_to :venue

has_many :votes
end
5 changes: 5 additions & 0 deletions app/models/venue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Venue < ApplicationRecord
has_and_belongs_to_many :rooms

enum category: { 'Tex-Mex': 0, Japanese: 1, Italian: 2, Mexican: 3 }
end
2 changes: 2 additions & 0 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Vote < ApplicationRecord
end
7 changes: 7 additions & 0 deletions app/views/attendees/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.attendee do
json.id @attendee.id
json.room_name @attendee.room.name
json.attendees do
json.array! @attendee.room.attendees, :id, :name
end
end
6 changes: 6 additions & 0 deletions app/views/rooms/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.room do
json.name @room.name
json.invite_code @room.invite_code
json.venue_ids @room.venues.map(&:id)
json.status @room.status
end
3 changes: 3 additions & 0 deletions app/views/shared/errors.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @errors do |error_message|
json.message error_message
end
8 changes: 8 additions & 0 deletions app/views/venues/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
json.array! @venues do |venue|
json.name venue.name
json.instagram_link venue.instagram_link
json.menu_link venue.menu_link
json.cost venue.cost
json.rating venue.rating
json.category venue.category
end
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
config.eager_load = false

# Show full error reports.
config.consider_all_requests_local = true
# config.consider_all_requests_local = true

# Enable server timing
config.server_timing = true
Expand Down
14 changes: 6 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Rails.application.routes.draw do

# GET route to know that API is online.
# Limited to json requests - sends a 200 response with empty headers and empty response body
if Rails.env.development?
get '/', to: proc { [200, {}, ['']] }, constraints: { format: 'json' }
constraints format: 'json' do
post '/rooms', to: 'rooms#create'
post 'rooms/:invite', to: 'rooms#join'
post '/venues', to: 'venues#create'
post '/attendees', to: 'attendees#create'
get '/venues', to: 'venues#index'
end



end
29 changes: 29 additions & 0 deletions db/migrate/20250410174025_create_rooms_and_venues.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class CreateRoomsAndVenues < ActiveRecord::Migration[7.1]
def change
create_table :rooms do |t|
t.string :name
t.string :invite_code
t.integer :host_id
t.integer :status

t.timestamps
end

create_table :venues do |t|
t.string :name
t.string :instagram_link
t.string :menu_link
t.integer :cost
t.integer :rating
t.integer :category

t.timestamps
end

create_table :rooms_venues do |t|
# Intentionally keeping the id column to reference at vote time
t.belongs_to :room
t.belongs_to :venue
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20250410174408_create_attendees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateAttendees < ActiveRecord::Migration[7.1]
def change
create_table :attendees do |t|
t.references :room
t.string :name
t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20250410174650_create_votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateVotes < ActiveRecord::Migration[7.1]
def change
create_table :votes do |t|
t.references :rooms_venues, null: false, foreign_key: true
t.references :attendee, null: false, foreign_key: true
t.timestamps
end
end
end
48 changes: 47 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
# Character.create(name: "Luke", movie: movies.first)

raise 'SEEDING NOT ALLOWED IN PRODUCTION' if Rails.env.production?
# Seeds::UserSeeder.new.run

100.times do
FactoryBot::create(:venue)
end

# Seeds::UserSeeder.new.run
14 changes: 14 additions & 0 deletions lib/tasks/rebuild.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace :db do
desc 'Drop, migrate and seed the database'
task rebuild: :environment do
puts 'Dropping database...'
Rake::Task['db:drop'].invoke
puts 'Creating database...'
Rake::Task['db:create'].invoke
puts 'Migrating database...'
Rake::Task['db:migrate'].invoke
puts 'Seeding database...'
Rake::Task['db:seed'].invoke
puts 'Database rebuilt successfully!'
end
end
5 changes: 5 additions & 0 deletions spec/factories/attendees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :attendee do
name { Faker::Name.name }
end
end
6 changes: 6 additions & 0 deletions spec/factories/room_venues.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :room_venue do
association :room
association :venue
end
end
7 changes: 7 additions & 0 deletions spec/factories/rooms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :room do
invite_code { SecureRandom.hex(10) }
venue { venue }
host_id { attendee }
end
end
9 changes: 9 additions & 0 deletions spec/factories/venues.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :venue do
name { Faker::Restaurant.name }
menu_link { 'http://google.com' }
cost { 100 }
rating { 5 }
category { Venue.categories.values.sample }
end
end
5 changes: 5 additions & 0 deletions spec/factories/votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :vote do
room_venue
end
end
Loading
Loading