Module: Zena::Use::VersionHash::ModelMethods

Included in:
Node
Defined in:
lib/zena/use/version_hash.rb

Instance Method Summary collapse

Instance Method Details

#rebuild_vhashObject



97
98
99
100
101
102
103
# File 'lib/zena/use/version_hash.rb', line 97

def rebuild_vhash
  cached = VersionHash.cached_values_from_records(connection.select_all("SELECT id,lang,status,publish_from FROM #{Version.table_name} WHERE node_id = #{self.id} ORDER BY lang ASC, status DESC", "Version Load"))
  # We also rebuild publish_from here: yes, that's a leak with Workflow.
  self[:publish_from] = cached[:publish_from]
  self[:vhash] = cached[:vhash].to_json
  @vhash = cached[:vhash]
end

#v_public?Boolean

Return true if the current version can be seen by the public.

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
# File 'lib/zena/use/version_hash.rb', line 69

def v_public?
  visitor.is_anon? ||
  begin
    anon = visitor.site.anon
    # visible by anonymous
    can_read?(anon, anon.group_ids) &&
    # anonymous would see this exact version
    version.id == version_id(visitor.lang, visitor.site.anon.group_ids)
  end
end

#version(lang = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zena/use/version_hash.rb', line 50

def version(lang = nil)
  @version ||= begin
    if v_id = version_id(lang)
      version = ::Version.find(v_id)
    else
      version = ::Version.new
    end
    version.node = self
    version
  end
end

#version_id(lang = nil, ugps = visitor.group_ids) ⇒ Object



62
63
64
65
66
# File 'lib/zena/use/version_hash.rb', line 62

def version_id(lang = nil, ugps = visitor.group_ids)
  lang ||= visitor.lang
  access = can_see_redactions?(ugps) ? vhash['w'] : vhash['r']
  access[lang] || access[self[:ref_lang]] || access.values.first
end

#vhashObject



93
94
95
# File 'lib/zena/use/version_hash.rb', line 93

def vhash
  @vhash ||= JSON.parse(self[:vhash] || '{"r":{}, "w":{}}')
end

#visible_versionsObject

Return the list of versions that are stored in the vhash and could be loaded depending on the visitor.



82
83
84
85
86
87
88
89
90
91
# File 'lib/zena/use/version_hash.rb', line 82

def visible_versions
  @visible_versions ||= begin
    ids = (vhash['w'].values + vhash['r'].values).uniq
    ::Version.find(ids).tap do |list|
      list.each do |version|
        version.node = self
      end
    end
  end
end