Skip to content

Commit

Permalink
Enums are stored correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
veezus committed Apr 8, 2024
1 parent 9c770fa commit 93fdde5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 5 additions & 3 deletions app/models/partners/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Profile < Base
validate :client_share_is_0_or_100
validate :has_at_least_one_request_setting

enum :agency_type, {
AGENCY_TYPES = {
"career" => "Career technical training",
"abuse" => "Child abuse resource center",
"church" => "Church outreach ministry",
Expand Down Expand Up @@ -138,7 +138,9 @@ class Profile < Base
"2ycollege" => "Two-Year College",
"wic" => "Women, Infants and Children",
"other" => "Other"
}, prefix: "agency_type"
}

enum :agency_type, AGENCY_TYPES.to_h { |s, _| [s, s] }, prefix: "agency_type"

self.ignored_columns = %w[
evidence_based_description
Expand All @@ -155,7 +157,7 @@ class Profile < Base
]

def agency_type_description
self.class.agency_types[agency_type]
AGENCY_TYPES[agency_type]
end

def client_share_total
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</div>
<div class="card-body">
<%= form.input :name, label: "Agency Name", class: "form-control", wrapper: :input_group %>
<%= profile_form.input :agency_type, collection: Partners::Profile.agency_types.invert, label: "Agency Type", class: "form-control", wrapper: :input_group %>
<%= profile_form.input :agency_type, collection: Partners::Profile.agency_types, label: "Agency Type", class: "form-control", wrapper: :input_group %>
<%= profile_form.input :other_agency_type, label: "Other Agency Type", class: "form-control", wrapper: :input_group %>
<div class="form-group row">
<label class="control-label col-md-3">501(c)(3) IRS Determination Letter</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ConvertPartnerProfileAgencyTypesToEnums < ActiveRecord::Migration[7.0]
def up
Partners::Profile::AGENCY_TYPES.each do |enum, desc|
ActiveRecord::Base.connection.execute <<-SQL
UPDATE partner_profiles
SET agency_type = '#{enum}'
WHERE agency_type = '#{desc}'
SQL
end
end

def down
Partners::Profile::AGENCY_TYPES.each do |enum, desc|
ActiveRecord::Base.connection.execute <<-SQL
UPDATE partner_profiles
SET agency_type = '#{desc}'
WHERE agency_type = '#{enum}'
SQL
end
end
end
2 changes: 1 addition & 1 deletion 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_03_20_193521) do
ActiveRecord::Schema[7.0].define(version: 2024_04_08_171955) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down

0 comments on commit 93fdde5

Please sign in to comment.