Skip to content

Commit

Permalink
Merge pull request #1767 from stefanvermaas/main
Browse files Browse the repository at this point in the history
Allow more control over the application layout
  • Loading branch information
matteodepalo authored Feb 8, 2024
2 parents 4ed3e7f + ee081c7 commit 8fc434a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
21 changes: 21 additions & 0 deletions docs/shopify_app/controller-concerns.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,26 @@ Implements Rails' [protect_from_forgery](https://api.rubyonrails.org/classes/Act
#### EmbeddedApp
If your ShopifyApp configuration has the `embedded_app` config set to true, [P3P header](https://www.w3.org/P3P/) and [content security policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) are handled for you.

By default, the `EmbeddedApp` concern also sets the layout file to be `app/views/layouts/embedded_app.html.erb`.

Sometimes one wants to run an embedded app in non-embedded mode. For example:

- When the remote environment is a CI;
- When the remote environment is a preview/PR app;
- When the developer wants to run the app in a non-embedded mode for testing.

To use the same application layout for every application controller, a developer can now overwrite the `#use_embedded_app_layout?` method.

```ruby
class ApplicationController
# Ensures every controller is using the standard app/views/layouts/application.html.erb layout.
#
# @return [true, false]
def use_embedded_app_layout?
false
end
end
```

#### EnsureBilling
If billing is enabled for the app, the active payment for the session is queried and enforced if needed. If billing is required the user will be redirected to a page requesting payment.
16 changes: 12 additions & 4 deletions lib/shopify_app/controller_concerns/embedded_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ module EmbeddedApp
include ShopifyApp::FrameAncestors

included do
if ShopifyApp.configuration.embedded_app?
after_action(:set_esdk_headers)
layout("embedded_app")
end
layout :embedded_app_layout
after_action :set_esdk_headers, if: -> { ShopifyApp.configuration.embedded_app? }
end

protected

def use_embedded_app_layout?
ShopifyApp.configuration.embedded_app?
end

private

def embedded_app_layout
"embedded_app" if use_embedded_app_layout?
end

def set_esdk_headers
response.set_header("P3P", 'CP="Not used"')
response.headers.except!("X-Frame-Options")
Expand Down
66 changes: 66 additions & 0 deletions test/controllers/concerns/embedded_app_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require "test_helper"
require "action_view/testing/resolvers"

class EmbeddedAppTest < ActionController::TestCase
class BaseTestController < ActionController::Base
abstract!

self.view_paths = [
ActionView::FixtureResolver.new(
"layouts/application.erb" => "Application Layout <%= yield %>",
"layouts/embedded_app.erb" => "Embedded App Layout <%= yield %>",
"embedded_app_test/embedded_app_test/index.html.erb" => "OK",
),
]
end

class ApplicationTestController < BaseTestController
layout "application"
end

class EmbeddedAppTestController < ApplicationTestController
include ShopifyApp::EmbeddedApp

def index; end
end

tests EmbeddedAppTestController

setup do
Rails.application.routes.draw do
get "/embedded_app", to: "embedded_app_test/embedded_app_test#index"
end
end

test "uses the embedded app layout when running in embedded mode" do
ShopifyApp.configuration.embedded_app = true

get :index
assert_template layout: "embedded_app"
end

test "uses the default layout when running in non-embedded mode" do
ShopifyApp.configuration.embedded_app = false

get :index
assert_template layout: "application"
end

test "sets the ESDK headers when running in embedded mode" do
ShopifyApp.configuration.embedded_app = true

get :index
assert_equal @controller.response.headers["P3P"], 'CP="Not used"'
assert_not_includes @controller.response.headers, "X-Frame-Options"
end

test "does not touch the ESDK headers when running in non-embedded mode" do
ShopifyApp.configuration.embedded_app = false

get :index
assert_not_includes @controller.response.headers, "P3P"
assert_includes @controller.response.headers, "X-Frame-Options"
end
end

0 comments on commit 8fc434a

Please sign in to comment.