Module: Zena::Use::Urls::Common

Included in:
TextDocument::AssetHelper, ControllerMethods, ViewMethods
Defined in:
lib/zena/use/urls.rb

Constant Summary collapse

CACHESTAMP_FORMATS =
['jpg', 'png', 'gif', 'css', 'js']

Instance Method Summary collapse

Instance Method Details

#append_query_params(path, opts) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/zena/use/urls.rb', line 148

def append_query_params(path, opts)
  if opts == {}
    path
  else
    cachestamp = opts.delete(:cachestamp)
    tz = opts.delete(:tz)
    list = opts.keys.map do |k|
      # FIXME: DOC
      if k.to_s == 'encode_params'
        opts[k].split(',').map(&:strip).map do |key|
          value = params[key]
          if value.kind_of?(Hash)
            {key => value}.to_query
          elsif !value.blank?
            "#{key}=#{CGI.escape(value)}"
          else
            nil
          end
        end
      elsif value = opts[k]
        if value.respond_to?(:strftime_tz)
          "#{k}=#{CGI.escape(value.strftime_tz(_(Zena::Use::Dates::DATETIME), tz))}"
        elsif value.kind_of?(Hash)
          "#{k}=#{value.to_query}"
        elsif value.kind_of?(Node)
          "#{k}=#{value.zip}"
        elsif !value.nil?
          "#{k}=#{CGI.escape(value.to_s)}"
        else
          nil
        end
      else
        nil
      end
    end.flatten.compact
    if cachestamp
      result = path + "?#{cachestamp}" + (list.empty? ? '' : "&#{list.sort.join('&')}")
      result
    else
      # TODO: replace '&' by '&' ? Or escape later ? Use h before zen_path in templates ? What about css/xls/other stuff ?
      # Best solution: use 'h' in template when set in default
      path + (list.empty? ? '' : "?#{list.sort.join('&')}")
    end
  end
end

#basepath_as_url(path) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/zena/use/urls.rb', line 138

def basepath_as_url(path)
  path.split('/').map do |zip|
    if n = secure(Node) { Node.find_by_zip(zip) }
      n.title.url_name
    else
      nil
    end
  end.compact.join('/')
end

#cachestamp_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/zena/use/urls.rb', line 209

def cachestamp_format?(format)
  CACHESTAMP_FORMATS.include?(format)
end

#data_path(node, opts = {}) ⇒ Object

Return the path to a document’s data



201
202
203
204
205
206
207
# File 'lib/zena/use/urls.rb', line 201

def data_path(node, opts={})
  if node.kind_of?(Document)
    zen_path(node, opts.merge(:format => node.prop['ext']))
  else
    zen_path(node, opts)
  end
end

#host_with_portObject

We do not have access to the request. Port and host should be passed from view.



263
264
265
# File 'lib/zena/use/urls.rb', line 263

def host_with_port
  current_site.host
end

#http_protocolObject



258
259
260
# File 'lib/zena/use/urls.rb', line 258

def http_protocol
  'http'
end

#make_cachestamp(node, mode) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/zena/use/urls.rb', line 219

def make_cachestamp(node, mode)
  if mode
    if node.kind_of?(Image)
      if iformat = Iformat[mode]
        "#{node.updated_at.to_i + iformat[:hash_id]}"
      else
        # random (will raise a 404 error anyway)
        "#{node.updated_at.to_i + Time.now.to_i}"
      end
    else
      # same format but different mode ? foobar_iphone.css ?
      # will not be used.
      node.updated_at.to_i.to_s
    end
  else
    node.updated_at.to_i.to_s
  end
end

#path_paramsObject

Url parameters (without action,controller,path,prefix)



249
250
251
252
253
254
255
256
# File 'lib/zena/use/urls.rb', line 249

def path_params
  res = {}
  params.each do |k,v|
    next if [:action, :controller, :path, :prefix, :id].include?(k.to_sym)
    res[k.to_sym] = v
  end
  res
end

#prefixObject



24
25
26
27
28
29
30
# File 'lib/zena/use/urls.rb', line 24

def prefix
  if visitor.is_anon?
    visitor.lang
  else
    AUTHENTICATED_PREFIX
  end
end

#query_paramsObject

Url parameters (without format/mode/prefix…)



239
240
241
242
243
244
245
246
# File 'lib/zena/use/urls.rb', line 239

def query_params
  res = {}
  path_params.each do |k,v|
    next if [:mode, :format, :asset, :cachestamp].include?(k.to_sym)
    res[k.to_sym] = v
  end
  res
end

#should_cachestamp?(node, format, asset) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
# File 'lib/zena/use/urls.rb', line 213

def should_cachestamp?(node, format, asset)
  cachestamp_format?(format)
  #  &&
  # ((node.kind_of?(Document) && node.prop['ext'] == format) || asset)
end

Path to remove a node link.



60
61
62
63
# File 'lib/zena/use/urls.rb', line 60

def unlink_node_path(node, options={})
  return '#' unless node.can_write? && node.link_id
  node_link_path(node.zip, node.link_id, options)
end

#zen_path(node, options = {}) ⇒ Object

Path for a node. Options can be :format, :host and :mode. ex ‘/en/document34_print.html’



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/zena/use/urls.rb', line 67

def zen_path(node, options={})
  return '#' unless node

  if anchor = options.delete(:anchor)
    return "#{zen_path(node, options)}##{anchor}"
  end

  opts   = options.dup
  format = opts.delete(:format)
  if format.blank?
    format = 'html'
  elsif format == 'data'
    if node.kind_of?(Document)
      format = node.ext
    else
      format = 'html'
    end
  end

  pre    = opts.delete(:prefix) || (visitor.is_anon? && opts.delete(:lang)) || prefix
  mode   = opts.delete(:mode)

  if host = opts.delete(:host)
    if ssl = opts.delete(:ssl)
      http = 'https'
    else
      http = http_protocol
    end
    abs_url_prefix = "#{http}://#{host}"
  else
    abs_url_prefix = ''
  end

  if node.kind_of?(Document) && format == node.ext
    if node.public? && !visitor.site.authentication?
      # force the use of a cacheable path for the data, even when navigating in '/oo'
      pre = node.version.lang
    end
  end

  if asset = opts.delete(:asset)
    mode   = nil
  end


  if should_cachestamp?(node, format, asset)
    opts[:cachestamp] = make_cachestamp(node, mode)
  else
    opts.delete(:cachestamp) # cachestamp
  end

  path = if !asset && node[:id] == visitor.site[:root_id] && mode.nil? && format == 'html'
    "#{abs_url_prefix}/#{pre}" # index page
  elsif node[:custom_base]
    "#{abs_url_prefix}/#{pre}/" +
    basepath_as_url(node.basepath) +
    (mode  ? "_#{mode}"  : '') +
    (asset ? ".#{asset}" : '') +
    (format == 'html' ? '' : ".#{format}")
  else
    "#{abs_url_prefix}/#{pre}/" +
    (node.basepath.blank? ? '' : "#{basepath_as_url(node.basepath)}/") +
    (node.klass.downcase   ) +
    (node[:zip].to_s       ) +
    (mode  ? "_#{mode}"  : '') +
    (asset ? ".#{asset}" : '') +
    ".#{format}"
  end
  append_query_params(path, opts)
end

#zen_url(node, opts = {}) ⇒ Object

Url for a node. Options are ‘mode’ and ‘format’ ex ‘test.host/en/document34_print.html



196
197
198
# File 'lib/zena/use/urls.rb', line 196

def zen_url(node, opts={})
  zen_path(node,{:host => host_with_port}.merge(opts))
end