Skip to content

Instantly share code, notes, and snippets.

@thepatrick
Created January 13, 2011 02:02
Show Gist options
  • Save thepatrick/777274 to your computer and use it in GitHub Desktop.
Save thepatrick/777274 to your computer and use it in GitHub Desktop.
class Movie < ActiveRecord::Base
validates_uniqueness_of :uripart # brute force to make sure we don't let a dup in
def permalink (full = false)
r = "/movie/" + self.uripart
r = SM_CONFIG[:BASE].gsub(/\/$/, "") + r if full
r
end
def thoughts_html
BlueCloth.new(self.thoughts).to_html unless self.thoughts.nil?
end
def new_flag
self.seenon > 4.days.ago
end
def top10_flag
Movie.find_by_sql("SELECT COUNT(*) as higher_ranked FROM movies WHERE score >= (SELECT score FROM movies WHERE id = " + self.id.to_s + ")")[0].higher_ranked.to_i < 11
end
#SELECT COUNT(*) FROM pftqg_simplelog.movies WHERE score > (SELECT score FROM pftqg_simplelog.movies WHERE id = 877)
def before_validation_on_create
if !self.uripart or self.uripart == ''
# no permalink was specified, so let's create one automatically
self.uripart = self.title.downcase.strip.gsub("'", "").gsub(/([^a-z0-9-]+)/, "-")
end
end
# convert text using our filter and clean up dashes
# see above for info on the rescue returns
def before_validation_on_update
if !self.uripart or self.uripart == ''
# no permalink was specified, so let's create one automatically
self.uripart = self.title.downcase.strip.gsub("'", "").gsub(/([^a-z0-9-]+)/, "-")
end
end
# get a list of posts for the feed
def self.find_for_feed
self.find(:all, :conditions => ['seenon <= ?', Time.sl_local], :order => 'seenon desc', :limit => Preference.get_setting('ITEMS_IN_FEED').to_i)
end
end
class MoviesController < ApplicationController
# only authorized people should see this stuff
before_filter :user_authorize, :except => [:sync_down, :sync_up, :sync_auth_check]
layout false
$admin_page_title = 'Administration'
def admin2
@movies = Movie.find :all, :order => 'seenon DESC'
end
def admin2_movie_list
unless params[:search].nil? or params[:search].strip.eql?("")
search_q = "%" + params[:search] + "%"
@movies = Movie.find :all, :conditions => ['title like ?', search_q], :order => 'seenon DESC'
else
@movies = Movie.find :all, :order => 'seenon DESC'
end
render :partial => 'admin2_movie_list'
end
def admin2_movie_edit
@movie = Movie.find(params[:id])
render :partial => 'admin2_movie_edit'
end
def admin2_movie_save
@movie = Movie.find(params[:id])
@didSave = @movie.update_attributes(params[:movie])
render :partial => 'admin2_movie_edit'
end
def admin2_movie_new
@movie = Movie.new
render :partial => 'admin2_movie_new'
end
def admin2_movie_create
@movie = Movie.new(params[:movie])
if @movie.save
flash[:notice] = 'Movie was successfully created.'
@didCreate = true
render :partial => 'admin2_movie_edit'
else
render :partial => 'admin2_movie_new'
end
end
def admin2_movie_destroy
Movie.find(params[:id]).destroy
render :text => "Movie " + params[:id] + " deleted."
end
def sync_auth_check
unless (Author.authorize(params[:user], params[:pass], true))
render :text => "AUTHFAILED" and return
end
render :text => "OK"
end
def sync_down
unless (Author.authorize(params[:user], params[:pass], true))
render :text => "Auth failed." and return
end
@since = params[:since]
@movies = Movie.find(:all, :conditions => ["(updated_at IS NULL AND seenon > '"+@since+"') OR updated_at > '"+ @since + "'", @since])
end
def sync_up
unless (Author.authorize(params[:user], params[:pass], true))
render :text => "Auth failed." and return
end
mov = Movie.find_by_id params[:movie][:id]
if(mov)
mov.update_attributes(params[:movie])
else
mov = Movie.new(params[:movie])
mov.save
end
render :text => "OK"
end
# uses the author class' authorize method and checks for a valid user
def user_authorize
@author = Author.authorize(cookies[SL_CONFIG[:USER_EMAIL_COOKIE]], cookies[SL_CONFIG[:USER_HASH_COOKIE]], false, true)
unless @author
# we didn't find a author... send them to the login page
session[:came_from] = request.parameters
flash[:notice] = 'Please log in'
redirect_to :controller => 'author', :action => 'login'
end
end
def index
redirect_to :action => 'admin2'
end
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def preview
body = params[:movie][:thoughts]
render :text => BlueCloth.new(body).to_html
end
end
ActiveRecord::Schema.define(:version => 44) do
create_table "movies", :force => true do |t|
t.string "title", :default => "", :null => false
t.string "uripart", :limit => 100, :default => "", :null => false
t.integer "score", :default => 0, :null => false
t.text "thoughts"
t.integer "seenit"
t.string "haveitpath"
t.datetime "seenon", :null => false
t.datetime "updated_at"
t.integer "bluray", :default => 0, :null => false
t.integer "dvd1", :default => 0, :null => false
t.integer "dvd4", :default => 0, :null => false
t.integer "dvd2", :default => 0, :null => false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment