Skip to content

Commit

Permalink
Fix labels issues
Browse files Browse the repository at this point in the history
  • Loading branch information
m-an committed May 8, 2018
1 parent 2d1c6ff commit 157cdc0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/profiles_controller.rb
Expand Up @@ -14,7 +14,7 @@ def show

def document_label
account = @profile.account
if account.labels.find_or_create_by(key: :document).update(value: params[:state])
if account.labels.find_or_create_by(key: :document, scope: :private).update(value: params[:state])
redirect_to admin_profile_path(@profile), notice: 'Document label was successfully updated.'
else
redirect_to admin_profiles_path
Expand Down
10 changes: 6 additions & 4 deletions app/models/label.rb
Expand Up @@ -25,7 +25,7 @@ class Label < ApplicationRecord
validates :key,
length: 3..255,
format: { with: /\A[A-Za-z0-9_-]+\z/ },
uniqueness: { scope: :account_id }
uniqueness: { scope: [:account_id, :scope] }

validates :value,
length: 3..255,
Expand All @@ -37,22 +37,24 @@ class Label < ApplicationRecord
private

def update_level_if_label_defined
return unless Level.exists?(key: key)
return unless scope == 'private'
account.reload.update_level
send_document_review_notification if key == 'document'
end

def send_document_review_notification
if value == 'verified'
ProfileReviewMailer.approved(account).deliver_now
EventAPI.notify('system.document.verified', account_uid: account.uid)
elsif value == 'rejected'
ProfileReviewMailer.rejected(account).deliver_now
EventAPI.notify('system.document.rejected', account_uid: account.uid)
end
end
end

# == Schema Information
# Schema version: 20180402133658
# Schema version: 20180507162420
#
# Table name: labels
#
Expand All @@ -67,7 +69,7 @@ def send_document_review_notification
# Indexes
#
# index_labels_on_account_id (account_id)
# index_labels_on_key_and_value_and_account_id (key,value,account_id) UNIQUE
# index_labels_on_key_and_scope_and_account_id (key,scope,account_id) UNIQUE
#
# Foreign Keys
#
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/profiles/show.html.erb
Expand Up @@ -40,7 +40,7 @@
<b> Document: </b>
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<%= @profile.account.labels.find_by(key: 'document').value %>
<%= @profile.account.labels.find_by(key: 'document', scope: 'private')&.value %>
</a>
<div class="dropdown-menu">
<% Document::STATES.each do |state|%>
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20180507162420_add_unique_index_to_labels.rb
@@ -0,0 +1,6 @@
class AddUniqueIndexToLabels < ActiveRecord::Migration[5.1]
def change
remove_index :labels, [:key, :value, :account_id]
add_index :labels, [:key, :scope, :account_id], unique: true
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180430172330) do
ActiveRecord::Schema.define(version: 20180507162420) do

create_table "accounts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "uid", null: false
Expand Down Expand Up @@ -63,7 +63,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_labels_on_account_id"
t.index ["key", "value", "account_id"], name: "index_labels_on_key_and_value_and_account_id", unique: true
t.index ["key", "scope", "account_id"], name: "index_labels_on_key_and_scope_and_account_id", unique: true
end

create_table "levels", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
Expand Down
80 changes: 80 additions & 0 deletions spec/models/label_spec.rb
Expand Up @@ -119,5 +119,85 @@
end
end
end

context '2 labels with same keys' do
let!(:account) { create(:account) }
let!(:label_public) do
create :label,
account: account,
key: email_verified_level.key,
value: email_verified_level.value,
scope: 'public'
end

let!(:label_private) do
create :label,
account: account,
key: email_verified_level.key,
value: email_verified_level.value,
scope: 'private'
end

context 'can be created with different scopes' do
it 'account has both labels' do
expect(account.labels.first).to eq label_public
expect(account.labels.second).to eq label_private
end

it 'account has level 1' do
expect(account.level).to eq 1
end

context 'when private label changes' do
it 'account level downgrades when value changed' do
expect { label_private.update(value: 'rejected') }.to change { account.reload.level }.to 0
end

it 'account level downgrades when key changed' do
expect { label_private.update(key: 'email0') }.to change { account.reload.level }.to 0
end
end

context 'when public label changes' do
it 'account level does not change' do
expect { label_public.update(value: 'rejected') }.to_not change { account.reload.level }
end
end
end
end

context 'with public labels duplicating levels' do
let!(:account) { create(:account) }
let!(:label_email_public) do
create :label,
account: account,
key: email_verified_level.key,
value: email_verified_level.value,
scope: 'public'
end
let!(:label_phone_public) do
create :label,
account: account,
key: phone_verified_level.key,
value: phone_verified_level.value,
scope: 'public'
end
let!(:label_identity_public) do
create :label,
account: account,
key: identity_verified_level.key,
value: identity_verified_level.value,
scope: 'public'
end
let!(:label_document_public) do
create :label,
account: account,
key: document_verified_level.key,
value: document_verified_level.value,
scope: 'public'
end

it { expect(account.reload.level).to eq 0 }
end
end
end

0 comments on commit 157cdc0

Please sign in to comment.