Created
May 29, 2015 17:59
-
-
Save johnpmitsch/83d4b1c568b0aac52c3c to your computer and use it in GitHub Desktop.
find_by_version
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
# takes something like key="version" operator="<=" value="1.3" | |
def self.find_by_version(key, operator, value) | |
major, minor = value.split('.') | |
major ||= 0 | |
if /[<>]/ =~ operator | |
minor ||= 0 | |
query = where("major #{operator} :major OR (major = :major AND minor #{operator} :minor)", :major => major, :minor => minor) | |
else | |
if minor.nil? | |
query = where("major #{operator} (?)", major) | |
else | |
query = where("major #{operator} (?) and minor #{operator} (?)", major, minor) | |
end | |
end | |
_, conditions = query.to_sql.split("WHERE") | |
{ :conditions => conditions } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment