vrfs-927: fixing latest promotions
This commit is contained in:
parent
08e7496a40
commit
85d6129a50
|
|
@ -2,14 +2,15 @@ ActiveAdmin.register JamRuby::PromoLatest, :as => 'Latest' do
|
|||
|
||||
menu :label => 'Home Page Latest'
|
||||
|
||||
config.sort_order = 'position ASC aasm_state DESC updated_at DESC'
|
||||
config.batch_actions = false
|
||||
config.sort_order = ''
|
||||
# config.clear_action_items!
|
||||
config.filters = false
|
||||
|
||||
form :partial => 'form'
|
||||
|
||||
index do
|
||||
column 'Latest' do |pp| pp.latest_display_name end
|
||||
column 'State' do |pp| pp.aasm_state end
|
||||
column 'Position' do |pp| pp.position end
|
||||
column 'Updated' do |pp| pp.updated_at end
|
||||
|
|
@ -18,6 +19,7 @@ ActiveAdmin.register JamRuby::PromoLatest, :as => 'Latest' do
|
|||
|
||||
show do
|
||||
attributes_table do
|
||||
row 'Latest' do |pp| pp.latest_display_name end
|
||||
row 'State' do |obj| obj.aasm_state end
|
||||
row 'Position' do |obj| obj.position end
|
||||
row 'Updated' do |obj| obj.updated_at end
|
||||
|
|
@ -27,23 +29,26 @@ ActiveAdmin.register JamRuby::PromoLatest, :as => 'Latest' do
|
|||
controller do
|
||||
|
||||
def new
|
||||
@promo = JamRuby::PromoBuzz.new
|
||||
@promo = JamRuby::PromoLatest.new
|
||||
@promo.aasm_state = 'active'
|
||||
@latests = PromoLatest.latest_candidates
|
||||
super
|
||||
end
|
||||
|
||||
def create
|
||||
promo = PromoBuzz.create_with_params(params[:jam_ruby_promo_latest])
|
||||
super
|
||||
promo = PromoLatest.create_with_params(params[:jam_ruby_promo_latest])
|
||||
redirect_to('/admin/latests')
|
||||
end
|
||||
|
||||
def edit
|
||||
@promo = resource
|
||||
@latests = PromoLatest.latest_candidates
|
||||
super
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
resource.update_with_params(params[:jam_ruby_promo_latest]).save!
|
||||
redirect_to('/admin/latests')
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
<%= semantic_form_for([:admin, @promo], :html => {:multipart => true}, :url => @promo.new_record? ? admin_latests_path : "/admin/latests/#{@promo.id}") do |f| %>
|
||||
<%= f.inputs :name => "Recording or Session", :for => :latest do |latest_form| %>
|
||||
<%= latest_form.input :id, :as => :select, :collection => @latests.collect { |ll| [ll[:name], ll[:id]] }, :label => "Latest", :required => true, :selected => @promo.latest.try(:id) %>
|
||||
<% end %>
|
||||
<%= f.inputs do %>
|
||||
<%= f.input(:position, :label => "Position", :input_html => {:maxlength => 4}) %>
|
||||
<%= f.input(:aasm_state, :as => :select, :collection => Promotional::STATES, :label => 'Status') %>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
class JamRuby::Promotional < ActiveRecord::Base
|
||||
self.table_name = :promotionals
|
||||
|
||||
attr_accessible :expires_at, :position, :aasm_state
|
||||
default_scope :order => 'aasm_state ASC, position ASC, updated_at DESC'
|
||||
|
||||
attr_accessible :position, :aasm_state
|
||||
|
||||
include AASM
|
||||
HIDDEN_STATE = :hidden
|
||||
|
|
@ -32,6 +34,10 @@ class JamRuby::Promotional < ActiveRecord::Base
|
|||
aasm_state
|
||||
end
|
||||
|
||||
def self.active_promotionals
|
||||
self.where(:aasm_state => ACTIVE_STATE).limit(100)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class JamRuby::PromoBuzz < JamRuby::Promotional
|
||||
|
|
@ -41,6 +47,8 @@ class JamRuby::PromoBuzz < JamRuby::Promotional
|
|||
obj = self.new
|
||||
obj.text_short = params[:text_short]
|
||||
obj.text_long = params[:text_long]
|
||||
obj.position = params[:position]
|
||||
obj.aasm_state = params[:aasm_state]
|
||||
obj.save!
|
||||
obj
|
||||
end
|
||||
|
|
@ -63,4 +71,57 @@ 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
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,13 +3,15 @@ require 'factory_girl'
|
|||
namespace :db do
|
||||
desc "Add a simple one track recording to the database"
|
||||
task single_recording: :environment do
|
||||
@user = User.find_by_email('test@jamkazam.com')
|
||||
User.where(:musician => true).order('RANDOM()').limit(10).each do |uu|
|
||||
@user = uu
|
||||
next if @user.connections.present?
|
||||
@connection = FactoryGirl.create(:connection, :user => @user)
|
||||
@track = FactoryGirl.create(:track, :connection => @connection, :instrument => Instrument.find('violin'), :client_track_id => "t1")
|
||||
@music_session = FactoryGirl.create(:music_session, :creator => @user, :musician_access => true)
|
||||
@music_session.connections << @connection
|
||||
@music_session.save
|
||||
@recording = FactoryGirl.create(:recording, :music_session => @music_session, :owner => @user, :id=>"r1")
|
||||
@recording = FactoryGirl.create(:recording, :music_session => @music_session, :owner => @user, :id=>"R#{rand(10000)}")
|
||||
@recorded_track = RecordedTrack.create_from_track(@track, @recording)
|
||||
@recorded_track.save
|
||||
#@recording = Recording.start(@music_session, @user)
|
||||
|
|
@ -19,6 +21,7 @@ namespace :db do
|
|||
@recording.claim(@user, "name", "description", @genre, true, true)
|
||||
@recording.reload
|
||||
@claimed_recording = @recording.claimed_recordings.first
|
||||
end
|
||||
end
|
||||
|
||||
task clean: :environment do
|
||||
|
|
|
|||
Loading…
Reference in New Issue