Module: EmbedMe::LinkHelper
- Included in:
- CodeGenerator
- Defined in:
- lib/embed_me/link_helper.rb
Instance Method Summary collapse
-
#current_page_embed_url ⇒ Object
Checks if there is an embedded version for the current request.
-
#embeddable_link_to(name = nil, options = nil, html_options = {}, &block) ⇒ Object
Creates an HTML link element.
-
#embedded_link_available?(url_data) ⇒ Boolean
Checks whether there is an embedded version of a specific route.
-
#merged_embedded(url_data) ⇒ Object
Extracts the Route generation parameters and merges the embedded value, which is used to generate embedding specific URLs.
Instance Method Details
#current_page_embed_url ⇒ Object
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)
()
# => http://localhost:3000/embed
(E.g. post_path(id: 1))
()
# => http://localhost:3000/embed/posts/1
Assuming the current request is NOT embeddable: (E.g. private_path)
()
# => nil
105 106 107 108 109 110 111 112 113 |
# File 'lib/embed_me/link_helper.rb', line 105 def # return if no link available return nil unless (request.path) # get current link current_page = (request.path) current_page.merge!({only_path: false}) url_for(current_page) end |
#embeddable_link_to(name = nil, options = nil, html_options = {}, &block) ⇒ Object
Creates an HTML link element. To do this, it checks whether the request comes from an embedded resource and whether there is an embedded version of the requested route. If both are true, a link is generated that redirects to the embedded version. If no embedded version of the route exists, but the request is within an embedding, the link will be opened in a new tab. If the request is not embedded, the link is created as normal.
Examples
Assuming the current request is NOT embedded, then:
("Not Embeddable", private_path)
# => <a href="/private">Not Embeddable</a>
("Embeddable", )
# => <a href="/embeddable">Embeddable</a>
Assuming the current request IS embedded, then:
("Not Embeddable", private_path)
# => <a target="_blank" href="/private">Not Embeddable</a>
("Embeddable", )
# => <a href="/embed/embeddable">Embeddable</a>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/embed_me/link_helper.rb', line 26 def (name = nil, = nil, = {}, &block) if && () # generate embedded link if possible merged_params = () link_to(name, merged_params, , &block) elsif # open link in new tab if no embedded link available .merge!({target: "_blank"}) link_to(name, , , &block) else # normal behaviour if not embedded link_to(name, , , &block) end end |
#embedded_link_available?(url_data) ⇒ Boolean
Checks whether there is an embedded version of a specific route. Returns a boolean Value.
Examples
(private_path)
# => false
(root_path)
# => true
()
# => true
54 55 56 57 58 59 60 61 |
# File 'lib/embed_me/link_helper.rb', line 54 def (url_data) merged_params = (url_data) # test if embedded flag is in path or url parameter = url_for(merged_params) = Rails.application.routes.recognize_path() [:embedded].present? end |
#merged_embedded(url_data) ⇒ Object
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
(posts_path)
# => {:controller=>"posts", :action=>"index", :embedded=>true}
(controller: "posts", action: "index")
# => {:controller=>"posts", :action=>"index", :embedded=>true}
74 75 76 77 78 79 80 81 82 |
# File 'lib/embed_me/link_helper.rb', line 74 def (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 |