Skip to content

Commit

Permalink
Add models for UserLanguages backfill values
Browse files Browse the repository at this point in the history
  • Loading branch information
ludamillion committed Oct 13, 2022
1 parent 88cb640 commit 43bc4e9
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/controllers/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def update

def add_to_volunteer
authorize @language
current_user.languages << @language
if current_user.save
begin
current_user.languages << @language
redirect_to edit_users_path, notice: "#{@language.name} was added to your languages list."
else
rescue ActiveRecord::RecordInvalid
redirect_to edit_users_path, notice: "Error unable to add #{@language.name} to your languages list!"
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/language.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Language < ApplicationRecord
belongs_to :casa_org
has_and_belongs_to_many :users
has_many :user_languages
has_many :users, through: :user_languages

validates :name, presence: true
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class User < ApplicationRecord
has_many :sms_notification_events, through: :user_sms_notification_events
has_many :notes, as: :notable
has_one :address, dependent: :destroy
has_and_belongs_to_many :languages
has_many :user_languages
has_many :languages, through: :user_languages

accepts_nested_attributes_for :user_sms_notification_events, :address, allow_destroy: true

Expand Down
23 changes: 23 additions & 0 deletions app/models/user_language.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class UserLanguage < ApplicationRecord
belongs_to :user
belongs_to :language

validates :language, uniqueness: {scope: :user}
end

# == Schema Information
#
# Table name: user_languages
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# language_id :bigint
# user_id :bigint
#
# Indexes
#
# index_user_languages_on_language_id (language_id)
# index_user_languages_on_language_id_and_user_id (language_id,user_id) UNIQUE
# index_user_languages_on_user_id (user_id)
#
14 changes: 14 additions & 0 deletions db/migrate/20221011044911_add_user_languages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddUserLanguages < ActiveRecord::Migration[7.0]
disable_ddl_transaction!

def change
create_table :user_languages do |t|
t.references :user
t.references :language

t.timestamps
end

add_index :user_languages, [:language_id, :user_id], unique: true, algorithm: :concurrently
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class PopulateUserLanguagesFromLanguagesUsers < ActiveRecord::Migration[7.0]
def change
query = Arel.sql("select language_id, user_id from languages_users")
old_join_table_entries = ActiveRecord::Base.connection.execute(query).to_a

old_join_table_entries.each do |entry|
UserLanguage.create(user_id: entry["user_id"], language_id: entry["language_id"])
end
end
end
30 changes: 29 additions & 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: 2022_10_03_202112) do
ActiveRecord::Schema[7.0].define(version: 2022_10_12_203806) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -429,6 +429,14 @@
t.index ["user_id"], name: "index_preference_sets_on_user_id"
end

create_table "preferences", force: :cascade do |t|
t.bigint "user_id"
t.jsonb "case_volunteer_columns", default: "{}", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_preferences_on_user_id"
end

create_table "sent_emails", force: :cascade do |t|
t.bigint "user_id"
t.bigint "casa_org_id", null: false
Expand Down Expand Up @@ -462,6 +470,16 @@
t.string "version", null: false
end

create_table "user_languages", force: :cascade do |t|
t.bigint "user_id"
t.bigint "language_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["language_id", "user_id"], name: "index_user_languages_on_language_id_and_user_id", unique: true
t.index ["language_id"], name: "index_user_languages_on_language_id"
t.index ["user_id"], name: "index_user_languages_on_user_id"
end

create_table "user_reminder_times", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "reminder_sent"
Expand Down Expand Up @@ -516,6 +534,16 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.bigint "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at", precision: nil
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "additional_expenses", "case_contacts"
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/user_languages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :user_language do
user
language
end
end
3 changes: 2 additions & 1 deletion spec/models/language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe Language, type: :model do
it { is_expected.to belong_to(:casa_org) }
it { is_expected.to have_and_belong_to_many(:users) }
it { is_expected.to have_many(:user_languages) }
it { is_expected.to have_many(:users).through(:user_languages) }

it { is_expected.to validate_presence_of(:name) }
end
12 changes: 12 additions & 0 deletions spec/models/user_language_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "rails_helper"

RSpec.describe UserLanguage, type: :model do
it { is_expected.to belong_to(:language) }
it { is_expected.to belong_to(:user) }

it "validates uniqueness of language scoped to user" do
existing_record = create(:user_language)
new_record = build(:user_language, user: existing_record.user, language: existing_record.language)
expect(new_record).not_to be_valid
end
end
2 changes: 2 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
it { is_expected.to have_many(:casa_cases).through(:case_assignments) }
it { is_expected.to have_many(:case_contacts) }
it { is_expected.to have_many(:sent_emails) }
it { is_expected.to have_many(:user_languages) }
it { is_expected.to have_many(:languages).through(:user_languages) }

it { is_expected.to have_many(:followups).with_foreign_key(:creator_id) }

Expand Down
24 changes: 20 additions & 4 deletions spec/requests/languages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,28 @@
end

context "when request params are invalid" do
it "should raise error when Language do not exist" do
expect {
context "when the language does not exist" do
it "should raise error" do
expect {
patch add_to_volunteer_languages_path, params: {
language_id: 800
}
}.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when the language is already present for the user" do
before { create(:user_language, user: volunteer, language: random_lang) }

it "should raise error" do
patch add_to_volunteer_languages_path, params: {
language_id: 800
language_id: random_lang.id
}
}.to raise_error(ActiveRecord::RecordNotFound)

expect(response.status).to eq 302
expect(response).to redirect_to(edit_users_path)
expect(flash[:notice]).to eq "Error unable to add #{random_lang.name} to your languages list!"
end
end
end
end
Expand Down

0 comments on commit 43bc4e9

Please sign in to comment.