Created
July 10, 2009 16:26
-
-
Save camwest/144599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Episode < ActiveRecord::Base | |
| # accessors | |
| attr_accessor :flex_video | |
| def video_url | |
| video.url | |
| end | |
| def video_url=(url) | |
| #do nothing | |
| end | |
| # associations | |
| belongs_to :channel | |
| has_attached_file :video, | |
| :storage => 's3', | |
| :s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/amazon_s3.yml")[RAILS_ENV], | |
| :url => ":s3_alias_url", | |
| :s3_host_alias => "assets.#{MAIN_HOST}", | |
| :path => ":attachment/:id/:style/:basename.:extension" | |
| # validations | |
| validates_presence_of :name | |
| validates_attachment_content_type :video, :content_type => [ "video/x-flv" ] | |
| # callbacks | |
| before_validation :check_video_content_type, :if => Proc.new { |episode| episode.flex_video } | |
| #class methods | |
| def self.flex_params | |
| {:only => [:id, :name, :approved], :methods => :video_url} | |
| end | |
| protected | |
| private | |
| # Fixes the content type and assigns the video properly before validation | |
| def check_video_content_type | |
| self.flex_video.content_type = fix_content_type(self.flex_video.original_filename) | |
| self.video = self.flex_video | |
| end | |
| def fix_content_type(original_filename) #content_type | |
| if ( original_filename.split(".").last == "flv" ) | |
| content_type = "video/x-flv" | |
| else | |
| content_type = MIME::Types.type_for(original_filename).to_s | |
| end | |
| return content_type | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment