Created
August 23, 2015 14:53
-
-
Save skateman/aac790125174052ca05b to your computer and use it in GitHub Desktop.
PseudoColumns can mask columns with default values, useful when joining tables in ActiveRecord
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
module PseudoColumns | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# Specify the columns in {name => default_value} format | |
def pseudo_columns(columns = {}) | |
columns.each do |k, v| | |
define_method(k) do | |
attributes.fetch(__method__.to_s, v) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment