├── .gitignore ├── .travis.yml ├── Gemfile ├── MIT-LICENSE ├── README.md ├── Rakefile ├── gemfiles ├── Gemfile-rails.4.1.x └── Gemfile-rails.4.2.x ├── lib ├── generators │ └── rails_amp │ │ ├── install_generator.rb │ │ └── templates │ │ ├── rails_amp.yml │ │ └── rails_amp_application.amp.erb ├── rails_amp.rb └── rails_amp │ ├── config.rb │ ├── overrider.rb │ ├── railtie.rb │ ├── version.rb │ └── view_helpers │ ├── action_view.rb │ └── image_tag_helper.rb ├── rails_amp.gemspec └── spec ├── controllers └── application_controller_spec.rb ├── dummy ├── .rspec ├── Rakefile ├── app │ ├── assets │ │ ├── config │ │ │ └── manifest.js │ │ ├── images │ │ │ ├── .keep │ │ │ └── rails.png │ │ ├── javascripts │ │ │ ├── application.js │ │ │ ├── cable.js │ │ │ └── channels │ │ │ │ └── .keep │ │ └── stylesheets │ │ │ └── application.css │ ├── channels │ │ └── application_cable │ │ │ ├── channel.rb │ │ │ └── connection.rb │ ├── controllers │ │ ├── admin │ │ │ ├── sessions_controller.rb │ │ │ └── users_controller.rb │ │ ├── application_controller.rb │ │ ├── concerns │ │ │ └── .keep │ │ ├── home_controller.rb │ │ └── users_controller.rb │ ├── helpers │ │ └── application_helper.rb │ ├── jobs │ │ └── application_job.rb │ ├── mailers │ │ └── application_mailer.rb │ ├── models │ │ ├── application_record.rb │ │ ├── concerns │ │ │ └── .keep │ │ └── user.rb │ └── views │ │ ├── admin │ │ ├── sessions │ │ │ └── index.html.erb │ │ └── users │ │ │ ├── index.html.erb │ │ │ └── show.html.erb │ │ ├── home │ │ ├── _amp_info.html.erb │ │ ├── about.html.erb │ │ ├── help.html.erb │ │ └── index.html.erb │ │ ├── layouts │ │ ├── application.html.erb │ │ ├── mailer.html.erb │ │ ├── mailer.text.erb │ │ └── rails_amp_application.amp.erb │ │ └── users │ │ ├── index.html.erb │ │ └── show.html.erb ├── bin │ ├── bundle │ ├── rails │ ├── rake │ ├── setup │ └── update ├── config.ru ├── config │ ├── application.rb │ ├── boot.rb │ ├── cable.yml │ ├── database.yml │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ ├── initializers │ │ ├── application_controller_renderer.rb │ │ ├── assets.rb │ │ ├── backtrace_silencers.rb │ │ ├── cookies_serializer.rb │ │ ├── filter_parameter_logging.rb │ │ ├── inflections.rb │ │ ├── mime_types.rb │ │ ├── new_framework_defaults.rb │ │ ├── session_store.rb │ │ └── wrap_parameters.rb │ ├── locales │ │ └── en.yml │ ├── puma.rb │ ├── rails_amp.yml │ ├── routes.rb │ ├── secrets.yml │ └── spring.rb ├── db │ ├── migrate │ │ └── 20170130074559_create_users.rb │ └── schema.rb ├── lib │ └── assets │ │ └── .keep ├── log │ └── .keep └── public │ ├── 404.html │ ├── 422.html │ ├── 500.html │ ├── apple-touch-icon-precomposed.png │ ├── apple-touch-icon.png │ └── favicon.ico ├── rails_amp_spec.rb ├── rails_helper.rb ├── spec_helper.rb ├── support └── config │ ├── amp_format.yml │ ├── controller_actions.yml │ ├── controller_all.yml │ ├── disable_all.yml │ ├── enable_all.yml │ ├── enable_analytics.yml │ └── various.yml ├── utilities.rb └── view_helpers └── action_view_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | spec/dummy/db/*.sqlite3 5 | spec/dummy/db/*.sqlite3-journal 6 | spec/dummy/log/*.log 7 | spec/dummy/tmp/ 8 | /vendor/bundle/ 9 | .DS_Store 10 | *.gem 11 | *.rbc 12 | Gemfile.lock 13 | gemfiles/*.lock 14 | coverage 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | rvm: 4 | - 2.3.1 5 | - 2.2.3 6 | gemfile: 7 | - Gemfile 8 | - gemfiles/Gemfile-rails.4.2.x 9 | - gemfiles/Gemfile-rails.4.1.x 10 | script: 11 | - bundle exec rspec spec 12 | notifications: 13 | email: false 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 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] 15 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RailsAmp [](https://travis-ci.org/takafumir/rails_amp) 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 | 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 | 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 | 205 | 206 |
207 | 208 |