moosan63の日記、技術メモ

日記とか、技術メモとか

deviseでモデルごとに認証の方法を変える

devise使ってて、モデルごとに認証の方法を変えるのってできるのー?って思ったのでメモ。

やりたいこと
Userモデル -> e-mailとパスワードでログイン認証
Agentモデル -> ログインIDとパスワードでログイン認証

やりかた
Userモデルはまあそのまま
ここでAgentモデルを色々といじる
まず、migrationファイル

#db/migration/#{migration_hash}.rb
class ChangeAgentDataTypes < ActiveRecord::Migration
  def up
    add_index :agents, :email, :unique => false
  end

  def down
  end
end

:unique => falseにする。
ここで設定しないと、弾かれちゃう。

次にmodelのファイル

#agent.rb
 devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable,
           :authentication_keys => [:login_key] #これを自分が認証に使いたいものに変える

よくいろんなところで
config/initializer/devise.rb
を変更しましょうとか出てくるけど、ここを変えちゃうと全部が変わっちゃうので、個別に変えるときはこうする。

capistranoでデプロイ自動化

capistranoでかずー氏アイコンジェネレーターのデプロイを自動化した。
あとはjenkinsと組み合わせてテスト駆動開発を進めていきたい。
ということで設定したcapistranoのメモを。

インストール
gemで配布されてる。

gem install capistrano

初期設定
capifyコマンドで入れられる!

capify .

[add] writing './Capfile'
[add] making directory './config'
[add] writing './config/deploy.rb'
[done] capified!

あとはconfig/deploy.rbに設定を書いていけばOK.
いちお、自分のものを重要なところ隠しつつ載せると

set :application, "kazooshi_gen"
set :repository,  "git@your.repository.path.git"

set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

set :user, "app_server_user_name"
set :use_sudo, false
set :port, 22
set :deploy_to, "set_to_app_path"
ssh_options[:forward_agent] = true
ssh_options[:keys] = '/path/to/your/id_rsa/for/app_server/'

role :web, "moosan.org"       # Your HTTP server, Apache/etc
role :app, "moosan.org"       # This may be the same as your `Web` server
#role :db,  "your primary db-server here", :primary => true # This is where Rails migrations will run
#role :db,  "your slave db-server here"

# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

これを書き終わったら

cap deploy

これで終わり!
デプロイ先に指定したディレクトリに

current release

という2つのディレクトリができてるはず。
releaseは今までのデプロイされたものが全て入っていて、currentは最新のreleaseのシンボリックリンク
もうちょっと色々と設定できるけど、まだ全部は把握してないのでそのうちまとめる
・ω・

ruby, openCV

http://ser1zw.hatenablog.com/entry/20120216/1329322182
かずー氏アイコンジェネレーターで、背景除去を組み込んでみたいので、ruby openCVを使って実装しようかなーと思っている。
とりあえず調査中
グラフカットを実装するといいらしい。

railsのform_tagでajax

rails3のform_tagでajaxが簡単に実装できたのでメモ

#view

<%= form_tag(url_path, :remote=>true, :id=>"id_of_form") do %>

で、

:remote=>true

を設定することによってajaxの処理を受け付けるようなフォームにできる。
あとは以下のように普通に書いていき、

          <h3>ご意見ご要望</h3>
          <%= form_tag("/users/inquiry", :remote=>true, :id=>"inq_form") do %>
          <ul>
              <li><%= text_area_tag :user_inquiry %></li>
              <li><%= submit_tag "送信",:id=>"sub_btn" %></li>
          </ul>
          <% end %>

指定されたコントローラーにも、通常通り処理を書く。

ここまででも十分楽にしてくれているけど、更に便利なのが
javascript

        $()           
          .bind("ajax:beforeSend", function(){

          })
          .bind("ajax:success", function(xhr){

          })
          .bind("ajax:complete", function(xhr){

          })
          .bind("ajax:error", function(xhr){

          })
      });

と、イベントの処理を簡単に記述することができる。
あと、もちろんform_forでも使える。

手を出してみたいものメモ

  • gitlab(gitサーバー構築、まあしばらくはbitbucket/githubでもいいけど)
  • BDD(cucamber, rspecをもっと使いこなせるようにしたい)
  • CI(jenkinsの導入に挑戦中・・・)
  • 自動デプロイ(capistranoで、jenkinstとあわせてテストが通ったら自動デプロイされるようにしたいなあ)