1
0

user_credential.rb 625 B

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