SSL更新のサーバ再起動前に確認で使うコマンド
いつも忘れるので。
openssl x509 -noout -modulus -in /path/to/crt.crt | openssl md5 => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx openssl rsa -noout -modulus -in /path/to/key.key | openssl md5 => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx openssl req -noout -modulus -in /path/to/csr.csr | openssl md5 => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
が同じ結果になる
send_file, send_data の include
Spree::Api
に自作Apiを追加するとき、send_file が NoMethodError でしたので include する方法を調べました。
app/controllers/spree/api/your_extension_controller.rb module Spree module Api class YourExtensionController < Spree::Api::BaseController # for send_file include ActionController::DataStreaming def download # authorize with administrator token as get paramater if it need authorize! :download, Spree::YourExtension, params[:token] file_path = "path/to/sample/file.csv" respond_to do |format| format.html { redirect_to :action => "download", :format => "csv"} format.csv { send_file file_path, :filename => File.basename(file_path) , type: "application/csv" } end end end end end
routing はこのような感じで。
your_extension_root/config/routes.rb Spree::Core::Engine.routes.draw do # for your_extension routing get 'api/download_your_extension/:id', controller: 'api/your_extension', action: 'download', defaults: { format: 'csv' }, as: :api_your_extension end
http://YOURDOMAIN.com/api/download_your_extension/file.csv?token=YOUR_API_TOKEN
または
http://YOURDOMAIN.com/api/download_your_extension/file?token=YOUR_API_TOKEN => redirect http://YOURDOMAIN.com/api/download_your_extension/file.csv?token=YOUR_API_TOKEN
でファイルをダウンロードできます。
YOUR_API_TOKEN は admin/users 画面上で管理者ユーザのアカウントで Generate してください。
tokenでの認証は、管理者自身がダウンロードするのであれば、都度ログインすれば不要なことですけどね。
*linuxで最近更新されたファイルを検索する
find . -type f -mmin -86400
1日以内なら 24*60*60 で 86400を指定
Rails4.1以前のEnum
Rails4.1以降はEnum使えますし素晴らしい記事をありがとうございます。
外部キーみたいに数字でステータスを持たせるけどデータベース側にはマスタを持たせないっていうときに書いたもののメモ。
今使っているのRailsは4.0.4なので、自力でモデルにハッシュを書いて、viewではoptions_for_selectでパスーンと組み立てています。
こんな感じで区分値を持たせて
class SupplierStockType AMPLE_STOCK = {:id => 1, :name => I18n.t('ample_stock')} SMALL_STOCK = {:id => 2, :name => I18n.t('small_stock')} NOT_IN_STOCK = {:id => 3, :name => I18n.t('not_in_stock')} def self.all self.constants.map { |value| const_get(value) } end end
view/helper側でこう組み立てる
options_for_select(SupplierStockType.all.collect {|p| [ p[:name], p[:id] ] })
rails new したい
今日、数人が共有して使っている開発用サーバでrails環境を作りました。
単に運用中のサーバ構成を侵食しないで構築すればよいだけなんですけど、考えるのが面倒になってしまっていやーね。
仕事、楽しんでやりたいデスネ。
現在のプロジェクトでbundlerを使ってますが、
bundle init というコマンドをなぜか今日初めて知りました…。
新しいRailsアプリを自動生成する方法は rails _4.0.4_ new appname しかないと思っていたんですが、
Gemfile を作って bundle install でも別によかったのです。
bundle initするとGemfileのひな形ができます。
Gemfileってシンプルなのにいざ1から書こうとすると、覚えてない。
\それではGemfileを召喚します/
$ mkdir test $ cd test $ bundle init Writing new Gemfile to /home/myuser/public_html/railsapp/newtest/Gemfile $ less Gemfile # A sample Gemfile source "https://rubygems.org" # gem "rails" $ vi Gemfile gem "rails" の列のコメントをはずす $ bundle install --path vendor/bundle Fetching gem metadata from https://rubygems.org/........... Resolving dependencies... Installing rake 10.3.2 Installing i18n 0.6.11 Installing json 1.8.1 … Installing rails 4.1.5 Your bundle is complete! It was installed into ./vendor/bundle
うぉー!
これで、ラクに rails new したいがためだけに、rbenvのrubyのrubygemにrailsをインストールする必要がなくなったのでした。ちゃん。
xlsxでダウンロードできるように MineType を登録する
pdfでもなんでも使えることになると思うのですが、メモ〜
app/config/initializers/mime_types.rb
Mime::Type.register "application/xlsx", :xlsx
controllers
def action # do something respond_to do |format| format.xlsx { send_file モデル名.to_xlsx ,type: "application/xlsx" } format.html end end
views
<%= link_to "xlsx" , action_path(format: :xlsx) %>
エクセルファイルでデータ出力する要件があって、リポジトリを眺めていて見つけたRubyXLを使ってみました。
こんなデータが入っているxlsx(OFFICE 2007以降)のA1を上書きするみたいなコードサンプルです。
report.xlsx
| A | B | C | D
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1 | a-1 | b-1 | c-1 | d-1
2 | a-2 | b-2 | c-2 | d-2
class モデル名 < ActiveRecord::Base def self.to_xlsx file_base_uri = "/target/file/directory/path/" template_file = "report.xlsx" dl_file_name = "report_#{Time.now.to_i}.xlsx" Rails.logger.info "PARSING xlsx Start" # testing to load template xlsx tmp_workbook = RubyXL::Parser.parse(file_base_uri + template_file_name)[0].extract_data.each.with_index(0) { |r, index| Rails.logger.info r[0] Rails.logger.info r[1] Rails.logger.info r[2] Rails.logger.info r[3] } Rails.logger.info "PARSING xlsx End" # copy as download file FileUtils.cp(file_base_uri + template_file_name, file_base_uri + dl_file_name) Rails.logger.info "CREATING xlsx Start" new_workbook = RubyXL::Parser.parse(file_base_uri + dl_file_name) # overwrite cell new_workbook[0].add_cell(0, 0, "なにかの日本語") # save file new_workbook.write(dl_file_name) Rails.logger.info "CREATING xlsx End" return dl_filename end end
app/log/development.log PARSING xlsx Start a-1 b-1 c-1 d-1 a-2 b-2 c-2 d-2 PARSING xlsx End CREATING xlsx Start CREATING xlsx End Sent file report_1404875869.xlsx (0.1ms)
report_1404875869.xlsx
| A | B | C | D
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1 | なにかの日本語 | b-1 | c-1 | d-1
2 | a-2 | b-2 | c-2 | d-2
今回の要件だとこの機能で充分でした。
なにかあったらrooとかaxlsxとか試してみるかもー。
-
- -
追記〜
蛇足かもしれないけど
each.with_index(0)
インデックスの最初の値を指定できるメソッドです。
0から始まる each_with_index もよく使いますが、
開始indexを指定できてブロックで使えるのです;;イイヨネ;;