Class: Spree::ActiveSaleEvent

Inherits:
SaleEvent
  • Object
show all
Defined in:
app/models/spree/active_sale_event.rb

Instance Method Summary collapse

Methods inherited from SaleEvent

#invalid_dates?, #live?, #live_and_active?, not_blank_and_sorted_by, paginate, #past?, #start_and_end_dates_available?, #upcoming?

Instance Method Details

#children_sorted_by_positionObject



61
62
63
# File 'app/models/spree/active_sale_event.rb', line 61

def children_sorted_by_position
  self.children.sort_by{ |child| child.position.nil? ? 0 : child.position }
end

#eventable_image_available?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/active_sale_event.rb', line 57

def eventable_image_available?
  !!eventable.try(:image_available?)
end

#eventable_nameObject



49
50
51
# File 'app/models/spree/active_sale_event.rb', line 49

def eventable_name
  eventable.try(:name)
end

#eventable_name=(name) ⇒ Object



53
54
55
# File 'app/models/spree/active_sale_event.rb', line 53

def eventable_name=(name)
  self.eventable = self.eventable_type.constantize.find_by_name(name) if name.present?
end

#is_live?(object) ⇒ Boolean

Spree::ActiveSaleEvent.is_live? method should only/ always represents live and active events and not just live events.

Returns:

  • (Boolean)


25
26
27
28
29
# File 'app/models/spree/active_sale_event.rb', line 25

def is_live? object
  object_class_name = object.class.name
  return object.live_and_active? if object_class_name == self.name
  %w(Spree::Product Spree::Variant Spree::Taxon).include?(object_class_name) ? object.live? : false
end

#update_parent_active_salesObject

This callback basically makes sure that parents for an event lives longer. Or at least parents live for the time when event is live.



38
39
40
41
42
43
44
45
46
47
# File 'app/models/spree/active_sale_event.rb', line 38

def update_parent_active_sales
  active_sale_events = self.self_and_ancestors
  parents = self.ancestors
  oldest_start_date = active_sale_events.not_blank_and_sorted_by(:start_date).first
  latest_end_date = active_sale_events.not_blank_and_sorted_by(:end_date).last
  parents.each{ |parent| 
    parent.update_attributes(:start_date => oldest_start_date) if parent.start_date.nil? ? true : (parent.start_date > oldest_start_date) 
    parent.update_attributes(:end_date => latest_end_date) if parent.end_date.nil? ? true : (parent.end_date < latest_end_date)
  }
end


31
32
33
34
# File 'app/models/spree/active_sale_event.rb', line 31

def update_permalink
  prefix = {"Spree::Taxon" => "t", "Spree::Product" => "products"}
  self.permalink = [prefix[self.eventable_type], self.eventable.permalink].join("/") unless self.eventable.nil?
end