module Zena
module Use
module Refactor
module Common
# TODO: test
def lang
@lang ||= visitor.lang
end
end # Common
module ControllerMethods
include Common
# Read the parameters and add errors to the object if it is considered spam. Save it otherwize.
def save_if_not_spam(obj, params)
# do nothing (overwritten by plugins like zena_captcha)
obj.save
end
end # ControllerMethods
module ViewMethods
include Common
# Quote for html values (input tag, alt attribute, etc)
def fquote(text)
text.to_s.gsub("'",''')
end
# TODO: see if this is still needed. Creates a pseudo random string to avoid browser side ajax caching
def rnd
Time.now.to_i
end
# We need to create the accessor for zafu calls to the helper to work when compiling templates. Do not ask me why this works...
def session
@session || {}
end
# We need to create the accessor for zafu calls to the helper to work when compiling templates. Do not ask me why this works...
# this is messing up with Zena::View::TestCase's own flash method.
# def flash
# @flash || {}
# end
# TODO: refactor with new RedCloth
def add_place_holder(str)
@placeholders ||= {}
key = "[:::#{self.object_id}.#{@placeholders.keys.size}:::]"
@placeholders[key] = str
key
end
# Replace placeholders by their real values
def replace_placeholders(str)
(@placeholders || {}).each do |k,v|
str.gsub!(k,v)
end
str
end
# return a readable text version of a file size
# TODO: use number_to_human_size instead
def fsize(size)
size = size.to_f
if size >= 1024 * 1024 * 1024
sprintf("%.2f Gb", size/(1024*1024*1024))
elsif size >= 1024 * 1024
sprintf("%.1f Mb", size/(1024*1024))
elsif size >= 1024
sprintf("%i Kb", (size/(1024)).ceil)
else
sprintf("%i octets", size)
end
end
# TODO: is this still used ?
def show(obj, sym, opt={})
return show_title(obj, opt) if sym == :v_title
if opt[:as]
key = "#{opt[:as]}#{obj.zip}.#{obj.version.number}"
preview_for = opt[:as]
opt.delete(:as)
else
key = "#{sym}#{obj.zip}.#{obj.version.number}"
end
if opt[:text]
text = opt[:text]
opt.delete(:text)
else
text = obj.send(sym)
if text.blank? && sym == :v_summary
text = obj.version.text
opt[:images] = false
else
opt.delete(:limit)
end
end
if [:v_text, :v_summary].include?(sym)
if obj.kind_of?(TextDocument) && sym == :v_text
lang = obj.content_lang
lang = lang ? " lang='#{lang}'" : ""
text = "#{text}
"
end
text = zazen(text, opt)
klass = " class='zazen'"
else
klass = ""
end
if preview_for
render_to_string :partial=>'nodes/show_attr', :locals=>{:id=>obj[:id], :text=>text, :preview_for=>preview_for, :key=>key, :klass=>klass,
:key_on=>"#{key}#{Time.now.to_i}_on", :key_off=>"#{key}#{Time.now.to_i}_off"}
else
"