ECのウェブ担当者のメモ

ECサイトを運営管理している、WEB担当プログラマのメモ

スポンサーリンク

DEPRECATION WARNING cleanup! is deprecated and will be removed from Rails 5.1

f:id:jun9632:20170222111224p:plain

Rails5でdelayed_jobを起動したら、以下のようなWARNINGが出力されました。

DEPRECATION WARNING: cleanup! is deprecated and will be removed from Rails 5.1 (use Rails.application.reloader.reload! instead of cleanup + prepare) (called from <top (required)> at bin/delayed_job:5

気持ちが悪いのGoogle大先生に聞いてみたところ下記がヒットしました。

github.com

請負になりますが、

initializers配下にdj_rails5_patches.rbを下記のように作りました。

config/initializers/dj_rails5_patches.rb

module DelayedWorkerPatches
  def reload!
    return unless self.class.reload_app?
    if defined?(ActiveSupport::Reloader)
      Rails.application.reloader.reload!
    else
      ActionDispatch::Reloader.cleanup!
      ActionDispatch::Reloader.prepare!
    end
  end
end

module Delayed
  class Worker
    prepend DelayedWorkerPatches
  end
end

これで、ワーニングは出力されないようになりました。 ありがうございます。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門

autocomplete off 対策には、autocomplete new-password

f:id:jun9632:20170210110532p:plain

クロームなんかで、パスワードフィールドに勝手に入力してくるのやめて欲しいなと思って

autocomplete='off'

を設定しても、効かないじゃん!って思ったことありませんか?

そんなときは、new-passwordです。

autocomplete='new-password'

そうすると、空で表示してくれるようです。 ただ、まだ全部のブラザには対応していないんじゃないかという噂もあるの注意して使ってください。

slimのフォームのサンプルは以下です。

     .col-xs-12
        .form-group
          = f.label :email
          = f.text_field :email, class: 'form-control'
      .col-xs-12
        .form-group
          = f.label :password
          = f.password_field :password, autocomplete: 'new-password', class: 'form-control'

関連記事

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門

RailsのAction Mailerを使ってSendGridのカテゴリーを設定する

f:id:jun9632:20170209185254p:plain

RailsのAction Mailerを使ってSendGridのカテゴリーを設定する方法です。

まずSendGridはこちら

sendgrid.kke.co.jp

SendGridは、主にメール配信のサービスになります。 最近使いはじめて、最高に便利なサービスです。

無料の利用枠もあるので、興味のある方は是非使ってみてください。

で、本題に入ります。

SendGridでは、メール毎にカテゴリーを設定して、配信することが出来て、 どのカテゴリーのメールが、届いたとか、開かれたとか、クリックされたとか カテゴリ毎にグルーピングしながら、解析するこができる機能があるんです。

でで、RailsのAction Mailerを使ってカテゴリーを設定するには、

以下の様に、メールのヘッダに SMTPAPIパラメータにcateoryを設定する必要があります。

def signup_mail

    xsmtp_api_params = { category: ['signup'] }
    headers['X-SMTPAPI'] = JSON.pretty_generate(xsmtp_api_params)

    opt = {}
    opt[:to] = 'aaaaaa@mail.com'
    opt[:subject] = '件名aaaaaa'
    mail(opt)
end

こんな感じで、設定すると、このメールにsignupカテゴリーが設定されます。

SMTPAPIの説明についてはSendGridのブログを参考にしてください。

sendgrid.kke.co.jp

関連記事

marketing-web.hatenablog.com

Railsレシピブック 183の技

Railsレシピブック 183の技

参考記事

本当にありがとうございます。

qiita.com

(1点 header[‘X-SMTPAPI’] => headers[‘X-SMTPAPI’] が正解だと思われます。)

ActionView::MissingTemplateの対策

f:id:jun9632:20170208181346p:plain

ActionView::MissingTemplate

そうです。 Railsでviewでテンプレートが無いときに発生します。

基本的には、テンプレートを準備しなかったミスだとは思うんですが、

動的に部分テンプレートを見つけて描画したりするときなどは、 もしテンプレートがあれば、その部分テンプレートを描画。 テンプレートがなければ、何もしないでスキップするなんて、 処理をやりたいことがあると思います。

そんな時に便利なのが、

lookup_context.exists?

です。

使い方は

- if lookup_context.exists?(@shop_code, "shop", true)
    = render("shop/#{@shop_code}")

仮に、下記のような、テンプレートファイルがあったときに

views/shop/_shop_123.html.slim

@shop_codeが以下のような、テンプレートファイル名と一致すれば、そのテンプレートを描画してくれます。

@shop_code = 'shop_123'

下記のようにマッチしなければ、そのままスキップ

@shop_code = 'shop_456'

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門

Awesome Nested Setのチートシート

f:id:jun9632:20170130164755p:plain

こちらRailsのカテゴリを階層で管理してくれるGem Awesome Nested Setです

github.com

そのチートシートです。

github.com

主に使いそうなところ

これで一気にループ回せます。

Category.each_with_level(Category.root.self_and_descendants) do |category, level|
  ...
end

使いそうなメソッド一覧

my_cat.root                  root for this node
my_cat.level                 the level of this object in the tree (e.g. root = 0)
my_cat.parent                the node's immediate parent
my_cat.children              array of immediate children (just those in the next level)
my_cat.ancestors             array of all parents, parents' parents, etc, excluding self
my_cat.self_and_ancestors    array of all parents, parents' parents, etc, including self
my_cat.siblings              array of brothers and sisters (all at that level), excluding self
my_cat.self_and_siblings     array of brothers and sisters (all at that level), including self
my_cat.descendants           array of all children, children's children, etc., excluding self
my_cat.self_and_descendants  array of all children, children's children, etc., including self
my_cat.leaves                array of all descendants that have no children
my_cat.root?                         true if this is a root node
my_cat.child?                        true if this is a child node (i.e. it has a parent)
my_cat.is_ancestor_of?(obj)          true if nested by any obj
my_cat.is_or_is_ancestor_of?(obj)    true if nested by any obj or self is obj
my_cat.is_descendant_of?(obj)        true if self is nested under obj
my_cat.is_or_is_descendant_of?(obj)  true if self is nested under obj or self is obj
my_cat.leaf?                         true if this is a leaf node (i.e. it has no children)

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門

GitHubでForkしたリポジトリをmargeする

f:id:jun9632:20170112104812p:plain

下記の様に、cloneして作ったリポジトリに対して、親のmyapp.gitをmargeする方法です。

$ git clone git@bitbucket.org:xxxxxx/myapp.git

org_originという名前で、親のリポジトリを参照できるようにします。

$ git remote add org_origin git@bitbucket.org:xxxxxx/myapp.git

後は, fetchとmergeすれば完了です

$ git fetch org_origin
$ git merge org_origin/master

関連記事

marketing-web.hatenablog.com

参考記事

ありがとうございました

http://qiita.com/xtetsuji/items/555a1ef19ed21ee42873

RubyでStringを配列に変換する

f:id:jun9632:20161201164715p:plain

rubyでStringを配列に変換する方法です。

使う関数は

split(pattern)

です。

たとえば

080-1234-5678

tel = '080-1234-5678'
tel.split('-')

すると

["080", "1234", "5678"]

という感じに配列に変換してくれます。

配列に分割出来なかったら

例えば、

“00-00"を'-‘で配列にすると

"00-00".split('-')

以下のような結果になりますが

["00", "00"]

もし、"00-00"を'=‘で分割しようとすると

"00-00".split('=')

結果は、以下のように、その文字列自体を配列に入れて返してくれます

["00-00"]

正規表現を使って分割

これまで、文字列を使って分割してきましたが、正規表現を使って分割することもできます。

’32145617741345’なんて文字列を、'1'で分割するときに

'32145617741345'.split('1')

と書いてもいいですが、

'32145617741345'.split(/1/)

としても、以下のような結果が取得できます。

["32", "456", "774", "345"]

今回の例だと単純過ぎるので、そんな使い方はあまりしないと思いますが、 もっと複雑なパターンで分割したいときなどは、正規表現も有効に使えると思います。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

恋するプログラム―Rubyでつくる人工無脳 (プレミアムブックス版)

恋するプログラム―Rubyでつくる人工無脳 (プレミアムブックス版)