Skip to content

Commit

Permalink
Rename acts_as_file to has_one_file and clean up some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed May 5, 2009
1 parent fd55539 commit b2aa603
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 116 deletions.
2 changes: 1 addition & 1 deletion app/models/attachment.rb
Expand Up @@ -16,7 +16,7 @@ class Attachment < ActiveRecord::Base
belongs_to :page
belongs_to :site
scope_by_site_id
acts_as_file DB_ATTACHMENTS_PATH
has_one_file :path => DB_ATTACHMENTS_PATH

def visible_to?(person)
(message and person.can_see?(message)) or page
Expand Down
2 changes: 1 addition & 1 deletion app/models/publication.rb
Expand Up @@ -16,7 +16,7 @@ class Publication < ActiveRecord::Base

scope_by_site_id

acts_as_file DB_PUBLICATIONS_PATH
has_one_file :path => DB_PUBLICATIONS_PATH
acts_as_logger LogItem

validates_presence_of :name
Expand Down
4 changes: 0 additions & 4 deletions vendor/plugins/acts_as_file/README

This file was deleted.

1 change: 0 additions & 1 deletion vendor/plugins/acts_as_file/init.rb

This file was deleted.

1 change: 0 additions & 1 deletion vendor/plugins/acts_as_file/install.rb

This file was deleted.

96 changes: 0 additions & 96 deletions vendor/plugins/acts_as_file/lib/acts_as_file.rb

This file was deleted.

4 changes: 0 additions & 4 deletions vendor/plugins/acts_as_file/tasks/acts_as_photo_tasks.rake

This file was deleted.

8 changes: 0 additions & 8 deletions vendor/plugins/acts_as_file/test/acts_as_photo_test.rb

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions vendor/plugins/has_one_file/init.rb
@@ -0,0 +1 @@
require 'has_one_file'
93 changes: 93 additions & 0 deletions vendor/plugins/has_one_file/lib/has_one_file.rb
@@ -0,0 +1,93 @@
require 'active_record'

module Seven1m
module HasOneFile

def self.included(mod)
mod.extend(ClassMethods)
end

module ClassMethods
def has_one_file(options)
options.symbolize_keys!
file_env = RAILS_ENV == 'production' ? '' : ('.' + RAILS_ENV)
class_eval <<-END
CONTENT_TYPES = {
'gif' => "image/gif",
'jpg' => "image/jpeg",
'png' => "image/png",
'swf' => "application/x-shockwave-flash",
'pdf' => "application/pdf",
'doc' => "application/msword",
'zip' => "application/zip",
'mp3' => "audio/mpeg",
'wma' => "audio/x-ms-wma",
'wav' => "audio/x-wav",
'css' => "text/css",
'html' => "text/html",
'js' => "text/javascript",
'txt' => "text/plain",
'xml' => "text/xml",
'mpeg' => "video/mpeg",
'mpg' => "video/mpeg",
'mov' => "video/quicktime",
'avi' => "video/x-msvideo",
'asf' => "video/x-ms-asf",
'wmv' => "video/x-ms-wmv"
}
def has_file?
!file_path.nil?
end
def file_name
return nil unless id
matches = Dir[File.join('#{options[:path]}', id.to_s + '#{file_env}.*')].select { |p| p.index('#{file_env}') }.map { |p| File.split(p).last }
matches.any? ? matches.first : nil
end
def file_path
file_name ? File.join('#{options[:path]}', file_name) : nil
end
def base_file_path
return nil unless id
File.join('#{options[:path]}', id.to_s + '#{file_env}')
end
def file_content_type
file_name ? CONTENT_TYPES[file_name.split('.').last] : nil
end
def file_size
@file_size ||= File::Stat.new(file_path).size
end
def file=(file)
raise 'Cannot save file before saving record.' unless id
File.delete file_path if file_path
if file
filename = file.original_filename
file = file.read
output_path = File.join('#{options[:path]}', id.to_s + '#{file_env}.' + filename.split('.').last.downcase)
File.open(output_path, 'w') do |f|
f.write(file)
end
end
end
def destroy_with_file_deletion
self.file = nil
destroy_without_file_deletion
end
alias_method_chain :destroy, :file_deletion
END
end
end

end
end

ActiveRecord::Base.class_eval do
include Seven1m::HasOneFile
end

0 comments on commit b2aa603

Please sign in to comment.