Created
April 14, 2011 08:42
-
-
Save pkondzior/919134 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 Item | |
extend ActiveModel::Naming | |
extend ActiveModel::Callbacks | |
include ActiveModel::AttributeMethods | |
include ActiveModel::Serialization | |
include ActiveModel::Conversion | |
include ActiveModel::Validations | |
define_attribute_methods [:title, :path, :file, :upload_only] | |
define_model_callbacks :save, :only => :before | |
attr_accessor :title, :path, :file, :upload_only | |
validates_presence_of(:title, :unless => Proc.new { |item| item.upload_only }) | |
validates_presence_of(:path, :if => Proc.new { |item| item.file.nil? }) | |
before_save :make_file_path | |
def initialize(attributes = {}) | |
attributes.stringify_keys! | |
@title = attributes["title"] | |
@path = attributes["path"] | |
@file = attributes["file"] | |
@upload_only = attributes["upload_only"] | |
end | |
def persisted? | |
false | |
end | |
def make_file_path | |
if self.file | |
dir = Rails.root.join('tmp') | |
dir.mkpath unless dir.exist? | |
dest = dir.join(self.file.original_filename) | |
FileUtils.cp(self.file.path, dest) | |
self.path = dest | |
end | |
end | |
def save | |
run_callbacks(:save) do | |
self.valid? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment