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

set short_uuid #1706

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions app/controllers/admin/reporting_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def hisps

def lifespan
@form_lifespans = Submission.select('form_id, count(*) as num_submissions, (max(submissions.created_at) - min(submissions.created_at)) as lifespan').group(:form_id)
@forms = Form.select(:id, :name, :organization_id, :uuid).where('exists (select id, uuid from submissions where submissions.form_id = forms.id)')
@forms = Form.select(:id, :name, :organization_id, :uuid, :short_uuid).where('exists (select id from submissions where submissions.form_id = forms.id)')
@orgs = Organization.order(:name)
@org_summary = []
@orgs.each do |org|
Expand All @@ -36,7 +36,7 @@ def lifespan
end

def no_submissions
@forms = Form.published.select(:id, :name, :organization_id, :uuid).where("not exists (select id, uuid from submissions where submissions.form_id = forms.id and submissions.created_at > current_date - interval '30' day)").order(:organization_id)
@forms = Form.published.select(:id, :name, :organization_id, :uuid, :short_uuid).where("not exists (select id, uuid from submissions where submissions.form_id = forms.id and submissions.created_at > current_date - interval '30' day)").order(:organization_id)
@orgs = Organization.order(:name)
@org_summary = []
@orgs.each do |org|
Expand Down
39 changes: 13 additions & 26 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class Form < ApplicationRecord
acts_as_taggable_on :tags

validates :name, presence: true
validates :disclaimer_text, length: { in: 0..1000, allow_blank: true }
validates :uuid, presence: true
validates :short_uuid, presence: true
validates :delivery_method, presence: true
validates :disclaimer_text, length: { in: 0..1000, allow_blank: true }
validates :anticipated_delivery_count, numericality: true, allow_nil: true
validate :omb_number_with_expiration_date
validate :valid_form_kinds
validate :target_for_delivery_method
validate :ensure_modal_text

before_create :set_uuid
before_validation :set_uuid, on: :create
after_create :create_first_form_section
before_destroy :ensure_no_responses

Expand Down Expand Up @@ -124,12 +126,6 @@ def suppress_submit_button
(questions.size == 1 && questions.collect(&:question_type).include?('custom_text_display'))
end

def self.find_by_short_uuid(short_uuid)
return nil unless short_uuid && short_uuid.length == 8

where('uuid LIKE ?', "#{short_uuid}%").first
end

def self.find_by_legacy_touchpoints_id(id)
return nil unless id && id.length < 4

Expand All @@ -146,10 +142,6 @@ def to_param
short_uuid
end

def short_uuid
uuid[0..7]
end

def send_notifications?
notification_emails.present?
end
Expand All @@ -158,14 +150,6 @@ def create_first_form_section
form_sections.create(title: (I18n.t 'form.page_1'), position: 1)
end

# def to_param
# short_uuid
# end

def short_uuid
uuid[0..7]
end

# used to initially set tags (or reset them, if necessary)
def set_submission_tags!
submission_tags = submissions.collect(&:tags).uniq.sort_by { |i| i.name }
Expand Down Expand Up @@ -228,19 +212,21 @@ def events

def duplicate!(new_user:)
new_form = dup
new_form.aasm_state = :created
new_form.name = "Copy of #{name}"
new_form.title = new_form.name
new_form.survey_form_activations = 0
new_form.response_count = 0
new_form.questions_count = 0
new_form.last_response_created_at = nil
new_form.aasm_state = :created
new_form.uuid = nil
new_form.short_uuid = nil
new_form.legacy_touchpoint_id = nil
new_form.legacy_touchpoint_uuid = nil
new_form.template = false
new_form.notification_emails = nil
new_form.organization = new_user.organization
new_form.legacy_form_embed = false
new_form.template = false
new_form.save!

# Manually remove the Form Section created with create_first_form_section
Expand Down Expand Up @@ -286,10 +272,6 @@ def self.archive_expired!
end
end

def set_uuid
self.uuid = SecureRandom.uuid if uuid.blank?
end

def self.send_inactive_form_emails_since(days_ago)
inactive_published_forms = find_inactive_forms_since(days_ago)

Expand Down Expand Up @@ -764,6 +746,11 @@ def self.forms_whose_retention_period_has_passed

private

def set_uuid
self.uuid ||= SecureRandom.uuid
self.short_uuid ||= self.uuid[0..7]
end

def set_submitted_at
self.update(submitted_at: Time.current)
end
Expand Down
18 changes: 18 additions & 0 deletions db/migrate/20241126184348_form_short_uuid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class FormShortUuid < ActiveRecord::Migration[7.2]
def change
add_column :forms, :short_uuid, :string, limit: 8
add_index :forms, :short_uuid, unique: true

remove_index :forms, :uuid
add_index :forms, :uuid, unique: true

remove_index :submissions, :uuid
add_index :submissions, :uuid, unique: true

Form.all.each do |form|
form.update({
short_uuid: form.uuid[0..7]
})
end
end
end
6 changes: 4 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.2].define(version: 2024_11_12_222412) do
ActiveRecord::Schema[7.2].define(version: 2024_11_26_184348) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -309,12 +309,14 @@
t.boolean "legacy_form_embed", default: false
t.datetime "archived_at"
t.string "audience", default: "public", comment: "indicates whether a form is intended for a public or internal audience"
t.string "short_uuid", limit: 8
t.index ["legacy_touchpoint_id"], name: "index_forms_on_legacy_touchpoint_id"
t.index ["legacy_touchpoint_uuid"], name: "index_forms_on_legacy_touchpoint_uuid"
t.index ["organization_id"], name: "index_forms_on_organization_id"
t.index ["service_id"], name: "index_forms_on_service_id"
t.index ["short_uuid"], name: "index_forms_on_short_uuid", unique: true
t.index ["user_id"], name: "index_forms_on_user_id"
t.index ["uuid"], name: "index_forms_on_uuid"
t.index ["uuid"], name: "index_forms_on_uuid", unique: true
end

create_table "ivn_component_links", force: :cascade do |t|
Expand Down
1 change: 0 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ def production_suitable_seeds
fiscal_year: 2021,
quarter: 2,
rating: 'TRUE',
likert_or_thumb_question: "",
number_of_interactions: 123_456,
number_of_people_offered_the_survey: 9_876,
likert_or_thumb_question: "thumbs_up_down",
Expand Down
Loading