├── .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 [![Build Status](https://travis-ci.org/takafumir/rails_amp.svg?branch=master)](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 | Rails AMP 209 | 210 | 211 | 212 | 213 | 221 | 222 | 223 | 229 | 230 | <%= rails_amp_google_analytics_head %> 231 | <%= rails_amp_html_header %> 232 | 233 | 234 | <%= rails_amp_google_analytics_page_tracking %> 235 | <%= yield %> 236 | 237 | 238 | ``` 239 | 240 | Customize the page data type by JSON-LD with schema.org, and write custom css styles in the ` 26 | 27 | <%= rails_amp_google_analytics_head %> 28 | <%= rails_amp_html_header %> 29 | 30 | 31 | <%= rails_amp_google_analytics_page_tracking %> 32 | <%= yield %> 33 | 34 | 35 | -------------------------------------------------------------------------------- /lib/rails_amp.rb: -------------------------------------------------------------------------------- 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_path, action_name) 83 | target_actions = target_actions( key_to_controller(controller_path) ) 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_path, action_name) 92 | amp_format? && target?(controller_path, action_name) 93 | end 94 | }) 95 | end 96 | -------------------------------------------------------------------------------- /lib/rails_amp/config.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | 3 | module RailsAmp 4 | class Config 5 | # Return the current format, default is ''. The only configuration value that is not global. 6 | def format 7 | @format ||= '' 8 | end 9 | 10 | # Set the current format pseudo-globally, i.e. in the Thread.current hash. 11 | def format=(format) 12 | @format = format.to_s 13 | end 14 | 15 | # Return the config file path, default is "#{Rails.root}/config/rails_amp.yml". 16 | def config_file 17 | @@config_file ||= "#{Rails.root}/config/rails_amp.yml" 18 | end 19 | 20 | # Set the config file path. 21 | def config_file=(config_file) 22 | @@config_file = config_file 23 | end 24 | 25 | # Return the yaml loaded config, default is YAML.load_file(config_file). 26 | def load_config 27 | @@load_config ||= config_load_config 28 | end 29 | 30 | # Set the config by loading yaml. 31 | def load_config=(load_config) 32 | @@load_config = load_config 33 | end 34 | 35 | # Return the default amp format, default is :amp 36 | def default_format 37 | @@default_format ||= config_default_format 38 | end 39 | 40 | # Set the default amp format. 41 | def default_format=(default_format) 42 | @@default_format = default_format.to_sym 43 | end 44 | 45 | # Return the amp enabled controller actions. 46 | def targets 47 | @@targets ||= config_targets 48 | end 49 | 50 | # Set the amp enabled controller actions. 51 | def targets=(targets) 52 | @@targets = targets 53 | end 54 | 55 | # Return the analytics account, default is ''. 56 | def analytics 57 | @@analytics ||= config_analytics 58 | end 59 | 60 | # Set the analytics account. 61 | def analytics=(analytics) 62 | @@analytics = analytics 63 | end 64 | 65 | # Return the lookup_context formats for amp, default is [:html]. 66 | def lookup_formats 67 | @@lookup_formats ||= config_lookup_formats 68 | end 69 | 70 | # Set the lookup_context formats for amp. 71 | def lookup_formats=(lookup_formats) 72 | @@lookup_formats = lookup_formats 73 | end 74 | 75 | private 76 | 77 | def config_load_config 78 | YAML.load_file(config_file) 79 | end 80 | 81 | def config_default_format 82 | load_config['default_format'].try(:to_sym) || :amp 83 | end 84 | 85 | def config_targets 86 | targets = load_config['targets'] 87 | return {} if targets.blank? 88 | 89 | if targets.is_a?(Hash) && targets.has_key?('application') 90 | return targets 91 | end 92 | 93 | if targets.is_a?(Hash) 94 | return targets.map{ |k, v| [k, v.to_s.split(/\s+/)] }.to_h 95 | end 96 | 97 | {} 98 | end 99 | 100 | def config_analytics 101 | load_config['analytics'] || '' 102 | end 103 | 104 | def config_lookup_formats 105 | load_config['lookup_formats'].to_s.split(/\s+/).map(&:to_sym).presence || [:html] 106 | end 107 | end 108 | end 109 | -------------------------------------------------------------------------------- /lib/rails_amp/overrider.rb: -------------------------------------------------------------------------------- 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_path, 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 | unless performed? 28 | respond_to do |format| 29 | format.send(RailsAmp.default_format.to_sym) do 30 | # search amp format(default is .amp) .html templates 31 | lookup_context.formats = [RailsAmp.default_format] + RailsAmp.lookup_formats 32 | render layout: 'rails_amp_application.amp' 33 | end 34 | end 35 | end 36 | end 37 | end 38 | end 39 | prepend RailsAmp::ActionOverrider 40 | end 41 | end 42 | end 43 | 44 | module ActionOverrider 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/rails_amp/railtie.rb: -------------------------------------------------------------------------------- 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 18 | -------------------------------------------------------------------------------- /lib/rails_amp/version.rb: -------------------------------------------------------------------------------- 1 | module RailsAmp 2 | VERSION = '0.2.2' 3 | end 4 | -------------------------------------------------------------------------------- /lib/rails_amp/view_helpers/action_view.rb: -------------------------------------------------------------------------------- 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_path, controller.action_name) 8 | 9 | amp_uri = URI.parse(request.url) 10 | if request.path == root_path 11 | amp_path = "#{controller.controller_path}/#{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().html_safe 19 | end 20 | 21 | def rails_amp_html_header 22 | header =<<"EOS" 23 | 24 | 25 | 26 | EOS 27 | header.html_safe 28 | end 29 | 30 | def rails_amp_google_analytics_head 31 | return '' if RailsAmp.analytics.blank? 32 | 33 | analytics_head =<<"EOS" 34 | 35 | 36 | EOS 37 | analytics_head.html_safe 38 | end 39 | 40 | def rails_amp_google_analytics_page_tracking 41 | return '' if RailsAmp.analytics.blank? 42 | 43 | analytics_code =<<"EOS" 44 | 45 | 46 | 59 | 60 | EOS 61 | analytics_code.html_safe 62 | end 63 | 64 | def rails_amp_canonical_url 65 | request.url.gsub(".#{RailsAmp.default_format.to_s}", '') 66 | end 67 | 68 | def amp_renderable? 69 | RailsAmp.amp_renderable?(controller.controller_path, controller.action_name) 70 | end 71 | 72 | ::ActionView::Base.send :include, self 73 | end 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /lib/rails_amp/view_helpers/image_tag_helper.rb: -------------------------------------------------------------------------------- 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 9 | fallback heights layout media noloading on placeholder sizes 10 | class 11 | ].freeze 12 | 13 | # ref: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/asset_tag_helper.rb#L228 14 | def amp_image_tag(source, options={}) 15 | options = options.symbolize_keys 16 | check_for_image_tag_errors(options) if defined?(check_for_image_tag_errors) 17 | 18 | src = options[:src] = path_to_image(source, skip_pipeline: options.delete(:skip_pipeline)) 19 | 20 | unless src.start_with?("cid:") || src.start_with?("data:") || src.blank? 21 | options[:alt] = options.fetch(:alt){ image_alt(src) } 22 | end 23 | 24 | options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size] 25 | 26 | if options[:width].blank? || options[:height].blank? 27 | options[:width], options[:height] = compute_image_size(source) 28 | end 29 | 30 | options[:layout] ||= 'fixed' 31 | options.select! { |key, _| key.to_s.in?(AMP_IMG_PERMITTED_ATTRIBUTES) } 32 | tag('amp-img', options) + ''.html_safe 33 | end 34 | 35 | # override image_tag helper in ActionView::Helpers::AssetTagHelper 36 | def image_tag(source, options={}) 37 | if controller && RailsAmp.amp_renderable?(controller.controller_path, controller.action_name) 38 | amp_image_tag(source, options) 39 | else 40 | super 41 | end 42 | end 43 | 44 | private 45 | 46 | def compute_image_size(source) 47 | source_for_fastimage = source 48 | unless source =~ ::ActionView::Helpers::AssetUrlHelper::URI_REGEXP 49 | # find_asset is a Sprockets method 50 | asset_env = Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application) 51 | source_for_fastimage = asset_env.find_asset(source).try(:pathname).to_s.presence || 52 | File.join(Rails.public_path, source) 53 | end 54 | FastImage.size(source_for_fastimage) || [300, 300] 55 | end 56 | 57 | ::ActionView::Base.send :prepend, self 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /rails_amp.gemspec: -------------------------------------------------------------------------------- 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 'byebug' 23 | s.add_development_dependency 'rspec-rails' 24 | end 25 | -------------------------------------------------------------------------------- /spec/controllers/application_controller_spec.rb: -------------------------------------------------------------------------------- 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() 22 | ) 23 | expect(response.body).to include( 24 | %Q() 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() 32 | ) 33 | expect(response.body).to include( 34 | %Q() 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() 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() 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{(Rails)|(Rails)} 66 | ) 67 | expect(image_tag('rails.png')).to match( 68 | %r{(Rails)|(Rails)} 69 | ) 70 | # According to dummy app default view `home/_amp_info` 71 | expect(response.body).to match( 72 | %r{(Rails)|(Rails)} 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{()|()} 87 | ) 88 | expect(image_tag('rails.png')).to match( 89 | %r{()|()} 90 | ) 91 | # According to dummy app default view `home/_amp_info` 92 | expect(response.body).to match( 93 | %r{()|()} 94 | ) 95 | end 96 | end 97 | 98 | context '#redirect GET' do 99 | it 'allows redirect_to or render' do 100 | if Gem.loaded_specs["rails"].version.to_s >= "5" 101 | get 'index', params: { redirect_test: true }, format: RailsAmp.default_format.to_s 102 | else 103 | get 'index', redirect_test: true, format: RailsAmp.default_format.to_s 104 | end 105 | 106 | expect(response).to redirect_to(root_path) 107 | end 108 | end 109 | end 110 | end 111 | 112 | describe HomeController do 113 | context '#help GET' do 114 | # Home#help is not available for amp. 115 | it 'does not return amphtml link tag' do 116 | get 'help' 117 | expect(rails_amp_amphtml_link_tag).to eq '' 118 | end 119 | 120 | context 'with html format' do 121 | it 'is not renderable by amp' do 122 | get 'help' 123 | expect(amp_renderable?).to eq false 124 | end 125 | end 126 | 127 | context 'with amp format' do 128 | it 'is not renderable by amp' do 129 | expect do 130 | get 'help', format: RailsAmp.default_format.to_s 131 | end.to raise_error(StandardError) # ActionView::MissingTemplate or ActionController::UnknownFormat 132 | end 133 | end 134 | end 135 | end 136 | 137 | describe Admin::SessionsController do 138 | # Admin::SessionsController#index is not available for amp. 139 | it 'does not return amphtml link tag' do 140 | get 'index' 141 | expect(rails_amp_amphtml_link_tag).to eq '' 142 | end 143 | 144 | context 'with html format' do 145 | it 'is not renderable by amp' do 146 | get 'index' 147 | expect(amp_renderable?).to eq false 148 | end 149 | end 150 | 151 | context 'with amp format' do 152 | it 'is not renderable by amp' do 153 | expect do 154 | get 'index', format: RailsAmp.default_format.to_s 155 | end.to raise_error(StandardError) # ActionView::MissingTemplate or ActionController::UnknownFormat 156 | end 157 | end 158 | end 159 | 160 | describe Admin::UsersController do 161 | render_views 162 | 163 | context '#index GET' do 164 | # Admin::Users#index is available for amp. 165 | it 'has correct amphtml link tag' do 166 | get 'index' 167 | expect(rails_amp_amphtml_link_tag).to eq( 168 | %() 169 | ) 170 | expect(response.body).to include( 171 | %() 172 | ) 173 | end 174 | 175 | it 'has correct amphtml link tag with params' do 176 | get 'index', params: { sort: 'name' } 177 | expect(rails_amp_amphtml_link_tag).to eq( 178 | %() 179 | ) 180 | expect(response.body).to include( 181 | %() 182 | ) 183 | end 184 | 185 | it 'has correct canonical url' do 186 | get 'index', format: RailsAmp.default_format.to_s 187 | expect(request.url).to eq("#{request.base_url}/admin/users.#{RailsAmp.default_format}") 188 | expect(rails_amp_canonical_url).to eq("#{request.base_url}/admin/users") 189 | expect(response.body).to include( 190 | %() 191 | ) 192 | end 193 | 194 | it 'has correct canonical url with params' do 195 | get 'index', format: RailsAmp.default_format.to_s, params: { sort: 'name' } 196 | expect(request.url).to eq("#{request.base_url}/admin/users.#{RailsAmp.default_format}?sort=name") 197 | expect(rails_amp_canonical_url).to eq("#{request.base_url}/admin/users?sort=name") 198 | expect(response.body).to include( 199 | %() 200 | ) 201 | end 202 | 203 | context 'with html format' do 204 | it 'is not renderable by amp' do 205 | get 'index' 206 | expect(amp_renderable?).to eq false 207 | end 208 | 209 | it 'has normal image tag' do 210 | get 'index' 211 | expect(image_tag('rails.png', size: '30x20', border: '0')).to match( 212 | %r{(Rails)|(Rails)} 213 | ) 214 | expect(image_tag('rails.png')).to match( 215 | %r{(Rails)|(Rails)} 216 | ) 217 | # According to dummy app default view `home/_amp_info` 218 | expect(response.body).to match( 219 | %r{(Rails)|(Rails)} 220 | ) 221 | end 222 | end 223 | 224 | context 'with amp format' do 225 | it 'is renderable by amp' do 226 | get 'index', format: RailsAmp.default_format.to_s 227 | expect(amp_renderable?).to eq true 228 | end 229 | 230 | it 'has amp-img tag' do 231 | get 'index', format: RailsAmp.default_format.to_s 232 | expect(image_tag('rails.png', size: '30x20', border: '0', class: 'a_class')).to match( 233 | %r{()|()} 234 | ) 235 | expect(image_tag('rails.png')).to match( 236 | %r{()|()} 237 | ) 238 | # According to dummy app default view `home/_amp_info` 239 | expect(response.body).to match( 240 | %r{()|()} 241 | ) 242 | end 243 | end 244 | end 245 | end 246 | -------------------------------------------------------------------------------- /spec/dummy/.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /spec/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative 'config/application' 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | 2 | //= link_tree ../images 3 | //= link_directory ../javascripts .js 4 | //= link_directory ../stylesheets .css 5 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/app/assets/images/.keep -------------------------------------------------------------------------------- /spec/dummy/app/assets/images/rails.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/app/assets/images/rails.png -------------------------------------------------------------------------------- /spec/dummy/app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 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 . 14 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/javascripts/cable.js: -------------------------------------------------------------------------------- 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_self 5 | //= require_tree ./channels 6 | 7 | (function() { 8 | this.App || (this.App = {}); 9 | 10 | App.cable = ActionCable.createConsumer(); 11 | 12 | }).call(this); 13 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/javascripts/channels/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/app/assets/javascripts/channels/.keep -------------------------------------------------------------------------------- /spec/dummy/app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 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 | */ 16 | -------------------------------------------------------------------------------- /spec/dummy/app/channels/application_cable/channel.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Channel < ActionCable::Channel::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy/app/channels/application_cable/connection.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Connection < ActionCable::Connection::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/admin/sessions_controller.rb: -------------------------------------------------------------------------------- 1 | # Just for test. 2 | module Admin 3 | class SessionsController < ApplicationController 4 | def index 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/admin/users_controller.rb: -------------------------------------------------------------------------------- 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 26 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | protect_from_forgery with: :exception 3 | end 4 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /spec/dummy/app/controllers/home_controller.rb: -------------------------------------------------------------------------------- 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 18 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/users_controller.rb: -------------------------------------------------------------------------------- 1 | # Just for test. 2 | class UsersController < ApplicationController 3 | def index 4 | redirect_to root_path if params['redirect_test'].to_s == "true" 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 | -------------------------------------------------------------------------------- /spec/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | default from: 'from@example.com' 3 | layout 'mailer' 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy/app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /spec/dummy/app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/app/models/concerns/.keep -------------------------------------------------------------------------------- /spec/dummy/app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/app/views/admin/sessions/index.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

Session

3 | 4 | <%# Just for test. %> 5 | <%= render 'home/amp_info' %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/admin/users/index.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

Admin - Users List

3 | 4 | <%# Just for test. %> 5 | <%= render 'home/amp_info' %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/admin/users/show.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

Admin - User Profile

3 | 4 | <%= render 'home/amp_info' %> 5 | -------------------------------------------------------------------------------- /spec/dummy/app/views/home/_amp_info.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

RailsAmp Information

3 |

Rails Version: <%= Rails.version %>

4 |

RailsAmp Object ID: <%= RailsAmp.object_id %>

5 |

RailsAmp format: <%= RailsAmp.format.inspect %>

6 |

RailsAmp default_format: <%= RailsAmp.default_format.inspect %>

7 |

RailsAmp targets: <%= RailsAmp.targets.inspect %>

8 |

RailsAmp analytics: <%= RailsAmp.analytics.inspect %>

9 |

RailsAmp analytics class: <%= RailsAmp.analytics.class %>

10 |

RailsAmp MimeTypes: <%= Mime::EXTENSION_LOOKUP %>

11 |

RailsAmp Ancestors: <%= controller.class.ancestors %>

12 |

<%= image_tag('rails.png', size: '30x20', border: '0') %>

13 |

<%= image_tag('rails.png') %>

14 | 15 | <% if amp_renderable? %> 16 |

This is an amp page.

17 | <% else %> 18 |

This is a html page.

19 | <% end %> 20 | -------------------------------------------------------------------------------- /spec/dummy/app/views/home/about.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 | <%= render 'home/amp_info' %> 3 | -------------------------------------------------------------------------------- /spec/dummy/app/views/home/help.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 | <%= render 'home/amp_info' %> 3 | -------------------------------------------------------------------------------- /spec/dummy/app/views/home/index.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 | <%= render 'home/amp_info' %> 3 | -------------------------------------------------------------------------------- /spec/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Dummy 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 | 11 | 12 | 13 | <%= yield %> 14 | 15 | 16 | -------------------------------------------------------------------------------- /spec/dummy/app/views/layouts/mailer.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /spec/dummy/app/views/layouts/mailer.text.erb: -------------------------------------------------------------------------------- 1 | <%= yield %> 2 | -------------------------------------------------------------------------------- /spec/dummy/app/views/layouts/rails_amp_application.amp.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Rails AMP 6 | 7 | 8 | 9 | 10 | 11 | 19 | 20 | 21 | 27 | 28 | <%= rails_amp_google_analytics_page_tracking %> 29 | <%= rails_amp_html_header %> 30 | 31 | 32 | <%= yield %> 33 | 34 | 35 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/index.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

Users List

3 | 4 | <%# Just for test. %> 5 | <%= render 'home/amp_info' %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/show.html.erb: -------------------------------------------------------------------------------- 1 | <%# Just for test. %> 2 |

User Profile

3 | 4 | <%= render 'home/amp_info' %> 5 | -------------------------------------------------------------------------------- /spec/dummy/bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /spec/dummy/bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path('../config/application', __dir__) 3 | require_relative '../config/boot' 4 | require 'rails/commands' 5 | -------------------------------------------------------------------------------- /spec/dummy/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../config/boot' 3 | require 'rake' 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /spec/dummy/bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a starting point to setup your application. 15 | # Add necessary setup steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | # puts "\n== Copying sample files ==" 22 | # unless File.exist?('config/database.yml') 23 | # cp 'config/database.yml.sample', 'config/database.yml' 24 | # end 25 | 26 | puts "\n== Preparing database ==" 27 | system! 'bin/rails db:setup' 28 | 29 | puts "\n== Removing old logs and tempfiles ==" 30 | system! 'bin/rails log:clear tmp:clear' 31 | 32 | puts "\n== Restarting application server ==" 33 | system! 'bin/rails restart' 34 | end 35 | -------------------------------------------------------------------------------- /spec/dummy/bin/update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a way to update your development environment automatically. 15 | # Add necessary update steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | puts "\n== Updating database ==" 22 | system! 'bin/rails db:migrate' 23 | 24 | puts "\n== Removing old logs and tempfiles ==" 25 | system! 'bin/rails log:clear tmp:clear' 26 | 27 | puts "\n== Restarting application server ==" 28 | system! 'bin/rails restart' 29 | end 30 | -------------------------------------------------------------------------------- /spec/dummy/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative 'config/environment' 4 | 5 | run Rails.application 6 | -------------------------------------------------------------------------------- /spec/dummy/config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | # Pick the frameworks you want: 4 | require "active_record/railtie" 5 | require "action_controller/railtie" 6 | require "action_view/railtie" 7 | require "action_mailer/railtie" 8 | # require "active_job/railtie" 9 | # require "action_cable/engine" 10 | # require "rails/test_unit/railtie" 11 | require "sprockets/railtie" 12 | 13 | Bundler.require(*Rails.groups) 14 | require "rails_amp" 15 | 16 | module Dummy 17 | class Application < Rails::Application 18 | # Settings in config/environments/* take precedence over those specified here. 19 | # Application configuration should go into files in config/initializers 20 | # -- all .rb files in that directory are automatically loaded. 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /spec/dummy/config/boot.rb: -------------------------------------------------------------------------------- 1 | # Set up gems listed in the Gemfile. 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__) 3 | 4 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) 5 | $LOAD_PATH.unshift File.expand_path('../../../lib', __dir__) 6 | -------------------------------------------------------------------------------- /spec/dummy/config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: async 6 | 7 | production: 8 | adapter: redis 9 | url: redis://localhost:6379/1 10 | -------------------------------------------------------------------------------- /spec/dummy/config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /spec/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports. 13 | config.consider_all_requests_local = true 14 | 15 | # Enable/disable caching. By default caching is disabled. 16 | if Rails.root.join('tmp/caching-dev.txt').exist? 17 | config.action_controller.perform_caching = true 18 | 19 | config.cache_store = :memory_store 20 | config.public_file_server.headers = { 21 | 'Cache-Control' => 'public, max-age=172800' 22 | } 23 | else 24 | config.action_controller.perform_caching = false 25 | 26 | config.cache_store = :null_store 27 | end 28 | 29 | # Don't care if the mailer can't send. 30 | config.action_mailer.raise_delivery_errors = false 31 | 32 | config.action_mailer.perform_caching = false 33 | 34 | # Print deprecation notices to the Rails logger. 35 | config.active_support.deprecation = :log 36 | 37 | # Raise an error on page load if there are pending migrations. 38 | config.active_record.migration_error = :page_load 39 | 40 | # Debug mode disables concatenation and preprocessing of assets. 41 | # This option may cause significant delays in view rendering with a large 42 | # number of complex assets. 43 | config.assets.debug = true 44 | 45 | # Suppress logger output for asset requests. 46 | config.assets.quiet = true 47 | 48 | # Raises error for missing translations 49 | # config.action_view.raise_on_missing_translations = true 50 | 51 | # Use an evented file watcher to asynchronously detect changes in source code, 52 | # routes, locales, etc. This feature depends on the listen gem. 53 | # config.file_watcher = ActiveSupport::EventedFileUpdateChecker 54 | end 55 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both threaded web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Disable serving static files from the `/public` folder by default since 18 | # Apache or NGINX already handles this. 19 | config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 20 | 21 | # Compress JavaScripts and CSS. 22 | config.assets.js_compressor = :uglifier 23 | # config.assets.css_compressor = :sass 24 | 25 | # Do not fallback to assets pipeline if a precompiled asset is missed. 26 | config.assets.compile = false 27 | 28 | # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 29 | 30 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 31 | # config.action_controller.asset_host = 'http://assets.example.com' 32 | 33 | # Specifies the header that your server uses for sending files. 34 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 35 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 36 | 37 | # Mount Action Cable outside main process or domain 38 | # config.action_cable.mount_path = nil 39 | # config.action_cable.url = 'wss://example.com/cable' 40 | # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Use the lowest log level to ensure availability of diagnostic information 46 | # when problems arise. 47 | config.log_level = :debug 48 | 49 | # Prepend all log lines with the following tags. 50 | config.log_tags = [ :request_id ] 51 | 52 | # Use a different cache store in production. 53 | # config.cache_store = :mem_cache_store 54 | 55 | # Use a real queuing backend for Active Job (and separate queues per environment) 56 | # config.active_job.queue_adapter = :resque 57 | # config.active_job.queue_name_prefix = "dummy_#{Rails.env}" 58 | config.action_mailer.perform_caching = false 59 | 60 | # Ignore bad email addresses and do not raise email delivery errors. 61 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 62 | # config.action_mailer.raise_delivery_errors = false 63 | 64 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 65 | # the I18n.default_locale when a translation cannot be found). 66 | config.i18n.fallbacks = true 67 | 68 | # Send deprecation notices to registered listeners. 69 | config.active_support.deprecation = :notify 70 | 71 | # Use default logging formatter so that PID and timestamp are not suppressed. 72 | config.log_formatter = ::Logger::Formatter.new 73 | 74 | # Use a different logger for distributed setups. 75 | # require 'syslog/logger' 76 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 77 | 78 | if ENV["RAILS_LOG_TO_STDOUT"].present? 79 | logger = ActiveSupport::Logger.new(STDOUT) 80 | logger.formatter = config.log_formatter 81 | config.logger = ActiveSupport::TaggedLogging.new(logger) 82 | end 83 | 84 | # Do not dump schema after migrations. 85 | config.active_record.dump_schema_after_migration = false 86 | end 87 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure public file server for tests with Cache-Control for performance. 16 | # config.public_file_server.enabled = true 17 | # config.public_file_server.headers = { 18 | # 'Cache-Control' => 'public, max-age=3600' 19 | # } 20 | 21 | # Show full error reports and disable caching. 22 | config.consider_all_requests_local = true 23 | config.action_controller.perform_caching = false 24 | 25 | # Raise exceptions instead of rendering exception templates. 26 | config.action_dispatch.show_exceptions = false 27 | 28 | # Disable request forgery protection in test environment. 29 | config.action_controller.allow_forgery_protection = false 30 | # config.action_mailer.perform_caching = false 31 | 32 | # Tell Action Mailer not to deliver emails to the real world. 33 | # The :test delivery method accumulates sent emails in the 34 | # ActionMailer::Base.deliveries array. 35 | config.action_mailer.delivery_method = :test 36 | 37 | # Print deprecation notices to the stderr. 38 | config.active_support.deprecation = :stderr 39 | 40 | # Raises error for missing translations 41 | # config.action_view.raise_on_missing_translations = true 42 | end 43 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # ApplicationController.renderer.defaults.merge!( 4 | # http_host: 'example.org', 5 | # https: false 6 | # ) 7 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = '1.0' 5 | 6 | # Add additional assets to the asset load path 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | 9 | # Precompile additional assets. 10 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 11 | # Rails.application.config.assets.precompile += %w( search.js ) 12 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/cookies_serializer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Specify a serializer for the signed and encrypted cookie jars. 4 | # Valid options are :json, :marshal, and :hybrid. 5 | Rails.application.config.action_dispatch.cookies_serializer = :json 6 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | Mime::Type.register_alias 'text/html', RailsAmp.default_format 6 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/new_framework_defaults.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # 3 | # This file contains migration options to ease your Rails 5.0 upgrade. 4 | # 5 | # Read the Guide for Upgrading Ruby on Rails for more info on each option. 6 | 7 | # Enable per-form CSRF tokens. Previous versions had false. 8 | # Rails.application.config.action_controller.per_form_csrf_tokens = true 9 | 10 | # Enable origin-checking CSRF mitigation. Previous versions had false. 11 | # Rails.application.config.action_controller.forgery_protection_origin_check = true 12 | 13 | # Make Ruby 2.4 preserve the timezone of the receiver when calling `to_time`. 14 | # Previous versions had false. 15 | # ActiveSupport.to_time_preserves_timezone = true 16 | 17 | # Require `belongs_to` associations by default. Previous versions had false. 18 | # Rails.application.config.active_record.belongs_to_required_by_default = true 19 | 20 | # Do not halt callback chains when a callback returns false. Previous versions had true. 21 | # ActiveSupport.halt_callback_chains_on_return_false = false 22 | 23 | # Configure SSL options to enable HSTS with subdomains. Previous versions had false. 24 | # Rails.application.config.ssl_options = { hsts: { subdomains: true } } 25 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.session_store :cookie_store, key: '_dummy_session' 4 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters) 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /spec/dummy/config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /spec/dummy/config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum, this matches the default thread size of Active Record. 6 | # 7 | threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i 8 | threads threads_count, threads_count 9 | 10 | # Specifies the `port` that Puma will listen on to receive requests, default is 3000. 11 | # 12 | port ENV.fetch("PORT") { 3000 } 13 | 14 | # Specifies the `environment` that Puma will run in. 15 | # 16 | environment ENV.fetch("RAILS_ENV") { "development" } 17 | 18 | # Specifies the number of `workers` to boot in clustered mode. 19 | # Workers are forked webserver processes. If using threads and workers together 20 | # the concurrency of the application would be max `threads` * `workers`. 21 | # Workers do not work on JRuby or Windows (both of which do not support 22 | # processes). 23 | # 24 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 25 | 26 | # Use the `preload_app!` method when specifying a `workers` number. 27 | # This directive tells Puma to first boot the application and load code 28 | # before forking the application. This takes advantage of Copy On Write 29 | # process behavior so workers use less memory. If you use this option 30 | # you need to make sure to reconnect any threads in the `on_worker_boot` 31 | # block. 32 | # 33 | # preload_app! 34 | 35 | # The code in the `on_worker_boot` will be called if you are using 36 | # clustered mode by specifying a number of `workers`. After each worker 37 | # process is booted this block will be run, if you are using `preload_app!` 38 | # option you will want to use this block to reconnect to any threads 39 | # or connections that may have been created at application boot, Ruby 40 | # cannot share connections between processes. 41 | # 42 | # on_worker_boot do 43 | # ActiveRecord::Base.establish_connection if defined?(ActiveRecord) 44 | # end 45 | 46 | # Allow puma to be restarted by `rails restart` command. 47 | plugin :tmp_restart 48 | -------------------------------------------------------------------------------- /spec/dummy/config/rails_amp.yml: -------------------------------------------------------------------------------- 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 | admin/users: index show 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 40 | -------------------------------------------------------------------------------- /spec/dummy/config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 3 | root 'home#index' 4 | 5 | get '/home/index', to: 'home#index' 6 | get '/home/help', to: 'home#help' 7 | get '/home/about', to: 'home#about' 8 | namespace :admin do 9 | resources :sessions 10 | resources :users, :only => [:index, :show] 11 | end 12 | resources :users, :only => [:index, :show] 13 | end 14 | -------------------------------------------------------------------------------- /spec/dummy/config/secrets.yml: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rails secret` to generate a secure secret key. 9 | 10 | # Make sure the secrets in this file are kept private 11 | # if you're sharing your code publicly. 12 | 13 | development: 14 | secret_key_base: 35372676bdd25cac1026ad001a118b67b3c99c32362ee7e5c58aa2946e92f500d5d8dbeb4744a89b8e956591cb723ea47e667f86160caaa24685c383359a8e5a 15 | 16 | test: 17 | secret_key_base: f117c9869d454579acfa1dbe21365ff60fb272ea3f6ff2637a47cc5095eabb589e91c3d6eb4b05515e33e851007ed733cd361ec01b460fedb9f0f41ad2599159 18 | 19 | # Do not keep production secrets in the repository, 20 | # instead read values from the environment. 21 | production: 22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23 | -------------------------------------------------------------------------------- /spec/dummy/config/spring.rb: -------------------------------------------------------------------------------- 1 | %w( 2 | .ruby-version 3 | .rbenv-vars 4 | tmp/restart.txt 5 | tmp/caching-dev.txt 6 | ).each { |path| Spring.watch(path) } 7 | -------------------------------------------------------------------------------- /spec/dummy/db/migrate/20170130074559_create_users.rb: -------------------------------------------------------------------------------- 1 | class CreateUsers < ActiveRecord::Migration 2 | def change 3 | create_table :users do |t| 4 | t.string :name 5 | t.string :email 6 | 7 | t.timestamps 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /spec/dummy/db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(version: 20170130074559) do 14 | 15 | create_table "users", force: :cascade do |t| 16 | t.string "name" 17 | t.string "email" 18 | t.datetime "created_at" 19 | t.datetime "updated_at" 20 | end 21 | 22 | end 23 | -------------------------------------------------------------------------------- /spec/dummy/lib/assets/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/lib/assets/.keep -------------------------------------------------------------------------------- /spec/dummy/log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/log/.keep -------------------------------------------------------------------------------- /spec/dummy/public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /spec/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /spec/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /spec/dummy/public/apple-touch-icon-precomposed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/public/apple-touch-icon-precomposed.png -------------------------------------------------------------------------------- /spec/dummy/public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/public/apple-touch-icon.png -------------------------------------------------------------------------------- /spec/dummy/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takafumir/rails_amp/8a4b6199fd86a75a41db37269f6ad4a9cd097399/spec/dummy/public/favicon.ico -------------------------------------------------------------------------------- /spec/rails_amp_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | 3 | describe RailsAmp do 4 | it 'returns config directory for test' do 5 | expect(config_dir).to match( %r(/support/config\z) ) 6 | end 7 | 8 | it 'converts correct controller and key' do 9 | expect(RailsAmp.controller_to_key(UsersController)).to eq 'users' 10 | expect(RailsAmp.controller_to_key(HomeController)).to eq 'home' 11 | expect(RailsAmp.key_to_controller('users')).to eq UsersController 12 | expect(RailsAmp.key_to_controller('home')).to eq HomeController 13 | expect(RailsAmp.key_to_controller('admin/users')).to eq Admin::UsersController 14 | end 15 | 16 | describe Config do 17 | it 'returns correct config default values' do 18 | expect(RailsAmp.config_file).to eq "#{Rails.root}/config/rails_amp.yml" 19 | expect(RailsAmp.default_format).to eq :amp 20 | expect(RailsAmp.targets).to eq("users"=>["index", "show"], "admin/users"=>["index", "show"]) 21 | expect(RailsAmp.analytics).to eq '' 22 | end 23 | 24 | specify 'RailsAmp.var equals to RailsAmp.config.var' do 25 | expect(RailsAmp.format).to eq RailsAmp.config.format 26 | expect(RailsAmp.config_file).to eq RailsAmp.config.config_file 27 | expect(RailsAmp.default_format).to eq RailsAmp.config.default_format 28 | expect(RailsAmp.targets).to eq RailsAmp.config.targets 29 | expect(RailsAmp.analytics).to eq RailsAmp.config.analytics 30 | end 31 | end 32 | 33 | context 'when using default /config/rails_amp.yml' do 34 | it 'returns correct config values' do 35 | expect(RailsAmp.targets).to eq("users"=>["index", "show"], "admin/users"=>["index", "show"]) 36 | expect(RailsAmp.target_actions(UsersController)).to eq ['index', 'show'] 37 | expect(RailsAmp.target_actions(HomeController)).to eq [] 38 | expect(RailsAmp.target?('users', 'index')).to eq true 39 | expect(RailsAmp.target?('users', 'show')).to eq true 40 | expect(RailsAmp.target?('home', 'index')).to eq false 41 | expect(RailsAmp.target?('home', 'help')).to eq false 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 45 | end 46 | end 47 | 48 | context 'when changing amp default format' do 49 | before(:each) do 50 | RailsAmp.config_file = "#{config_dir}/amp_format.yml" 51 | RailsAmp.reload_config! 52 | end 53 | 54 | after(:each) do 55 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 56 | RailsAmp.reload_config! 57 | end 58 | 59 | it 'returns correct config values' do 60 | expect(RailsAmp.config_file).to eq "#{config_dir}/amp_format.yml" 61 | expect(RailsAmp.default_format).to eq :mobile 62 | end 63 | end 64 | 65 | context 'when using amp on some controller actions' do 66 | before(:each) do 67 | RailsAmp.config_file = "#{config_dir}/controller_actions.yml" 68 | RailsAmp.reload_config! 69 | end 70 | 71 | after(:each) do 72 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 73 | RailsAmp.reload_config! 74 | end 75 | 76 | it 'returns correct config values' do 77 | expect(RailsAmp.config_file).to eq "#{config_dir}/controller_actions.yml" 78 | expect(RailsAmp.targets).to eq({ 'home' => ['about', 'help'], 'users' => ['show'] }) 79 | expect(RailsAmp.disable_all?).to eq false 80 | expect(RailsAmp.enable_all?).to eq false 81 | expect(RailsAmp.controller_all?('home')).to eq false 82 | expect(RailsAmp.controller_all?('users')).to eq false 83 | expect(RailsAmp.controller_actions?('home')).to eq true 84 | expect(RailsAmp.controller_actions?('users')).to eq true 85 | expect(RailsAmp.target_actions(UsersController)).to eq ['show'] 86 | expect(RailsAmp.target_actions(HomeController)).to eq ['about', 'help'] 87 | expect(RailsAmp.target?('users', 'index')).to eq false 88 | expect(RailsAmp.target?('users', 'show')).to eq true 89 | expect(RailsAmp.target?('home', 'index')).to eq false 90 | expect(RailsAmp.target?('home', 'help')).to eq true 91 | expect(RailsAmp.target?('home', 'about')).to eq true 92 | end 93 | end 94 | 95 | context 'when using amp on controller all actions' do 96 | before(:each) do 97 | RailsAmp.config_file = "#{config_dir}/controller_all.yml" 98 | RailsAmp.reload_config! 99 | end 100 | 101 | after(:each) do 102 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 103 | RailsAmp.reload_config! 104 | end 105 | 106 | it 'returns correct config values' do 107 | expect(RailsAmp.config_file).to eq "#{config_dir}/controller_all.yml" 108 | expect(RailsAmp.targets).to eq({ 'home' => [] }) 109 | expect(RailsAmp.disable_all?).to eq false 110 | expect(RailsAmp.enable_all?).to eq false 111 | expect(RailsAmp.controller_all?('home')).to eq true 112 | expect(RailsAmp.controller_actions?('home')).to eq false 113 | expect(RailsAmp.target_actions(UsersController)).to eq [] 114 | expect(RailsAmp.target_actions(HomeController)).to eq ['index', 'help', 'about'] 115 | expect(RailsAmp.target?('users', 'index')).to eq false 116 | expect(RailsAmp.target?('users', 'show')).to eq false 117 | expect(RailsAmp.target?('home', 'index')).to eq true 118 | expect(RailsAmp.target?('home', 'help')).to eq true 119 | expect(RailsAmp.target?('home', 'about')).to eq true 120 | end 121 | end 122 | 123 | context 'when not using amp on all controllers actions' do 124 | before(:each) do 125 | RailsAmp.config_file = "#{config_dir}/disable_all.yml" 126 | RailsAmp.reload_config! 127 | end 128 | 129 | after(:each) do 130 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 131 | RailsAmp.reload_config! 132 | end 133 | 134 | it 'returns correct config values' do 135 | expect(RailsAmp.config_file).to eq "#{config_dir}/disable_all.yml" 136 | expect(RailsAmp.targets).to eq({}) 137 | expect(RailsAmp.disable_all?).to eq true 138 | expect(RailsAmp.enable_all?).to eq false 139 | expect(RailsAmp.target_actions(UsersController)).to eq [] 140 | expect(RailsAmp.target_actions(HomeController)).to eq [] 141 | expect(RailsAmp.target?('users', 'index')).to eq false 142 | expect(RailsAmp.target?('users', 'show')).to eq false 143 | expect(RailsAmp.target?('home', 'index')).to eq false 144 | expect(RailsAmp.target?('home', 'help')).to eq false 145 | expect(RailsAmp.target?('home', 'about')).to eq false 146 | end 147 | end 148 | 149 | context 'when using amp on all controllers actions' do 150 | before(:each) do 151 | RailsAmp.config_file = "#{config_dir}/enable_all.yml" 152 | RailsAmp.reload_config! 153 | end 154 | 155 | after(:each) do 156 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 157 | RailsAmp.reload_config! 158 | end 159 | 160 | it 'returns correct config values' do 161 | expect(RailsAmp.config_file).to eq "#{config_dir}/enable_all.yml" 162 | expect(RailsAmp.targets).to eq({ 'application' => 'all' }) 163 | expect(RailsAmp.disable_all?).to eq false 164 | expect(RailsAmp.enable_all?).to eq true 165 | expect(RailsAmp.target_actions(UsersController)).to eq ['index', 'show'] 166 | expect(RailsAmp.target_actions(HomeController)).to eq ['index', 'help', 'about'] 167 | expect(RailsAmp.target?('users', 'index')).to eq true 168 | expect(RailsAmp.target?('users', 'show')).to eq true 169 | expect(RailsAmp.target?('home', 'index')).to eq true 170 | expect(RailsAmp.target?('home', 'help')).to eq true 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 174 | end 175 | end 176 | 177 | context 'when using google analytics' do 178 | before(:each) do 179 | RailsAmp.config_file = "#{config_dir}/enable_analytics.yml" 180 | RailsAmp.reload_config! 181 | end 182 | 183 | after(:each) do 184 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 185 | RailsAmp.reload_config! 186 | end 187 | 188 | it 'returns correct config values' do 189 | expect(RailsAmp.config_file).to eq "#{config_dir}/enable_analytics.yml" 190 | expect(RailsAmp.analytics).to eq 'UA-12345-6' 191 | end 192 | end 193 | 194 | context 'when using various configs' do 195 | before(:each) do 196 | RailsAmp.config_file = "#{config_dir}/various.yml" 197 | RailsAmp.reload_config! 198 | end 199 | 200 | after(:each) do 201 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 202 | RailsAmp.reload_config! 203 | end 204 | 205 | it 'returns correct config values' do 206 | expect(RailsAmp.config_file).to eq "#{config_dir}/various.yml" 207 | expect(RailsAmp.default_format).to eq :amphtml 208 | expect(RailsAmp.targets).to eq({ 'home' => ['index', 'about'], 'users' => ['index'] }) 209 | expect(RailsAmp.analytics).to eq 'UA-12345-6' 210 | expect(RailsAmp.disable_all?).to eq false 211 | expect(RailsAmp.enable_all?).to eq false 212 | expect(RailsAmp.controller_all?('home')).to eq false 213 | expect(RailsAmp.controller_all?('users')).to eq false 214 | expect(RailsAmp.controller_actions?('home')).to eq true 215 | expect(RailsAmp.controller_actions?('users')).to eq true 216 | expect(RailsAmp.target_actions(UsersController)).to eq ['index'] 217 | expect(RailsAmp.target_actions(HomeController)).to eq ['index', 'about'] 218 | expect(RailsAmp.target?('users', 'index')).to eq true 219 | expect(RailsAmp.target?('users', 'show')).to eq false 220 | expect(RailsAmp.target?('home', 'index')).to eq true 221 | expect(RailsAmp.target?('home', 'help')).to eq false 222 | expect(RailsAmp.target?('home', 'about')).to eq true 223 | expect(RailsAmp.amp_format?).to eq false 224 | end 225 | 226 | context 'with amp render' do 227 | before(:each) do 228 | RailsAmp.format = RailsAmp.default_format.to_s 229 | end 230 | 231 | after(:each) do 232 | RailsAmp.format = '' 233 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 234 | RailsAmp.reload_config! 235 | end 236 | 237 | it 'should be rendrabale with amp' do 238 | expect(RailsAmp.amp_format?).to eq true 239 | expect(RailsAmp.amp_renderable?('users', 'index')).to eq true 240 | expect(RailsAmp.amp_renderable?('users', 'show')).to eq false 241 | expect(RailsAmp.amp_renderable?('home', 'index')).to eq true 242 | expect(RailsAmp.amp_renderable?('home', 'help')).to eq false 243 | expect(RailsAmp.amp_renderable?('home', 'about')).to eq true 244 | end 245 | end 246 | end 247 | end 248 | -------------------------------------------------------------------------------- /spec/rails_helper.rb: -------------------------------------------------------------------------------- 1 | # This file is copied to spec/ when you run 'rails generate rspec:install' 2 | ENV['RAILS_ENV'] ||= 'test' 3 | require File.expand_path('../dummy/config/environment', __FILE__) 4 | # Prevent database truncation if the environment is production 5 | abort("The Rails environment is running in production mode!") if Rails.env.production? 6 | require 'spec_helper' 7 | require 'rspec/rails' 8 | # Add additional requires below this line. Rails is not loaded until this point! 9 | require File.expand_path('../utilities', __FILE__) 10 | require 'rails_amp/view_helpers/action_view' 11 | require 'rails_amp/view_helpers/image_tag_helper' 12 | 13 | # Requires supporting ruby files with custom matchers and macros, etc, in 14 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 15 | # run as spec files by default. This means that files in spec/support that end 16 | # in _spec.rb will both be required and run as specs, causing the specs to be 17 | # run twice. It is recommended that you do not name files matching this glob to 18 | # end with _spec.rb. You can configure this pattern with the --pattern 19 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 20 | # 21 | # The following line is provided for convenience purposes. It has the downside 22 | # of increasing the boot-up time by auto-requiring all files in the support 23 | # directory. Alternatively, in the individual `*_spec.rb` files, manually 24 | # require only the support files necessary. 25 | # 26 | # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 27 | 28 | # Checks for pending migration and applies them before tests are run. 29 | # If you are not using ActiveRecord, you can remove this line. 30 | ActiveRecord::Migration.maintain_test_schema! 31 | 32 | RSpec.configure do |config| 33 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 34 | config.fixture_path = "#{::Rails.root}/spec/fixtures" 35 | 36 | # If you're not using ActiveRecord, or you'd prefer not to run each of your 37 | # examples within a transaction, remove the following line or assign false 38 | # instead of true. 39 | config.use_transactional_fixtures = true 40 | 41 | # RSpec Rails can automatically mix in different behaviours to your tests 42 | # based on their file location, for example enabling you to call `get` and 43 | # `post` in specs under `spec/controllers`. 44 | # 45 | # You can disable this behaviour by removing the line below, and instead 46 | # explicitly tag your specs with their type, e.g.: 47 | # 48 | # RSpec.describe UsersController, :type => :controller do 49 | # # ... 50 | # end 51 | # 52 | # The different available types are documented in the features, such as in 53 | # https://relishapp.com/rspec/rspec-rails/docs 54 | config.infer_spec_type_from_file_location! 55 | 56 | # Filter lines from Rails gems in backtraces. 57 | config.filter_rails_from_backtrace! 58 | # arbitrary gems may also be filtered via: 59 | # config.filter_gems_from_backtrace("gem name") 60 | 61 | # Include modules 62 | config.include Utilities 63 | config.include ActionView::Helpers::AssetTagHelper 64 | config.include RailsAmp::ViewHelpers::ImageTagHelper 65 | config.include RailsAmp::ViewHelpers::ActionView 66 | end 67 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rails generate rspec:install` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # The `.rspec` file also contains a few flags that are not defaults but that 16 | # users commonly want. 17 | # 18 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 19 | RSpec.configure do |config| 20 | # rspec-expectations config goes here. You can use an alternate 21 | # assertion/expectation library such as wrong or the stdlib/minitest 22 | # assertions if you prefer. 23 | config.expect_with :rspec do |expectations| 24 | # This option will default to `true` in RSpec 4. It makes the `description` 25 | # and `failure_message` of custom matchers include text for helper methods 26 | # defined using `chain`, e.g.: 27 | # be_bigger_than(2).and_smaller_than(4).description 28 | # # => "be bigger than 2 and smaller than 4" 29 | # ...rather than: 30 | # # => "be bigger than 2" 31 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 32 | end 33 | 34 | # rspec-mocks config goes here. You can use an alternate test double 35 | # library (such as bogus or mocha) by changing the `mock_with` option here. 36 | config.mock_with :rspec do |mocks| 37 | # Prevents you from mocking or stubbing a method that does not exist on 38 | # a real object. This is generally recommended, and will default to 39 | # `true` in RSpec 4. 40 | mocks.verify_partial_doubles = true 41 | end 42 | 43 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 44 | # have no way to turn it off -- the option exists only for backwards 45 | # compatibility in RSpec 3). It causes shared context metadata to be 46 | # inherited by the metadata hash of host groups and examples, rather than 47 | # triggering implicit auto-inclusion in groups with matching metadata. 48 | config.shared_context_metadata_behavior = :apply_to_host_groups 49 | 50 | # The settings below are suggested to provide a good initial experience 51 | # with RSpec, but feel free to customize to your heart's content. 52 | =begin 53 | # This allows you to limit a spec run to individual examples or groups 54 | # you care about by tagging them with `:focus` metadata. When nothing 55 | # is tagged with `:focus`, all examples get run. RSpec also provides 56 | # aliases for `it`, `describe`, and `context` that include `:focus` 57 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 58 | config.filter_run_when_matching :focus 59 | 60 | # Allows RSpec to persist some state between runs in order to support 61 | # the `--only-failures` and `--next-failure` CLI options. We recommend 62 | # you configure your source control system to ignore this file. 63 | config.example_status_persistence_file_path = "spec/examples.txt" 64 | 65 | # Limits the available syntax to the non-monkey patched syntax that is 66 | # recommended. For more details, see: 67 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 68 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 69 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 70 | config.disable_monkey_patching! 71 | 72 | # Many RSpec users commonly either run the entire suite or an individual 73 | # file, and it's useful to allow more verbose output when running an 74 | # individual spec file. 75 | if config.files_to_run.one? 76 | # Use the documentation formatter for detailed output, 77 | # unless a formatter has already been configured 78 | # (e.g. via a command-line flag). 79 | config.default_formatter = 'doc' 80 | end 81 | 82 | # Print the 10 slowest examples and example groups at the 83 | # end of the spec run, to help surface which specs are running 84 | # particularly slow. 85 | config.profile_examples = 10 86 | 87 | # Run specs in random order to surface order dependencies. If you find an 88 | # order dependency and want to debug it, you can fix the order by providing 89 | # the seed, which is printed after each run. 90 | # --seed 1234 91 | config.order = :random 92 | 93 | # Seed global randomization in this process using the `--seed` CLI option. 94 | # Setting this allows you to use `--seed` to deterministically reproduce 95 | # test failures related to randomization by passing the same `--seed` value 96 | # as the one that triggered the failure. 97 | Kernel.srand config.seed 98 | =end 99 | end 100 | -------------------------------------------------------------------------------- /spec/support/config/amp_format.yml: -------------------------------------------------------------------------------- 1 | # ### Change default amp format. The default value is amp. 2 | # ### If you want to use 'mobile' as amp format, set 'mobile' to default_format. 3 | # ### And you can access to amp page like /users/index.mobile 4 | default_format: mobile 5 | -------------------------------------------------------------------------------- /spec/support/config/controller_actions.yml: -------------------------------------------------------------------------------- 1 | # ### Enable amp on home#about, home#help, users#show 2 | # ### controller: action1 action2 action3 ... 3 | targets: 4 | home: about help 5 | users: show 6 | -------------------------------------------------------------------------------- /spec/support/config/controller_all.yml: -------------------------------------------------------------------------------- 1 | # ### Enable amp on home and users all actions. 2 | targets: 3 | home: 4 | -------------------------------------------------------------------------------- /spec/support/config/disable_all.yml: -------------------------------------------------------------------------------- 1 | # ### Disable amp completely. 2 | targets: 3 | -------------------------------------------------------------------------------- /spec/support/config/enable_all.yml: -------------------------------------------------------------------------------- 1 | # ### Enable amp on all controllers and actions. 2 | targets: 3 | application: all 4 | -------------------------------------------------------------------------------- /spec/support/config/enable_analytics.yml: -------------------------------------------------------------------------------- 1 | # ### Enable Google Analytics page tracking. Set your Google Analytics Account. 2 | analytics: UA-12345-6 3 | -------------------------------------------------------------------------------- /spec/support/config/various.yml: -------------------------------------------------------------------------------- 1 | # ### Enable amp on home#index, home#about, users#index 2 | targets: 3 | home: index about 4 | users: index 5 | 6 | # ### Enable Google Analytics page tracking. Set your Google Analytics Account. 7 | analytics: UA-12345-6 8 | 9 | # ### Change default amp format. The default value is amp. 10 | # ### If you want to use 'amphtml' as amp format, set 'amphtml' to default_format. 11 | # ### And you can access to amp page like /users/index.amphtml 12 | default_format: amphtml 13 | -------------------------------------------------------------------------------- /spec/utilities.rb: -------------------------------------------------------------------------------- 1 | module Utilities 2 | def config_dir 3 | File.dirname(__FILE__) + '/support/config' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/view_helpers/action_view_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | 3 | # Specs for view helpers: 4 | # rails_amp_html_header, rails_amp_google_analytics_head, rails_amp_google_analytics_page_tracking 5 | # These helpers don't use controller or request object. 6 | describe RailsAmp::ViewHelpers::ActionView do 7 | context '#rails_amp_html_header' do 8 | it 'returns correct amp header' do 9 | expect(rails_amp_html_header).to include('style amp-boilerplate') 10 | expect(rails_amp_html_header).to include('script async src="https://cdn.ampproject.org') 11 | end 12 | end 13 | 14 | context '#rails_amp_google_analytics disabled' do 15 | it 'returns analytics off' do 16 | expect(RailsAmp.analytics).to eq '' 17 | expect(rails_amp_google_analytics_head).to eq '' 18 | expect(rails_amp_google_analytics_page_tracking).to eq '' 19 | end 20 | end 21 | 22 | context '#rails_amp_google_analytics enabled' do 23 | before(:each) do 24 | RailsAmp.analytics = 'UA-12345-6' 25 | end 26 | 27 | after(:each) do 28 | RailsAmp.config_file = "#{Rails.root}/config/rails_amp.yml" 29 | RailsAmp.reload_config! 30 | end 31 | 32 | it 'returns analytics on' do 33 | expect(RailsAmp.analytics).to eq 'UA-12345-6' 34 | expect(rails_amp_google_analytics_head).to include('script async custom-element="amp-analytics"') 35 | expect(rails_amp_google_analytics_page_tracking).to include('"account": "UA-12345-6"') 36 | end 37 | end 38 | 39 | end 40 | --------------------------------------------------------------------------------