Merge pull request #1026 from okriuchykhin/ok_SCI_2148

Add uniqueness validation for SamplesTable [SCI-2148][SCI-2149]
This commit is contained in:
okriuchykhin 2018-03-09 16:42:33 +01:00 committed by GitHub
commit 7cbfa5936a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View file

@ -1,9 +1,9 @@
class UserSamplesController < ApplicationController
def save_samples_table_status
samples_table = SamplesTable.where(user: @current_user,
team: params[:team])
team: params[:team]).first
if samples_table
samples_table.first.update(status: params[:state])
samples_table.update(status: params[:state])
else
SamplesTable.create(user: @current_user,
team: params[:team],
@ -19,14 +19,19 @@ class UserSamplesController < ApplicationController
end
def load_samples_table_status
@samples_table_state = SamplesTable.find_status(current_user,
current_team).first
samples_table_state = SamplesTable.find_status(current_user,
current_team).first
if samples_table_state.blank?
st = SamplesTable.new(user: current_user, team: current_team)
st.save
samples_table_state = st.status
end
respond_to do |format|
if @samples_table_state
if samples_table_state
format.json do
render json: {
state: @samples_table_state
state: samples_table_state
}
end
end

View file

@ -1,9 +1,10 @@
class SamplesTable < ApplicationRecord
validates :user, :team, presence: true
belongs_to :user, inverse_of: :samples_tables, optional: true
belongs_to :team, inverse_of: :samples_tables, optional: true
validates :user, :team, presence: true
validates :user, uniqueness: { scope: :team }
scope :find_status,
->(user, team) { where(user: user, team: team).pluck(:status) }