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
@@ -0,0 +1,44 @@
|
|
1
|
+
module RailsAmp
|
2
|
+
module Overrider
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_action do
|
7
|
+
RailsAmp.format = request[:format]
|
8
|
+
if RailsAmp.amp_renderable?(controller_name, action_name) # default_format is :amp
|
9
|
+
override_actions_with_rails_amp
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def override_actions_with_rails_amp
|
17
|
+
klass = self.class # klass is controller class
|
18
|
+
return if klass.ancestors.include?(RailsAmp::ActionOverrider)
|
19
|
+
actions = RailsAmp.target_actions(klass)
|
20
|
+
|
21
|
+
klass.class_eval do
|
22
|
+
# override amp target actions
|
23
|
+
RailsAmp::ActionOverrider.module_eval do
|
24
|
+
actions.to_a.each do |action|
|
25
|
+
define_method action.to_sym do
|
26
|
+
super()
|
27
|
+
respond_to do |format|
|
28
|
+
format.send(RailsAmp.default_format.to_sym) do
|
29
|
+
# search amp format(default is .amp) .html templates
|
30
|
+
lookup_context.formats = [RailsAmp.default_format] + RailsAmp.lookup_formats
|
31
|
+
render layout: 'rails_amp_application.amp'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
prepend RailsAmp::ActionOverrider
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module ActionOverrider
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails_amp/overrider'
|
2
|
+
|
3
|
+
module RailsAmp
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'rails_amp' do |app|
|
6
|
+
ActiveSupport.on_load :action_controller do
|
7
|
+
include RailsAmp::Overrider
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load :action_view do
|
11
|
+
require 'rails_amp/view_helpers/action_view'
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'rails_amp/view_helpers/image_tag_helper'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module RailsAmp
|
2
|
+
module ViewHelpers
|
3
|
+
module ActionView
|
4
|
+
|
5
|
+
# To add header code in default layout like application.html.erb.
|
6
|
+
def rails_amp_amphtml_link_tag
|
7
|
+
return '' unless RailsAmp.target?(controller.controller_name, controller.action_name)
|
8
|
+
|
9
|
+
amp_uri = URI.parse(request.url)
|
10
|
+
if request.path == root_path
|
11
|
+
amp_path = "#{controller.controller_name}/#{controller.action_name}.#{RailsAmp.default_format}"
|
12
|
+
else
|
13
|
+
amp_path = ".#{RailsAmp.default_format}"
|
14
|
+
end
|
15
|
+
amp_uri.path = amp_uri.path + amp_path
|
16
|
+
amp_uri.query = ERB::Util.h(amp_uri.query) if amp_uri.query.present?
|
17
|
+
|
18
|
+
%Q(<link rel="amphtml" href="#{amp_uri.to_s}" />).html_safe
|
19
|
+
end
|
20
|
+
|
21
|
+
def rails_amp_html_header
|
22
|
+
header =<<"EOS"
|
23
|
+
<!-- Snipet for amp library. -->
|
24
|
+
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
|
25
|
+
<script async src="https://cdn.ampproject.org/v0.js"></script>
|
26
|
+
EOS
|
27
|
+
header.html_safe
|
28
|
+
end
|
29
|
+
|
30
|
+
def rails_amp_google_analytics_page_tracking
|
31
|
+
if RailsAmp.analytics.present?
|
32
|
+
analytics_code =<<"EOS"
|
33
|
+
<!-- Google Analytics Page Tracking for amp pages. -->
|
34
|
+
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
|
35
|
+
<amp-analytics type="googleanalytics" id="rails_amp_analytics">
|
36
|
+
<script type="application/json">
|
37
|
+
{
|
38
|
+
"vars": {
|
39
|
+
"account": "#{RailsAmp.analytics}"
|
40
|
+
},
|
41
|
+
"triggers": {
|
42
|
+
"trackPageview": {
|
43
|
+
"on": "visible",
|
44
|
+
"request": "pageview"
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
</script>
|
49
|
+
</amp-analytics>
|
50
|
+
EOS
|
51
|
+
return analytics_code.html_safe
|
52
|
+
end
|
53
|
+
|
54
|
+
''
|
55
|
+
end
|
56
|
+
|
57
|
+
def rails_amp_canonical_url
|
58
|
+
request.url.gsub(".#{RailsAmp.default_format.to_s}", '')
|
59
|
+
end
|
60
|
+
|
61
|
+
def amp_renderable?
|
62
|
+
RailsAmp.amp_renderable?(controller.controller_name, controller.action_name)
|
63
|
+
end
|
64
|
+
|
65
|
+
::ActionView::Base.send :include, self
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'fastimage'
|
2
|
+
|
3
|
+
module RailsAmp
|
4
|
+
module ViewHelpers
|
5
|
+
module ImageTagHelper
|
6
|
+
# ref: https://www.ampproject.org/docs/reference/components/amp-img
|
7
|
+
AMP_IMG_PERMITTED_ATTRIBUTES = %w(
|
8
|
+
src srcset alt attribution height width fallback heights layout media noloading on placeholder sizes
|
9
|
+
)
|
10
|
+
|
11
|
+
# ref: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/asset_tag_helper.rb#L228
|
12
|
+
def amp_image_tag(source, options={})
|
13
|
+
options = options.symbolize_keys
|
14
|
+
check_for_image_tag_errors(options) if defined?(check_for_image_tag_errors)
|
15
|
+
|
16
|
+
src = options[:src] = path_to_image(source, skip_pipeline: options.delete(:skip_pipeline))
|
17
|
+
|
18
|
+
unless src.start_with?("cid:") || src.start_with?("data:") || src.blank?
|
19
|
+
options[:alt] = options.fetch(:alt){ image_alt(src) }
|
20
|
+
end
|
21
|
+
|
22
|
+
options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]
|
23
|
+
|
24
|
+
if options[:width].blank? || options[:height].blank?
|
25
|
+
options[:width], options[:height] = compute_image_size(source)
|
26
|
+
end
|
27
|
+
|
28
|
+
options[:layout] ||= 'fixed'
|
29
|
+
options.select! { |key, _| key.to_s.in?(AMP_IMG_PERMITTED_ATTRIBUTES) }
|
30
|
+
tag('amp-img', options) + '</amp-img>'.html_safe
|
31
|
+
end
|
32
|
+
|
33
|
+
# override image_tag helper in ActionView::Helpers::AssetTagHelper
|
34
|
+
def image_tag(source, options={})
|
35
|
+
if controller && RailsAmp.amp_renderable?(controller.controller_name, controller.action_name)
|
36
|
+
amp_image_tag(source, options)
|
37
|
+
else
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def compute_image_size(source)
|
45
|
+
source_for_fastimage = source
|
46
|
+
unless source =~ ::ActionView::Helpers::AssetUrlHelper::URI_REGEXP
|
47
|
+
# find_asset is a Sprockets method
|
48
|
+
source_for_fastimage = Rails.application.assets.find_asset(source).try(:pathname).to_s.presence ||
|
49
|
+
File.join(Rails.public_path, source)
|
50
|
+
end
|
51
|
+
FastImage.size(source_for_fastimage) || [300, 300]
|
52
|
+
end
|
53
|
+
|
54
|
+
::ActionView::Base.send :prepend, self
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/rails_amp.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rails_amp/version'
|
2
|
+
|
3
|
+
if defined?(Rails::Railtie)
|
4
|
+
require 'rails_amp/railtie'
|
5
|
+
else
|
6
|
+
raise 'rails_amp is not compatible with Rails 2.* or older'
|
7
|
+
end
|
8
|
+
|
9
|
+
module RailsAmp
|
10
|
+
autoload :Config, 'rails_amp/config'
|
11
|
+
|
12
|
+
extend(Module.new {
|
13
|
+
# Get RailsAmp configuration object.
|
14
|
+
def config
|
15
|
+
Thread.current[:rails_amp_config] ||= RailsAmp::Config.new
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set RailsAmp configuration object.
|
19
|
+
def config=(value)
|
20
|
+
Thread.current[:rails_amp_config] = value
|
21
|
+
end
|
22
|
+
|
23
|
+
# Write methods which delegates to the configuration object.
|
24
|
+
%w( format config_file load_config default_format targets analytics lookup_formats ).each do |method|
|
25
|
+
module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
|
26
|
+
def #{method}
|
27
|
+
config.#{method}
|
28
|
+
end
|
29
|
+
|
30
|
+
def #{method}=(value)
|
31
|
+
config.#{method} = (value)
|
32
|
+
end
|
33
|
+
DELEGATORS
|
34
|
+
end
|
35
|
+
|
36
|
+
def reload_config!
|
37
|
+
config.load_config = config.send(:config_load_config)
|
38
|
+
config.default_format = config.send(:config_default_format)
|
39
|
+
config.targets = config.send(:config_targets)
|
40
|
+
config.analytics = config.send(:config_analytics)
|
41
|
+
config.lookup_formats = config.send(:config_lookup_formats)
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
|
45
|
+
def disable_all?
|
46
|
+
targets.blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
def enable_all?
|
50
|
+
targets['application'] == 'all'
|
51
|
+
end
|
52
|
+
|
53
|
+
def controller_all?(key)
|
54
|
+
targets.has_key?(key) && targets[key].blank?
|
55
|
+
end
|
56
|
+
|
57
|
+
def controller_actions?(key)
|
58
|
+
targets.has_key?(key) && targets[key].present?
|
59
|
+
end
|
60
|
+
|
61
|
+
def target_actions(controller)
|
62
|
+
extract_target_actions(controller) - %w(new create edit update destroy)
|
63
|
+
end
|
64
|
+
|
65
|
+
def extract_target_actions(controller)
|
66
|
+
return [] if disable_all?
|
67
|
+
return controller.action_methods.to_a if enable_all?
|
68
|
+
key = controller_to_key(controller)
|
69
|
+
return controller.action_methods.to_a if controller_all?(key)
|
70
|
+
return targets[key] if controller_actions?(key)
|
71
|
+
[]
|
72
|
+
end
|
73
|
+
|
74
|
+
def controller_to_key(controller)
|
75
|
+
controller.name.underscore.sub(/_controller\z/, '')
|
76
|
+
end
|
77
|
+
|
78
|
+
def key_to_controller(key)
|
79
|
+
(key.camelcase + 'Controller').constantize
|
80
|
+
end
|
81
|
+
|
82
|
+
def target?(controller_name, action_name)
|
83
|
+
target_actions = target_actions( key_to_controller(controller_name) )
|
84
|
+
action_name.in?(target_actions)
|
85
|
+
end
|
86
|
+
|
87
|
+
def amp_format?
|
88
|
+
format == default_format.to_s
|
89
|
+
end
|
90
|
+
|
91
|
+
def amp_renderable?(controller_name, action_name)
|
92
|
+
amp_format? && target?(controller_name, action_name)
|
93
|
+
end
|
94
|
+
})
|
95
|
+
end
|
data/rails_amp.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'rails_amp/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'rails_amp'
|
6
|
+
s.version = RailsAmp::VERSION
|
7
|
+
s.authors = ['Takafumi Yamano']
|
8
|
+
s.email = ['takafumiyamano@gmail.com']
|
9
|
+
s.homepage = 'https://github.com/takafumir/rails_amp'
|
10
|
+
s.summary = 'AMP(Accelerated Mobile Pages) plugin for Ruby on Rails.'
|
11
|
+
s.description = 'RailsAmp is a Ruby on Rails plugin that makes it easy to build views for AMP(Accelerated Mobile Pages).'
|
12
|
+
s.license = 'MIT'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files`.split("\n").grep(%r{^(test|spec|features)/})
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
|
18
|
+
s.add_dependency 'rails', '>= 4.1.0'
|
19
|
+
s.add_dependency 'fastimage'
|
20
|
+
|
21
|
+
s.add_development_dependency 'sqlite3'
|
22
|
+
s.add_development_dependency 'rspec-rails'
|
23
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe ApplicationController do
|
4
|
+
# just for test
|
5
|
+
describe 'hello world' do
|
6
|
+
specify { expect('hello world').to eq 'hello world' }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# Specs for ActionView helpers: rails_amp_amphtml_link_tag, rails_amp_canonical_url, amp_renderable?.
|
11
|
+
# And specs for ImageTagHelper: amp_image_tag, image_tag
|
12
|
+
# These helpers use controller or request object.
|
13
|
+
describe UsersController do
|
14
|
+
render_views
|
15
|
+
|
16
|
+
context '#index GET' do
|
17
|
+
# Users#index is available for amp.
|
18
|
+
it 'has correct amphtml link tag' do
|
19
|
+
get 'index'
|
20
|
+
expect(rails_amp_amphtml_link_tag).to eq(
|
21
|
+
%Q(<link rel="amphtml" href="#{request.base_url}/users.#{RailsAmp.default_format.to_s}" />)
|
22
|
+
)
|
23
|
+
expect(response.body).to include(
|
24
|
+
%Q(<link rel="amphtml" href="#{request.base_url}/users.#{RailsAmp.default_format.to_s}" />)
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'has correct amphtml link tag with params' do
|
29
|
+
get 'index', params: {sort: 'name'}
|
30
|
+
expect(rails_amp_amphtml_link_tag).to eq(
|
31
|
+
%Q(<link rel="amphtml" href="#{request.base_url}/users.#{RailsAmp.default_format.to_s}?sort=name" />)
|
32
|
+
)
|
33
|
+
expect(response.body).to include(
|
34
|
+
%Q(<link rel="amphtml" href="#{request.base_url}/users.#{RailsAmp.default_format.to_s}?sort=name" />)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'has correct canonical url' do
|
39
|
+
get 'index', format: RailsAmp.default_format.to_s
|
40
|
+
expect(request.url).to eq("#{request.base_url}/users.#{RailsAmp.default_format.to_s}")
|
41
|
+
expect(rails_amp_canonical_url).to eq("#{request.base_url}/users")
|
42
|
+
expect(response.body).to include(
|
43
|
+
%Q(<link rel="canonical" href="#{request.base_url}/users" />)
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'has correct canonical url with params' do
|
48
|
+
get 'index', format: RailsAmp.default_format.to_s, params: {sort: 'name'}
|
49
|
+
expect(request.url).to eq("#{request.base_url}/users.#{RailsAmp.default_format.to_s}?sort=name")
|
50
|
+
expect(rails_amp_canonical_url).to eq("#{request.base_url}/users?sort=name")
|
51
|
+
expect(response.body).to include(
|
52
|
+
%Q(<link rel="canonical" href="#{request.base_url}/users?sort=name" />)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with html format' do
|
57
|
+
it 'is not renderable by amp' do
|
58
|
+
get 'index'
|
59
|
+
expect(amp_renderable?).to eq false
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'has normal image tag' do
|
63
|
+
get 'index'
|
64
|
+
expect(image_tag('rails.png', {size: '30x20', border: '0'})).to match(
|
65
|
+
%r(<img border="0" src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" width="30" height="20" />)
|
66
|
+
)
|
67
|
+
expect(image_tag('rails.png')).to match(
|
68
|
+
%r(<img src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" />)
|
69
|
+
)
|
70
|
+
# According to dummy app default view `home/_amp_info`
|
71
|
+
expect(response.body).to match(
|
72
|
+
%r(<img border="0" src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" width="30" height="20" />)
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with amp format' do
|
78
|
+
it 'is renderable by amp' do
|
79
|
+
get 'index', format: RailsAmp.default_format.to_s
|
80
|
+
expect(amp_renderable?).to eq true
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'has amp-img tag' do
|
84
|
+
get 'index', format: RailsAmp.default_format.to_s
|
85
|
+
expect(image_tag('rails.png', {size: '30x20', border: '0'})).to match(
|
86
|
+
%r(<amp-img src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" layout="fixed" width="30" height="20" /></amp-img>)
|
87
|
+
)
|
88
|
+
# `width="400" height="400"` is removed because request.base_url returns dummy url for test.
|
89
|
+
expect(image_tag('rails.png')).to match(
|
90
|
+
%r(<amp-img src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" layout="fixed" /></amp-img>)
|
91
|
+
)
|
92
|
+
# According to dummy app default view `home/_amp_info`
|
93
|
+
expect(response.body).to match(
|
94
|
+
%r(<amp-img src="/(images|assets)/rails-?\w*?.jpg" alt="Rails" layout="fixed" width="30" height="20" /></amp-img>)
|
95
|
+
)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context '#show GET' do
|
101
|
+
let(:user) { User.create(name: 'Taro', email: 'taro@example.com') }
|
102
|
+
|
103
|
+
context 'with html format' do
|
104
|
+
it 'is not renderable by amp' do
|
105
|
+
get 'show', params: {id: user.id}
|
106
|
+
expect(amp_renderable?).to eq false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'with amp format' do
|
111
|
+
it 'is renderable by amp' do
|
112
|
+
get 'show', format: RailsAmp.default_format.to_s, params: {id: user.id}
|
113
|
+
expect(amp_renderable?).to eq true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe HomeController do
|
120
|
+
context '#help GET' do
|
121
|
+
# Home#help is not available for amp.
|
122
|
+
it 'does not return amphtml link tag' do
|
123
|
+
get 'help'
|
124
|
+
expect(rails_amp_amphtml_link_tag).to eq ''
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with html format' do
|
128
|
+
it 'is not renderable by amp' do
|
129
|
+
get 'help'
|
130
|
+
expect(amp_renderable?).to eq false
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with amp format' do
|
135
|
+
it 'is not renderable by amp' do
|
136
|
+
expect do
|
137
|
+
get 'help', format: RailsAmp.default_format.to_s
|
138
|
+
end.to raise_error(ActionController::UnknownFormat)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
data/spec/dummy/.rspec
ADDED
data/spec/dummy/Rakefile
ADDED
File without changes
|
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// Action Cable provides the framework to deal with WebSockets in Rails.
|
2
|
+
// You can generate new channels where WebSocket features live using the rails generate channel command.
|
3
|
+
//
|
4
|
+
//= require action_cable
|
5
|
+
//= require_self
|
6
|
+
//= require_tree ./channels
|
7
|
+
|
8
|
+
(function() {
|
9
|
+
this.App || (this.App = {});
|
10
|
+
|
11
|
+
App.cable = ActionCable.createConsumer();
|
12
|
+
|
13
|
+
}).call(this);
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Just for test.
|
2
|
+
class HomeController < ApplicationController
|
3
|
+
def index
|
4
|
+
end
|
5
|
+
|
6
|
+
def help
|
7
|
+
end
|
8
|
+
|
9
|
+
def about
|
10
|
+
respond_to do |format|
|
11
|
+
format.html # about.html.erb
|
12
|
+
format.json { render json:
|
13
|
+
{ object_id: RailsAmp.config.object_id, format: RailsAmp.config.format }
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Just for test.
|
2
|
+
class UsersController < ApplicationController
|
3
|
+
def index
|
4
|
+
@users = User.all
|
5
|
+
end
|
6
|
+
|
7
|
+
def show
|
8
|
+
@user = User.find(params[:id])
|
9
|
+
end
|
10
|
+
|
11
|
+
def new
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
end
|
16
|
+
|
17
|
+
def edit
|
18
|
+
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy
|
24
|
+
end
|
25
|
+
end
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<%# Just for test. %>
|
2
|
+
<h2>RailsAmp Information</h2>
|
3
|
+
<p>Rails Version: <%= Rails.version %></p>
|
4
|
+
<p>RailsAmp Object ID: <%= RailsAmp.object_id %></p>
|
5
|
+
<p>RailsAmp format: <%= RailsAmp.format.inspect %></p>
|
6
|
+
<p>RailsAmp default_format: <%= RailsAmp.default_format.inspect %></p>
|
7
|
+
<p>RailsAmp targets: <%= RailsAmp.targets.inspect %></p>
|
8
|
+
<p>RailsAmp analytics: <%= RailsAmp.analytics.inspect %></p>
|
9
|
+
<p>RailsAmp analytics class: <%= RailsAmp.analytics.class %></p>
|
10
|
+
<p>RailsAmp MimeTypes: <%= Mime::EXTENSION_LOOKUP %></p>
|
11
|
+
<p>RailsAmp Ancestors: <%= controller.class.ancestors %></p>
|
12
|
+
<p><%= image_tag('rails.png', size: '30x20', border: '0') %></p>
|
13
|
+
<p><%= image_tag('rails.png') %></p>
|
14
|
+
|
15
|
+
<% if amp_renderable? %>
|
16
|
+
<p>This is an amp page.</p>
|
17
|
+
<% else %>
|
18
|
+
<p>This is a html page.</p>
|
19
|
+
<% end %>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
|
7
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
8
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
9
|
+
<%= rails_amp_amphtml_link_tag %>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<%= yield %>
|
14
|
+
</body>
|
15
|
+
</html>
|