135 lines
3.0 KiB
Ruby
135 lines
3.0 KiB
Ruby
class JamRuby::Promotional < ActiveRecord::Base
|
|
self.table_name = :promotionals
|
|
|
|
default_scope :order => 'aasm_state ASC, position ASC, updated_at DESC'
|
|
|
|
attr_accessible :position, :aasm_state
|
|
|
|
include AASM
|
|
HIDDEN_STATE = :hidden
|
|
ACTIVE_STATE = :active
|
|
EXPIRED_STATE = :expired
|
|
STATES = [HIDDEN_STATE, ACTIVE_STATE, EXPIRED_STATE]
|
|
|
|
aasm do
|
|
state HIDDEN_STATE, :initial => true
|
|
state ACTIVE_STATE
|
|
state EXPIRED_STATE
|
|
|
|
event :activate do
|
|
transitions :from => [HIDDEN_STATE, EXPIRED_STATE], :to => ACTIVE_STATE
|
|
end
|
|
|
|
event :expire do
|
|
transitions :from => [HIDDEN_STATE, ACTIVE_STATE], :to => EXPIRED_STATE
|
|
end
|
|
|
|
event :hide do
|
|
transitions :from => [HIDDEN_STATE, ACTIVE_STATE], :to => HIDDEN_STATE
|
|
end
|
|
|
|
end
|
|
|
|
def state
|
|
aasm_state
|
|
end
|
|
|
|
def self.active(max_count=10)
|
|
rel = self.where(:aasm_state => ACTIVE_STATE)
|
|
if 0 < (mc = max_count.to_i)
|
|
rel = rel.limit(mc)
|
|
end
|
|
rel
|
|
end
|
|
|
|
end
|
|
|
|
class JamRuby::PromoBuzz < JamRuby::Promotional
|
|
attr_accessible :image, :text_short, :text_long, :position, :aasm_state, :key
|
|
|
|
def self.create_with_params(params)
|
|
obj = self.new
|
|
obj.update_with_params(params)
|
|
obj.save!
|
|
obj
|
|
end
|
|
|
|
def update_with_params(params)
|
|
self.text_short = params[:text_short]
|
|
self.text_long = params[:text_long]
|
|
self.position = params[:position]
|
|
self.aasm_state = params[:aasm_state]
|
|
self.key = params[:key]
|
|
self
|
|
end
|
|
|
|
def admin_title
|
|
"Buzz #{created_at.strftime('%Y-%m-%d %H-%M')}"
|
|
end
|
|
|
|
def image_name
|
|
fn = image ? image.path || image.filename : nil
|
|
File.basename(fn) if fn
|
|
end
|
|
|
|
def image_url
|
|
self.image.direct_fog_url(with_path: true)
|
|
end
|
|
|
|
end
|
|
|
|
class JamRuby::PromoLatest < JamRuby::Promotional
|
|
belongs_to :latest, :polymorphic => true
|
|
|
|
attr_accessible :latest
|
|
|
|
def music_session
|
|
self.latest if self.latest.is_a? MusicSession
|
|
end
|
|
|
|
def recording
|
|
self.latest if self.latest.is_a? Recording
|
|
end
|
|
|
|
def self.create_with_params(params)
|
|
obj = self.new
|
|
obj.update_with_params(params)
|
|
obj.save!
|
|
obj
|
|
end
|
|
|
|
def update_with_params(params)
|
|
if (latest_id = params[:latest_id]).present?
|
|
self.latest = Recording.where(:id => latest_id).limit(1).first ||
|
|
MusicSession.where(:id => latest_id).limit(1).first
|
|
end
|
|
self.position = params[:position]
|
|
self.aasm_state = params[:aasm_state]
|
|
self
|
|
end
|
|
|
|
def self.latest_display_name(ll)
|
|
return '' unless ll
|
|
nm = if ll.is_a?(Recording)
|
|
ll.band.present? ? ll.band.name : ll.owner.name
|
|
else
|
|
ll.band.present? ? ll.band.name : ll.user.name
|
|
end
|
|
"#{ll.class.name.demodulize}: #{nm} (#{ll.created_at})"
|
|
end
|
|
|
|
def latest_display_name
|
|
self.class.latest_display_name(self.latest)
|
|
end
|
|
|
|
def self.active_latests
|
|
self.where(:aasm_state => 'active')
|
|
.all
|
|
.map(&:latest)
|
|
end
|
|
|
|
def self.active(max_count=10)
|
|
super.includes(:latest).select { |pp| pp.latest.present? ? pp : nil }.compact
|
|
end
|
|
end
|