Module: Versionable::ClassMethods

Defined in:
lib/versionable.rb

Instance Method Summary collapse

Instance Method Details

#new_versionObject

Sets the version of this assocation to the current time in integer form.



33
34
35
36
37
38
39
# File 'lib/versionable.rb', line 33

def new_version
  # the expiry needs to be longer than any page that might use this as a
  # cache key.
  time = Time.now.to_i
  versionable_options[:cache].write(version_cache_key, time, :expires_in => versionable_options[:ttl])
  time
end

#objectObject

Returns the object that version applies to.



55
56
57
58
59
60
61
# File 'lib/versionable.rb', line 55

def object
  if inside_association?
    proxy_owner
  else
    self
  end
end

#versionObject

Returns the current version of the assocation. Likely to be nil.



28
29
30
# File 'lib/versionable.rb', line 28

def version
  versionable_options[:cache].read(version_cache_key)
end

#version_cache_keyObject

Returns the association version key.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/versionable.rb', line 42

def version_cache_key
  parts =
    if object.class == Class
      [object]
    else
      [object.class, object.id]
    end
  
  parts << version_name
  parts * ':'
end

#version_nameObject

Returns the name portion of the version cache key



64
65
66
67
68
69
70
# File 'lib/versionable.rb', line 64

def version_name
  if inside_association?
    "#{proxy_reflection.name}_version"
  else
    "version"
  end
end