Skip to content

Instantly share code, notes, and snippets.

@ashrewdmint
Created July 19, 2009 18:00
Show Gist options
  • Save ashrewdmint/149985 to your computer and use it in GitHub Desktop.
Save ashrewdmint/149985 to your computer and use it in GitHub Desktop.
class Import < ActiveRecord::Base
belongs_to :user
validates_presence_of :user
validate :data_must_be_valid_file, :data_must_be_csv
def data_must_be_valid_file
limit = 100
errors.add(:data, 'must be a file') unless data.class == Tempfile
errors.add(:data, 'must not be less than ' + limit.to_s + ' Kb') unless data.size < limit * 1024
end
def data_must_be_csv
begin
FasterCSV.parse(data.read)
rescue Exception => e
errors.add(:data, "must be a valid CSV file: #{e}")
end
end
before_save :read_file
protected
def read_file
logger.debug(data) # => <File>
#data = data.read (Error! Can't evaluate nil.read)
logger.debug(self.data) # => <File>
self.data = self.data.read # => Nothin'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment