app/models/spotlight/role.rb in blacklight-spotlight-0.13.0 vs app/models/spotlight/role.rb in blacklight-spotlight-0.14.0
- old
+ new
@@ -1,11 +1,11 @@
module Spotlight
##
# Exhibit authorization roles
class Role < ActiveRecord::Base
ROLES = %w(admin curator)
- belongs_to :exhibit
+ belongs_to :resource, polymorphic: true
belongs_to :user, class_name: Spotlight::Engine.config.user_class, autosave: true
validates :role, inclusion: { in: ROLES }
validates :user_key, presence: true
validate :user_must_exist, if: -> { user_key.present? }
validate :user_must_be_unique, if: :user
@@ -33,9 +33,9 @@
# This is just like
# validates :user, uniqueness: { scope: :exhibit}
# but it puts the error message on the user_key instead of user so that the form will render correctly
def user_must_be_unique
- errors.add(:user_key, 'already a member of this exhibit') if Spotlight::Role.where(exhibit_id: exhibit_id, user_id: user.id).where.not(id: id).any?
+ errors.add(:user_key, 'already a member of this exhibit') if Spotlight::Role.where(resource: resource, user: user).where.not(id: id).any?
end
end
end