Alpineでパッケージをインストールしようとしたらエラーがでたので解消する

メモ

ERROR: unsatisfiable constraints:
  py-pip (missing):
    required by: world[py-pip]
ERROR: Service 'web' failed to build: The command '/bin/sh -c apk add py-pip' returned a non-zero code: 1

インストール可能なパッケージ一覧(インデックス)の更新オプションを付けると解消される (--update)

apk add --update py-pip

Macでファイル内の文字列を再帰的に置換するコマンド

カレントディレクトリ以下すべてのファイル内のHOGEという文字列をFUGAに置換する

find ./ -type f | xargs sed -i '' 's/HOGE/FUGA/g'
  • xargs … 標準入力から受け取ったデータを、任意のコマンドに引数として与えるコマンド

  • sed … StreamEditor. 入力ストリームに対する文字列の置換を行う。Linux のものとは微妙に動作が異なるらしい

参考: Macでsedコマンドが思うように動かなくてハマった

Warning: Material-UI: The white color was not parsed correctly, because it has an unsupported format (color name or RGB %). This may cause issues in component rendering.

const muiTheme = getMuiTheme({
  raisedButton: {
    textColor: 'white',
    color: purple900,
  }
});

こんな感じにMaterial-UIの色を変えて遊んでたらエラーが出た:

Warning: Material-UI: The white color was not parsed correctly,
  because it has an unsupported format (color name or RGB %). This may cause issues in component rendering.

ヘックスコードに変えれば問題はない:

const muiTheme = getMuiTheme({
  raisedButton: {
    textColor: '#ffffff',
    color: purple900,
  }
});

参考: https://github.com/callemall/material-ui/issues/6781

Rails5 FactoryGirlでコールバックをスキップする方法

Rails4では使えたFoo.skip_callback(:create, :after, :bar)がRails5では使えない。

ArgumentError:
       After create callback :do_something has not been defined

対策として、次のようにFGにおいてメソッドを上書きする:

class User < ActiveRecord::Base
  after_create :run_something
end
FactoryGirl.define do
  factory :user do

    after(:build) do |user|
      class << user
       def run_something
         true
       end
      end
    end

    factory :user_with_run_something do
      after(:build) do |user|
        class << user
          def run_something
            super
          end
        end
      end
    end
  end
end

コールバックメソッドの発火タイミングによって、after(:build)も変更する必要がある

参考:

http://stackoverflow.com/questions/8751175/skip-callbacks-on-factory-girl-and-rspec/22916595#22916595

https://github.com/thoughtbot/factory_girl/issues/931

first_nameカラム、last_nameカラムからフルネーム検索するメソッド

Controller:

class ProfilesController < ApplicationController

  def search
    @profiles = Profile.search_by_full_name(search_params[:name])
  end

  private

  def search_params
    params.require(:search_profile).permit(:name)
  end
end

Model:

class Profile < ApplicationRecord

  def self.search_by_full_name(query)
    query.delete!(' ')
    where("CONCAT_WS('', first_name, last_name) LIKE ?", "%#{query}%")
  end
end

いろいろ応用できそう

Webpack v1からv2へのマイグレーションメモ

自分が使っていたところだけを簡単に。詳しくはhttps://webpack.js.org/guides/migrating

以下、webpack.config.jsの変更点:

resolve.extenstions

空の文字列は必要なくなったので削除する。ちなみにデフォルトでは.js.jsonがresolveされている。

// v1
resolve: {
  extensions: ["", ".js", ".json"],
},
// v2
resolve: {
  extensions: [".js", ".json"],
},

拡張子を指定することで./foojsファイルならばrequire("./foo")が可能になる。

module.loadersmodule.rules

module.loaderシンタックスはまだサポートされているが、module.rulesにかえるほうが望ましい。

// v1
module: {
  loaders: [
    {
      test: /\.css$/,
      loader: [
        "style-loader", 
        "css-loader?modules=true" 
      ]
    }
  ]
}
// v2
module: {
  rules: [
    {
      test: /\.css$/,
      use: [
        {
          "style-loader"
        },
        {
          "css-loader",
          options: {
            module: true
          }
        }
      ]
    }
  ]
}

loaderのチェイン

複数のloaderを!でつなげるのはmodule.loadersオプションでしか使えない。

// v1
module: {
  loaders: [
    {
      test: /\.css$/,
      loader: "style-loader!css-loader"
    }
  ]
}
// v2
module: {
  rules: [
    {
      test: /\.css$/,
      use: [
        "style-loader",
        "css-loader"
      ]
    }
  ]
}

-loaderの省略はできなくなった

loader指定時に、-loaderを省略することはできなくなった。

// ×
module: {
  rules: [
    {
      use: [
        "style",
        "css",
        "less"
      ]
    }
  ]
}
// ○
module: {
  rules: [
    {
      use: [
        "style-loader",
        "css-loader",
        "less-loader"
      ]
    }
  ]
}

json-loaderを指定する必要はなくなった

json-loaderがなくとも、自動的にJSONファイルを読み込む。

// ×
module: {
  rules: [
    {
      test: /\.json/,
      loader: "json-loader"
    }
  ]
}
// ○
module: {
  rules: [
  ]
}

module.preLoadersmodule.postLoadersは削除された

// v1
module: {
  preLoaders: [
    {
      test: /\.js$/,
      loader: "eslint-loader"
    }
  ]
}
// v2
module: {
  rules: [
    {
      test: /\.js$/,
      enforce: "pre",
      loader: "eslint-loader"
    }
  ]
}

UglifyJsPlugin sourceMap

UglifyJsPluginsourceMapオプションはデフォルトでfalseになった。sourceMapが欲しい場合にはtrue設定する必要がある。

devtool: "source-map",
plugins: [
  new UglifyJsPlugin({
    sourceMap: true
  })
]

UglifyJsPlugin warnings

UglifyJsPlugincompress.warningsオプションはデフォルトでfalseになった。warningsが欲しい場合にはtrue設定する必要がある。

devtool: "source-map",
plugins: [
  new UglifyJsPlugin({
    compress: {
      warnings: true
    }
  })
]

UglifyJsPlugin minimize loaders

UglifyJsPluginはloadersをminimizeモードに切り替えなくなった。

loadersのminimizeモードは webpack 3 以降に削除される。

古いloadersとの互換性を保つために、pluginを用いてminimizeモードに切り替えることができる。

plugins: [
  new webpack.LoaderOptionsPlugin({
    minimize: true
  })
]

DedupePluginは削除された

webpack.optimize.DedupePluginはもう必要ないのでwebpackの設定から削除する。

OccurrenceOrderPluginはデフォルトで設定される

もうわざわざ指定する必要はない。

debug

webpack 1 では、debugオプションはloadersをdebugモードに切り替える。

loadersのdebugモードは webpack 3 以降に削除される。

古いloadersとの互換性を保つために、pluginを用いてdebugモードに切り替える。

plugins: [
  new webpack.LoaderOptionsPlugin({
    debug: true
  })
]