Instantly share code, notes, and snippets.

Embed
What would you like to do?
Query the "current" version from ChiliProject

This implements (more or less) the {{version}} macro from http://projects.andriylesyuk.com/projects/wiking

It can be used like this:

This is the {% if project.current_version < "2.0" %}beautiful{% else  %}awful{% endif %} plugin! Do you agree?

The current version is {{ project.current_version.name }}.

Note: The patches just add methods, they don't override existing ones.

# Patch the ProjectDrop to return the last non-open version per your original
# implementation. This is made available to the Liquid context as a VersionDrop.
#
# TODO: Do we really want the last non-open version or do we want the last
# closed instead, i.e. what about locked versions?
#
# Also this heuristic might break for more complex project structures. E.g. in
# the ChiliProject core we maintain multiple major branches and have versions
# for each branch. The last closed version is not necessarily the
# newest / stable one if by chance the last closed one is a legacy release).
# Generally, I find the heuristic not obvious to grasp.
require_dependency 'drops/project_drop'
class ProjectDrop
def current_version
@object.shared_versions.visible.all(:conditions => ["#{Version.table_name}.status != ?", "open"]).sort.last
end
end
# Patch the Version class to make it usable in Liquid
require_dependency 'models/version'
class Version
def to_liquid
VersionDrop.new(self)
end
end
# Create the VersionDrop. This is the object representation of a Version in a
# Liquid context
#
# NOTE: The drop API in ChiliProject is not considered stable yet and might
# break with only short notice!
class VersionDrop < BaseDrop
include Comparable
allowed_methods :name
allowed_methods :description
allowed_methods :start_date
allowed_methods :effective_date
allowed_methods :due_date
allowed_methods :wiki_page_title
allowed_methods :status
allowed_methods :project
allowed_methods :to_s
def <=>(comparable)
# comparses the current VersionDrop to another Version, VersionDrop or
# version name (as a String)
#
# If the respective version is not visible or invalid, we are considered
# larger
case comparable
when Version
version = comparable
when VersionDrop
# this is rather ugly and might break in the future
obj = comparable.instance_variable_get("@object")
version = obj if obj && obj.visible?
else
version = @object.project.shared_versions.visible.find_by_name(version.to_s)
end
if version.is_a?(Version)
@object <=> version
else
self.name <=> comparable.to_s
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment