Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created July 8, 2010 10:20
Show Gist options
  • Save bogdan/467852 to your computer and use it in GitHub Desktop.
Save bogdan/467852 to your computer and use it in GitHub Desktop.
class Game < ActiveRecord::Base
include ProductsStateMachine
include ProductsRating
include Commentable
include GamesAndMusicMethods
include VideoAttachable
belongs_to :company
belongs_to :genre
has_many :game_files, :dependent => :destroy, :validate => true, :autosave => true
has_many :activation_codes
delegate :name, :city, :country, :prefix => true, :to => :company
validates_presence_of :title, :company_id, :genre_id
liquid_methods :product_name
has_attached_file :title_image, :styles => { :small => "100x47#", :show_game => "535x255#", :featured => "420x200#", :normal => "208x208", :screenshot => "460x215#" },
:url => "/system/:class/:attachment/:id/:style/:filename"
validates_attachment_content_type :title_image,
:content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'],
:message => "only image files are allowed"
has_attached_file :image, :styles => { :small => "100x47#", :show_game => "535x255#", :normal => "208x208", :screenshot => "800x600#" },
:url => "/system/:class/:attachment/:id/:style/:filename"
validates_attachment_content_type :image,
:content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'],
:message => "only image files are allowed"
has_attached_file :image2, :styles => { :small => "100x47#", :show_game => "535x255#", :normal => "208x208", :screenshot => "800x600#" },
:url => "/system/:class/:attachment/:id/:style/:filename"
validates_attachment_content_type :image2,
:content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'],
:message => "only image files are allowed"
has_attached_file :image3, :styles => { :small => "100x47#", :show_game => "535x255#", :normal => "208x208", :screenshot => "800x600#" },
:url => "/system/:class/:attachment/:id/:style/:filename"
validates_attachment_content_type :image3,
:content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'],
:message => "only image files are allowed"
has_attached_file :image4, :styles => { :small => "100x47#", :show_game => "535x255#", :normal => "208x208", :screenshot => "800x600#" },
:url => "/system/:class/:attachment/:id/:style/:filename"
validates_attachment_content_type :image4,
:content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'],
:message => "only image files are allowed"
DRM = ['none', 'your own DRM', 'Yawma DRM'] # TODO need delete in later commits
module DrmTypes
NONE = 0
YAWMA = 1
GAME_DEV = 2
TEXT = {
NONE => 'none',
YAWMA => 'Yawma DRM',
GAME_DEV => 'your own DRM'
}
end
FEATURED_GAMES_COUNT = 8
define_index do
indexes title, :sortable => true, :as => :name
indexes company.name, :as => :userable_name
indexes genre.name, :as => :genre
where "games.state = 'activated'"
has created_at
has company_id, :as => :userable_id
end
def text_drm_type
DrmTypes::TEXT[drm_type]
end
def game_dev_drm?
drm_type == DrmTypes::GAME_DEV
end
def screenshots
[image, image2, image3, image4].delete_if{|img| !img.file?}
end
def all_images
screenshots.unshift( title_image )
end
def main_image
title_image
end
def self.new_with_files(game_params, files_params, activation_codes)
game = new( game_params )
activation_codes.strip.gsub("\r", "").split(/\s*\n\s*/).each do |code|
game.activation_codes.build(:code => code)
end
files_params.each do |key, file|
keys = key.split('_')
game.game_files.build(:platform => keys[1], :version => keys[2], :file => file) unless file.blank?
end unless files_params.blank?
game
end
def update_with_files(game_params, files_params, activation_codes)
self.attributes = game_params
activation_codes.strip.gsub("\r", "").split(/\s*\n\s*/).each do |code|
self.activation_codes.build(:code => code) unless self.activation_codes.detect {|ac| ac.code == code}
end
unless files_params.blank?
tmp_files = game_files.dup
files_params.each do |key, file|
unless file.blank?
keys = key.split('_')
tmp_files.delete_if {|gf| gf.platform == keys[1] && gf.version == keys[2]}
game_file = GameFile.new(:platform => keys[1], :version => keys[2], :file => file, :game_id => self.id)
tmp_files << game_file
end
end
self.game_files = tmp_files
end
self.save
end
def self.find_by_user(id, user)
user.administrator? ? find( id ) : user.userable.games.find( id )
end
def self.paginate_all_by_user( user, order_type, page )
options = {:joins => :genre, :order => "genres.name #{order_type}, company_id ASC", :page => page}
user.administrator? ? activated.paginate( options ) : user.userable.games.paginate( options )
end
def validate
screenshots.each do |image|
if image.file? && !image.queued_for_write.empty?
tempfile = image.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file( tempfile ) rescue nil
errors.add_to_base("Please select image with resolution starting with 100*100 pixels") if dimensions.try(:width).to_i < 100 || dimensions.try(:height).to_i < 100
end
end
end
def do_after_deactivation
reset_is_featured
UserMailer.deliver_deactivate_product_notification( self, self.company.user )
end
def do_after_activation
UserMailer.deliver_product_activation_notification( self.company.user, self )
true
end
after_create do |game|
UserMailer.deliver_product_waiting_activation( game.company.user, game )
end
def platforms
platforms = Array.new
platforms << "Mac" if game_files.mac.any?
platforms << "PC" if game_files.pc.any?
"for #{platforms.join(' and ')}"
end
def created_by(with_country = true)
owner = "by <b>#{company_name}</b>"
owner << ", #{company_city}" unless company_city.blank?
owner << ", #{company_country}" if !company_country.blank? && with_country
owner
end
def self.lists_for_store( params, current_customer = nil )
tab = params[:tab]
case tab
when 'new_to_store'; activated.order_by_create.by_genre( params[:genre] || Genre.games.first.name ).limit(5)
when 'best_sellers'; best_sellers(:count => 5, :genre_name => params[:genre] || Genre.games.first.name)
when 'recommended'; current_customer ? current_customer.recommended_games_for_store( :genre_name => params[:genre] || Genre.games.first.name, :count => 5) : Array.new
when 'randomizer'; activated.by_genre( params[:genre] || Genre.games.first.name ).random.limit(5)
else Array.new
end
end
def to_param
"#{id}-#{title.parameterize}"
end
def product_owner
company
end
def self.best_sellers(options = {})
options = {:count => 4}.update( options )
conditions = ["state = 'activated'"]
if options[:genre_name].present?
genre = Genre.find_by_name( options[:genre_name] )
conditions << "genre_id = #{genre.id}" if genre
end
find_by_sql <<-SQL
SELECT * FROM (
SELECT games.*, (select count(*) from orders where success = true and orderable_type = 'Game' and orderable_id = games.id) as g_count
FROM games
WHERE #{conditions.join(' AND ')}
) as subquery
WHERE subquery.g_count > 0
ORDER BY g_count DESC
LIMIT #{options[:count]}
SQL
end
end
# == Schema Information
#
# Table name: games
#
# id :integer not null, primary key
# title :string(512)
# version :string(255)
# company_id :integer
# description :text
# price :decimal(8, 2) default(0.0)
# image_file_name :string(255)
# image_content_type :string(255)
# image_file_size :integer
# created_at :datetime
# updated_at :datetime
# image2_file_name :string(255)
# image2_content_type :string(255)
# image2_file_size :integer
# image3_file_name :string(255)
# image3_content_type :string(255)
# image3_file_size :integer
# image4_file_name :string(255)
# image4_content_type :string(255)
# image4_file_size :integer
# state :string(255)
# title_image_file_name :string(255)
# title_image_content_type :string(255)
# title_image_file_size :integer
# languages :string(512)
# release_date :datetime
# operation_system :string(255)
# processor :string(255)
# memory :string(255)
# graphics :string(255)
# directx :string(255)
# hard_drive :string(255)
# sound :string(255)
# drm :string(255)
# single_player :boolean default(FALSE)
# multiplayer :boolean default(FALSE)
# is_featured :boolean default(FALSE)
# rating :decimal(8, 2) default(0.0)
# genre_id :integer
# drm_type :integer default(0)
# is_free :boolean default(FALSE)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment