Skip to content
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

feat(survey): capture customer's response and stop active subscriptions #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions app/controllers/survey_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class SurveyController < ApplicationController
before_action :authenticate_user!

# If a user re-subscribes within the grace period, we don't want to process the job.
GRACE_PERIOD = 1.week

def create
current_user.surveys.create(survey_param)
current_user.stop_active_subscriptions
SeeYouSoonJob.set(wait: GRACE_PERIOD).perform_later(current_user)

redirect_to see_you_soon_path, notice: "We hope to see you again!"
end

private

def survey_param
params.require(:survey).permit(:description, :reason, :competitor_url)
end
end
9 changes: 9 additions & 0 deletions app/jobs/see_you_soon_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SeeYouSoonJob < ApplicationJob
queue_as :default

# Delete all records that are part of the paid feature.
def perform(user)
user.posts.each { |post| post.destroy }
user.uploaded_pictures.each { |upload| upload.destroy }
end
end
22 changes: 22 additions & 0 deletions app/models/survey.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Survey < ApplicationRecord
REASONS = %w[expensive not_interested_anymore difficult_to_use other].freeze

belongs_to :user

validates :competitor_url, presence: true, format: { with: /\A^(http|https)\z/i }
validates :reason, presence: true
validate :reason_in_list
validate :description_required_if_other

private

def reason_in_list
errors.add(:reason, "not in list") if REASONS.exclude?(reason)
end

def description_required_if_other
if reason == "other" && description.blank?
errors.add(:description, "cannot be blank")
end
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class User < ApplicationRecord
has_many :surveys

validates :email, presence: true
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Rails.application.routes.draw do
resources :surveys, only: :create
end
13 changes: 13 additions & 0 deletions db/migrate/20230509084017_create_surveys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateSurveys < ActiveRecord::Migration[7.0]
def change
create_table :surveys do |t|
t.belongs_to :user, null: false, foreign_key: true

t.string :competitor_url, null: false
t.text :description
t.string :reason, null: false

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

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