Created
September 19, 2018 15:27
-
-
Save jbodah/1ab19a03e07befdb498c98fce5cf8dbc to your computer and use it in GitHub Desktop.
adding Arel-supported alias_column support to Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'arel/attributes/attribute' | |
module ColumnAlias | |
module ArelAttributePatch | |
def name | |
return super unless relation.engine.respond_to?(:column_aliases) | |
name_given = super | |
relation.engine.column_aliases[name_given] || name_given | |
end | |
end | |
module ActiveRecord | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def alias_column(from, to) | |
@column_aliases ||= {} | |
@column_aliases[from.to_s] = to.to_s | |
end | |
def column_aliases | |
@column_aliases | |
end | |
end | |
end | |
end | |
Arel::Attributes::Attribute.prepend(ColumnAlias::ArelAttributePatch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment