142 lines
3.5 KiB
Ruby
142 lines
3.5 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=100)
|
|
rel = self.where(:aasm_state => ACTIVE_STATE)
|
|
rel = rel.limit(mc) if 0 < (mc = max_count.to_i)
|
|
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 self.latest_candidates
|
|
recordings = Recording
|
|
.where('music_session_id IS NOT NULL')
|
|
.order('created_at DESC')
|
|
.limit(10)
|
|
sessions = MusicSession
|
|
.where("music_sessions.id NOT IN ('#{recordings.map(&:music_session_id).join("','")}')")
|
|
.order('created_at DESC')
|
|
.limit(10)
|
|
latests = (recordings + sessions).sort { |o1,o2| o2.created_at <=> o1.created_at }
|
|
latests.collect do |ll|
|
|
nm = if ll.is_a?(Recording)
|
|
"#{ll.class.name.demodulize}: #{ll.band.present? ? ll.band.name : ll.owner.name}"
|
|
else
|
|
"#{ll.class.name.demodulize}: #{ll.band.present? ? ll.band.name : ll.creator.name}"
|
|
end
|
|
{ :name => nm, :id => ll.id, :record => ll }
|
|
end
|
|
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).all[0] ||
|
|
MusicSession.where(:id => latest_id).limit(1).all[0]
|
|
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.creator.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
|
|
|
|
end
|