user_credential.rb 566 B

123456789101112131415161718192021222324
  1. class UserCredential < ActiveRecord::Base
  2. MODES = %w[text java_script]
  3. belongs_to :user
  4. validates :credential_name, presence: true, uniqueness: { case_sensitive: true, scope: :user_id }
  5. validates :credential_value, presence: true
  6. validates :mode, inclusion: { in: MODES }
  7. validates :user_id, presence: true
  8. before_validation :default_mode_to_text
  9. before_save :trim_fields
  10. protected
  11. def trim_fields
  12. credential_name.strip!
  13. credential_value.strip!
  14. end
  15. def default_mode_to_text
  16. self.mode = 'text' unless mode.present?
  17. end
  18. end