scenario.rb 839 B

123456789101112131415161718192021222324
  1. class Scenario < ActiveRecord::Base
  2. include HasGuid
  3. attr_accessible :name, :agent_ids, :description, :public, :source_url, :tag_fg_color, :tag_bg_color
  4. belongs_to :user, :counter_cache => :scenario_count, :inverse_of => :scenarios
  5. has_many :scenario_memberships, :dependent => :destroy, :inverse_of => :scenario
  6. has_many :agents, :through => :scenario_memberships, :inverse_of => :scenarios
  7. validates_presence_of :name, :user
  8. validates_format_of :tag_fg_color, :tag_bg_color,
  9. # Regex adapted from: http://stackoverflow.com/a/1636354/3130625
  10. :with => /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, :allow_nil => true,
  11. :message => "must be a valid hex color."
  12. validate :agents_are_owned
  13. protected
  14. def agents_are_owned
  15. errors.add(:agents, "must be owned by you") unless agents.all? {|s| s.user == user }
  16. end
  17. end