2017年4月18日火曜日

rspecでtimecopで日時を固定したのにFactoryGirlのデータが固定されない

rspecのテストで特定の日時でのテストをしたくて、
timecopで日時を固定したのにFactoryGirlのデータが固定された日付ではなく、
現在日時のデータになってしまったので、調べました。

環境は以下です
timecop 0.8.0
factory_girl 4.5.0
rspec 3.5.0

テストデータとテストはこんな感じです。
FactoryGirl.define do
  factory :aaa do
    target_date Date.today.tomorrow
  end
end

Timecop.freeze(Date.parse('2017-04-04'))
aaa = FactoryGirl.create(:aaa)
expect(aaa.target_date == Date.today.tomorrow).to eq(true)
実行すると、trueにならずにテスト失敗となります。。

FactoryGirlの書き方を以下のようにすると、ちゃんと固定された日時を元に日付が入りました
target_date { Date.today.tomorrow }

参考URL
http://stackoverflow.com/questions/33110516/timecop-does-not-work-with-datetime-attributes-defined-in-factorygirl

2017年4月13日木曜日

omniauth-facebookのバージョンあげたら値が取れなくなった

omniauth-facebookのバージョンが古かったのでバージョンをあげたところ、
取れてた氏名の値が取れなくなったので調べました。
変更前:2.0.0
変更後:4.0.0

これまでのコード
取得部分
last_name = auth.info.last_name
first_name = auth.info.first_name

config部分
config.omniauth :facebook, 'app_id', 'app_token', locale: 'ja_JP', provider_ignres_state: true, scope: 'email, public_profile'

調べてみると、configに追加するだけで取れるようになるようです。
以下のように修正しました。
config.omniauth :facebook, 'app_id', 'app_token', locale: 'ja_JP', provider_ignres_state: true, scope: 'email, public_profile', info_fields: 'email, first_name, last_name'


参考URL
http://stackoverflow.com/questions/33090322/get-first-name-and-last-name-fields-from-facebook-omniauth

2017年4月4日火曜日

active_adminのバージョンあげたらshowの処理が変わってたのでパッチ当てた

active_adminのバージョンを1.0.0.pre5にあげたらこれまでshowで見れてた
xxx_idなどが表示されなくなったので調べました。

https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource/attributes.rb#L27
どうやらこの処理でxxx_idやxxx_countなどの値が表示対象から外されるようになったようです。
ただ、それだと困るとの意見があったのでパッチを当てることにしました。

こちらのクックパッドさんの投稿を参考にして以下のようなパッチを書きました。
http://techlife.cookpad.com/entry/a-guide-to-monkey-patchers

require 'active_admin/version'
unless ActiveAdmin::VERSION == '1.0.0.pre5'
  raise "Confirm need this patch"
end

module ActiveAdmin
  class Resource
    module Attributes
      def reject_col?(c)
        return false
      end
    end
  end
end