Created
January 14, 2015 10:08
-
-
Save vraravam/e081e591bd4b18df1fa7 to your computer and use it in GitHub Desktop.
Ruby module using arel for null-checks and time comparisons
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
module TimeSplits | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def not_null?(attribute) | |
where(arel_table[attribute].not_eq(nil)) | |
end | |
def in_future?(attribute, default_threshold = Time.current) | |
not_null?(attribute).where(arel_table[attribute].gt(default_threshold)) | |
end | |
def in_past?(attribute, default_threshold = Time.current) | |
not_null?(attribute).where(arel_table[attribute].lt(default_threshold)) | |
end | |
def now_or_in_past?(attribute, default_threshold = Time.current) | |
not_null?(attribute).where(arel_table[attribute].lteq(default_threshold)) | |
end | |
def now_or_in_future?(attribute, default_threshold = Time.current) | |
not_null?(attribute).where(arel_table[attribute].gteq(default_threshold)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment