Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
around_filter with_scope && best practices
1 Post
around_filter with_scope && best practices

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 &lt;&lt; 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:

  1. why around_filter doesn't work?

  2. extends model Person to import csv is a good practice?

  3. Is There any way to do a kind of (TypeCast) inObject?

thanks :D

1 Post
Login to add your message