Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gkop/2142053 to your computer and use it in GitHub Desktop.
Save gkop/2142053 to your computer and use it in GitHub Desktop.
Adding functionality to every model in a system
# This file is in lib
module AutoStripTextAttributes
extend ActiveSupport::Concern
included do
text_columns = columns.collect do |c|
c.name.to_sym if c.type == :string || c.type == :text
end.compact
auto_strip_attributes(*text_columns) unless text_columns.empty?
end
end
require 'auto_strip_text_attributes'
# This file is in config/initializers
module ActiveRecordModelTracker
def inherited(by_our_model)
by_our_model.__send__ :include, AutoStripTextAttributes
super
end
end
ActiveRecord::Base.send :include, ActiveRecordModelTracker unless $rails_rake_task
@gkop
Copy link
Author

gkop commented Mar 20, 2012

I use #include instead of #extend because I want to be able to call class methods from my mixin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment