module EmbedMe::LinkHelper

Public Instance Methods

current_page_embed_url() click to toggle source

Checks if there is an embedded version for the current request. If this is the case, the link to the embedded version is returned. If there is no embedded version, nil is returned.

Examples

Assuming the current request IS embeddable: (E.g. root_path)

current_page_embed_url()
# => http://localhost:3000/embed

(E.g. post_path(id: 1))

current_page_embed_url()
# => http://localhost:3000/embed/posts/1

Assuming the current request is NOT embeddable: (E.g. private_path)

current_page_embed_url()
# => nil
# File lib/embed_me/link_helper.rb, line 105
def current_page_embed_url
  # return if no link available
  return nil unless embedded_link_available?(request.path)

  # get current link
  current_page = merged_embedded(request.path)
  current_page.merge!({only_path: false})
  url_for(current_page)
end
merged_embedded(url_data) click to toggle source

Extracts the Route generation parameters and merges the embedded value, which is used to generate embedding specific URLs. Returns a hash containing url generation data.

Examples

merged_embedded(posts_path)
# => {:controller=>"posts", :action=>"index", :embedded=>true}

merged_embedded(controller: "posts", action: "index")
# => {:controller=>"posts", :action=>"index", :embedded=>true}
# File lib/embed_me/link_helper.rb, line 74
def merged_embedded(url_data)
  # extract link parameters
  original_link = url_for(url_data)
  original_link_params = Rails.application.routes.recognize_path(original_link)

  # merge embedded flag
  merged_params = original_link_params.merge({embedded: true})
  merged_params
end