Skip to content

Commit 74b00b7

Browse files
committed
Integrate with ember-cli-rails
EmberCLI-Rails supports configuring alternative [deployment strategies]. Modify the gem to properly expose itself as a deployment strategy. [deployment strategies]:https://github.com/thoughtbot/ember-cli-rails#deployment-strategies
1 parent 6b66dbf commit 74b00b7

File tree

6 files changed

+106
-247
lines changed

6 files changed

+106
-247
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
master
22
------
33

4+
* Integrate with `ember-cli-rails` deployment strategies. [#4]
5+
* Rename `EmberCLI` module to `EmberCli`. [#4]
6+
* Rename `ember-cli` directories to `ember_cli`. [#4]
7+
8+
[#4]: https://github.com/seanpdoyle/ember-cli-rails-deploy-redis/pull/4
9+
410
0.0.2
511
-----
612

README.md

Lines changed: 77 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,127 @@
11
# EmberCli::Deploy::Redis
22

3-
[EmberCLI Rails] is an integration story between (surprise suprise) EmberCLI and
4-
Rails 3.1 and up.
3+
[EmberCLI Rails] is a tool to unify your EmberCLI and Rails Workflows.
54

65
[ember-cli-deploy] is a simple, flexible deployment for your Ember CLI app.
76

8-
[ember-deploy-redis] is the redis-adapter implementation to use Redis with ember-deploy.
7+
[ember-cli-deploy-redis] is an `ember-cli-deploy` plugin to upload `index.html`
8+
to a Redis store.
99

10-
`ember-cli-rails-deploy-redis` wires up all three.
10+
`ember-cli-rails-deploy-redis` is a gem that integrates all three.
1111

12-
[EmberCLI Rails]: https://github.com/rwz/ember-cli-rails
13-
[ember-cli-deploy]: https://github.com/ember-cli/ember-cli-deploy
14-
[ember-deploy-redis]: https://github.com/LevelbossMike/ember-deploy-redis
12+
This project streamlines the process of pushing and serving EmberCLI-built
13+
assets from Rails.
1514

16-
## Installation
15+
[EmberCLI Rails]: https://github.com/thoughtbot/ember-cli-rails
16+
[ember-cli-deploy]: http://ember-cli.com/ember-cli-deploy/
17+
[ember-cli-deploy-redis]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis
18+
19+
## Install
1720

1821
Add this line to your application's Gemfile:
1922

2023
```ruby
21-
gem 'ember-cli-rails-deploy-redis'
24+
group :production do
25+
gem "ember-cli-rails-deploy-redis"
26+
end
2227
```
2328

2429
And then execute:
2530

2631
```bash
27-
$ bundle
32+
$ bundle install
2833
```
2934

30-
## Usage
35+
## Setup
3136

32-
The EmberCLI community recently unified the various deployment techniques into a
33-
single, core-team supported project: [ember-cli-deploy][ember-cli-deploy].
37+
First, [configure your application to integrate with
38+
`ember-cli-rails`][ember-cli-rails-setup].
3439

35-
This project attempts to streamline the process of pushing and serving
36-
EmberCLI-built static assets.
40+
Then, to integrate with `ember-cli-deploy`'s ["Lightning Fast Deploys"][lightning]
41+
(using the Redis adapter) in `production`, instantiate configure EmberCLI-Rails
42+
to deploy with the `EmberCli::Deploy::Redis` strategy:
3743

38-
To integrate with `ember-cli-deploy`'s ["Lightning Fast Deploys"][lightning]
39-
(using the Redis adapter), instantiate an `EmberCli::Deploy::Redis`
40-
in your controller:
44+
[ember-cli-rails-setup]: https://github.com/thoughtbot/ember-cli-rails#setup
4145

4246
```ruby
43-
require "ember_cli/deploy/redis"
44-
45-
class ApplicationController < ActionController::Base
46-
def index
47-
@deploy = EmberCli::Deploy::Redis.new(namespace: "frontend")
47+
# config/initializers/ember.rb
4848

49-
render text: @deploy.html, layout: false
50-
end
49+
EmberCli.configure do |config|
50+
config.app :frontend, deploy: { production: EmberCli::Deploy::Redis }
5151
end
5252
```
5353

54-
`EmberCli::Deploy::Redis` takes a `namespace` (the name of your app declared in
55-
your initializer) and handles all interaction with the Redis instance.
54+
Finally, set:
5655

57-
This is great for `staging` and `production` deploys, but introduces an extra
58-
step in the feedback loop during development.
56+
* `ENV["REDIS_URL"]` to the URL [ember-cli-deploy-redis references][redis-config]
57+
* `ENV["SKIP_EMBER"] = 1` to skip EmberCLI compilation during deploys and
58+
requests
5959

60-
Luckily, `EmberCli::Deploy::Redis` also accepts an `index_html` override, which
61-
will replace the call to the Redis instance. This allows integration with the
62-
normal `ember-cli-rails` workflow:
60+
[redis-config]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis#configuration-options
6361

64-
```ruby
65-
require "ember_cli/deploy/redis"
62+
## Deploy
6663

67-
class ApplicationController < ActionController::Base
68-
def index
69-
@deploy = EmberCli::Deploy::Redis.new(
70-
namespace: "frontend",
71-
index_html: index_html,
72-
)
64+
Deploy your application through [ember-cli-deploy-redis][deploy].
7365

74-
render text: @deploy.html, layout: false
75-
end
66+
[deploy]: https://github.com/ember-cli-deploy/ember-cli-deploy-redis#quick-start
7667

77-
private
68+
## Use without `ember-cli-rails`
7869

79-
def index_html
80-
if serve_with_ember_cli_rails?
81-
render_to_string(:index)
82-
end
83-
end
70+
Although this gem was designed to integrate with `ember-cli-rails`, there isn't
71+
a direct dependency on it.
8472

85-
def serve_with_ember_cli_rails?
86-
! %w[production staging].include?(Rails.env)
87-
end
88-
end
89-
```
73+
Similar behavior can be achieved by using a gem like [`html_page`][html_page]:
9074

91-
Additionally, having access to the outbound HTML beforehand also enables
92-
controllers to inject additional markup, such as metadata, CSRF tokens, or
93-
analytics tags:
75+
[html_page]: https://github.com/seanpdoyle/html_page
9476

77+
```rb
78+
require "html_page"
9579

96-
```ruby
97-
require "ember_cli/deploy/redis"
80+
class EmberHelper
81+
def render_html(html)
82+
capturer = HtmlPage::Capture.new(self, &block)
9883

99-
class ApplicationController < ActionController::Base
100-
def index
101-
@deploy = EmberCli::Deploy::Redis.new(
102-
namespace: "frontend",
103-
index_html: index_html,
84+
head, body = capturer.capture
85+
86+
renderer = HtmlPage::Renderer.new(
87+
content: html,
88+
head: head,
89+
body: body,
10490
)
10591

106-
@deploy.append_to_head(render_to_string(partial: "my_csrf_and_metadata")
107-
@deploy.append_to_body(render_to_string(partial: "my_analytics")
92+
render inline: renderer.render
93+
end
94+
end
95+
96+
class EmberController
97+
def index
98+
redis = EmberCli::Deploy::Redis.new(ember_app)
10899

109-
render text: @deploy.html, layout: false
100+
@html_from_redis = redis.index_html
101+
102+
render layout: false
103+
end
104+
105+
private
106+
107+
def ember_app
108+
OpenStruct.new(name: "my-ember-app")
110109
end
111-
# ...
112110
end
113111
```
114112

115-
[ember-cli-deploy]: https://github.com/ember-cli/ember-cli-deploy
116-
[lightning]: https://github.com/ember-cli/ember-cli-deploy#lightning-approach-workflow
113+
```erb
114+
<!-- app/views/ember/index.html.erb -->
115+
<%= render_html @html_from_redis do |head, body| %>
116+
<% head.append do %>
117+
<title>Appended to the `head` element</title>
118+
<% end %>
119+
120+
<% body.append do %>
121+
<p>Appended to the `body` element</p>
122+
<% end %>
123+
<% end %>
124+
```
117125

118126
## Development
119127

lib/ember_cli/deploy/page.rb

Lines changed: 0 additions & 55 deletions
This file was deleted.

lib/ember_cli/deploy/redis.rb

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,54 @@
11
require "active_support/core_ext/object/blank"
2-
require "ember_cli/deploy/page"
32
require "redis"
43

54
module EmberCli
65
module Deploy
76
class Redis
8-
def initialize(namespace:, index_html: nil, redis_client: build_client)
9-
@redis_client = redis_client
10-
@namespace = namespace
11-
@index_html = index_html
12-
@body_markup = []
13-
@head_markup = []
7+
def initialize(app)
8+
@app = app
149
end
1510

16-
def append_to_body(markup)
17-
body_markup << markup
18-
end
19-
20-
def append_to_head(markup)
21-
head_markup << markup
11+
def index_html
12+
redis_client.get(deploy_key).presence || index_html_missing!
2213
end
2314

24-
def html
25-
if index_html.present?
26-
page = Page.new(html: index_html)
27-
28-
body_markup.each do |markup|
29-
page.append_to_body(markup)
30-
end
15+
private
3116

32-
head_markup.each do |markup|
33-
page.append_to_head(markup)
34-
end
17+
attr_reader :app
3518

36-
page.build
37-
else
38-
index_html_missing!
39-
end
19+
def redis_client
20+
@redis_client ||= ::Redis.new(url: ENV.fetch("REDIS_URL"))
4021
end
4122

42-
private
43-
44-
attr_reader :body_markup, :head_markup, :namespace, :redis_client
45-
46-
def index_html
47-
@index_html ||= redis_client.get(deploy_key).presence
23+
def namespace
24+
app.name
4825
end
4926

5027
def current_key
5128
"#{namespace}:current"
5229
end
5330

5431
def deploy_key
55-
key = redis_client.get(current_key).presence || deployment_not_activated!
56-
"#{namespace}:#{key}"
57-
end
32+
key = redis_client.get(current_key).presence ||
33+
deployment_not_activated!
5834

59-
def build_client
60-
::Redis.new(url: ENV.fetch("REDIS_URL"))
35+
"#{namespace}:#{key}"
6136
end
6237

6338
def index_html_missing!
64-
message = <<-FAIL
39+
raise KeyError.new <<-FAIL
6540
HTML for #{deploy_key} is missing.
6641
6742
Did you forget to call `ember deploy`?
6843
FAIL
69-
70-
raise KeyError, message
7144
end
7245

7346
def deployment_not_activated!
74-
message = <<-FAIL
47+
raise KeyError.new <<-FAIL
7548
#{current_key} is empty.
7649
7750
Did you forget to call `ember deploy:activate`?
7851
FAIL
79-
80-
raise KeyError, message
8152
end
8253
end
8354
end

spec/lib/ember_cli/deploy/page_spec.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)