Skip to content

Instantly share code, notes, and snippets.

@felixflores
Created December 17, 2009 00:37
Show Gist options
  • Save felixflores/258377 to your computer and use it in GitHub Desktop.
Save felixflores/258377 to your computer and use it in GitHub Desktop.
class MoviesController < ApplicationController
def index
if params[:filter] == "coming_soon"
@movies = Movie.coming_soon
else
@movies_for_the_week = Movie.for_the_week
end
@view = params[:filter]
end
def new
@movie = Movie.new
@movie.showtimes.build
@movie.showtimes.first.hours.build
end
def create
@movie = Movie.new(params[:movie])
if @movie.save
flash[:notice] = "Movie successfully created!"
redirect_to movies_path
else
render :action => :new
end
end
def update
@movie = Movie.find(params[:id])
@movie.showtimes.delete_all
if @movie.update_attributes(params[:movie])
flash[:notice] = 'Movie was successfully updated.'
redirect_to movies_path
else
render :action => "edit"
end
end
def edit
@movie = Movie.find(params[:id])
end
end
class Movie < ActiveRecord::Base
RATINGS = %w[G PG PG-13 R NC-17]
acts_as_taggable_on :genre, :cast, :director
has_many :showtimes, :dependent => :delete_all
has_attached_file :poster,
:styles => { :carousel => "318x470#", :small => "145x214#", :medium => "230x342#" },
:storage => (APP_CONFIG["s3_bucket"] ? :s3 : :filesystem),
:s3_credentials => "#{RAILS_ROOT}/config/app_config.yml",
:path => (APP_CONFIG["s3_bucket"] ? ":attachment/:id/:style.:extension" : ":rails_root/public/system/:attachment/:id/:style/:filename"),
:bucket => APP_CONFIG["s3_bucket"]
accepts_nested_attributes_for :showtimes, :allow_destroy => true
validates_associated :showtimes
validates_presence_of :title
validates_presence_of :description
validates_presence_of :poster
validates_format_of :trailer, :with => /v=[^&]+/, :message => "is invalid", :if => :has_video?
validates_attachment_size :poster, :less_than => 5.megabytes
validates_attachment_content_type :poster, :content_type => ['image/jpeg', 'image/pjpeg', 'image/jpg', 'image/png', 'image/gif']
named_scope :coming_soon, lambda {{:conditions => ['premiere > ?', Date.today]}}
named_scope :that_plays, lambda { |day| {
:joins => :showtimes,
:group => "showtimes.day",
:having => ["showtimes.day = ?", day] }}
before_save :cache_premiere
def self.for_the_week
movies = []
for i in 0..6
date = Date.today+i
items = self.that_plays date
movies[i] = { :date => date, :items => items }
end
movies
end
def trailer_video(size = :small)
width = 0
height = 0
case size
when :small
width = 250
height = 154
when :medium
width = 480
height = 295
when :large
width = 560
height = 340
when :high_quality
width = 640
height = 385
when :high_definition
width = 853
height = 505
end
embed = "<object width='#{width}' height='#{height}'>"
embed << "<param name='movie' value='http://www.youtube.com/v/#{trailer_v}&hl=en_US&fs=1&hd=1'></param>"
embed << "<param name='allowFullScreen' value='true'></param>"
embed << "<param name='allowscriptaccess' value='always'></param>"
embed << "<embed src='http://www.youtube.com/v/#{trailer_v}&hl=en_US&fs=1&hd=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='#{width}' height='#{height}'></embed>"
embed << "</object>"
end
protected
def cache_premiere
self.premiere = self.showtimes.collect{|s| s.day }.sort.first
end
def trailer_v
trailer.scan(/v=[^&]+/)[0].delete("v=")
end
def has_video?
not trailer.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment