Created
July 19, 2009 18:00
-
-
Save ashrewdmint/149985 to your computer and use it in GitHub Desktop.
This file contains 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 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