softwarebook開発環境の復元

ようやくMacBookに元の環境が戻った。現在はproductionモードでテスト運用中
今回、ソースコードを色付けして記述する(シンタックス・ハイライト)という方法があることを初めて知って、使ってみた。ちょっと、色がうるさい気がする...。スーパーpre記法のカスタマイズを参考に設定を変えてみたけど、RadRailsのデフォルトカラーと同じにするのは難しい...。

ダウンロード

今回、Railsの開発環境のためにダウンロードしたのは以下4つのファイル。

Locomotive_2.0.8.dmg Rails環境のためのソフトウェアパッケージ
radrails-0.7.2-macosx-carbon.tar XcodeのようなGUIの開発環境
pleiades_1.2.0.p5.zip RadRailsの日本語化のため
subversion-client-1.3.1.dmg ファイルの履歴管理のため

MySQLの代わりにSqlite3*1を利用することにしたので、以前より手軽に導入できた。Sqlite3では、データベースの設定も必要ないので開発中はこっちの方がいいかもしれない。以下のような魅力がある。

    • config/database.ymlの設定がとっても簡単。ユーザーやパスワードの設定が必要ない。
    • データベースを新規追加しなくても、いきなりマイグレションが実行できる。
    • データはdbフォルダにファイルとして保存されることになる。ソースコードと一緒に配布できるので、すぐにサンプルとして動作確認できる。

Locomotiveの設定

Locomotive2フォルダをアプリケーションフォルダにドラッグ&ドロップするだけ。

パスを通す

ターミナルからもコマンドが使えるように.bash_profileを新規作成して、以下の設定をしておく。

~/.bash_profile
export PATH=/Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/:/usr/local/bin:/usr/local/sbin:$PATH

RadRailsの設定

日本語化

解凍したradrailsフォルダをアプリケーションフォルダにドラッグ&ドロップしたら、まずは日本語化。手順については「RadRailsを日本語化する。」で。ダウンロードしたファイルは、pleiades_1.2.0.p5とバージョンが上がっているが、同じ手順で日本語化できた。

環境設定

MacBookにLocomotiveをインストールした環境で、RadRailsを利用する設定。メニューから環境設定を開いて、以下のように設定した。その他の設定はお好みで。RadRailsを日本語化しておくと、環境設定で設定内容のおおよその見当は付くようになった。

Ruby >> Ri/rdoc >>
RDoc パス /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/rdoc
Ri パス /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/ri
Ruby >> インストール済みのインタープリター >>
インタープリター名 1.8.4 in Locomotive
ロケーション /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/ruby
Rails >> 構成 >>
Rails パス /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/rails
Rake パス /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/rake
Mongrel パス /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/mongrel_rails
一般 >> ワークスペース >>
自動的に更新 チェックあり
テキスト・ファイル・エンコード その他 UTF-8
新規テキスト・ファイルの行区切り文字 その他 Unix

subversionの設定

インストーラーの手順に従ってインストールしておく。設定と簡単な使い方は「Subversionでファイルのバージョン管理をしてみる。」で。

Sqlite3のRailsプロジェクトを作成

ホームにrailsappフォルダを作成して、ターミナルから以下を実行。

cd ~/railsapp
rails softwarebook --database=sqlite3

すると、config/database.ymlが以下のように設定されている。この設定は、このまま利用できる。MySQLの時より簡単だ。

# SQLite version 3.x
#   gem install sqlite3-ruby
development:
  adapter: sqlite3
  database: db/development.sqlite3

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3

production:
  adapter: sqlite3
  database: db/production.sqlite3

プラグインのインストール

Specialプラグイン

RadRailsの「Railsプラグイン」タブに存在しないので、ターミナルで以下のコマンドを実行した。

cd ~/railsapp/softwarebook
script/plugin install http://wota.jp/svn/rails/plugins/branches/stable/special
Engines、LoginEngine、UserEngineプラグイン

RadRailsの「Railsプラグイン」タブから選択して、簡単にインストールできた。

モデルの作成

RadRailsの「ジェネレーター」タブで...

  • 左のリストからmodelを選択して、右項目にsoftwareを入力して、実行ボタン。
  • 左のリストからmodelを選択して、右項目にkeywordを入力して、実行ボタン。

データベースの構築

マイグレーションの設定

