Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/theme.php on line 623

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/custom_posts/custom_posts.php on line 22

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 46

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 67

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 75

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 92

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiRender.php on line 70

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 181

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 538

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 695

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 1087

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112
おれせか

おれせか

なんかそれっぽいのをうにうに。

  • ruby1.9.3, rails3.2.7
  • 日本語含むデータ。「"」や「,」も入るかも。
  • ExcelでもだいじょうぶなようにShift_JISで。
  • meta_searchで条件絞ったりしたものをそのまんま取得したい。

コントローラではこんなかんじ。

class UserController < ActiveRecord::Base
  # 〜略〜
  def index
    @search = User.search(params[:search])
    @users = @search.relation.latest
    # csv以外ならページング付
    @users = @users.page(params[:page]) unless request.format == Mime::CSV
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
      format.csv  { send_data User.csv(@users), type: 'text/csv; charset=shift_jis', filename: "hoge.csv" }
    end
  end
  # 〜略〜
end

モデル側はこげなかんじ。

class User
  # 〜略〜
  def self.csv(users)
    require 'csv'
    headers = %w(名前 住所 メモ)
    # force_quotesオプションで全fieldにクオート強制される
    csv_data = CSV.generate(headers: headers, write_headers: true, force_quotes: true) do |csv|
      enum = if users.is_a?(Array)
        # Arrayだったらそのまま
        users.each
      else
        # Relationとかだったらfind_in_batchesで分割して
        Enumerator.new do |yielder|
          users.find_in_batches do |records|
            records.each { |record| yielder.yield record }
          end
        end
      end
      enum.each do |user|
        csv << [
          user.name,
          user.address,
          user.note
        ]
      end
    end
    csv_data.encode(Encoding::SJIS) # sjisにへんかーん
  end
  # 〜略〜
end

.csvみたいなURLで呼び出したらcsvダウンロードに。

http://〜〜〜〜/users/index.csv
PAGETOP

https://github.com/collectiveidea/delayed_job

同じ種類のJobなんだけど、実行するタイミングや状況によって失敗時の再試行回数かえたいなーっていう要件があったので色々ごにょごにょしたメモ。

ちなみにのverなんかは以下でのおはなし

  • rails (3.2.7)
  • delayed_job (3.0.3)
  • delayed_job_active_record (0.3.2)

スマートにやる方法がわかんなかったので、わりと無理やりな。
ちなみにdelayed_jobにActiveRecord使用時のおはなし。

まず、delayed_jobsテーブルにmax_attemptsを追加。

rails g でサクっと。

bundle exec rails g migration add_max_attempts_to_delayed_jobs max_attempts:integer

こんなの↓ができる。

class AddMaxAttemptsToDelayedJobs < ActiveRecord::Migration
  def change
    add_column :delayed_jobs, :max_attempts, :integer
  end
end

migrateで適用。ドッコイショー

bundle exec rake db:migrate

んで、DB上のmax_attemptsにsetしたりgetしたりできるようにする。

initializers/delayed_job.rbあたりに

if defined?(Delayed::Backend::ActiveRecord::Job)
  module JobWithMaxAttemptsAttribute
    def self.included(base)
      base.class_eval do
        attr_accessible :max_attempts
        alias_method_chain :max_attempts, :attribute
      end
    end
    def max_attempts_with_attribute
      self[:max_attempts] || max_attempts_without_attribute
    end
  end
  Delayed::Backend::ActiveRecord::Job.send(:include, JobWithMaxAttemptsAttribute)
end

module名やらメソッド名は超テキトー。英語力ないですしおすし。

ほいでdelay実行時に

delayの引数でキーmax_attemptsにお望みの再試行回数を入れる。

HogeMailer.delay(max_attempts: 1).fuga(record)

指定しなかったりnilだったらデフォの回数になるはず。

PAGETOP

Deprecated: Function split() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/lib/func.php on line 75

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/plugin/ref.inc.php on line 207

最近、新しいmacbookと修理に出してもどってきた旧macbookとで続けざまにpcのセットアップをしたわけなんですが、firefoxのあの動作なんてaddonでやってたんだっけなーと思い出せないことが多かったので自分用覚書。

タブとかUIまわり

  • ツリー型タブ
    タブをサイドに表示。親子関係がパッと見でわかるのいいよねー。
  • マルチプルタブハンドラ
    ↑のツリータブとあわせてよく使う。まとめてババっと移動させたり、閉じたり。
  • All-in-One Sidebar
    履歴やらアドオンやらをサイドバーにまとめちゃうやつ。
  • Download Statusbar
    ファイルダウンロード状況なんかを下部ステータスバーにしちゃう。
  • Tab Mix Plus
    何を新規タブで開くだとかフォーカスさせるだとかタブメニューいじったりだとか。
  • Tab Permissions
    タブ単位での画像やらjsやらflashのon/offを。
  • Menu Editor
    tabmixplusやらtreestyletabsやらでカオスったメニューをいじったりするのに使う。
  • TabGroups Menu
    タブグループももりもり使ってタブ開くので目的のタブにたどり着きやすくするために。
  • UI Fixer
    アドオンバーをいじれるようにしたりとか。
  • Locationbar²
    urlの上の階層にすばやくアクセスしたり。

キー関連

  • Customizable Shortcuts
    ショートカットいじれないとやだやだー。
  • Firemacs
    入力フォームの中とかでCtrl+AだとかCtrl+Dだとかのemacsキーバインド使いたいので。

共有とか

  • Fox To Phone
    携帯にurlやら選択した文字列やらをワンクリックで送信。chrome to phoneのfirefox版。
  • Hatena Bookmark
    はてブサイドバー。
  • Pocket
    旧Read it later。携帯にも入れてあるので携帯→pcなんかの時にも便利。
  • Tombloo
    Tumblrやらなんやらに使う。
  • Xmarks
    ブックマーク共有。家でwindowsもあるしブラウザもいくつも使うので。

開発関連

その他

  • AutoPagerize
    スクロールしたら次ページ読み込むアレ。必須。
  • Make Link
    ページのタイトル+URLなんかをpukiwiki形式だとかMarkdown形式だとか任意の形式にしてクリップボードに送ってくれるやつ。

とりあえず今入ってたの全部書きだした。

いろいろ表示させるとこんなかんじ。

PAGETOP

Search