lib/scrivito/cms_routing.rb in scrivito_sdk-0.65.2 vs lib/scrivito/cms_routing.rb in scrivito_sdk-0.66.0.rc1

- old
+ new

@@ -1,18 +1,18 @@ module Scrivito -class CmsRouting < Struct.new(:request, :main_app) +class CmsRouting < Struct.new(:request, :main_app, :scrivito_engine, :image_options) LINK_TO_EMPTY_LINKLIST = "#__empty_linklist" LINK_TO_EMPTY_BLOB = "#__empty_blob" def self.match_protocol(url, request) request.ssl? && !url.starts_with?('https') ? url.gsub(/^http/, 'https') : url end def path_or_url(target, path_or_url, options = {}) - if cms_with_editing_context?(target) - path_or_url_with_editing_context(target, path_or_url, options) + if needs_editing_context?(target) + path_or_url_needs_editing_context(target, path_or_url, options) else path_or_url_without_editing_context(target, path_or_url, options) end end @@ -29,23 +29,27 @@ options['anchor'] = uri.fragment rescue end end - path_or_url(obj, "path", options) + if editing_context.display_mode == 'editing' + id_path_or_url_for_objs(obj, :path, options) + else + path_or_url(obj, :path, options) + end else "#__target_object_not_reachable" end end end end private - def path_or_url_with_editing_context(target, path_or_url, options) + def path_or_url_needs_editing_context(target, path_or_url, options) path_or_url_without_editing_context(target, path_or_url, - options.merge(editing_context.to_params)) + options.merge(editing_context.to_params)) end def path_or_url_without_editing_context(target, path_or_url, options) if target.is_a?(Link) path_or_url_for_links(target, path_or_url, options) @@ -56,37 +60,34 @@ path_or_url_for_links(target.first, path_or_url, options) else return LINK_TO_EMPTY_LINKLIST end elsif target.is_a?(Binary) - target.url + binary_url(target, path_or_url) else raise "scrivito_path or scrivito_url was called with an instance of #{target.class}. "+ "It must only be called with an Obj or a Link or a non-empty LinkList." end end - def path_or_url_for_links(link, path_or_url, options = {}) + def path_or_url_for_links(link, path_or_url, options) url = basic_url_or_path_for_link(link, path_or_url, - Rack::Utils.parse_nested_query(link.query).merge(options)) + Rack::Utils.parse_nested_query(link.query).merge(options)) url = url + "##{link.fragment}" if link.fragment.present? url end - def path_or_url_for_objs(obj, path_or_url, options = {}) - if editing_context.display_mode == "editing" - return id_path_or_url_for_objs(obj, path_or_url, options) - end - + def path_or_url_for_objs(obj, path_or_url, options) permalink = obj.permalink if permalink - main_app.public_send("scrivito_permalink_#{path_or_url}", options.merge(:permalink => permalink)) + main_app + .public_send("scrivito_permalink_#{path_or_url}", options.merge(:permalink => permalink)) elsif obj.homepage? main_app.public_send("scrivito_root_#{path_or_url}", options) elsif obj.binary? - if obj.binary_url - obj.binary_url + if binary = obj.binary + binary_url(binary, path_or_url) else LINK_TO_EMPTY_BLOB end else slug = obj.slug.present? ? obj.slug.sub(/^\//, '') : nil @@ -110,11 +111,11 @@ def editing_context EditingContextMiddleware.from_request(request) end - def cms_with_editing_context?(target) + def needs_editing_context?(target) editor_authenticated? && (!target.is_a?(Link) || target.internal?) end def basic_url_or_path_for_link(link, path_or_url, options = {}) if link.internal? @@ -136,9 +137,31 @@ parsed_url.to_s end def extract_query(uri) Rack::Utils.parse_query(uri.query) + end + + def binary_url(binary, path_or_url) + binary = transform_binary(binary) + if url_from_cache = binary.url_from_cache + BinaryRewrite.call(request, url_from_cache) + else + encrypted_params = BinaryParamVerifier.generate(binary) + scrivito_engine.public_send("binary_#{path_or_url}", encrypted_params: encrypted_params) + end + end + + def transform_binary(binary) + if transformation_definition = image_options[:transform] + binary.transform(transformation_definition) + else + binary + end + end + + def image_options + super || {} end end end