I’m having a little trouble with a mixin. Here is the long and short, I am using a technique I found here:
http://redcorundum.blogspot.com/2006/06/mixing-in-class-methods.html
To be able to mixin class and instance methods in the same module (works great btw). I’m using it to make a quick exporter for my models, where I can just mixin the module for the models where I need it. Here’s the brief version of the code:
Class methods are declared in the “define_class_methods” method
the export function
require ‘mixin_class_methods’
module Exporter
mixin_class_methods { |klass| }
define_class_methods {
def export(division, columns_to_exclude) rows_to_export.to_csv end def rows_to_export # must export someting declared in the division # ie division.courses division.students division.send(export_object_name) end def export_object_name self.to_s.downcase.pluralize end def export_column_names(columns_to_exclude=nil) self.column_names end
} end
class Course < ActiveRecord::Base include Exporter
acts_as_csv_exportable :default, self.export_column_names end
When I have it setup like this, it runs as expected. However, I want to DRY things up and move all the export functionality into the module. If I comment out the “acts_as_csv_exportable :default, self.export_column_names” line and move it to the mixin I get an undefined method name for “export_column_names”.
Anyone have any ideas how to get around this?