Skip to content

Instantly share code, notes, and snippets.

@lanrion
Created August 18, 2014 06:15
Show Gist options
  • Save lanrion/7ab76f5f40f5e6adba32 to your computer and use it in GitHub Desktop.
Save lanrion/7ab76f5f40f5e6adba32 to your computer and use it in GitHub Desktop.
UnderscoreAttributeNameConcern
module UnderscoreAttributeNameConcern
  extend ActiveSupport::Concern
  included do |included_class|
    included_class.column_names.each do |attr_name|
      method_name = attr_name.underscore
      define_method(method_name) do
        read_attribute(attr_name)
      end

      writer_method_name = "#{method_name}="
      define_method(writer_method_name) do |value|
        write_attribute(attr_name, value)
      end
    end
  end
end

[BeBase].each do |base_class|
  base_class.class_eval do
    def self.inherited(subclass)
      super
      if subclass.name != "Keyword"
        subclass.class_eval do
          include UnderscoreAttributeNameConcern
        end
      end

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