-
Notifications
You must be signed in to change notification settings - Fork 0
IIIRR-29 Update for issue in tables and prevent answer duplication #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,23 +40,29 @@ def handle(event) | |
return | ||
end | ||
|
||
server = Server.find_or_initialize_by(server_id: event.server.id) | ||
|
||
# Create the answer record | ||
answer = Answer.create!( | ||
answer = Answer.create( | ||
puzzle_id: puzzle.id, | ||
user_id: user.id, | ||
server_id: server.id, | ||
choice: answer, # 'ruby' or 'rails' | ||
is_correct: puzzle.answer.to_s == answer # Correct answer check | ||
) | ||
|
||
# Respond to the user with the result | ||
event.respond( | ||
content: nil, | ||
embeds: [ answer_embed(answer, puzzle) ], | ||
ephemeral: true # Only the user sees this message | ||
) | ||
if answer | ||
# Respond to the user with the result | ||
event.respond( | ||
content: nil, | ||
embeds: [ answer_embed(answer, puzzle) ], | ||
ephemeral: true # Only the user sees this message | ||
) | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After looking further into this it seems as though |
||
# If the user has already answered, prevent them from changing their answer | ||
event.respond( | ||
content: "You have already answered this puzzle. You cannot change your answer.", | ||
ephemeral: true # Only the user sees this message | ||
) | ||
nil | ||
end | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class Answer < ApplicationRecord | ||
belongs_to :puzzle | ||
belongs_to :user | ||
|
||
validates :puzzle_id, uniqueness: { scope: [ :user_id, :server_id ] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an extra layer of validation |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class Server < ApplicationRecord | ||
has_and_belongs_to_many :users, join_table: "users_servers" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure how this was working before |
||
has_one :channel | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
class User < ApplicationRecord | ||
has_and_belongs_to_many :servers | ||
has_many :servers | ||
has_and_belongs_to_many :servers, join_table: "users_servers" | ||
has_many :answers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove repetitive |
||
|
||
enum :role, admin: 0, member: 1 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqueIndexToAnswers < ActiveRecord::Migration[8.0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add migration with db uniqueness for each answer |
||
def change | ||
add_index :answers, [ :puzzle_id, :user_id, :server_id ], unique: true, name: 'index_answers_on_puzzle_user_server' | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,47 @@ | ||
|
||
# db/seeds.rb | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While testing this in the rails console I was having several issues so I updated the seed file to more closely resemble the relationships. |
||
# Create Puzzle records | ||
Puzzle.create!([ | ||
{ question: "Ruby or Rails provided this method? Array.new(5) { |i| i * 2 }", answer: :ruby, explanation: "`Array.new` is a core Ruby method that creates a new array with a specified size and optional block for initialization. This is part of Ruby’s core library." }, | ||
{ question: "Ruby or Rails provided this method? File.open('log.txt', 'w') { |file| file.write('Hello, World!') }", answer: :ruby, explanation: "`File.open` is a core Ruby method for opening files. It’s part of Ruby's standard library for file handling, not part of Rails." }, | ||
{ question: "Ruby or Rails provided this method? render json: @user", answer: :rails, explanation: "`render json:` is a Rails method used to render a JSON response from a controller action." }, | ||
{ question: "Ruby or Rails provided this method? link_to 'Home', root_path", answer: :rails, explanation: "`link_to` is a Rails helper method that generates HTML links. `root_path` is a Rails path helper." }, | ||
{ question: "Ruby or Rails provided this method? params[:id]", answer: :rails, explanation: "`params[:id]` is used in Rails to fetch query parameters or URL parameters in controller actions." } | ||
]) | ||
# ====== Create Puzzle records ====== | ||
puzzles = [ | ||
{ | ||
question: "Ruby or Rails provided this method? Array.new(5) { |i| i * 2 }", | ||
answer: :ruby, | ||
explanation: "`Array.new` is a core Ruby method that creates a new array with a specified size and optional block for initialization. This is part of Ruby’s core library." | ||
}, | ||
{ | ||
question: "Ruby or Rails provided this method? File.open('log.txt', 'w') { |file| file.write('Hello, World!') }", | ||
answer: :ruby, | ||
explanation: "`File.open` is a core Ruby method for opening files. It’s part of Ruby's standard library for file handling, not part of Rails." | ||
}, | ||
{ | ||
question: "Ruby or Rails provided this method? render json: @user", | ||
answer: :rails, | ||
explanation: "`render json:` is a Rails method used to render a JSON response from a controller action." | ||
}, | ||
{ | ||
question: "Ruby or Rails provided this method? link_to 'Home', root_path", | ||
answer: :rails, | ||
explanation: "`link_to` is a Rails helper method that generates HTML links. `root_path` is a Rails path helper." | ||
}, | ||
{ | ||
question: "Ruby or Rails provided this method? params[:id]", | ||
answer: :rails, | ||
explanation: "`params[:id]` is used in Rails to fetch query parameters or URL parameters in controller actions." | ||
} | ||
] | ||
|
||
puzzles.each do |p| | ||
Puzzle.find_or_create_by!(question: p[:question]) do |puzzle| | ||
puzzle.answer = p[:answer] | ||
puzzle.explanation = p[:explanation] | ||
end | ||
end | ||
|
||
# Create a server | ||
Server.create!(server_id: 1179555097060061245, name: "OmbuTest") | ||
# ====== Create the Server ====== | ||
server = Server.find_or_create_by!(server_id: 1179555097060061245) do |s| | ||
s.name = "OmbuTest" | ||
end | ||
|
||
# Seed data for 10 users with answers | ||
# ====== Create Users and associate with Server ====== | ||
users = [ | ||
{ user_id: 101, username: "user1", role: "member" }, | ||
{ user_id: 102, username: "user2", role: "member" }, | ||
|
@@ -28,16 +56,24 @@ | |
] | ||
|
||
users.each do |user_data| | ||
user = User.create(user_id: user_data[:user_id], username: user_data[:username], role: user_data[:role]) | ||
user = User.find_or_create_by!(user_id: user_data[:user_id]) do |u| | ||
u.username = user_data[:username] | ||
u.role = user_data[:role] | ||
end | ||
|
||
# Associate user with the server if not already linked | ||
user.servers << server unless user.servers.include?(server) | ||
|
||
# Randomly assign scores to answers (you can adjust as needed) | ||
3.times do | ||
Answer.create!( | ||
user_id: user.id, | ||
puzzle_id: Puzzle.all.sample.id, | ||
server_id: Server.first.id, | ||
choice: [ "ruby", "rails" ].sample, | ||
is_correct: [ true, false ].sample | ||
) | ||
# Seed random answers for this user if they have none | ||
if user.answers.where(server_id: server.id).empty? | ||
3.times do | ||
Answer.create!( | ||
user_id: user.id, | ||
puzzle_id: Puzzle.all.sample.id, | ||
server_id: server.id, | ||
choice: [ "ruby", "rails" ].sample, | ||
is_correct: [ true, false ].sample | ||
) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this as it was repetitive.