Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RobinDaugherty/d4b67afe1919a33051d0 to your computer and use it in GitHub Desktop.
Save RobinDaugherty/d4b67afe1919a33051d0 to your computer and use it in GitHub Desktop.
ember_cli_deploy_redis_ruby v1.0.0 Rails controller implementation
class ProjectNamePageController < BaseEmberCliDeployController
def index
render_page('index.html')
end
# This is a page that is needed in our application for integration with PicMonkey.
# It is built in our ember-cli project and deployed alongside `index.html`.
def picmonkey_done
render_page('picmonkey-done.html')
end
private
# It's recommended that developers use a standard hostname in development for many reasons,
# but for example so that this works without modification by each developer. This is the
# hostname that the project runs on in development.
def ember_dev_hostname
"projectname.dev"
end
def ember_application_name
"projectname"
end
end
require 'net/http'
class BaseEmberCliDeployController < ApplicationController
disable_forgery_protection only: %i(live_reload)
def live_reload
if Rails.configuration.ember_cli_project_development
render text: dev_page_contents('ember-cli-live-reload.js'), content_type: Mime::JS
else
render_error :not_found
end
end
private
def dev_url
"http#{'s' if request.ssl?}://#{ember_dev_hostname}"
end
def asset_corrected_dev_html(html)
html.gsub('assets/', "#{dev_url}/assets/")
end
def active_revision
@active_revision ||= deployed_application.active_revision
end
def deployed_application
@deployed_application ||= EmberCliDeployRedis::Application.new(ember_application_name)
end
def specified_revision
specified_revision_name = params[:_ember_app].try(:[], deployed_application.name)
return nil unless specified_revision_name
@specified_revision ||= deployed_application.revision_by_name specified_revision_name
end
# Render the contents of a specific page that was deployed by ember, or if in development,
# the page contents fetched directly from the ember app running locally, with its
# asset references modified so that they will be loaded from the local ember app by the browser.
# If a block is given, the block is used to modify the content before it is rendered.
def render_page(page, content_type = Mime::HTML)
content = page_contents(page)
content = yield(content) if block_given?
render text: content, content_type: content_type
end
def page_contents(page)
if specified_revision
specified_revision.contents_of(page)
elsif Rails.configuration.ember_cli_project_development
asset_corrected_dev_html(dev_page_contents(page))
elsif active_revision
active_revision.contents_of(page)
else
fail "No deployed #{ember_application_name} revision"
end
end
def dev_page_contents(page)
uri = URI("#{dev_url}/#{page}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body
end
end
require 'spec_helper'
describe BaseEmberCliDeployController do
let(:controller) { described_class.new }
let(:ember_application_name) { 'app_name' }
let(:ember_dev_hostname) { 'projectname.dev' }
let(:request) { double('request', ssl?: request_is_ssl, parameters: params) }
let(:request_is_ssl) { false }
let(:params) { {} }
before do
allow(controller).to receive(:ember_application_name).and_return(ember_application_name)
allow(controller).to receive(:ember_dev_hostname).and_return(ember_dev_hostname)
allow(controller).to receive(:request).and_return(request)
end
describe '#render_page' do
let(:do_the_thing) { controller.send(:render_page, page) }
let(:page) { 'test-page.html' }
context 'with a block' do
let(:html) { double('html') }
let(:modified_html) { double('modified_html') }
before do
allow(controller).to receive(:page_contents).with(page).and_return(html)
allow(controller).to receive(:render)
end
it 'gives the html to the block' do
controller.send(:render_page, page) do |html_in|
expect(html_in).to eq(html)
end
end
it 'returns the response from the block' do
expect(controller).to receive(:render).with(text: modified_html, content_type: Mime::HTML)
controller.send(:render_page, page) do |_|
modified_html
end
end
end
context 'with ember_cli_project_development enabled' do
before do
Rails.configuration.ember_cli_project_development = true
end
after do
Rails.configuration.ember_cli_project_development = false
end
before do
allow(controller).to receive(:dev_page_contents).and_return(html)
allow(controller).to receive(:asset_corrected_dev_html).and_return(corrected_html)
end
let(:html) { double('html') }
let(:corrected_html) { double('corrected_html') }
it 'calls render with the page html as text' do
expect(controller).to receive(:dev_page_contents)
.with(page)
expect(controller).to receive(:asset_corrected_dev_html)
.with(html)
expect(controller).to receive(:render).with(text: corrected_html, content_type: Mime::HTML)
do_the_thing
end
context 'with an alternate content type' do
let(:do_the_thing) { controller.send(:render_page, page, Mime::JS) }
it 'sends the correct content type' do
expect(controller).to receive(:render).with(text: anything, content_type: Mime::JS)
do_the_thing
end
end
context 'when a parameter specifies a revision' do
let(:specified_revision) { double('specified_revision', contents_of: true) }
it 'renders the contents of the page from the specified revision' do
allow(controller).to receive(:specified_revision).and_return(specified_revision)
expect(specified_revision).to receive(:contents_of).with(page)
.and_return('specified_revision_html')
expect(controller)
.to receive(:render).with(text: 'specified_revision_html', content_type: Mime::HTML)
do_the_thing
end
end
end
context 'with ember_cli_project_development disabled' do
context 'when no parameter specifies ember app revision' do
let(:active_revision) { double('active_revision', contents_of: true) }
before do
allow(controller).to receive(:active_revision).and_return(active_revision)
allow(controller).to receive(:render)
end
it 'gets the active Revision' do
expect(controller).to receive(:active_revision)
do_the_thing
end
context 'with a deployed Revision' do
let(:html) { double('html') }
it 'renders the contents of the page from the Revision' do
expect(active_revision).to receive(:contents_of).with(page)
.and_return(html)
expect(controller).to receive(:render).with(text: html, content_type: Mime::HTML)
do_the_thing
end
context 'with the page missing from the deployment' do
before do
expect(active_revision).to receive(:contents_of).with(page)
.and_raise(EmberCliDeployRedis::FileMissingFromDeployment)
end
it 'raises an exception' do
expect { do_the_thing }.to raise_error
end
end
end
context 'without a deployed Revision' do
let(:active_revision) { nil }
it 'raises an exception' do
expect { do_the_thing }.to raise_error
end
end
end
context 'when a parameter specifies a revision' do
let(:specified_revision) { double('specified_revision', contents_of: true) }
it 'renders the contents of the page from the specified revision' do
allow(controller).to receive(:specified_revision).and_return(specified_revision)
expect(specified_revision).to receive(:contents_of).with(page)
.and_return('specified_revision_html')
expect(controller)
.to receive(:render).with(text: 'specified_revision_html', content_type: Mime::HTML)
do_the_thing
end
end
end
end
describe '#asset_corrected_dev_html' do
let(:html) { 'test assets/stuff.css test' }
context 'when the client is not connected via SSL' do
let(:replaced_html) { 'test http://projectname.dev/assets/stuff.css test' }
subject { controller.send(:asset_corrected_dev_html, html) }
it 'modifies the HTML provided' do
expect(subject).to eq(replaced_html)
end
end
context 'when the client is connected via SSL' do
let(:request_is_ssl) { true }
let(:replaced_html) { 'test https://projectname.dev/assets/stuff.css test' }
subject { controller.send(:asset_corrected_dev_html, html) }
it 'modifies the HTML provided' do
expect(subject).to eq(replaced_html)
end
end
end
describe '#active_revision' do
subject { controller.send(:active_revision) }
let(:active_revision) { double('active_revision') }
let(:deployed_application) { double('deployed_application') }
before do
allow(controller).to receive(:deployed_application)
.and_return(deployed_application)
end
it 'returns the active Revision' do
expect(deployed_application).to receive(:active_revision).and_return(active_revision)
expect(subject).to eq(active_revision)
end
context 'when no Revision exists' do
it 'returns nil' do
expect(deployed_application).to receive(:active_revision).and_return(nil)
expect(subject).to be_nil
end
end
end
describe 'specified_revision' do
subject { controller.send(:specified_revision) }
let(:active_revision) { double('active_revision') }
let(:deployed_application) { double('deployed_application', name: 'application_name') }
let(:specific_revision) { double('specific_revision') }
let(:specific_revision_name) { double('specific_revision_name') }
before do
allow(controller).to receive(:deployed_application).and_return(deployed_application)
allow(deployed_application).to receive(:revision_by_name).and_return(specific_revision)
end
context 'when a revision is specified' do
let(:params) { { _ember_app: { 'application_name' => 'specific_revision_name' } } }
it 'returns the active Revision' do
expect(deployed_application).to receive(:revision_by_name).with('specific_revision_name')
expect(subject).to eq(specific_revision)
end
end
context 'when a revision is specified for a different app' do
let(:params) { { _ember_app: { 'different_application_name' => 'specific_revision_name' } } }
it 'returns nil' do
expect(deployed_application).to_not receive(:revision_by_name)
expect(subject).to be_nil
end
end
context 'when no revision is specified for any app' do
it 'returns nil' do
expect(deployed_application).to_not receive(:revision_by_name)
expect(subject).to be_nil
end
end
end
describe '#dev_page_contents' do
let(:html) { 'testtesttest' }
let!(:html_request) {
stub_request(:get, %r{http://projectname.dev/test-page.html})
.to_return(body: html, status: 200)
}
subject { controller.send(:dev_page_contents, 'test-page.html') }
it 'requests the requested page from projectname.dev' do
subject
expect(html_request).to have_been_made.once
end
it { is_expected.to eq(html) }
end
end
require 'rails_helper'
describe ProjectnamePageController do
let(:json_body) { JSON.parse(response.body) if response.body.present? }
context "route for index" do
it 'points to the index action' do
expect(get: "/")
.to route_to(controller: 'projectname_page', action: 'index')
end
end
context "route for signed-out" do
it 'points to the signed_out action' do
expect(get: "/signed-out")
.to route_to(controller: 'projectname_page', action: 'signed_out')
end
end
context "route for 404" do
it 'points to the index action' do
expect(get: "/something-that-doesnt-exist")
.to route_to(
controller: 'projectname_page',
action: 'index',
not_found: "something-that-doesnt-exist",
)
end
end
context "route for '/picmonkey-done.html'" do
it "points to the picmonkey_done action" do
expect(get: "/picmonkey-done.html")
.to route_to(controller: 'projectname_page', action: 'picmonkey_done')
end
end
context "route for '/ember-cli-live-reload.js'" do
it "points to the live_reload action" do
expect(get: "/ember-cli-live-reload.js")
.to route_to(controller: 'projectname_page', action: 'live_reload')
end
end
context "GET index" do
context "as HTML" do
let(:the_request) { get 'index', format: 'html' }
before do
allow(controller).to receive(:render_page) do
controller.instance_eval do
render text: 'testtest'
end
end
end
it "returns html" do
the_request
expect(response).to have_content_type(Mime::HTML)
expect(response.body).to eq('testtest')
end
it 'uses #render_page' do
the_request
expect(controller).to have_received(:render_page).with('index.html')
end
end
end
context "GET picmonkey_done" do
context "as HTML" do
let(:the_request) { get 'picmonkey_done', format: 'html' }
before do
allow(controller).to receive(:render_page) do
controller.instance_eval do
render text: 'testtest'
end
end
end
it "returns html" do
the_request
expect(response).to have_content_type(Mime::HTML)
expect(response.body).to eq('testtest')
end
it 'uses #render_page' do
the_request
expect(controller).to have_received(:render_page).with('picmonkey-done.html')
end
end
end
context "GET live_reload" do
let(:the_request) { get 'live_reload' }
context 'with ember_cli_project_development enabled' do
before do
Rails.configuration.ember_cli_project_development = true
allow(controller).to receive(:dev_page_contents).and_return('testtest')
end
after do
Rails.configuration.ember_cli_project_development = false
end
it "returns javascript" do
the_request
expect(response).to have_content_type(Mime::JS)
expect(response.body).to eq('testtest')
end
it 'uses #dev_page_contents' do
the_request
expect(controller).to have_received(:dev_page_contents).with('ember-cli-live-reload.js')
end
end
context 'with ember_cli_project_development disabled' do
it "returns a 404" do
the_request
expect(response).to have_http_status(:not_found)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment