app/models/chart.rb in artfully_ose-1.3.0.pre3 vs app/models/chart.rb in artfully_ose-1.3.0.pre4

- old
+ new

@@ -1,12 +1,13 @@ class Chart < ActiveRecord::Base - include Ticket::Foundry - foundry :using => :sections, :with => lambda { { :venue => event.venue.name } } attr_accessor :skip_create_first_section - attr_accessible :name, :is_template, :event_id, :organization_id, :sections_attributes, :ticket_types_attributes, :organization, :event, :skip_create_first_section + attr_accessible :name, :is_template, :event_id, :organization_id, + :sections_attributes, :ticket_types_attributes, :organization, + :event, :skip_create_first_section, :venue_id, :public_note belongs_to :event + belongs_to :venue belongs_to :organization has_one :show has_many :sections, :order => 'name DESC' has_many :ticket_types, :through => :sections, :order => 'ticket_types.price DESC' accepts_nested_attributes_for :sections, :reject_if => lambda { |a| a[:capacity].blank? }, :allow_destroy => true @@ -15,11 +16,11 @@ validates :name, :presence => true, :length => { :maximum => 255 } scope :template, where(:is_template => true) def as_json(options = {}) - h = super(options) + h = super(options.merge({:except => :venue_id})) h[:sections] = sections h end def widget_sections @@ -31,12 +32,18 @@ # This method will copy chart.is_template def copy! duplicate(:without => "id", :with => { :name => "#{name} (Copy)" }) end - def dup! - duplicate(:without => "id", :with => { :is_template => false }) + # + # Pass :save => true to persist the new chart + # The only way we can hook up ticket types to the dupe is if we save + # + # So unsaved charts will not have ticket types attached + # + def dup!(options = {}) + duplicate(options.merge({:without => "id", :with => { :is_template => false }})) end def create_first_section if sections.empty? self.sections.build({ :name => "General Admission", @@ -65,14 +72,21 @@ # # params_hash is the params[:chart] with :section_attributes as a key. # This is how they're submitted from the ticket types form # - def update_attributes_from_params(params_hash = {}) - upgrade_event + def update_attributes_from_params(params_hash = {}, parent_chart = nil) update_attributes(params_hash) + upgrade_event + sync_with_venue(params_hash) end + + def sync_with_venue(params_hash) + if self.event + self.event.venue.update_default_chart_from(self.event.default_chart) + end + end #If this is a free event, and they've specified prices on this chart, then upgrade to a paid event def upgrade_event if !event.nil? && event.free? && has_paid_sections? event.is_free = false @@ -89,25 +103,22 @@ def self.get_default_name(prefix) prefix + ', default chart' end - def self.default_chart_for(event) - raise TypeError, "Expecting an Event" unless event.kind_of? Event - @chart = self.new - @chart.name = self.get_default_name(event.name) - @chart.event_id = event.id - @chart - end - def has_paid_sections? !self.ticket_types.drop_while{|s| s.price.to_i == 0}.empty? end - private + protected + # + # Both AssignedChart and GeneralAdmissionChart implement this method in their subclasses + # Both implementations make use of this via super() + # def duplicate(options = {}) rejections = Array.wrap(options[:without]) + rejections << "type" additions = options[:with] || {} attrs = self.attributes.reject { |key, value| rejections.include?(key) }.merge(additions) self.class.new(attrs).tap do |copy| copy.sections = self.sections.collect { |section| section.dup! }