Skip to content

Commit

Permalink
Rework bootcamp data model
Browse files Browse the repository at this point in the history
  • Loading branch information
iHiD committed Nov 7, 2024
1 parent 84ab64c commit 48ed7e2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
46 changes: 29 additions & 17 deletions app/controllers/bootcamp_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@ def index
end

def start_enrolling
@name = @bootcamp_data&.name || @bootcamp_data&.user&.name
@email = @bootcamp_data&.email || @bootcamp_data&.user&.email
@package = params[:package]
create_bootcamp_data! unless @bootcamp_data

return unless @bootcamp_data && !@bootcamp_data.enrolled?
@name = @bootcamp_data.name || @bootcamp_data&.user&.name
@email = @bootcamp_data.email || @bootcamp_data&.user&.email
@package = params[:package]

@bootcamp_data.started_enrolling_at = Time.current
@bootcamp_data.package = @package
@bootcamp_data.save!
unless @bootcamp_data.enrolled? # rubocop:disable Style/GuardClause
@bootcamp_data.started_enrolling_at = Time.current
@bootcamp_data.package = @package
@bootcamp_data.save!
end
end

def do_enrollment
unless @bootcamp_data
user = User.create!(
name: params[:name],
handle: "bootcamp-#{SecureRandom.hex(8)}",
email: "bootcamp-#{SecureRandom.hex(8)}@exercism.org",
password: SecureRandom.hex(8)
)
@bootcamp_data = user.create_bootcamp_data!
end
create_bootcamp_data! unless @bootcamp_data

@bootcamp_data.update!(
enrolled_at: Time.current,
Expand Down Expand Up @@ -80,7 +74,7 @@ def stripe_session_status
if session.status == 'complete'
@bootcamp_data.update!(
paid_at: Time.current,
payment_intent_id: session.id
checkout_session_id: session.id
)
end

Expand All @@ -94,6 +88,15 @@ def confirmed; end

private
def use_user_bootcamp_data!
if session[:bootcamp_data_id]
begin
@bootcamp_data = User::BootcampData.find(session[:bootcamp_data_id])
return
rescue StandardError
# Continue down the next path if this breaks
end
end

user_id = cookies.signed[:_exercism_user_id]
return unless user_id

Expand All @@ -105,6 +108,15 @@ def use_user_bootcamp_data!
rescue ActiveRecord::RecordNotUnique
@bootcamp_data = user.bootcamp_data
end

session[:bootcamp_data_id] = @bootcamp_data.id
end

def create_bootcamp_data!
return if @bootcamp_data

@bootcamp_data = User::BootcampData.create!
session[:bootcamp_data_id] = @bootcamp_data.id
end

def retrieve_geolocated_data!
Expand Down
2 changes: 1 addition & 1 deletion app/models/user/bootcamp_data.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class User::BootcampData < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true

after_save do
User::Bootcamp::SubscribeToOnboardingEmails.defer(self)
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20241107133957_update_bootcamp_data_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UpdateBootcampDataFields < ActiveRecord::Migration[7.0]
def change
change_column_null :user_bootcamp_data, :user_id, true
add_column :user_bootcamp_data, :checkout_session_id, :string
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_11_04_095304) do
ActiveRecord::Schema[7.0].define(version: 2024_11_07_133957) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -1303,14 +1303,15 @@
end

create_table "user_bootcamp_data", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "user_id", null: true
t.integer "num_views", default: 0, null: false
t.datetime "last_viewed_at"
t.datetime "started_enrolling_at"
t.datetime "enrolled_at"
t.string "package"
t.datetime "paid_at"
t.string "payment_intent_id"
t.string "checkout_session_id"
t.string "name"
t.string "email"
t.string "ppp_country"
Expand Down

0 comments on commit 48ed7e2

Please sign in to comment.