user_credential.rb 563 B

12345678910111213141516171819202122232425
  1. class UserCredential < ActiveRecord::Base
  2. MODES = %w[text java_script]
  3. belongs_to :user
  4. validates_presence_of :credential_name
  5. validates_presence_of :credential_value
  6. validates_inclusion_of :mode, :in => MODES
  7. validates_presence_of :user_id
  8. validates_uniqueness_of :credential_name, :scope => :user_id
  9. before_validation :default_mode_to_text
  10. before_save :trim_fields
  11. protected
  12. def trim_fields
  13. credential_name.strip!
  14. credential_value.strip!
  15. end
  16. def default_mode_to_text
  17. self.mode = 'text' unless mode.present?
  18. end
  19. end