rails_amp 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b477424405e8a2a63fac02dc549d5e55604b6aa7
4
- data.tar.gz: 523e1fbb918d94ce89f15cbd7b021af62c761833
3
+ metadata.gz: 0f2e83b307d88b1a336d8228b358494c8adbdb77
4
+ data.tar.gz: 2a5e6431e3fe27a4f596718dd06493d6679bdddf
5
5
  SHA512:
6
- metadata.gz: 84937ec9c06080f49281cba75e5c097eca34726d22e2a5c022efaee3de0598bef247c484dfb6399496c73f7ff19f010b306f78b9f4bcd215d2dcb1e5e76905df
7
- data.tar.gz: 5d35623d87c559b8f4e4b000f242c9c8a52919105ee47265b5a9c056abb41f28634f421725c7676705c2657c9dfe1a38bd1a72bb6832e2661ae2bb6902901596
6
+ metadata.gz: c5b07210b263093fdceb26fb4e1cf6ee6ee29616ad74dbb67b3a24a28997fda649510bd1878e75eddce38f4f960c061ea7dcda91ebbdc5cc98762bebf5ea9c6d
7
+ data.tar.gz: ebe8d62c2457cc74f0560425620df26da128a99b5496e228371d9a8cdcb977815a6036a1a1107b4e44a5596935980e48113dda8745b9dde7b47cd9a5c3a89202
data/README.md CHANGED
@@ -252,7 +252,7 @@ In `http://example.com/home/index.amp`:
252
252
  If you want to use the root_url as the canonical url, you should customize the codes.
253
253
 
