Module: Zena::Use::Urls::Common
- Included in:
- TextDocument::AssetHelper, ControllerMethods, ViewMethods
- Defined in:
- lib/zena/use/urls.rb
Constant Summary collapse
- CACHESTAMP_FORMATS =
This is directly related to the FileMatch clause in httpd.rhtml (mod_expires for apaches)
%w{ico flv jpg jpeg png gif js css swf}
Instance Method Summary collapse
- #append_query_params(path, opts) ⇒ Object
- #basepath_as_url(path) ⇒ Object
- #cachestamp_format?(format) ⇒ Boolean
-
#data_path(node, opts = {}) ⇒ Object
Return the path to a document’s data.
-
#host_with_port ⇒ Object
We do not have access to the request.
- #http_protocol ⇒ Object
- #make_cachestamp(node, mode) ⇒ Object
-
#path_params ⇒ Object
Url parameters (without action,controller,path,prefix).
- #prefix ⇒ Object
-
#query_params ⇒ Object
Url parameters (without format/mode/prefix…).
- #should_cachestamp?(node, format, asset) ⇒ Boolean
-
#unlink_node_path(node, options = {}) ⇒ Object
Path to remove a node link.
-
#zen_path(node, options = {}) ⇒ Object
Path for a node.
-
#zen_url(node, opts = {}) ⇒ Object
Url for a node.
Instance Method Details
#append_query_params(path, opts) ⇒ Object
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 193 194 195 196 197 198 199 200 201 |
# File 'lib/zena/use/urls.rb', line 157 def append_query_params(path, opts) if opts == {} path else tz = opts.delete(:tz) list = opts.keys.map do |k| # FIXME: DOC if k.to_s == 'encode_params' list = opts[k] list = list.split(',').map(&:strip) unless list.kind_of?(Array) list.map do |key| value = params[key] if value.kind_of?(Hash) {key => value}.to_query elsif value.kind_of?(Array) {key => value.map{|v| v.blank? ? nil : v}.compact}.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 # 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 |
#basepath_as_url(path) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/zena/use/urls.rb', line 147 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
218 219 220 |
# File 'lib/zena/use/urls.rb', line 218 def cachestamp_format?(format) CACHESTAMP_FORMATS.include?(format) end |
#data_path(node, opts = {}) ⇒ Object
Return the path to a document’s data
210 211 212 213 214 215 216 |
# File 'lib/zena/use/urls.rb', line 210 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_port ⇒ Object
We do not have access to the request. Port and host should be passed from view.
274 275 276 |
# File 'lib/zena/use/urls.rb', line 274 def host_with_port current_site.host end |
#http_protocol ⇒ Object
269 270 271 |
# File 'lib/zena/use/urls.rb', line 269 def http_protocol 'http' end |
#make_cachestamp(node, mode) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/zena/use/urls.rb', line 228 def make_cachestamp(node, mode) str = 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 Digest::SHA1.hexdigest(str)[0..4] end |
#path_params ⇒ Object
Url parameters (without action,controller,path,prefix)
260 261 262 263 264 265 266 267 |
# File 'lib/zena/use/urls.rb', line 260 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 |
#prefix ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/zena/use/urls.rb', line 27 def prefix if visitor.is_anon? visitor.lang else AUTHENTICATED_PREFIX end end |
#query_params ⇒ Object
Url parameters (without format/mode/prefix…)
250 251 252 253 254 255 256 257 |
# File 'lib/zena/use/urls.rb', line 250 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
222 223 224 225 226 |
# File 'lib/zena/use/urls.rb', line 222 def should_cachestamp?(node, format, asset) cachestamp_format?(format) # && # ((node.kind_of?(Document) && node.prop['ext'] == format) || asset) end |
#unlink_node_path(node, options = {}) ⇒ Object
Path to remove a node link.
63 64 65 66 |
# File 'lib/zena/use/urls.rb', line 63 def unlink_node_path(node, ={}) return '#' unless node.can_write? && node.link_id node_link_path(node.zip, node.link_id, ) end |
#zen_path(node, options = {}) ⇒ Object
Path for a node. Options can be :format, :host and :mode. ex ‘/en/document34_print.html’
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 137 138 139 140 141 142 143 144 145 |
# File 'lib/zena/use/urls.rb', line 70 def zen_path(node, ={}) return '#' unless node if anchor = .delete(:anchor) return "#{zen_path(node, )}##{anchor}" end opts = .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 ep = opts[:encode_params] ep = ep.split(',').map(&:strip) if ep.delete('mode') mode ||= params[:mode] end opts[:encode_params] = ep end 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.v_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) stamp = make_cachestamp(node, mode) 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}" : '') + (stamp ? ".#{stamp}" : '') + (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}" : '') + (stamp ? ".#{stamp}" : '') + ".#{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’
205 206 207 |
# File 'lib/zena/use/urls.rb', line 205 def zen_url(node, opts={}) zen_path(node,{:host => host_with_port}.merge(opts)) end |