jam-cloud/ruby/lib/jam_ruby/models/promotional.rb

135 lines
3.0 KiB
Ruby
Raw Permalink Normal View History

class JamRuby::Promotional < ActiveRecord::Base
self.table_name = :promotionals
default_scope { order('aasm_state ASC, position ASC, updated_at DESC') }
2014-01-11 00:10:14 +00:00
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)
2014-03-05 15:35:43 +00:00
rel = self.where(:aasm_state => ACTIVE_STATE)
if 0 < (mc = max_count.to_i)
rel = rel.limit(mc)
end
2014-02-24 04:24:13 +00:00
rel
2014-01-11 00:10:14 +00:00
end
end
class JamRuby::PromoBuzz < JamRuby::Promotional
2014-02-02 03:59:25 +00:00
attr_accessible :image, :text_short, :text_long, :position, :aasm_state, :key
def self.create_with_params(params)
obj = self.new
2014-02-02 03:59:25 +00:00
obj.update_with_params(params)
obj.save!
obj
end
2014-02-02 03:59:25 +00:00
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]
2014-02-02 03:59:25 +00:00
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
2014-01-11 00:10:14 +00:00
attr_accessible :latest
2014-05-06 13:34:38 +00:00
def music_session
self.latest if self.latest.is_a? MusicSession
end
def recording
self.latest if self.latest.is_a? Recording
2014-01-11 00:10:14 +00:00
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 ||
2014-05-06 13:34:38 +00:00
MusicSession.where(:id => latest_id).limit(1).first
2014-01-11 00:10:14 +00:00
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
2014-01-11 00:10:14 +00:00
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