require 'patch_like_methods' class Patch < ActiveRecord::Base has_many :patch_versions, :order => 'created_at' has_many :patch_comments, :order => 'created_at' belongs_to :program belongs_to :author, :foreign_key => 'created_by', :class_name => 'User' validates_presence_of :filename validates_presence_of :program_id validates_presence_of :created_by include PatchLikeMethods APPLIED_CONDITION = "(applied_since_version IS NOT NULL AND applied_since_version <> '')" UNAPPLIED_CONDITION = "(applied_since_version IS NULL OR applied_since_version = '')" def latest_version if patch_versions.empty? patch_versions << PatchVersion.create(:comment => 'First version', :for_upstream_version => 'xxx') end patch_versions.last end def text; latest_version.text; end def text=(new_text); latest_version.text = new_text; end def applied_since_version=(new_version) super(new_version.to_s == '' ? nil : new_version) end end