[Ruby] 文字列中の半角・全角スペースを削除
- 2013 1/29
Ruby on Rails で、CSVのインポート/エクスポート機能を実装する上での参考情報をご紹介します。
Ruby 1.9 は FasterCSV が標準の CSV クラスとして採用されているので、require “csv” すればすぐに使える。
・Ruby on Rails Screencasts – RailsCasts
CSVインポート
・#396 Importing CSV and Excel – RailsCasts
roo というスプレッドシート(Open Office, Excel, Google Spreadsheetsなど)を操作する gem を使っている。
・roo:スプレッドシートを操るRubyライブラリ « TORQUES LABS
CSVエクスポート
・#362 Exporting CSV and Excel – RailsCasts
・Ruby1.9とRailsでCSV出力(SJIS) #Rails #Ruby – Qiita
・[rails] csvつくって出力するのよメモ | おれせか
CSVファイルはExcelから作成されることが多くて、文字コードをShift_JISからUTF-8へ変換して扱う必要があります。
その辺のノウハウがまとめられています。
・CSV を文字コード変換しつつロード #Ruby – Qiita
普通はテストで使う fixture_file_upload メソッドで CSV ファイルをアップロードしてから、モデルに追加したクラスメソッド import を呼び出してやります。
> include ActionDispatch::TestProcess > file = fixture_file_upload "tmp/test.csv", "text/comma-separated-values" => #<Rack::Test::UploadedFile:0x007fa36b03f960 @content_type="text/comma-separated-values", @original_filename="test.csv", @tempfile=#<File:/var/folders/46/bg9j7wkn5qg9n895016mlvk00000gn/T/test.csv20130124-12060-h3mn08>> > Product.import(file) (0.2ms) BEGIN ["a", "b", "c", "d"] ["1", "2", "3", "4"] (0.1ms) COMMIT => <#CSV io_type:File io_path:"/var/folders/46/bg9j7wkn5qg9n895016mlvk00000gn/T/test.csv20130124-12060-h3mn08" encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\r" quote_char:"\""> |
Ruby on Rails で before_filter に条件を設定するための参考サイトをメモ。
Ruby on Rails + RSpec で privateメソッドをテストする方法をメモ。
下記のように、Userモデルに private_method というprivateメソッドを作成した場合、
■ app/models/user.rb
class User < ActiveRecord::Base def public_method "public" end private def private_method "private" end end |
Rspec でのモデルのテストは下記のように、user.send(:private_method) で呼び出して行う。
■ spec/models/user_spec.rb
require 'spec_helper' describe User do it 'public' do user = User.new user.public_method.should == "public" end it 'private' do user = User.new user.send(:private_method).should == "private" end end |
【参考】
Ruby on Rails で、ある条件の時だけバリデーションを掛けたいときは with_options を使えばグルーピングできて素敵です。
・Ruby on Rails Guides: Active Record Validations and Callbacks
・paramがなければModelのValidationをskipすれば良いじゃない #Ruby #Rails – Qiita
5.4 Grouping conditional validations
Sometimes it is useful to have multiple validations use one condition, it can be easily achieved using with_options.
class User < ActiveRecord::Base with_options :if => :is_admin? do |admin| admin.validates :password, :length => { :minimum => 10 } admin.validates :email, :presence => true end endAll validations inside of with_options block will have automatically passed the condition :if => :is_admin?
Ruby on Rails の form_for や form helper で調べることが多い項目を自分用にまとめ。
■ f.select
id の指定の仕方でハマったときに読む。
・そろそろRailsのselectメソッドについて一言いっとくか – (゚∀゚)o彡 sasata299’s blog
・formのselectタグにidを設定する – 中国でRuby on Rails
select(object, method, choices, options = {}, html_options = {}) |
色々と随時更新していく予定です。
Ruby on Rails であるあるエラー、
ArgumentError (invalid byte sequence in US-ASCII) |
の対処法は、マルチバイト文字(日本語とか)を使っているファイルの先頭に、エンコーディングを指定する。
# encoding: utf-8 |
ちょっとすると、忘れてしまうのですよね。
Rubyで、状態遷移を管理できる state_machine が便利!ということで参考サイトをメモ。
・pluginaweek/state_machine · GitHub
Ruby on Rails でページごとにtitleを変更する方法をメモ。
■ ERB
<title><%= content_for?(:title) ? yield(:title) : "default title"%></title> |
■ Haml
%title>< - if content_for? :title = yield :title |デフォルトタイトル - else デフォルトタイトル |
metaタグのdescriptionやkeywordsも同じように書けます。
■ app/views/layouts/application.html.haml
- if content_for? :meta_description - meta_description = yield :meta_description - else - meta_description = 'default description' %meta(content="#{meta_description}" name="description") - if content_for? :meta_keywords - meta_keywords = yield :meta_keywords - else - meta_keywords = 'default keywords' %meta(content="#{meta_keywords}" name="keywords") |
■ どこかの view
= content_for :meta_description do #{@item.description} = content_for :meta_keywords do #{@item.name} = content_for :title do #{@item.name} |
Ruby とか Rails とかで全文検索エンジンSolrを使うなら「sunspot」ということで色々とメモ。
・sunspotのチュートリアル意訳 – maeshimaの日記
・結局、Rails 3.2 で 大量のデータを検索する場合、Sunspot な Solr が一番 いい感じ。という面白くないオチ。 | JAM☆ぱん
・Will_Paginate with Sunspot – Ruby on Rails | What I Broke
リファレンス
・Module: Sunspot — Documentation for nuatt_sunspot (1.1.0.3)
・RSpec and Sunspot · sunspot/sunspot Wiki
・Install And Test SOLR+Sunspot In Your Rails+RSpec Project | DZone