Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
Hi, I start a little program that I need to import csv files to model(Person) and I have a lot of layouts that need to read and import to database. I began creating a Person model and reading a csv file, my decision was create a class that extends model and parse a line like:
class PersonFromCsvLineNamePhoneMail < Person
def initialize(content)
self.name,
self.phone,
self.mail = content.split(",")
end
end
and import using:
File.readlines("people.csv").collect do |line|
PersonFromCsvLineNamePhoneMail.new(line).save
end
When I put it running in rails using controller and scoping by logged_user I need to create a filter for my Person model.
class Filter
def initialize(target_class, target_method)
@target_class = target_class
@target_method = target_method
end
def before(controller)
filters = controller.send(@target_method)
@target_class.instance_eval do
scoped_methods << with_scope(filters) { current_scoped_methods }
end
end
def after(controller)
@target_class.instance_eval do
scoped_methods.pop
end
end
end
and use in my application_controller:
around_filter Filter.new(Person, :person_created_by_logged_user)
protected
def person_created_by_logged_user
{:user_id => session[:user]}
end
It's doesn't work because I import other class and not a person and I need to put this statement:
around_filter Filter.new(PersonFromCsvLineNamePhoneMail, :person_created_by_logged_user)
I have some questions:
why around_filter doesn't work?
extends model Person to import csv is a good practice?
Is There any way to do a kind of (TypeCast) inObject?
thanks :D
