add account_id to submissions

pull/217/head
Pete Matsyburka 2 years ago
parent ddcb5c5d40
commit 8630c68631

@ -10,7 +10,8 @@ class StartFormController < ApplicationController
before_action :load_template before_action :load_template
def show def show
@submitter = @template.submissions.new.submitters.new(uuid: @template.submitters.first['uuid']) @submitter = @template.submissions.new(account_id: @template.account_id)
.submitters.new(uuid: @template.submitters.first['uuid'])
end end
def update def update
@ -68,6 +69,7 @@ class StartFormController < ApplicationController
end end
submitter.submission ||= Submission.new(template:, submitter.submission ||= Submission.new(template:,
account_id: template.account_id,
template_submitters: template.submitters, template_submitters: template.submitters,
source: :link) source: :link)

@ -15,11 +15,13 @@
# template_submitters :text # template_submitters :text
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :bigint not null
# created_by_user_id :bigint # created_by_user_id :bigint
# template_id :bigint not null # template_id :bigint not null
# #
# Indexes # Indexes
# #
# index_submissions_on_account_id (account_id)
# index_submissions_on_created_by_user_id (created_by_user_id) # index_submissions_on_created_by_user_id (created_by_user_id)
# index_submissions_on_slug (slug) UNIQUE # index_submissions_on_slug (slug) UNIQUE
# index_submissions_on_template_id (template_id) # index_submissions_on_template_id (template_id)
@ -31,7 +33,7 @@
# #
class Submission < ApplicationRecord class Submission < ApplicationRecord
belongs_to :template belongs_to :template
has_one :account, through: :template belongs_to :account
belongs_to :created_by_user, class_name: 'User', optional: true belongs_to :created_by_user, class_name: 'User', optional: true
has_many :submitters, dependent: :destroy has_many :submitters, dependent: :destroy

@ -36,7 +36,7 @@
class Submitter < ApplicationRecord class Submitter < ApplicationRecord
belongs_to :submission belongs_to :submission
has_one :template, through: :submission has_one :template, through: :submission
has_one :account, through: :template has_one :account, through: :submission
attribute :values, :string, default: -> { {} } attribute :values, :string, default: -> { {} }
attribute :preferences, :string, default: -> { {} } attribute :preferences, :string, default: -> { {} }

@ -0,0 +1,26 @@
# frozen_string_literal: true
class AddAccountIdToSubmissions < ActiveRecord::Migration[7.1]
class MigrationSubmission < ApplicationRecord
self.table_name = 'submissions'
end
class MigrationTemplate < ApplicationRecord
self.table_name = 'templates'
end
class MigrationAccount < ApplicationRecord
self.table_name = 'accounts'
end
def change
add_reference :submissions, :account, index: true, null: true
MigrationAccount.all.each do |account|
MigrationSubmission.where(template_id: MigrationTemplate.where(account_id: account.id).select(:id))
.update_all(account_id: account.id)
end
change_column_null :submissions, :account_id, false
end
end

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_02_01_210319) do ActiveRecord::Schema[7.1].define(version: 2024_02_03_113454) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -151,6 +151,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_01_210319) do
t.string "submitters_order", null: false t.string "submitters_order", null: false
t.string "slug", null: false t.string "slug", null: false
t.text "preferences", null: false t.text "preferences", null: false
t.bigint "account_id", null: false
t.index ["account_id"], name: "index_submissions_on_account_id"
t.index ["created_by_user_id"], name: "index_submissions_on_created_by_user_id" t.index ["created_by_user_id"], name: "index_submissions_on_created_by_user_id"
t.index ["slug"], name: "index_submissions_on_slug", unique: true t.index ["slug"], name: "index_submissions_on_slug", unique: true
t.index ["template_id"], name: "index_submissions_on_template_id" t.index ["template_id"], name: "index_submissions_on_template_id"

@ -6,8 +6,8 @@ class Ability
def initialize(user) def initialize(user)
can :manage, Template, account_id: user.account_id can :manage, Template, account_id: user.account_id
can :manage, TemplateFolder, account_id: user.account_id can :manage, TemplateFolder, account_id: user.account_id
can :manage, Submission, template: { account_id: user.account_id } can :manage, Submission, account_id: user.account_id
can :manage, Submitter, template: { account_id: user.account_id } can :manage, Submitter, submission: { account_id: user.account_id }
can :manage, User, account_id: user.account_id can :manage, User, account_id: user.account_id
can :manage, EncryptedConfig, account_id: user.account_id can :manage, EncryptedConfig, account_id: user.account_id
can :manage, EncryptedUserConfig, user_id: user.id can :manage, EncryptedUserConfig, user_id: user.id

@ -32,6 +32,7 @@ module Submissions
parse_emails(emails).uniq.map do |email| parse_emails(emails).uniq.map do |email|
submission = template.submissions.new(created_by_user: user, submission = template.submissions.new(created_by_user: user,
account_id: user.account_id,
source:, source:,
template_submitters: template.submitters) template_submitters: template.submitters)

@ -15,6 +15,7 @@ module Submissions
set_submission_preferences['send_email'] = true if params['send_completed_email'] set_submission_preferences['send_email'] = true if params['send_completed_email']
submission = template.submissions.new(created_by_user: user, source:, submission = template.submissions.new(created_by_user: user, source:,
account_id: user.account_id,
preferences: set_submission_preferences, preferences: set_submission_preferences,
template_submitters: [], submitters_order:) template_submitters: [], submitters_order:)

@ -6,6 +6,7 @@ FactoryBot.define do
created_by_user factory: %i[user] created_by_user factory: %i[user]
before(:create) do |submission, _| before(:create) do |submission, _|
submission.account_id = submission.template.account_id
submission.template_fields = submission.template.fields submission.template_fields = submission.template.fields
submission.template_schema = submission.template.schema submission.template_schema = submission.template.schema
submission.template_submitters = submission.template.submitters submission.template_submitters = submission.template.submitters

Loading…
Cancel
Save