# RailsAmp RailsAmp is a Ruby on Rails plugin that makes it easy to build views for AMP(Accelerated Mobile Pages). ## Supported Versions Rails 4.1, 4.2, 5.0 ## Installation In your Gemfile: ```ruby gem 'rails_amp' ``` And install: ```bash $ bundle install ``` And then, generate codes and files: ```bash $ rails generate rails_amp:install ``` This step generates the followings. ```bash insert config/initializers/mime_types.rb create config/rails_amp.yml create app/views/layouts/rails_amp_application.amp.erb ``` In config/initializers/mime_types.rb: ```ruby Mime::Type.register_alias 'text/html', RailsAmp.default_format ``` 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 ## Configurations You can change RailsAmp configurations in your `config/rails_amp.yml`. Write configs with yaml. In config/rails_amp.yml: ```yaml # ### Here are some config samples to use rails_amp. # -------------------------------------------------- # To enable amp on specific controllers and actions. # -------------------------------------------------- # ### Enable amp on users all actions. # targets: # users: # # ### Enable amp on users#index, users#show, posts#index, posts#show. # ### controller: action1 action2 action3 ... # targets: # users: index show # posts: index show # # ### Enable amp on all controllers and actions. # targets: # application: all # # ### Disable amp completely. # targets: # targets: users: index show # -------------------------------------------------- # To set initial configurations. # -------------------------------------------------- # ### Enable Google Analytics page tracking. Set your Google Analytics Account. # analytics: UA-*****-* # # ### Change default amp format. The default value is amp. # ### If you want to use 'mobile' as amp format, set 'mobile' to default_format. # ### And you can access the amp page like /users/index.mobile # default_format: mobile # # ### Set formats that used as amp. The default is html. # ### These formats are used in the order, when the amp specialized view like 'users/index.amp.erb' is not found. # lookup_formats: html xhtml ``` ### Examples Set the controllers and actions that you want to enable amp. Enable amp on users all actions except for new, create, edit, update, destroy actions. ```yaml targets: users: ``` Note that RailsAmp automatically excludes the post-method related actions `new, create, edit, update, destroy` that originally provided by Rails from the amp targets. Enable amp on some specific controllers and actions. e.g.) users#index, users#show, posts#show. ```yaml targets: users: index show posts: show ``` Enable amp on all controllers and actions. (It's a bit dangerous, so I don't recommend.) ```yaml targets: application: all ``` Disable amp completely. ```yaml targets: ``` Other configurations. Enable Google Analytics page tracking. Set your Google Analytics Account. ```yaml analytics: UA-*****-* ``` 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`. ```yaml default_format: mobile ``` 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. ```yaml lookup_formats: html xhtml ``` Note that you need to restart a server to reload the configurations after changing config/rails_amp.yml. ## Setup Add the following code in your default layout head like `application.html.erb`. In app/views/layouts/application.html.erb: ```html <%= rails_amp_amphtml_link_tag %> ``` This code will put out the html header to inform where the amp url is. ```html ``` ### AMP link for root_url(root_path) 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. In your config/routes.rb: ```ruby root 'home#index' ``` And, in config/rails_amp.yml: ```yaml targets: home: index ``` The helper `rails_amp_amphtml_link_tag` will put out the following in the root url. In `http://example.com/`: ```html ``` So, you need to add a routing for this amp url. In your config/routes.rb: ```ruby get '/home/index', to: 'home#index' ``` ## Customize AMP layout In app/views/layouts/rails_amp_application.amp.erb: ```html