Created
August 19, 2016 08:56
-
-
Save goosys/aeb067db2423f1d6f39183dcb3a4bae2 to your computer and use it in GitHub Desktop.
[Rails]{ActiveRecord][PostgreSQL] sort columns
This file contains 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
# | |
# config/initializers/extensions/active_record/attributes.rb | |
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/attributes.rb | |
# | |
module ActiveRecord #:nodoc: | |
module Attributes # :nodoc: | |
module ClassMethods | |
# Overriding method | |
def columns | |
# @columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name)) #Original | |
@columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name).sort!(&scheme_comparator)) | |
end | |
private | |
# New method | |
def scheme_comparator | |
lambda do |a,b| | |
a.name.end_with?("_at") ? | |
(b.name.end_with?("_at") ? 0 : 1) : | |
(b.name.end_with?("_at") ? -1 : 0) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment