ウェブサービスを作っています。

Webpacker4 で Vue と Pug を扱う

webpacker v4.0.2 で確認しています。

Webpacker4 + Vue.js の単一ファイルコンポーネント (SFC) で、

<template lang="pug">
  .hoge
</template>

のような Pug テンプレートを扱う方法です。


yarn add pug pug-plain-loader

config/webpack/loaders/pug.js

module.exports = {
  test: /\.pug$/,
  use: [{
    loader: 'pug-plain-loader'
  }]
}

config/webpack/environment.js

const { environment } = require('@rails/webpacker')
const pug = require('./loaders/pug')

environment.loaders.prepend('pug', pug)

参考

IE10以下で HTMLElement.dataset を使う

JavaScript で HTML 要素の data- 属性 を取得する際、モダンブラウザでは elem.dataset.xxx のように取得できます。

HTMLElement.dataset - Web API | MDN

ただ、この機能は IE10 以下で使うことができません。

element-dataset という npm モジュールを使うと、IE10 以下でも使えるようになります。


Webpacker を使用している前提です。

yarn add element-dataset

Webpack のエントリファイルなどに、以下を追加します。

import elementDatasetPolyfill from 'element-dataset'

elementDatasetPolyfill()

VCR で、タイムスタンプなどの動的パラメータを無視する

VCR 4.0.0 + RSpec 3.7.0 で確認しています。


spec/support/vcr.rb

VCR.configure do |c|
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
end

spec

vcr_options = {
  cassette_name: '...',
  match_requests_on: [:method, VCR.request_matchers.uri_without_params(:signature, :timestamp)]
}
it 'this is test...', vcr: vcr_options do
  ...
end

参考

Rails で本番環境の deprecation warning を独自ログに出力する

Rails 5.0.6 で確認しています。

もっと良い方法がある気もするので、ご存知でしたら教えていただけると幸いです。


config/environments/production.rb (デフォルトで :notify になっていると思います)

config.active_support.deprecation = :notify

config/initializers/deprecation.rb

logger = Logger.new(Rails.root.join('log/deprecation.log'))

ActiveSupport::Notifications.subscribe('deprecation.rails') do |*args|
  data = args.extract_options!
  logger.info data[:message]
  logger.info data[:callstack] * "\n"
end

参考

CircleCI 2.0 + circleci-bundle-update-pr で、自動 bundle update

昨日の続きで、CircleCI 2.0 を使い、毎日 bundle update を自動実行する設定例です。

circleci-bundle-update-pr という便利なツールを使用させてもらいます。

circleci-bundle-update-pr の README にも設定例は掲載されていますが、別の例ということで。

2017/12/01 現在の情報となります。

以下の例では、平日の日本時間午前 9:00 (UTC 0:00) に、ジョブを実行します。


.circleci/config.yml

ruby_image: &ruby_image
  circleci/ruby:2.4.2

bundle_cache_key: &bundle_cache_key
  bundle-{{ checksum "Gemfile.lock" }}

version: 2

jobs:
  build: (省略)

  ci-bundle-update:
    docker:
      - image: *ruby_image

    environment:
      TZ: "/usr/share/zoneinfo/Asia/Tokyo"

    steps:
      - checkout

      - restore_cache:
          key: *bundle_cache_key

      - run:
          name: circleci-bundle-update-pr
          command: |
            gem update bundler --no-document
            gem install circleci-bundle-update-pr
            bundle config --local path vendor/bundle
            circleci-bundle-update-pr

workflows:
  version: 2

  ci-bundle-update:
    triggers:
      - schedule:
          cron: "0 0 * * 1-5"  # UTC
          filters:
            branches:
              only: master

    jobs:
      - ci-bundle-update

あとは CircleCI のプロジェクト設定で、環境変数 GITHUB_ACCESS_TOKEN, GIT_USER_EMAIL, GIT_USER_NAME を設定します。

CircleCI 2.0 + Capistrano で自動デプロイ

CircleCI 2.0 で、Rails アプリを自動デプロイする設定例です。

ミドルウェアとして、MySQL と Elasticsearch を使用しています。

2017/11/30 現在の情報となります。


.circleci/config.yml (インデントが 4 スペースの場合があるので注意)

ruby_image: &ruby_image
  circleci/ruby:2.4.2

bundle_cache_key: &bundle_cache_key
  bundle-{{ checksum "Gemfile.lock" }}

version: 2

jobs:
  build:
    docker:
      - image: *ruby_image
        environment:
          DATABASE_URL: "mysql2://root@127.0.0.1/circle_test"
          RAILS_ENV: test
      - image: circleci/mysql:5.7
      - image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
        environment:
          xpack.security.enabled: false

    steps:
      - checkout

      - restore_cache:
          key: *bundle_cache_key

      - run:
          name: Bundle Install
          command: bundle install --jobs=4 --retry=3 --path vendor/bundle

      - save_cache:
          key: *bundle_cache_key
          paths:
            - vendor/bundle

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:3306 -timeout 1m

      - run:
          name: Wait for Elasticsearch
          command: dockerize -wait tcp://localhost:9200 -timeout 1m

      - run: bin/rails db:schema:load --trace

      - run: bundle exec rspec --format progress

  deploy:
    docker:
      - image: *ruby_image

    steps:
      - checkout

      - restore_cache:
          key: *bundle_cache_key

      - run:
          name: Deploy
          command: |
            bundle config --local path vendor/bundle
            .circleci/deploy.sh

workflows:
  version: 2

  build-and-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

.circleci/deploy.sh (実行権限をつける)

#!/bin/bash

set -ex

cat <<EOF >> $HOME/.ssh/config
Host xxx
  HostName xxx.xxx.xxx.xxx
  User xxx
  ...
EOF

bundle exec cap production deploy

あとは CircleCI のプロジェクト設定で、SSH 鍵の設定を行います。