Skip to content

Instantly share code, notes, and snippets.

@dmexe
Created August 2, 2011 09:31
Show Gist options
  • Save dmexe/1119889 to your computer and use it in GitHub Desktop.
Save dmexe/1119889 to your computer and use it in GitHub Desktop.
test
class Broadcast < ActiveRecord::Base
validates_presence_of :title, :permalink, :start_at, :video_id, :main_title
validates_format_of :permalink, :with => /[a-zA-Z0-9\-\_]+/, :allow_blank => true
has_many :guests, :dependent => :destroy
accepts_nested_attributes_for :guests, :allow_destroy => true, :reject_if => :all_blank
scope :visibled, where(:visible => true)
scope :ordered, order("broadcasts.start_at DESC")
scope :reverse_ordered, order("broadcasts.start_at ASC")
scope :past, where(["broadcasts.start_at < ?", 1.hour.from_now.utc])
scope :for_public, visibled.ordered
scope :for_archive, for_public.past
scope :not_ended, where(["broadcasts.ended IS NULL OR broadcasts.ended = ?", false])
def to_s
title
end
def to_param
permalink
end
def self.permalink(id)
find_by_permalink!(id)
end
def past?
start_at.utc < 1.hour.from_now.utc
end
def onair?
start_at.utc < 1.hour.from_now.utc && !ended?
end
def ended?
!!ended
end
def future?
start_at > 1.hour.from_now
end
def end!
update_attributes!(:ended => true, :ends_at => Time.now)
end
def ended=(val)
rs = val == true || val == "1" || val == "true"
self[:ended] = !!rs
self[:ends_at] = rs ? Time.now : nil
end
def make_copy!(attrs = {})
raw_attrs = %w{ title description start_at video_id main_title }
new_object = Broadcast.new
raw_attrs.each do |attr|
new_object.__send__("#{attr}=", self.__send__(attr))
end
attrs.each_pair do |k,v|
new_object.__send__("#{k}=", v)
end
new_object.permalink = "permalink-" + Time.now.to_i.to_s
new_object.visible = false
new_object.ended = false
self.class.transaction do
new_object.save!
self.guests.each do |guest|
guest.make_copy!(:broadcast => new_object)
end
end
new_object
end
def transcript=(val)
if val.blank?
self[:transcript] = nil
self[:transcript_html] = nil
else
self[:transcript] = val
self[:transcript_html] = TranscriptTranslator.new(val).to_html
end
end
def self.first_active
broadcast = Broadcast.visibled.reverse_ordered.not_ended.first
broadcast ||= Broadcast.for_public.first
end
end
require 'spec_helper'
describe Broadcast do
let(:broadcast){ Factory.build(:broadcast) }
subject{ broadcast }
it "should create given valid attributes" do
Broadcast.create!(broadcast.attributes)
end
%w{ title permalink start_at video_id main_title }.each do |m|
it "shoould validates presense of #{m}" do
should be_valid
broadcast.__send__ "#{m}=", ""
should have(1).error_on(m)
end
end
it "should validates format of permalink with format 'a-zA-Z0-9-_'" do
should be_valid
broadcast.permalink = 'Дима'
should have(1).error_on(:permalink)
end
it "should find visibled" do
b1 = Factory(:broadcast, :visible => false)
b2 = Factory(:broadcast, :visible => true)
Broadcast.visibled.should == [b2]
end
it "should find ordered" do
b1 = Factory(:broadcast, :start_at => 2.day.ago)
b2 = Factory(:broadcast, :start_at => 1.day.ago)
Broadcast.ordered.should == [b2,b1]
end
it "should find reverse_ordered" do
b1 = Factory(:broadcast, :start_at => 1.day.ago)
b2 = Factory(:broadcast, :start_at => 2.day.ago)
Broadcast.reverse_ordered.should == [b2,b1]
end
it "should find not ended" do
b1 = Factory(:broadcast, :ended => nil)
b2 = Factory(:broadcast, :ended => true)
Broadcast.not_ended.should == [b1]
end
it "should find past" do
b1 = Factory(:broadcast, :start_at => 59.minutes.from_now)
b2 = Factory(:broadcast, :start_at => 61.minutes.from_now)
Broadcast.past.should == [b1]
end
it "should find for public" do
Broadcast.for_public.all
end
it "shold find for archive" do
b1 = Factory(:broadcast, :start_at => 1.day.ago)
b2 = Factory(:broadcast, :start_at => 1.day.from_now)
b3 = Factory(:broadcast, :start_at => 1.day.ago, :visible => false)
Broadcast.for_archive.should == [b1]
end
it "should find by permalink" do
broadcast.save!
Broadcast.permalink(broadcast.permalink).should == broadcast
expect{ Broadcast.permalink("not exists") }.to raise_error(ActiveRecord::RecordNotFound)
end
it "should check past?" do
broadcast.start_at = 59.minutes.from_now
broadcast.past?.should == true
broadcast.start_at = 61.minutes.from_now
broadcast.past?.should == false
end
it "should check future?" do
broadcast.start_at = 61.minutes.from_now
broadcast.future?.should == true
broadcast.start_at = 59.minutes.from_now
broadcast.future?.should == false
end
it "should check ended?" do
broadcast.ended = false
broadcast.ended?.should == false
broadcast.ended = true
broadcast.ended?.should == true
end
it "should check onair?" do
broadcast.ended = false
broadcast.start_at = 30.minutes.from_now
broadcast.onair?.should == true
broadcast.ended = true
broadcast.start_at = 30.minutes.from_now
broadcast.onair?.should == false
broadcast.ended = false
broadcast.start_at = 61.minutes.from_now
broadcast.onair?.should == false
end
it "should end broadcast" do
broadcast.end!
broadcast.ended.should == true
broadcast.ends_at.should be
end
it "should end broadcast using ended=" do
broadcast.ended = "1"
broadcast.ended.should == true
broadcast.ends_at.should be
end
it "should using title for to_s" do
broadcast.to_s.should == broadcast.title
end
it "should using permalink for to_param" do
broadcast.to_param.should == broadcast.permalink
end
it "should make copy" do
broadcast.save!
guest = Factory(:guest, :broadcast => broadcast)
new_broadcast = broadcast.make_copy!
new_broadcast.should be
new_broadcast.title.should == broadcast.title
new_broadcast.description.should == broadcast.description
new_broadcast.permalink.should match(/permalink-[0-9]+/)
new_broadcast.guests.count.should == 1
new_broadcast.guests.first.name.should == guest.name
guest.destroy
new_broadcast.destroy
end
it "should store transcript html" do
broadcast.update_attributes!(:transcript => "text")
broadcast.transcript.should be
broadcast.transcript_html.should be
broadcast.transcript_html.should == "<div>text</div>\n"
end
it "should clear transcript html" do
broadcast.update_attributes!(:transcript => "text")
broadcast.update_attributes!(:transcript => "")
broadcast.transcript.should be_nil
broadcast.transcript_html.should be_nil
end
describe "first active" do
it "should find first not ended broadcast" do
b1 = Factory(:broadcast, :ended => nil, :start_at => 1.day.from_now, :visible => true)
b2 = Factory(:broadcast, :ended => nil, :start_at => 2.day.from_now, :visible => true)
Broadcast.first_active.should == b1
end
it "should find first public broadcast" do
b1 = Factory(:broadcast, :start_at => 1.day.ago, :visible => true, :ended => true)
b2 = Factory(:broadcast, :start_at => 2.day.ago, :visible => true, :ended => true)
Broadcast.first_active.should == b1
end
end
end
class BroadcastsController < ApplicationController
before_filter :init_broadcast, :except => [:index]
before_filter :login_required, :except => [:show]
def index
@broadcasts = Broadcast.ordered
end
def show
@broadcasts = Broadcast.for_public.past - [@broadcast]
end
def new
end
def create
save "new"
end
def edit
end
def update
save "edit"
end
def destroy
@broadcast.destroy
redirect_to broadcasts_path
end
def copy
@new_broadcast = @broadcast.make_copy!
respond_to do |want|
want.html { redirect_to edit_broadcast_path(@new_broadcast) }
want.json { render :json => @new_broadcast }
end
end
private
def init_broadcast
@broadcast = Broadcast.permalink(params[:id]) if params[:id]
@broadcast ||= Broadcast.new
end
def save(act)
@broadcast.update_attributes!(params[:broadcast])
respond_to do |want|
want.html { redirect_to broadcasts_path }
want.json { render :json => @broadcast }
end
rescue ActiveRecord::RecordInvalid
render :action => act
end
end
require 'spec_helper'
describe BroadcastsController do
let(:broadcast) { mock_model(Broadcast) }
let(:broadcast_id){ "1" }
let(:broadcasts) { ["broadcast"] }
let(:attrs) { {"name" => "value" } }
before(:each) do
@attr = {"name" => "value"}
@user = login_as
end
describe "GET /index" do
before(:each) do
stub(Broadcast).ordered{ broadcasts }
end
it "should be success" do
get :index
response.should be_success
end
it "should find and assign broadcasts" do
mock(Broadcast).ordered{ broadcasts }
get :index
assigns(:broadcasts).should == broadcasts
end
end
describe "GET /show" do
before(:each) do
stub(Broadcast).permalink{ broadcast }
stub(Broadcast).for_public.stub!.past{ broadcasts }
end
it "should have route /archive/:id" do
prms = { "id" => "id",
"controller" => "broadcasts",
"action" => "show" }
path = { :path => "/archive/id", :method => :get }
assert_recognizes prms, path
end
it "should generate route for broadcast" do
tm = Time.at(0)
stub(broadcast).start_at{ tm }
stub(broadcast).to_param{ "id" }
controller.show_broadcast_path(broadcast).should == "/archive/id"
controller.show_broadcast_url(broadcast).should == "http://test.host/archive/id"
end
it "should be success" do
do_show
response.should be_success
end
it "should find and assign broadcasts" do
mock(Broadcast).for_public.mock!.past{ broadcasts }
do_show
assigns(:broadcasts).should == broadcasts
end
it "should render show" do
do_show
response.should render_template("show")
end
def do_show
get :show, :id => broadcast_id
end
end
describe 'GET /new' do
let(:minaev){"minaev"}
before(:each) do
stub(Broadcast).new{ broadcast }
end
it "should be success" do
get :new
response.should be_success
end
it "should find and assign broadcast" do
mock(Broadcast).new{broadcast}
get :new
assigns(:broadcast).should == broadcast
end
end
describe 'POST /create' do
before(:each) do
stub(Broadcast).new{ broadcast }
stub(broadcast).update_attributes!
end
it "should be redirect" do
do_create
response.should be_redirect
end
it "should build broadcast" do
mock(Broadcast).new{broadcast}
do_create
assigns(:broadcast).should == broadcast
end
it "should create broadcast" do
mock(broadcast).update_attributes!(attrs)
do_create
end
describe "with errors" do
before(:each) do
mock(broadcast).update_attributes!(attrs){ raise ActiveRecord::RecordInvalid, broadcast }
end
it "should be success" do
do_create
response.should be_success
end
it "should render :new" do
do_create
response.should render_template("new")
end
end
def do_create
post :create, :broadcast => attrs
end
end
describe 'GET /edit' do
before(:each) do
stub(Broadcast).permalink{ broadcast }
end
it "should be success" do
do_edit
response.should be_success
end
it "should find and assign broadcast" do
mock(Broadcast).permalink(broadcast_id){broadcast}
do_edit
assigns(:broadcast).should == broadcast
end
def do_edit
get :edit, :id => broadcast_id
end
end
describe 'PUT /update' do
before(:each) do
stub(Broadcast).permalink{ broadcast }
stub(broadcast).update_attributes!
end
it "should be redirect when html requested" do
do_update
response.should be_redirect
end
it "should respond to json" do
do_update(:json)
response.should be_success
response.body.should == "Broadcast"
response.content_type.should == "application/json"
end
it "should find broadcast" do
mock(Broadcast).permalink(broadcast_id){broadcast}
do_update
assigns(:broadcast).should == broadcast
end
it "should update broadcast" do
mock(broadcast).update_attributes!(attrs)
do_update
end
describe "with errors" do
before(:each) do
mock(broadcast).update_attributes!(attrs){ raise ActiveRecord::RecordInvalid, broadcast }
end
it "should be success" do
do_update
response.should be_success
end
it "should render :edit" do
do_update
response.should render_template("edit")
end
end
def do_update(format = :html)
put :update, :broadcast => attrs, :id => broadcast_id, :format => format
end
end
describe "POST /copy" do
let(:new_broadcast){ "new_broadcast" }
before(:each) do
stub(Broadcast).permalink{broadcast}
stub(broadcast).make_copy!{new_broadcast}
end
it "should be redirect when html requested" do
do_copy(:html)
response.should be_redirect
end
it "should find and assign broadcast" do
mock(Broadcast).permalink(broadcast_id){broadcast}
do_copy
assigns(:broadcast).should == broadcast
end
it "should create copy and assign them" do
mock(broadcast).make_copy!{new_broadcast}
do_copy
assigns(:new_broadcast).should == new_broadcast
end
it "should respond to json" do
do_copy
response.should be_success
response.body.should == "new_broadcast"
response.content_type.should == "application/json"
end
def do_copy(format = :json)
post :copy, :id => broadcast_id, :format => format
end
end
describe "DELETE /destroy" do
before(:each) do
stub(Broadcast).permalink{broadcast}
stub(broadcast).destroy
end
it "should be redirect" do
do_destroy
response.should be_redirect
end
it "should find and assign broadcast" do
mock(Broadcast).permalink(broadcast_id){broadcast}
do_destroy
assigns(:broadcast).should == broadcast
end
it "should destroy broadcast" do
mock(broadcast).destroy
do_destroy
end
def do_destroy
delete :destroy, :id => broadcast_id
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment