diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a76fc..204fd4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ master ------ +* Integrate with `ember-cli-rails` deployment strategies. [#4] +* Rename `EmberCLI` module to `EmberCli`. [#4] +* Rename `ember-cli` directories to `ember_cli`. [#4] + +[#4]: https://github.com/seanpdoyle/ember-cli-rails-deploy-redis/pull/4 + 0.0.2 ----- diff --git a/README.md b/README.md index a6042b8..1828e83 100644 --- a/README.md +++ b/README.md @@ -1,119 +1,126 @@ -# EmberCLI::Rails - Deploy Redis +# EmberCli::Deploy::Redis -[EmberCLI Rails] is an integration story between (surprise suprise) EmberCLI and -Rails 3.1 and up. +[EmberCLI Rails] is a tool to unify your EmberCLI and Rails Workflows. [ember-cli-deploy] is a simple, flexible deployment for your Ember CLI app. -[ember-deploy-redis] is the redis-adapter implementation to use Redis with ember-deploy. +[ember-cli-deploy-redis] is an `ember-cli-deploy` plugin to upload `index.html` +to a Redis store. -`ember-cli-rails-deploy-redis` wires up all three. +`ember-cli-rails-deploy-redis` is a gem that integrates all three. -[EmberCLI Rails]: https://github.com/rwz/ember-cli-rails -[ember-cli-deploy]: https://github.com/ember-cli/ember-cli-deploy -[ember-deploy-redis]: https://github.com/LevelbossMike/ember-deploy-redis +This project streamlines the process of pushing and serving EmberCLI-built +assets from Rails. -## Installation +[EmberCLI Rails]: https://github.com/thoughtbot/ember-cli-rails +[ember-cli-deploy]: http://ember-cli.com/ember-cli-deploy/ +[ember-cli-deploy-redis]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis + +## Install Add this line to your application's Gemfile: ```ruby -gem 'ember-cli-rails-deploy-redis' +group :production do + gem "ember-cli-rails-deploy-redis" +end ``` And then execute: ```bash -$ bundle +$ bundle install ``` -## Usage +## Setup -The EmberCLI community recently unified the various deployment techniques into a -single, core-team supported project: [ember-cli-deploy][ember-cli-deploy]. +First, [configure your application to integrate with +`ember-cli-rails`][ember-cli-rails-setup]. -This project attempts to streamline the process of pushing and serving -EmberCLI-built static assets. +Then, to integrate with `ember-cli-deploy`'s ["Lightning Fast Deploys"][lightning] +(using the Redis adapter) in `production`, instantiate configure EmberCLI-Rails +to deploy with the `EmberCli::Deploy::Redis` strategy: -To integrate with `ember-cli-deploy`'s ["Lightning Fast Deploys"][lightning] -(using the Redis adapter), instantiate an `EmberCLI::Deploy::Redis` -in your controller: +[ember-cli-rails-setup]: https://github.com/thoughtbot/ember-cli-rails#setup ```ruby -require "ember-cli/deploy/redis" - -class ApplicationController < ActionController::Base - def index - @deploy = EmberCLI::Deploy::Redis.new(namespace: "frontend") +# config/initializers/ember.rb - render text: @deploy.html, layout: false - end +EmberCli.configure do |config| + config.app :frontend, deploy: { production: EmberCli::Deploy::Redis } end ``` -`EmberCLI::Deploy::Redis` takes a `namespace` (the name of your app declared in -your initializer) and handles all interaction with the Redis instance. +Finally, set `ENV["REDIS_URL"]` to the URL [ember-cli-deploy-redis references][redis-config]. -This is great for `staging` and `production` deploys, but introduces an extra -step in the feedback loop during development. +If you're deploying from Redis in `development` or `test`, disable +EmberCLI-Rails' build step by setting `ENV["SKIP_EMBER"] = true`. -Luckily, `EmberCLI::Deploy::Redis` also accepts an `index_html` override, which -will replace the call to the Redis instance. This allows integration with the -normal `ember-cli-rails` workflow: +[redis-config]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis#configuration-options -```ruby -require "ember-cli/deploy/redis" +## Deploy -class ApplicationController < ActionController::Base - def index - @deploy = EmberCLI::Deploy::Redis.new( - namespace: "frontend", - index_html: index_html, - ) +Deploy your application through [ember-cli-deploy-redis][deploy]. - render text: @deploy.html, layout: false - end +[deploy]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis#quick-start - private +## Use without `ember-cli-rails` - def index_html - if serve_with_ember_cli_rails? - render_to_string(:index) - end - end +Although this gem was designed to integrate with `ember-cli-rails`, there isn't +a direct dependency on it. - def serve_with_ember_cli_rails? - ! %w[production staging].include?(Rails.env) - end -end -``` +Similar behavior can be achieved by using a gem like [`html_page`][html_page]: -Additionally, having access to the outbound HTML beforehand also enables -controllers to inject additional markup, such as metadata, CSRF tokens, or -analytics tags: +[html_page]: https://github.com/seanpdoyle/html_page +```rb +require "html_page" -```ruby -require "ember-cli/deploy" +class EmberHelper + def render_html(html) + capturer = HtmlPage::Capture.new(self, &block) -class ApplicationController < ActionController::Base - def index - @deploy = EmberCLI::Deploy::Redis.new( - namespace: "frontend", - index_html: index_html, + head, body = capturer.capture + + renderer = HtmlPage::Renderer.new( + content: html, + head: head, + body: body, ) - @deploy.append_to_head(render_to_string(partial: "my_csrf_and_metadata") - @deploy.append_to_body(render_to_string(partial: "my_analytics") + render inline: renderer.render + end +end + +class EmberController + def index + redis = EmberCli::Deploy::Redis.new(ember_app) - render text: @deploy.html, layout: false + @html_from_redis = redis.index_html + + render layout: false + end + + private + + def ember_app + OpenStruct.new(name: "my-ember-app") end - # ... end ``` -[ember-cli-deploy]: https://github.com/ember-cli/ember-cli-deploy -[lightning]: https://github.com/ember-cli/ember-cli-deploy#lightning-approach-workflow +```erb + +<%= render_html @html_from_redis do |head, body| %> + <% head.append do %> +
Appended to the `body` element
+ <% end %> +<% end %> +``` ## Development diff --git a/ember-cli-rails-deploy-redis.gemspec b/ember-cli-rails-deploy-redis.gemspec index ba92e52..63d2831 100644 --- a/ember-cli-rails-deploy-redis.gemspec +++ b/ember-cli-rails-deploy-redis.gemspec @@ -1,11 +1,11 @@ # coding: utf-8 lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "ember-cli/deploy/version" +require "ember_cli/deploy/redis/version" Gem::Specification.new do |spec| spec.name = "ember-cli-rails-deploy-redis" - spec.version = EmberCLI::Deploy::VERSION + spec.version = EmberCli::Deploy::Redis::VERSION spec.authors = ["Sean Doyle"] spec.email = ["sean.p.doyle24@gmail.com"] diff --git a/lib/ember-cli/deploy/page.rb b/lib/ember-cli/deploy/page.rb deleted file mode 100644 index 8de5b54..0000000 --- a/lib/ember-cli/deploy/page.rb +++ /dev/null @@ -1,55 +0,0 @@ -module EmberCLI - module Deploy - class Page - def initialize(html:) - @html = html.clone - @body_markup = [] - @head_markup = [] - end - - def build - if has_head? - head_markup.each do |markup| - html.insert(head_position, markup) - end - end - - if has_body? - body_markup.each do |markup| - html.insert(body_position, markup) - end - end - - html - end - - def append_to_body(markup) - body_markup << markup - end - - def append_to_head(markup) - head_markup << markup - end - - private - - attr_reader :body_markup, :head_markup, :html - - def has_head? - html.include?("") - - page.append_to_body("") - page.append_to_body("") - - expect(page.build).to eq( - "" - ) - end - - it "appends to the head" do - page = build_page("Hello World
" - ember_cli_deploy = build_ember_cli_deploy(index_html: provided_html) - - index_html = ember_cli_deploy.html - - expect(index_html).to eq(provided_html) - end - end - - context "when the keys are present" do - it "retrieves the HTML from Redis" do - stub_index_html(html: "Hello World
") - ember_cli_deploy = build_ember_cli_deploy - - index_html = ember_cli_deploy.html - - expect(index_html).to eq("Hello World
") - end - end - - context "when the current index is missing" do - it "raises a helpful exception" do - deploy_key = "abc123" - stub_index_html(html: nil, deploy_key: deploy_key) - ember_cli_deploy = build_ember_cli_deploy - - expect { ember_cli_deploy.html }.to raise_error( - /HTML for #{namespace}:#{deploy_key} is missing/ - ) - end - end - - context "when the current key is unset" do - it "raises a helpful exception" do - stub_current_key(nil) - ember_cli_deploy = build_ember_cli_deploy - - expect { ember_cli_deploy.html }.to raise_error( - /#{namespace}:current is empty/ - ) - end - end - end - - around :each do |example| - with_modified_env REDIS_URL: "redis://localhost:1234" do - example.run - end - end - - def build_ember_cli_deploy(index_html: nil) - EmberCLI::Deploy::Redis.new(namespace: namespace, index_html: index_html) - end - - def namespace - "human-health" - end - - def stub_current_key(deploy_key) - current_key = "#{namespace}:current" - - redis.set(current_key, deploy_key) - end - - def stub_index_html(deploy_key: "123", html:) - stub_current_key(deploy_key) - redis.set("#{namespace}:#{deploy_key}", html) - end - - def redis - Redis.new(url: ENV["REDIS_URL"]) - end - - def with_modified_env(options, &block) - ClimateControl.modify(options, &block) - end -end diff --git a/spec/lib/ember_cli/deploy/redis_spec.rb b/spec/lib/ember_cli/deploy/redis_spec.rb new file mode 100644 index 0000000..aa5ba4e --- /dev/null +++ b/spec/lib/ember_cli/deploy/redis_spec.rb @@ -0,0 +1,74 @@ +require "spec_helper" +require "ember_cli/deploy/redis" + +describe EmberCli::Deploy::Redis do + describe "#index_html" do + context "when the keys are present" do + it "retrieves the HTML from Redis" do + stub_index_html(html: "Hello World
") + ember_cli_deploy = build_ember_cli_deploy + + index_html = ember_cli_deploy.index_html + + expect(index_html).to eq("Hello World
") + end + end + + context "when the current index is missing" do + it "raises a helpful exception" do + deploy_key = "abc123" + stub_index_html(html: nil, deploy_key: deploy_key) + ember_cli_deploy = build_ember_cli_deploy + + expect { ember_cli_deploy.index_html }.to raise_error( + /HTML for #{namespace}:#{deploy_key} is missing/ + ) + end + end + + context "when the current key is unset" do + it "raises a helpful exception" do + stub_current_key(nil) + ember_cli_deploy = build_ember_cli_deploy + + expect { ember_cli_deploy.index_html }.to raise_error( + /#{namespace}:current is empty/ + ) + end + end + end + + around :each do |example| + with_modified_env REDIS_URL: "redis://localhost:1234" do + example.run + end + end + + def build_ember_cli_deploy + app = double(name: namespace) + EmberCli::Deploy::Redis.new(app) + end + + def namespace + "ember-cli-rails-deploy-redis" + end + + def stub_current_key(deploy_key) + current_key = "#{namespace}:current" + + redis.set(current_key, deploy_key) + end + + def stub_index_html(deploy_key: "123", html:) + stub_current_key(deploy_key) + redis.set("#{namespace}:#{deploy_key}", html) + end + + def redis + Redis.new(url: ENV["REDIS_URL"]) + end + + def with_modified_env(options, &block) + ClimateControl.modify(options, &block) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c61149b..7fc448c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) -require "ember-cli/deploy/redis" +require "ember_cli/deploy/redis" require "fakeredis" require "climate_control"