app/models/spotlight/role.rb in blacklight-spotlight-0.4.1 vs app/models/spotlight/role.rb in blacklight-spotlight-0.5.0
- old
+ new
@@ -1,45 +1,41 @@
-class Spotlight::Role < ActiveRecord::Base
+module Spotlight
+ ##
+ # Exhibit authorization roles
+ class Role < ActiveRecord::Base
+ ROLES = %w(admin curator)
+ belongs_to :exhibit
+ belongs_to :user, class_name: '::User', 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
- ROLES = %w(admin curator)
- belongs_to :exhibit
- belongs_to :user, class_name: '::User', 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
-
- def user_key
- if user
- @user_key = user.user_key
- else
- @user_key
+ def user_key
+ if user
+ @user_key = user.user_key
+ else
+ @user_key
+ end
end
- end
- # setting user key causes the user to get set
- def user_key= key
- @user_key = key
- self.user ||= User.find_by_user_key(key)
- if user
- user.user_key = key
+ # setting user key causes the user to get set
+ def user_key=(key)
+ @user_key = key
+ self.user ||= ::User.find_by_user_key(key)
+ user.user_key = key if user
end
- end
- protected
+ protected
- def user_must_exist
- unless user.present?
- errors.add(:user_key, "User must sign up first.")
+ def user_must_exist
+ errors.add(:user_key, 'User must sign up first.') unless user.present?
end
- end
- # 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
- 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")
+ # 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?
end
end
-
end