require 'hpricot' class Event < ActiveRecord::Base belongs_to :host, :class_name => 'User' has_one :graphic has_many :news_items has_many :ticket_types has_many :tickets, :through => :ticket_types acts_as_geocodable :address => {:street => :address, :locality => :city, :region => :state, :postal_code => :zip} validates_presence_of :name, :starts_at, :host_id, :host validates_associated :graphic, :ticket_types before_save :save_flickr_feed after_create :save_graphic after_save :save_all_ticket_types def new_ticket_types=(ticket_types_hash) ticket_types_hash.values.each do |attributes| ticket_types.build(attributes) unless attributes.values.all?(&:blank?) end end def edit_ticket_types=(ticket_types_hash) ticket_types.each do |t| t.attributes = ticket_types_hash[t.id.to_s] end end def validate errors.add_to_base('Event must have at least one ticket type') if ticket_types.empty? errors.add(:name, "can't have any urls in it") if name =~ /https?:\/\// errors.add(:description, "can't have any urls in it") if description =~ /https?:\/\// errors.add(:description, "can't have any script tags in it") if description =~ /