254
254
  ```ruby
255
- <% if controller_name == 'home' && action_name == 'index' %>
255
+ <% if controller_path == 'home' && action_name == 'index' %>
256
256
  <link rel="canonical" href="<%= root_url %>" />
257
257
  <% else %>
258
258
  <link rel="canonical" href="<%= rails_amp_canonical_url %>" />
@@ -79,8 +79,8 @@ module RailsAmp
79
79
  (key.camelcase + 'Controller').constantize
80
80
  end
81
81
 
82
- def target?(controller_name, action_name)
83
- target_actions = target_actions( key_to_controller(controller_name) )
82
+ def target?(controller_path, action_name)
83
+ target_actions = target_actions( key_to_controller(controller_path) )
84
84
  action_name.in?(target_actions)
85
85
  end
86
86
 
@@ -88,8 +88,8 @@ module RailsAmp
88
88
  format == default_format.to_s
89
89
  end
90
90
 
91
- def amp_renderable?(controller_name, action_name)
92
- amp_format? && target?(controller_name, action_name)
91
+ def amp_renderable?(controller_path, action_name)
92
+ amp_format? && target?(controller_path, action_name)
93
93
  end
94
94
  })
95
95
  end
@@ -5,7 +5,7 @@ module RailsAmp
5
5
  included do
6
6
  before_action do
7
7
  RailsAmp.format = request[:format]
8
- if RailsAmp.amp_renderable?(controller_name, action_name) # default_format is :amp
8
+ if RailsAmp.amp_renderable?(controller_path, action_name) # default_format is :amp
9
9
  override_actions_with_rails_amp
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAmp
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -4,11 +4,11 @@ module RailsAmp
4
4
 
5
5
  # To add header code in default layout like application.html.erb.
6
6
  def rails_amp_amphtml_link_tag
7
- return '' unless RailsAmp.target?(controller.controller_name, controller.action_name)
7
+ return '' unless RailsAmp.target?(controller.controller_path, controller.action_name)
8
8
 
9
9
  amp_uri = URI.parse(request.url)
10
10
  if request.path == root_path
11
- amp_path = "#{controller.controller_name}/#{controller.action_name}.#{RailsAmp.default_format}"
11
+ amp_path = "#{controller.controller_path}/#{controller.action_name}.#{RailsAmp.default_format}"
12
12
  else
13
13
  amp_path = ".#{RailsAmp.default_format}"
14
14
  end
@@ -66,7 +66,7 @@ EOS
66
66
  end
67
67
 
68
68
  def amp_renderable?
69
- RailsAmp.amp_renderable?(controller.controller_name, controller.action_name)
69
+ RailsAmp.amp_renderable?(controller.controller_path, controller.action_name)
70
70
  end
71
71
 
72
72
  ::ActionView::Base.send :include, self
@@ -32,7 +32,7 @@ module RailsAmp
32
32
 
33
33
  # override image_tag helper in ActionView::Helpers::AssetTagHelper
34
34
  def image_tag(source, options={})
35
- if controller && RailsAmp.amp_renderable?(controller.controller_name, controller.action_name)
35
+ if controller && RailsAmp.amp_renderable?(controller.controller_path, controller.action_name)
36
36
  amp_image_tag(source, options)
37
37
  else
38
38
  super
@@ -121,3 +121,113 @@ describe HomeController do
121
121
  end
122
122
  end
123
123
  end
124
+
125
+ describe Admin::SessionsController do
126
+ # Admin::SessionsController#index is not available for amp.
127
+ it 'does not return amphtml link tag' do
128
+ get 'index'
129
+ expect(rails_amp_amphtml_link_tag).to eq ''
130
+ end
131
+
132
+ context 'with html format' do
133
+ it 'is not renderable by amp' do
134
+ get 'index'
135
+ expect(amp_renderable?).to eq false
136
+ end
137
+ end
138
+
139
+ context 'with amp format' do
140
+ it 'is not renderable by amp' do
141
+ expect do
142
+ get 'index', format: RailsAmp.default_format.to_s
143
+ end.to raise_error(StandardError) # ActionView::MissingTemplate or ActionController::UnknownFormat
144
+ end
145
+ end
146
+ end
147
+
148
+ describe Admin::UsersController do
149
+ render_views
150
+
151
+ context '#index GET' do
152
+ # Admin::Users#index is available for amp.
153
+ it 'has correct amphtml link tag' do
154
+ get 'index'
155
+ expect(rails_amp_amphtml_link_tag).to eq(
156
+ %(<link rel="amphtml" href="#{request.base_url}/admin/users.#{RailsAmp.default_format}" />)
157
+ )
158
+ expect(response.body).to include(
159
+ %(<link rel="amphtml" href="#{request.base_url}/admin/users.#{RailsAmp.default_format}" />)
160
+ )
161
+ end
162
+
163
+ it 'has correct amphtml link tag with params' do
164
+ get 'index', params: { sort: 'name' }
165
+ expect(rails_amp_amphtml_link_tag).to eq(
166
+ %(<link rel="amphtml" href="#{request.base_url}/admin/users.#{RailsAmp.default_format}?sort=name" />)
167
+ )
168
+ expect(response.body).to include(
169
+ %(<link rel="amphtml" href="#{request.base_url}/admin/users.#{RailsAmp.default_format}?sort=name" />)
170
+ )
171
+ end
172
+
173
+ it 'has correct canonical url' do
174
+ get 'index', format: RailsAmp.default_format.to_s
175
+ expect(request.url).to eq("#{request.base_url}/admin/users.#{RailsAmp.default_format}")
176
+ expect(rails_amp_canonical_url).to eq("#{request.base_url}/admin/users")
177
+ expect(response.body).to include(
178
+ %(<link rel="canonical" href="#{request.base_url}/admin/users" />)
179
+ )
180
+ end
181
+
182
+ it 'has correct canonical url with params' do
183
+ get 'index', format: RailsAmp.default_format.to_s, params: { sort: 'name' }
184
+ expect(request.url).to eq("#{request.base_url}/admin/users.#{RailsAmp.default_format}?sort=name")
185
+ expect(rails_amp_canonical_url).to eq("#{request.base_url}/admin/users?sort=name")
186
+ expect(response.body).to include(
187
+ %(<link rel="canonical" href="#{request.base_url}/admin/users?sort=name" />)
188
+ )
189
+ end
190
+
191
+ context 'with html format' do
192
+ it 'is not renderable by amp' do
193
+ get 'index'
194
+ expect(amp_renderable?).to eq false
195
+ end
196
+
197
+ it 'has normal image tag' do
198
+ get 'index'
199
+ expect(image_tag('rails.png', size: '30x20', border: '0')).to match(
200
+ %r{(<img border="0" src="/(images|assets)/rails-?\w*?.png" alt="Rails" width="30" height="20" />)|(<img alt="Rails" border="0" height="20" src="/(images|assets)/rails-?\w*?.png" width="30" />)}
201
+ )
202
+ expect(image_tag('rails.png')).to match(
203
+ %r{(<img src="/(images|assets)/rails-?\w*?.png" alt="Rails" />)|(<img alt="Rails" src="/(images|assets)/rails-?\w*?.png" />)}
204
+ )
205
+ # According to dummy app default view `home/_amp_info`
206
+ expect(response.body).to match(
207
+ %r{(<img border="0" src="/(images|assets)/rails-?\w*?.png" alt="Rails" width="30" height="20" />)|(<img alt="Rails" border="0" height="20" src="/(images|assets)/rails-?\w*?.png" width="30" />)}
208
+ )
209
+ end
210
+ end
211
+
212
+ context 'with amp format' do
213
+ it 'is renderable by amp' do
214
+ get 'index', format: RailsAmp.default_format.to_s
215
+ expect(amp_renderable?).to eq true
216
+ end
217
+
218
+ it 'has amp-img tag' do
219
+ get 'index', format: RailsAmp.default_format.to_s
220
+ expect(image_tag('rails.png', size: '30x20', border: '0')).to match(
221
+ %r{(<amp-img src="/(images|assets)/rails-?\w*?.png" alt="Rails" width="30" height="20" layout="fixed" /></amp-img>)|(<amp-img alt="Rails" height="20" layout="fixed" src="/(images|assets)/rails-?\w*?.png" width="30" /></amp-img>)}
222
+ )
223
+ expect(image_tag('rails.png')).to match(
224
+ %r{(<amp-img src="/(images|assets)/rails-?\w*?.png" alt="Rails" width="50" height="64" layout="fixed" /></amp-img>)|(<amp-img alt="Rails" height="64" layout="fixed" src="/(images|assets)/rails-?\w*?.png" width="50" /></amp-img>)}
225
+ )
226
+ # According to dummy app default view `home/_amp_info`
227
+ expect(response.body).to match(
228
+ %r{(<amp-img src="/(images|assets)/rails-?\w*?.png" alt="Rails" width="30" height="20" layout="fixed" /></amp-img>)|(<amp-img alt="Rails" height="20" layout="fixed" src="/(images|assets)/rails-?\w*?.png" width="30" /></amp-img>)}
229
+ )
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,7 @@
1
+ # Just for test.
2
+ module Admin
3
+ class SessionsController < ApplicationController
4
+ def index
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # Just for test.
2
+ module Admin
3
+ class UsersController < ::ApplicationController
4
+ def index
5
+ end
6
+
7
+ def show
8
+ end
9
+
10
+ def new
11
+ end
12
+
13
+ def create
14
+ end
15
+
16
+ def edit
17
+ end
18
+
19
+ def update
20
+ end
21
+
22
+ def destroy
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ <%# Just for test. %>
2
+ <h1>Session</h1>
3
+
4
+ <%# Just for test. %>
5
+ <%= render 'home/amp_info' %>
@@ -0,0 +1,5 @@
1
+ <%# Just for test. %>
2
+ <h1>Admin - Users List</h1>
3
+
4
+ <%# Just for test. %>
5
+ <%= render 'home/amp_info' %>
@@ -0,0 +1,4 @@
1
+ <%# Just for test. %>
2
+ <h1>Admin - User Profile</h1>
3
+
4
+ <%= render 'home/amp_info' %>
@@ -22,7 +22,7 @@
22
22
  #
23
23
  targets:
24
24
  users: index show
25
-
25
+ admin/users: index show
26
26
  # --------------------------------------------------
27
27
  # To set initial configurations.
28
28
  # --------------------------------------------------
@@ -5,6 +5,9 @@ Rails.application.routes.draw do
5
5
  get '/home/index', to: 'home#index'
6
6
  get '/home/help', to: 'home#help'
7
7
  get '/home/about', to: 'home#about'
8
-
8
+ namespace :admin do
9
+ resources :sessions
10
+ resources :users, :only => [:index, :show]
11
+ end
9
12
  resources :users, :only => [:index, :show]
10
13
  end
@@ -10,13 +10,14 @@ describe RailsAmp do
10
10
  expect(RailsAmp.controller_to_key(HomeController)).to eq 'home'
11
11
  expect(RailsAmp.key_to_controller('users')).to eq UsersController
12
12
  expect(RailsAmp.key_to_controller('home')).to eq HomeController
13
+ expect(RailsAmp.key_to_controller('admin/users')).to eq Admin::UsersController
13
14
  end
14
15
 
15
16
  describe Config do
16
17
  it 'returns correct config default values' do
17
18
  expect(RailsAmp.config_file).to eq "#{Rails.root}/config/rails_amp.yml"
18
19
  expect(RailsAmp.default_format).to eq :amp
19
- expect(RailsAmp.targets).to eq({ 'users' => ['index', 'show'] })
20
+ expect(RailsAmp.targets).to eq("users"=>["index", "show"], "admin/users"=>["index", "show"])
20
21
  expect(RailsAmp.analytics).to eq ''
21
22
  end
22
23
 
@@ -31,7 +32,7 @@ describe RailsAmp do
31
32
 
32
33
  context 'when using default /config/rails_amp.yml' do
33
34
  it 'returns correct config values' do
34
- expect(RailsAmp.targets).to eq({ 'users' => ['index', 'show'] })
35
+ expect(RailsAmp.targets).to eq("users"=>["index", "show"], "admin/users"=>["index", "show"])
35
36
  expect(RailsAmp.target_actions(UsersController)).to eq ['index', 'show']
36
37
  expect(RailsAmp.target_actions(HomeController)).to eq []
37
38
  expect(RailsAmp.target?('users', 'index')).to eq true
@@ -39,6 +40,8 @@ describe RailsAmp do
39
40
  expect(RailsAmp.target?('home', 'index')).to eq false
40
41
  expect(RailsAmp.target?('home', 'help')).to eq false
41
42
  expect(RailsAmp.target?('home', 'about')).to eq false
43
+ expect(RailsAmp.target?('admin/users', 'index')).to eq true
44
+ expect(RailsAmp.target?('admin/sessions', 'index')).to eq false
42
45
  end
43
46
  end
44
47
 
@@ -166,6 +169,8 @@ describe RailsAmp do
166
169
  expect(RailsAmp.target?('home', 'index')).to eq true
167
170
  expect(RailsAmp.target?('home', 'help')).to eq true
168
171
  expect(RailsAmp.target?('home', 'about')).to eq true
172
+ expect(RailsAmp.target?('admin/users', 'index')).to eq true
173
+ expect(RailsAmp.target?('admin/sessions', 'index')).to eq true
169
174
  end
170
175
  end
171
176
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_amp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takafumi Yamano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-08 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -105,6 +105,8 @@ files:
105
105
  - spec/dummy/app/assets/stylesheets/application.css
106
106
  - spec/dummy/app/channels/application_cable/channel.rb
107
107
  - spec/dummy/app/channels/application_cable/connection.rb
108
+ - spec/dummy/app/controllers/admin/sessions_controller.rb
109
+ - spec/dummy/app/controllers/admin/users_controller.rb
108
110
  - spec/dummy/app/controllers/application_controller.rb
109
111
  - spec/dummy/app/controllers/concerns/.keep
110
112
  - spec/dummy/app/controllers/home_controller.rb
@@ -115,6 +117,9 @@ files:
115
117
  - spec/dummy/app/models/application_record.rb
116
118
  - spec/dummy/app/models/concerns/.keep
117
119
  - spec/dummy/app/models/user.rb
120
+ - spec/dummy/app/views/admin/sessions/index.html.erb
121
+ - spec/dummy/app/views/admin/users/index.html.erb
122
+ - spec/dummy/app/views/admin/users/show.html.erb
118
123
  - spec/dummy/app/views/home/_amp_info.html.erb
119
124
  - spec/dummy/app/views/home/about.html.erb
120
125
  - spec/dummy/app/views/home/help.html.erb
@@ -214,6 +219,8 @@ test_files:
214
219
  - spec/dummy/app/assets/stylesheets/application.css
215
220
  - spec/dummy/app/channels/application_cable/channel.rb
216
221
  - spec/dummy/app/channels/application_cable/connection.rb
222
+ - spec/dummy/app/controllers/admin/sessions_controller.rb
223
+ - spec/dummy/app/controllers/admin/users_controller.rb
217
224
  - spec/dummy/app/controllers/application_controller.rb
218
225
  - spec/dummy/app/controllers/concerns/.keep
219
226
  - spec/dummy/app/controllers/home_controller.rb
@@ -224,6 +231,9 @@ test_files:
224
231
  - spec/dummy/app/models/application_record.rb
225
232
  - spec/dummy/app/models/concerns/.keep
226
233
  - spec/dummy/app/models/user.rb
234
+ - spec/dummy/app/views/admin/sessions/index.html.erb
235
+ - spec/dummy/app/views/admin/users/index.html.erb
236
+ - spec/dummy/app/views/admin/users/show.html.erb
227
237
  - spec/dummy/app/views/home/_amp_info.html.erb
228
238
  - spec/dummy/app/views/home/about.html.erb
229
239
  - spec/dummy/app/views/home/help.html.erb