How to stay DRY with a Ruby on Rails model? -


several objects of application need exported csv. based on #362 exporting csv , excel added following function model:

### private functions definitions private  def self.to_csv   csv.generate(:col_sep => ";") |csv| #could accept separator option     csv << column_names     all.each |column|       csv << column.attributes.values_at(*column_names)     end   end end 

how can i, @ model level, reuse function other models ?

thanks.

you can define module common methods:

module csvhelper   def to_csv     csv.generate(:col_sep => ";") |csv| #could accept separator option       csv << column_names       all.each |column|         csv << column.attributes.values_at(*column_names)       end     end   end end 

and use in classes:

extend csvhelper 

p.s. class's methods' scope not change private in scope of class.

if want have private singleton method, you'd following:

class foo   class << self     private      def bar     end   end end 

now bar method private.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -