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

[354] Make animals.sex an enum #371

Merged
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -7,6 +7,7 @@ ruby '2.6.6'
gem 'rails', '~> 6.0.3.3'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
gem 'activerecord-postgres_enum', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.3'
gem 'sass-rails', '~> 6.0'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -53,6 +53,9 @@ GEM
activerecord (6.0.3.3)
activemodel (= 6.0.3.3)
activesupport (= 6.0.3.3)
activerecord-postgres_enum (1.4.0)
activerecord (>= 5, < 6.1)
pg
activestorage (6.0.3.3)
actionpack (= 6.0.3.3)
activejob (= 6.0.3.3)
Expand Down Expand Up @@ -322,6 +325,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-postgres_enum (~> 1.4)
annotate
bootsnap (>= 1.1.0)
bootstrap (~> 4.5.2)
Expand Down
5 changes: 5 additions & 0 deletions app/models/animal.rb
@@ -1,5 +1,10 @@
class Animal < ApplicationRecord
include OrganizationScope

enum sex: {
unknown: 'unknown',
male: 'male',
female: 'female'
}
has_many :measurements
end
2 changes: 1 addition & 1 deletion app/views/animals/_form.html.erb
Expand Up @@ -38,7 +38,7 @@

<div class="field">
<%= form.label :sex, class: 'label' %>
<%= form.text_field :sex, class: 'input' %>
<%= form.collection_select :sex, Animal.sexes.map { |sex| [sex.first, sex.first.humanize] }, :first, :second, class: 'input' %>
</div>

<div class="field">
Expand Down
2 changes: 1 addition & 1 deletion app/views/animals/show.html.erb
Expand Up @@ -32,7 +32,7 @@

<p>
<strong>Sex:</strong>
<%= @animal.sex %>
<%= @animal.sex.humanize %>
</p>

</div>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20200922213826_create_sex_enum.rb
@@ -0,0 +1,9 @@
class CreateSexEnum < ActiveRecord::Migration[6.0]
def up
create_enum :animal_sex, %w[unknown male female]
end

def down
drop_enum :animal_sex
end
end
13 changes: 13 additions & 0 deletions db/migrate/20200922214542_migrate_animal_sex_to_postgres_enum.rb
@@ -0,0 +1,13 @@
class MigrateAnimalSexToPostgresEnum < ActiveRecord::Migration[6.0]
def up
execute <<-DDL
ALTER TABLE animals ALTER COLUMN sex TYPE animal_sex
USING CASE sex
WHEN 'male' THEN 'male'::animal_sex
WHEN 'female' THEN 'female'::animal_sex
WHEN NULL THEN 'unknown'::animal_sex
END;
ALTER TABLE animals ALTER COLUMN sex SET NOT NULL
DDL
end
end
10 changes: 8 additions & 2 deletions db/schema.rb
Expand Up @@ -10,18 +10,24 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_09_22_202022) do
ActiveRecord::Schema.define(version: 2020_09_22_214542) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_enum :animal_sex, [
"unknown",
"male",
"female",
], force: :cascade

create_table "animals", force: :cascade do |t|
t.integer "collection_year"
t.datetime "date_time_collected"
t.string "collection_position"
t.integer "pii_tag"
t.integer "tag_id"
t.string "sex"
t.enum "sex", null: false, enum_name: "animal_sex"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "organization_id"
Expand Down
4 changes: 2 additions & 2 deletions db/seeds.rb
Expand Up @@ -72,8 +72,8 @@

# Tanks can have Operations occur (add or remove animals, combine tank contents, etc)
# Tanks can also have Measurements (number of animals, temperature of tank, etc)
male = Animal.create!(sex: 'male', organization_id: white_abalone.id)
female = Animal.create!(sex: 'female', organization_id: pinto_abalone.id)
male = Animal.create!(sex: :male, organization_id: white_abalone.id)
female = Animal.create!(sex: :female, organization_id: pinto_abalone.id)
family = Family.create!(male: male, female: female, organization_id: white_abalone.id)
tank = Tank.create!(facility: Facility.find_by(code: 'PSRF'), name: 'AB-17', organization_id: white_abalone.id)
Operation.create!(tank: tank, animals_added: 800, family: family, operation_date: 7.days.ago, action: :add_family)
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/animals.rb
Expand Up @@ -5,7 +5,7 @@
collection_position { "MyString" }
pii_tag { 1 }
tag_id { 1 }
sex { Animal.sexes.keys.sample }
organization
sex { "MyString" }
end
end
4 changes: 2 additions & 2 deletions spec/features/families/create_spec.rb
Expand Up @@ -8,8 +8,8 @@
end

it "And fill out the form and click the submit button, family should be created" do
female = create(:animal, sex: 'female')
male = create(:animal, sex: 'male', pii_tag: 2)
female = create(:animal, sex: :female)
male = create(:animal, sex: :male, pii_tag: 2)

visit new_family_path

Expand Down