lib/scrivito/cms_routing.rb in scrivito_sdk-1.0.0 vs lib/scrivito/cms_routing.rb in scrivito_sdk-1.1.0.rc1
- old
+ new
@@ -1,15 +1,11 @@
module Scrivito
-class CmsRouting < Struct.new(:request, :main_app, :scrivito_engine, :image_options)
+class CmsRouting < Struct.new(:request, :context, :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 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)
@@ -24,17 +20,18 @@
if $2.present?
begin
uri = URI.parse($2)
options.merge!(extract_query(uri))
- options['anchor'] = uri.fragment
+ options[:anchor] = uri.fragment
rescue
end
end
if editing_context.display_mode == 'editing'
- id_path_or_url_for_objs(obj, :path, options)
+ options[:id] = obj.id
+ scrivito_engine.base_id_path(options)
else
path_or_url(obj, :path, options)
end
else
"#__target_object_not_reachable"
@@ -68,44 +65,51 @@
"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)
- url = basic_url_or_path_for_link(link, path_or_url,
- Rack::Utils.parse_nested_query(link.query).merge(options))
+ parsed_options = Rack::Utils.parse_nested_query(link.query)
+ combined_options = parsed_options.symbolize_keys.merge(options)
+
+ url = basic_url_or_path_for_link(link, path_or_url, combined_options)
url = url + "##{link.fragment}" if link.fragment.present?
url
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))
- elsif homepage?(obj)
- main_app.public_send("scrivito_root_#{path_or_url}", options)
+ if permalink && route_defined?(:permalink)
+ use_route(:permalink, path_or_url, options.merge(:permalink => permalink))
+ elsif homepage?(obj) && route_defined?(:homepage)
+ use_route(:homepage, path_or_url, options)
elsif obj.binary?
binary_obj_url(obj) || LINK_TO_EMPTY_BLOB
- else
+ elsif route_defined?(:slug_id)
slug = obj.slug.present? ? obj.slug.sub(/^\//, '') : nil
id_path_or_url_for_objs(obj, path_or_url, options.merge(slug: slug))
+ else
+ raise ScrivitoError, "The required scrivito route 'slug_id' is not defined. "\
+ "Please add a 'slug_id' definition to your routes.rb. See the documentation"\
+ " of 'scrivito_route' for further details."
end
end
def id_path_or_url_for_objs(obj, path_or_url, options)
- method_name = if Scrivito::Configuration.legacy_routing
- "cms_legacy_id_#{path_or_url}"
- else
- "cms_id_#{path_or_url}"
- end
-
options[:id] = obj.id
# Options must have the key slug.
# Otherwise Rails will use the slug from current request params.
options[:slug] ||= nil
- main_app.public_send(method_name, options)
+ use_route(:slug_id, path_or_url, options)
+ end
+
+ def route_defined?(name)
+ Route.defined?(context._routes, name)
+ end
+
+ def use_route(name, path_or_url, options)
+ Route.find(context._routes, name).generate(context, path_or_url, options)
end
def homepage?(obj)
RequestHomepage.call(request.env) == obj
end