Skip to content

Instantly share code, notes, and snippets.

@vraravam
Created January 14, 2015 10:08
Show Gist options
  • Save vraravam/e081e591bd4b18df1fa7 to your computer and use it in GitHub Desktop.
Save vraravam/e081e591bd4b18df1fa7 to your computer and use it in GitHub Desktop.
Ruby module using arel for null-checks and time comparisons
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