class TicketType < ActiveRecord::Base belongs_to :section belongs_to :show delegate :chart, :to => :section has_many :tickets attr_accessible :name, :price, :limit, :description, :storefront, :box_office, :members def as_json(options = {}) { "id" => self.id, "name" => self.name, "price" => self.price, "limit" => self.limit, "available" => self.available, "description" => self.description } end # # Returns tickets # def available_tickets(ticket_limit = 4) Ticket.available({:section_id => self.section.id, :ticket_type_id => nil }, [ticket_limit, self.available].min) end # # Returns an integer number of tickets available # def available available_in_section = Ticket.where(:section_id => self.section, :state => :on_sale, :cart_id => nil, :ticket_type_id => nil).count return available_in_section if unlimited? [ [limit - committed.length - locked.length, available_in_section].min, 0].max end def self.price_to_cents(price_in_dollars) (price_in_dollars.to_f * 100).to_i end # Each channel needs its own boolean column in the ticket types table. @@channels = { :storefront => "S", :box_office => "B", :members => "M"} @@channels.each do |channel_name, icon| attr_accessible channel_name self.class.send(:define_method, channel_name) do where(channel_name => true) end end def dup! TicketType.new(self.attributes.reject { |key, value| key == 'id' }, :without_protection => true) end def channels @@channels end def sold tickets.select {|t| t.sold?} end def committed tickets.select {|t| t.committed?} end def limit_to_s unlimited? ? "unlimited" : "#{self.limit} limit" end def unlimited? self.limit.nil? end def locked tickets.select {|t| t.locked?} end def comped tickets.select {|t| t.comped?} end def self.set_show(show) TicketType.joins(:section => :chart).where('charts.id = ?', show.chart_id).update_all(:show_id => show.id) end end