rails_amp 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +14 -0
- data/MIT-LICENSE +20 -0
- data/README.md +370 -0
- data/Rakefile +23 -0
- data/gemfiles/Gemfile-rails.4.1.x +4 -0
- data/gemfiles/Gemfile-rails.4.2.x +4 -0
- data/lib/generators/rails_amp/install_generator.rb +23 -0
- data/lib/generators/rails_amp/templates/rails_amp.yml +39 -0
- data/lib/generators/rails_amp/templates/rails_amp_application.amp.erb +34 -0
- data/lib/rails_amp/config.rb +106 -0
- data/lib/rails_amp/overrider.rb +44 -0
- data/lib/rails_amp/railtie.rb +17 -0
- data/lib/rails_amp/version.rb +3 -0
- data/lib/rails_amp/view_helpers/action_view.rb +68 -0
- data/lib/rails_amp/view_helpers/image_tag_helper.rb +57 -0
- data/lib/rails_amp.rb +95 -0
- data/rails_amp.gemspec +23 -0
- data/spec/controllers/application_controller_spec.rb +142 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +4 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/images/rails.png +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/javascripts/channels/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/home_controller.rb +17 -0
- data/spec/dummy/app/controllers/users_controller.rb +25 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/user.rb +2 -0
- data/spec/dummy/app/views/home/_amp_info.html.erb +19 -0
- data/spec/dummy/app/views/home/about.html.erb +2 -0
- data/spec/dummy/app/views/home/help.html.erb +2 -0
- data/spec/dummy/app/views/home/index.html.erb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/app/views/layouts/rails_amp_application.amp.erb +34 -0
- data/spec/dummy/app/views/users/index.html.erb +11 -0
- data/spec/dummy/app/views/users/show.html.erb +7 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +34 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +9 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +54 -0
- data/spec/dummy/config/environments/production.rb +86 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/puma.rb +47 -0
- data/spec/dummy/config/rails_amp.yml +39 -0
- data/spec/dummy/config/routes.rb +10 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/db/migrate/20170130074559_create_users.rb +10 -0
- data/spec/dummy/db/schema.rb +22 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/rails_amp_spec.rb +242 -0
- data/spec/rails_helper.rb +66 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/support/config/amp_format.yml +4 -0
- data/spec/support/config/controller_actions.yml +5 -0
- data/spec/support/config/controller_all.yml +3 -0
- data/spec/support/config/disable_all.yml +2 -0
- data/spec/support/config/enable_all.yml +3 -0
- data/spec/support/config/enable_analytics.yml +2 -0
- data/spec/support/config/various.yml +12 -0
- data/spec/utilities.rb +5 -0
- data/spec/view_helpers/action_view_spec.rb +37 -0
- metadata +287 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41090d7cbfa0a001a9e1648ae23cd8597f10f571
|
4
|
+
data.tar.gz: 93b900a423f87244c74ac738b4cff363638858c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42eeb0062a86bb316413c3fd9ab6ecef11ca34ba6d7f21fe4b47bfe6932f4db24dfa13ecfd3726f732ec4b3d05270ca4314675aede6fb529dc758037d2afeced
|
7
|
+
data.tar.gz: c08fe3e59be882193b4e9f6ba6b3be994aeb54607e34173dff1248294ca5a7f0e390348a0750c57e3813160b0a58d8cb1d2cced47a9318c4ac833e84949c0c3c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in rails_amp.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Takafumi Yamano
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,370 @@
|
|
1
|
+
# RailsAmp
|
2
|
+
|
3
|
+
RailsAmp is a Ruby on Rails plugin that makes it easy to build views for AMP(Accelerated Mobile Pages).
|
4
|
+
|
5
|
+
## Supported Versions
|
6
|
+
|
7
|
+
Rails 4.1, 4.2, 5.0
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
In your Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'rails_amp'
|
15
|
+
```
|
16
|
+
|
17
|
+
And install:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
And then, generate codes and files:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ rails generate rails_amp:install
|
27
|
+
```
|
28
|
+
|
29
|
+
This step generates the followings.
|
30
|
+
|
31
|
+
```bash
|
32
|
+
insert config/initializers/mime_types.rb
|
33
|
+
create config/rails_amp.yml
|
34
|
+
create app/views/layouts/rails_amp_application.amp.erb
|
35
|
+
```
|
36
|
+
|
37
|
+
In config/initializers/mime_types.rb:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
Mime::Type.register_alias 'text/html', RailsAmp.default_format
|
41
|
+
```
|
42
|
+
|
43
|
+
This line must be added to make rails to recognize the amp format. The default format is :amp. You can change the value in config/rails_amp.yml
|
44
|
+
|
45
|
+
## Configurations
|
46
|
+
|
47
|
+
You can change RailsAmp configurations in your `config/rails_amp.yml`. Write configs with yaml.
|
48
|
+
|
49
|
+
In config/rails_amp.yml:
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
# ### Here are some config samples to use rails_amp.
|
53
|
+
|
54
|
+
# --------------------------------------------------
|
55
|
+
# To enable amp on specific controllers and actions.
|
56
|
+
# --------------------------------------------------
|
57
|
+
# ### Enable amp on users all actions.
|
58
|
+
# targets:
|
59
|
+
# users:
|
60
|
+
#
|
61
|
+
# ### Enable amp on users#index, users#show, posts#index, posts#show.
|
62
|
+
# ### controller: action1 action2 action3 ...
|
63
|
+
# targets:
|
64
|
+
# users: index show
|
65
|
+
# posts: index show
|
66
|
+
#
|
67
|
+
# ### Enable amp on all controllers and actions.
|
68
|
+
# targets:
|
69
|
+
# application: all
|
70
|
+
#
|
71
|
+
# ### Disable amp completely.
|
72
|
+
# targets:
|
73
|
+
#
|
74
|
+
targets:
|
75
|
+
users: index show
|
76
|
+
|
77
|
+
# --------------------------------------------------
|
78
|
+
# To set initial configurations.
|
79
|
+
# --------------------------------------------------
|
80
|
+
# ### Enable Google Analytics page tracking. Set your Google Analytics Account.
|
81
|
+
# analytics: UA-*****-*
|
82
|
+
#
|
83
|
+
# ### Change default amp format. The default value is amp.
|
84
|
+
# ### If you want to use 'mobile' as amp format, set 'mobile' to default_format.
|
85
|
+
# ### And you can access the amp page like /users/index.mobile
|
86
|
+
# default_format: mobile
|
87
|
+
#
|
88
|
+
# ### Set formats that used as amp. The default is html.
|
89
|
+
# ### These formats are used in the order, when the amp specialized view like 'users/index.amp.erb' is not found.
|
90
|
+
# lookup_formats: html xhtml
|
91
|
+
```
|
92
|
+
|
93
|
+
### Examples
|
94
|
+
|
95
|
+
Set the controllers and actions that you want to enable amp.
|
96
|
+
|
97
|
+
Enable amp on users all actions except for new, create, edit, update, destroy actions.
|
98
|
+
|
99
|
+
```yaml
|
100
|
+
targets:
|
101
|
+
users:
|
102
|
+
```
|
103
|
+
|
104
|
+
Note that RailsAmp automatically excludes the post-method related actions `new, create, edit, update, destroy` that originally provided by Rails from the amp targets.
|
105
|
+
|
106
|
+
Enable amp on some specific controllers and actions. e.g.) users#index, users#show, posts#show.
|
107
|
+
|
108
|
+
```yaml
|
109
|
+
targets:
|
110
|
+
users: index show
|
111
|
+
posts: show
|
112
|
+
```
|
113
|
+
|
114
|
+
Enable amp on all controllers and actions. (It's a bit dangerous, so I don't recommend.)
|
115
|
+
|
116
|
+
```yaml
|
117
|
+
targets:
|
118
|
+
application: all
|
119
|
+
```
|
120
|
+
|
121
|
+
Disable amp completely.
|
122
|
+
|
123
|
+
```yaml
|
124
|
+
targets:
|
125
|
+
```
|
126
|
+
|
127
|
+
Other configurations.
|
128
|
+
|
129
|
+
Enable Google Analytics page tracking. Set your Google Analytics Account.
|
130
|
+
|
131
|
+
```yaml
|
132
|
+
analytics: UA-*****-*
|
133
|
+
```
|
134
|
+
|
135
|
+
Change the amp default format. The default value is 'amp'. If you want to use 'mobile' as the default format, set 'mobile' to default_format. And you can access the amp page like `http://example.com/users.mobile`.
|
136
|
+
|
137
|
+
```yaml
|
138
|
+
default_format: mobile
|
139
|
+
```
|
140
|
+
|
141
|
+
Change formats that used as amp. The default is html. These formats are used in the order, when the amp specialized view like `app/views/users/index.amp.erb` is not found.
|
142
|
+
|
143
|
+
```yaml
|
144
|
+
lookup_formats: html xhtml
|
145
|
+
```
|
146
|
+
|
147
|
+
Note that you need to restart a server to reload the configurations after changing config/rails_amp.yml.
|
148
|
+
|
149
|
+
## Setup
|
150
|
+
|
151
|
+
Add the following code in your default layout head like `application.html.erb`.
|
152
|
+
|
153
|
+
In app/views/layouts/application.html.erb:
|
154
|
+
|
155
|
+
```html
|
156
|
+
<%= rails_amp_amphtml_link_tag %>
|
157
|
+
```
|
158
|
+
|
159
|
+
This code will put out the html header to inform where the amp url is.
|
160
|
+
|
161
|
+
```html
|
162
|
+
<link rel="amphtml" href="http://example.com/users.amp" />
|
163
|
+
```
|
164
|
+
|
165
|
+
### AMP link for root_url(root_path)
|
166
|
+
|
167
|
+
When you enable amp on the controller and action for root_url, the helper `rails_amp_amphtml_link_tag` will put out the following amphtml link in the root url.
|
168
|
+
|
169
|
+
In your config/routes.rb:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
root 'home#index'
|
173
|
+
```
|
174
|
+
|
175
|
+
And, in config/rails_amp.yml:
|
176
|
+
|
177
|
+
```yaml
|
178
|
+
targets:
|
179
|
+
home: index
|
180
|
+
```
|
181
|
+
|
182
|
+
The helper `rails_amp_amphtml_link_tag` will put out the following in the root url.
|
183
|
+
|
184
|
+
In `http://example.com/`:
|
185
|
+
|
186
|
+
```html
|
187
|
+
<link rel="amphtml" href="http://example.com/home/index.amp" />
|
188
|
+
```
|
189
|
+
|
190
|
+
So, you need to add a routing for this amp url.
|
191
|
+
|
192
|
+
In your config/routes.rb:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
get '/home/index', to: 'home#index'
|
196
|
+
```
|
197
|
+
|
198
|
+
|
199
|
+
## Customize AMP layout
|
200
|
+
|
201
|
+
In app/views/layouts/rails_amp_application.amp.erb:
|
202
|
+
|
203
|
+
```html
|
204
|
+
<!doctype html>
|
205
|
+
<html amp>
|
206
|
+
<head>
|
207
|
+
<meta charset="utf-8">
|
208
|
+
<title>Rails AMP</title>
|
209
|
+
<link rel="canonical" href="<%= rails_amp_canonical_url %>" />
|
210
|
+
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
|
211
|
+
|
212
|
+
<!-- Set page data type by JSON-LD and schema.org. -->
|
213
|
+
<!-- If you don't use page data type, remove this block. -->
|
214
|
+
<script type="application/ld+json">
|
215
|
+
{
|
216
|
+
"@context": "http://schema.org",
|
217
|
+
"@type": "*****",
|
218
|
+
"mainEntityOfPage": "*****",
|
219
|
+
"datePublished": "*****"
|
220
|
+
}
|
221
|
+
</script>
|
222
|
+
|
223
|
+
<!-- Write custom css sytle for amp html here. -->
|
224
|
+
<style amp-custom>
|
225
|
+
body {
|
226
|
+
}
|
227
|
+
amp-img {
|
228
|
+
}
|
229
|
+
</style>
|
230
|
+
|
231
|
+
<%= rails_amp_google_analytics_page_tracking %>
|
232
|
+
<%= rails_amp_html_header %>
|
233
|
+
</head>
|
234
|
+
<body>
|
235
|
+
<%= yield %>
|
236
|
+
</body>
|
237
|
+
</html>
|
238
|
+
```
|
239
|
+
|
240
|
+
Customize the page data type by JSON-LD and schema.org, and write custom css styles in the `<style amp-custom>` block. You can customize any other parts of this amp layout file as you like, but you need to follow the amp restrictions.
|
241
|
+
|
242
|
+
### Canonical link for root_url(root_path)
|
243
|
+
|
244
|
+
When you enable amp on the controller and action for root_url, the helper `rails_amp_canonical_url` will put out the following canonical link tag in the amp url for root_url.
|
245
|
+
|
246
|
+
In `http://example.com/home/index.amp`:
|
247
|
+
|
248
|
+
```html
|
249
|
+
<link rel="canonical" href="http://example.com/home/index" />
|
250
|
+
```
|
251
|
+
|
252
|
+
If you want to use the root_url as the canonical url, you should customize the codes.
|
253
|
+
|
254
|
+
```html
|
255
|
+
<% if controller_name == 'home' && action_name == 'index' %>
|
256
|
+
<link rel="canonical" href="<%= root_url %>" />
|
257
|
+
<% else %>
|
258
|
+
<link rel="canonical" href="<%= rails_amp_canonical_url %>" />
|
259
|
+
<% end %>
|
260
|
+
```
|
261
|
+
|
262
|
+
## Usage
|
263
|
+
|
264
|
+
To access amp pages, add the amp default format at the end of the url before queries like the followings.
|
265
|
+
|
266
|
+
```
|
267
|
+
http://example.com/users.amp
|
268
|
+
http://example.com/users.amp?sort=name
|
269
|
+
```
|
270
|
+
|
271
|
+
If you change the amp default format, you have to use it as the format.
|
272
|
+
|
273
|
+
In your config/rails_amp.yml
|
274
|
+
|
275
|
+
```yaml
|
276
|
+
default_format: mobile
|
277
|
+
```
|
278
|
+
|
279
|
+
Access the amp pages.
|
280
|
+
|
281
|
+
```
|
282
|
+
http://example.com/users.mobile
|
283
|
+
http://example.com/users.mobile?sort=name
|
284
|
+
```
|
285
|
+
|
286
|
+
### When not creating another view for amp. (When the template for amp not found)
|
287
|
+
|
288
|
+
If you don't create another view for amp (When the template for amp not found), RailsAmp tries to find amp-available formats accoding to the config `lookup_formats`, and uses the existing html view for the amp page as is.
|
289
|
+
|
290
|
+
e.g.) When you enable amp on `users#index` in config and don't create another amp view like `app/views/users/index.amp.erb`, RailsAmp uses the existing html view like `app/views/users/index.html.erb` for the amp page.
|
291
|
+
|
292
|
+
Then, you can access the amp page as `http://example.com/users.amp` by adding the amp default format at the end of the url before queries.
|
293
|
+
|
294
|
+
### When creating another view for amp.
|
295
|
+
|
296
|
+
If you want to use specialized views for amp pages, you can create another view for amp like `app/views/users/index.amp.erb`. When accessing `http://example.com/users.amp`, the amp specialized view will be used.
|
297
|
+
|
298
|
+
If you change the amp default format, create the view template with the format.
|
299
|
+
|
300
|
+
In config/rails_amp.yml:
|
301
|
+
|
302
|
+
```yaml
|
303
|
+
default_format: mobile
|
304
|
+
```
|
305
|
+
|
306
|
+
Create the view template as `app/views/users/index.mobile.erb`, and access the amp page as `http://example.com/users.mobile`.
|
307
|
+
|
308
|
+
### View Helpers
|
309
|
+
|
310
|
+
You can use a helper `amp_renderable?` in views. Use `amp_renderable?` in your existing html views to switch some codes. Here is a sample to switch codes for Twitter tweet display.
|
311
|
+
|
312
|
+
```html
|
313
|
+
<% if amp_renderable? %>
|
314
|
+
<!-- For amp -->
|
315
|
+
<amp-twitter width=486 height=657
|
316
|
+
layout="responsive"
|
317
|
+
data-tweetid="*****"
|
318
|
+
data-cards="hidden">
|
319
|
+
<blockquote placeholder class="twitter-tweet" data-lang="en">*****</blockquote>
|
320
|
+
</amp-twitter>
|
321
|
+
<% else %>
|
322
|
+
<!-- For normal html -->
|
323
|
+
<blockquote class="twitter-tweet" data-lang="ja">*****</blockquote>
|
324
|
+
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
|
325
|
+
<% end %>
|
326
|
+
```
|
327
|
+
|
328
|
+
## Supported AMP tags
|
329
|
+
|
330
|
+
RailsAmp supports a tag `<amp-img>` so far.
|
331
|
+
|
332
|
+
### amp-img
|
333
|
+
|
334
|
+
You have to use the `<amp-img>` tag to render images in amp pages, instead of a normal html tag `<img>`. When you use a rails built-in helper `image_tag` in view templates, RailsAmp automatically renders images with the `<amp-img>` tag in amp format pages. Also, the helper `image_tag` renders images with the `<img>` tag in normal html format pages.
|
335
|
+
|
336
|
+
The `<amp-img>` tag requires `width` and `height` attributes. When the `image_tag` helper is used with a `size` or `width`, `height` option, the `<amp-img>` tag uses the option's value as its `width` and `height` attributes.
|
337
|
+
|
338
|
+
If the `image_tag` helper doesn't have a `size` or `width` or `height` option, RailsAmp computes the image size using FastImage. FastImage is a great gem to fetch the size and other information of an image quickly. If FastImage even cannot fetch the image size, the `<amp-img>` tag's width and height attributes are set to `300` and `300`.
|
339
|
+
|
340
|
+
So, for amp format pages, I recommend that you use the `image_tag` helper with a `size` or `width`, `height` option as much as possible.
|
341
|
+
|
342
|
+
## Check AMP validation
|
343
|
+
|
344
|
+
In Google Chrome browser:
|
345
|
+
|
346
|
+
1. Start the Developer Tools.
|
347
|
+
1. Select the Console tab.
|
348
|
+
1. Access the amp page with `#development=1` like `http://localhost:3000/users.amp#development=1`
|
349
|
+
|
350
|
+
If `AMP validation successful` shows, the amp page is valid. If some errors are detected, fix your existing html view using the `amp_renderable?` helper or create another amp specialized view.
|
351
|
+
|
352
|
+
## References
|
353
|
+
|
354
|
+
[Accelerated Mobile Pages Project](https://www.ampproject.org/)
|
355
|
+
|
356
|
+
## Contributing
|
357
|
+
|
358
|
+
1. Fork it ( https://github.com/takafumir/rails_amp/fork )
|
359
|
+
1. Create your feature branch (git checkout -b my-new-feature)
|
360
|
+
1. Commit your changes (git commit -am 'Add some feature')
|
361
|
+
1. Push to the branch (git push origin my-new-feature)
|
362
|
+
1. Create a new Pull Request
|
363
|
+
|
364
|
+
## License
|
365
|
+
|
366
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/takafumir/rails_amp/blob/master/MIT-LICENSE).
|
367
|
+
|
368
|
+
## Author
|
369
|
+
|
370
|
+
[Takafumi Yamano](https://github.com/takafumir)
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsAmp'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module RailsAmp
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def inject_amp_mime_type_into_file
|
8
|
+
inject_into_file 'config/initializers/mime_types.rb',
|
9
|
+
after: %Q(# Mime::Type.register "text/richtext", :rtf\n) do <<-'RUBY'
|
10
|
+
Mime::Type.register_alias 'text/html', RailsAmp.default_format
|
11
|
+
RUBY
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_config_file
|
16
|
+
copy_file 'rails_amp.yml', 'config/rails_amp.yml'
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_application_layout
|
20
|
+
copy_file 'rails_amp_application.amp.erb', 'app/views/layouts/rails_amp_application.amp.erb'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# ### Here are some config samples to use rails_amp.
|
2
|
+
|
3
|
+
# --------------------------------------------------
|
4
|
+
# To enable amp on specific controllers and actions.
|
5
|
+
# --------------------------------------------------
|
6
|
+
# ### Enable amp on users all actions.
|
7
|
+
# targets:
|
8
|
+
# users:
|
9
|
+
#
|
10
|
+
# ### Enable amp on users#index, users#show, posts#index, posts#show.
|
11
|
+
# ### controller: action1 action2 action3 ...
|
12
|
+
# targets:
|
13
|
+
# users: index show
|
14
|
+
# posts: index show
|
15
|
+
#
|
16
|
+
# ### Enable amp on all controllers and actions.
|
17
|
+
# targets:
|
18
|
+
# application: all
|
19
|
+
#
|
20
|
+
# ### Disable amp completely.
|
21
|
+
# targets:
|
22
|
+
#
|
23
|
+
targets:
|
24
|
+
users: index show
|
25
|
+
|
26
|
+
# --------------------------------------------------
|
27
|
+
# To set initial configurations.
|
28
|
+
# --------------------------------------------------
|
29
|
+
# ### Enable Google Analytics page tracking. Set your Google Analytics Account.
|
30
|
+
# analytics: UA-*****-*
|
31
|
+
#
|
32
|
+
# ### Change default amp format. The default value is amp.
|
33
|
+
# ### If you want to use 'mobile' as amp format, set 'mobile' to default_format.
|
34
|
+
# ### And you can access the amp page like /users/index.mobile
|
35
|
+
# default_format: mobile
|
36
|
+
#
|
37
|
+
# ### Set formats that used as amp. The default is html.
|
38
|
+
# ### These formats are used in the order, when the amp specialized view like 'users/index.amp.erb' is not found.
|
39
|
+
# lookup_formats: html xhtml
|
@@ -0,0 +1,34 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html amp>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<title>Rails AMP</title>
|
6
|
+
<link rel="canonical" href="<%= rails_amp_canonical_url %>" />
|
7
|
+
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
|
8
|
+
|
9
|
+
<!-- Set page data type by JSON-LD and schema.org. -->
|
10
|
+
<!-- If you don't use page data type, remove this block. -->
|
11
|
+
<script type="application/ld+json">
|
12
|
+
{
|
13
|
+
"@context": "http://schema.org",
|
14
|
+
"@type": "*****",
|
15
|
+
"mainEntityOfPage": "*****",
|
16
|
+
"datePublished": "*****"
|
17
|
+
}
|
18
|
+
</script>
|
19
|
+
|
20
|
+
<!-- Write custom css sytle for amp html here. -->
|
21
|
+
<style amp-custom>
|
22
|
+
body {
|
23
|
+
}
|
24
|
+
amp-img {
|
25
|
+
}
|
26
|
+
</style>
|
27
|
+
|
28
|
+
<%= rails_amp_google_analytics_page_tracking %>
|
29
|
+
<%= rails_amp_html_header %>
|
30
|
+
</head>
|
31
|
+
<body>
|
32
|
+
<%= yield %>
|
33
|
+
</body>
|
34
|
+
</html>
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module RailsAmp
|
2
|
+
class Config
|
3
|
+
# Return the current format, default is ''. The only configuration value that is not global.
|
4
|
+
def format
|
5
|
+
@format ||= ''
|
6
|
+
end
|
7
|
+
|
8
|
+
# Set the current format pseudo-globally, i.e. in the Thread.current hash.
|
9
|
+
def format=(format)
|
10
|
+
@format = format.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return the config file path, default is "#{Rails.root}/config/rails_amp.yml".
|
14
|
+
def config_file
|
15
|
+
@@config_file ||= "#{Rails.root}/config/rails_amp.yml"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set the config file path.
|
19
|
+
def config_file=(config_file)
|
20
|
+
@@config_file = config_file
|
21
|
+
end
|
22
|
+
|
23
|
+
# Return the yaml loaded config, default is YAML.load_file(config_file).
|
24
|
+
def load_config
|
25
|
+
@@load_config ||= config_load_config
|
26
|
+
end
|
27
|
+
|
28
|
+
# Set the config by loading yaml.
|
29
|
+
def load_config=(load_config)
|
30
|
+
@@load_config = load_config
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return the default amp format, default is :amp
|
34
|
+
def default_format
|
35
|
+
@@default_format ||= config_default_format
|
36
|
+
end
|
37
|
+
|
38
|
+
# Set the default amp format.
|
39
|
+
def default_format=(default_format)
|
40
|
+
@@default_format = default_format.to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return the amp enabled controller actions.
|
44
|
+
def targets
|
45
|
+
@@targets ||= config_targets
|
46
|
+
end
|
47
|
+
|
48
|
+
# Set the amp enabled controller actions.
|
49
|
+
def targets=(targets)
|
50
|
+
@@targets = targets
|
51
|
+
end
|
52
|
+
|
53
|
+
# Return the analytics account, default is ''.
|
54
|
+
def analytics
|
55
|
+
@@analytics ||= config_analytics
|
56
|
+
end
|
57
|
+
|
58
|
+
# Set the analytics account.
|
59
|
+
def analytics=(analytics)
|
60
|
+
@@analytics = analytics
|
61
|
+
end
|
62
|
+
|
63
|
+
# Return the lookup_context formats for amp, default is [:html].
|
64
|
+
def lookup_formats
|
65
|
+
@@lookup_formats ||= config_lookup_formats
|
66
|
+
end
|
67
|
+
|
68
|
+
# Set the lookup_context formats for amp.
|
69
|
+
def lookup_formats=(lookup_formats)
|
70
|
+
@@lookup_formats = lookup_formats
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def config_load_config
|
76
|
+
YAML.load_file(config_file)
|
77
|
+
end
|
78
|
+
|
79
|
+
def config_default_format
|
80
|
+
load_config['default_format'].try(:to_sym) || :amp
|
81
|
+
end
|
82
|
+
|
83
|
+
def config_targets
|
84
|
+
targets = load_config['targets']
|
85
|
+
return {} if targets.blank?
|
86
|
+
|
87
|
+
if targets.is_a?(Hash) && targets.has_key?('application')
|
88
|
+
return targets
|
89
|
+
end
|
90
|
+
|
91
|
+
if targets.is_a?(Hash)
|
92
|
+
return targets.map{ |k, v| [k, v.to_s.split(/\s+/)] }.to_h
|
93
|
+
end
|
94
|
+
|
95
|
+
{}
|
96
|
+
end
|
97
|
+
|
98
|
+
def config_analytics
|
99
|
+
load_config['analytics'] || ''
|
100
|
+
end
|
101
|
+
|
102
|
+
def config_lookup_formats
|
103
|
+
load_config['lookup_formats'].to_s.split(/\s+/).map(&:to_sym).presence || [:html]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|