rails3中的engine 比較強大,現在寫gem, 可以以 engine為基礎建構。而且慢慢地可以把rails程式分子產品拆分。Rails 3.1中更可以把engine以rake的方式加載。。比如
Rails.application.routes.draw do
mount Blog::Engine => "/blog"
end
建立一個engine, 有個 gem可讓我們的工作更加友善[url=http://github.com/josevalim/enginex]Enginex[/url]
下面以rails3 為例,建立一個簡單的rails3 engine::cms:隻包含一個文章的管理
事先安裝好enginex
sudo gem install enginex
1.建立基本架構并以rspec做為測試:
enginex cms -t rspec
enginex會為我們建立好一個engine的架構。
2.建立所需要的目錄
cd cms
mkdir -p app/controllers
mkdir -p app/models
mkdir -p app/views/layouts
mkdir -p public/javascripts
mkdir -p public/stylesheets
由于我自己測試寫的就不怎麼樣,是以這裡跳過寫測試部分,就不按 BDD流程來了。主要實作engine的功能
3.建立一個model--Post(這裡是基于AcciveRecord,如果用mongoid,就更簡單些,就不用管migration了)
touch app/models/post.rb
class Post < ActiveRecord::Base
end
4.建立 controller..(layout "cms"這裡是使engine使用自己的布局)
touch app/controllers/posts_controller.rb
添加CURD
class PostsController < ApplicationController
layout "cms"
respond_to :html
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.create(params[:post])
respond_with(@post)
end
def edit
@post=Post.find(params[:id])
end
def update
@post=Post.find(params[:id])
flash[:notice] = "Successfully updated..." if @post.update_attributes(params[:post])
respond_with @post
end
end
5.建立layout ---cms.html.erb
touch app/views/layouts/cms.html.erb
添加代碼:
<!DOCTYPE html>
<html>
<head>
<title>Cms</title>
<%= stylesheet_link_tag "style" %>
<%= javascript_include_tag "jquery","rails" %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="container">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<h1>Cms Engine</h1>
<%= yield %>
</div>
</body>
</html>
6.建立所需的js,和 css,到public檔案夾,
style.css簡單寫點
h1{
text-align:center;
color:red;}
7.建立index.html.erb,show.html.erb.......等所需要檔案到 app/views/posts/ 下,這裡就不多說明,就是普通寫法
8.建立lib/cms/engine.rb, 其中,app.middleware.use ::ActionDispatch::Static, "#{root}/public"這裡是告訴 engine, 使用自己的public目錄下的資源檔案 ,前提是,打包成gem, plugin模式下無效,需要另加處理
module Cms
class Engine < Rails::Engine
initializer "static assets" do |app|
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end
end
end
9.修改lib/cms.rb
require "cms/engine"
10.添加config/routes.rb
Rails.application.routes.draw do |map|
root :to => "posts#index"
resources :posts
end
11.因為使用的active_record,是以我們要使用migration,建立一個generator,複制migration檔案 lib/generators/cms/install_generator.rb
module Cms
class InstallGenerator < Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
desc "Copies migration to main project"
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
def copy_migration
migration_template "create_posts.rb", "db/migrate/create_posts.rb"
end
end
end
12...現在,建立立一個migration檔案到lib/generators/cms/templates/create_posts.rb
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
def self.down
drop_table :posts
end
end
這樣,我們就可以通過rails g cms:install 進行migration檔案的複制了
13. 打包成gem,要先悠 rakefile, 添加要打包的目錄,預設隻有 lib目錄
s.files = FileList["[A-Z]*", "lib/**/*","app/**/*","public/**/*","config/**/*"]
然後在終端rake gem生成 cms.gem在pkg目錄下,安裝pkg目錄下的 gem 既可
14.要使用這個gem,隻要建立工程,rails new test ,修改 gemfile,添加
gem "cms"
然後
rm public/index.html
rails g cms:index
rake db:migrate
rails s
打開浏覽器,loclhost:3000 就可以看到post頁面,并且使用的css是 gem 自身的。
-------暫時寫到這,public 資源,隻有在 gem 下有用,想要在plugin 狀态下也有用,要在cms根目錄下建立一個init.rb,然後加上
config.middleware.use ::ActionDispatch::Static, "#{root}/public"
mark:::
轉載注明:[url=http://www.iteye.com]javaeye[/url]--[url=http://doabit.iteye.com]doabit[/url]