上記モデルを作成した時に自動的に生成されるファイルを、以下のように設定した。

db/migrate/001_create_softwares.rb
class CreateSoftwares < ActiveRecord::Migration
  def self.up
    create_table :softwares do |t|
      t.column :title, :string
      t.column :description, :text
      t.column :url, :string
      t.column :created_on, :datetime
      t.column :updated_on, :datetime
      t.column :keyword_id, :integer
      t.column :user_id, :integer
    end
  end

  def self.down
    drop_table :softwares
  end
end
db/migrate/002_create_keywords.rb
class CreateKeywords < ActiveRecord::Migration
  def self.up
    create_table :keywords do |t|
      t.column :name, :string
    end
  end

  def self.down
    drop_table :keywords
  end
end
マイグレーションの実行
上記で設定したsoftware、keywordを作成 RadRailsの「Rake タスク」タブで、db:migrateを選択して、実行ボタン。
LoginEngine、UserEngineのプラグイン用のテーブルを作成 RadRailsの「Rake タスク」タブで、db:migrate:enginesを選択して、実行ボタン。
bootstrapの実行
UserEngineのroles、permissions、usersテーブルの初期設定 RadRailsの「Rake タスク」タブで、bootstrapを選択して、実行ボタン。

jascaffoldの実行

RadRailsの「ジェネレーター」タブで...

  • 左のリストにjascaffoldと入力して、右項目にsoftwareを入力して、実行ボタン。
  • 左のリストにjascaffoldと入力して、右項目にkeywordを入力して、実行ボタン。

configフォルダの設定

config/environment.rb
Ruby GetText、LoginEngine、UserEngineに関する設定
$KCODE = 'u'    # 'u'=UTF-8, 's'=SJIS, 'e'=EUC-JP
require 'jcode' # Stringクラスのメソッドなどを、$KCODEに指定した文字コードで適切に動作するように置き換える。
...(途中省略)...
# Include your application configuration below
# Ruby-GetText-Packageを使う宣言
require 'gettext/rails'

# Ruby GetTextにmsgmergeのパスを教えてあげる
ENV['MSGMERGE_PATH'] = '/Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/i386/bin/msgmerge'

module LoginEngine
  config :salt, "ror-salt" # 暗号化のキー、何でもいい
  
  # Source address for user emails
  config :email_from, '送信元メールアドレス' # 届いたメールのfrom欄に表示される。

  # Destination email for system errors
  config :admin_email, 'システム管理者のメールアドレス'

  # Sent in emails to users
  config :app_name, 'Softwarebook' # メールのタイトルに挿入されるキーワードになる。
end

module UserEngine
  config :admin_login, "zarigani"
  config :admin_email, 'zarigani.watch@gmail.com' # 'zariganiユーザーのメールアドレス'
  config :admin_password, 'zarigani'
end

Engines.start :login, :user # 必ず:loginを最初に書く

UserEngine.check_system_roles
config/environments/development.rb、production.rb
LoginEngineがメールを送信する時の設定
...(途中省略)...
ActionMailer::Base.server_settings = {
  :address => "smtp.mac.com",
  :port => 25,
  :domain => "smtp.mac.com",
  :user_name => ".Macのユーザー名", # メールアドレスの@の手前まで
  :password => ".Macのログインパスワード",
  :authentication => :login
}

appフォルダの設定

app/controllers/application.rb
Ruby GetText、LoginEngine、UserEngineに関する設定
# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
require 'login_engine'

class ApplicationController < ActionController::Base
  init_gettext "softwarebook"
  
  include LoginEngine
  include UserEngine
  
  helper :user
  model :user
  
  #before_filter :login_required
  before_filter :authorize_action
end
app/helpers/application_helper.rb
LoginEngine、UserEngineに関する設定
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include LoginEngine
  include UserEngine
end
app/views/layouts/user.rhtml
LoginEngine、UserEngineに関する設定
<html>
<head>
  <title>Softwares: <%= controller.action_name %></title>
  <%= engine_stylesheet "user_engine" %>
  <%= engine_javascript "user_engine" %>
</head>
<body>

<% for name in [:notice, :warning, :message] %>
  <%= "<p style=?"color: green?">#{flash[name]}</p>" if flash[name] %>
<% end %>

<%= yield  %>

</body>
</html>

*1:Locomotive_2.0.8に含まれている。