读书人

开发Rails插件的步骤和步骤

发布时间: 2012-09-08 10:48:07 作者: rapoo

开发Rails插件的方法和步骤

Rails丰富的插件真是强大,幻想着把各个功能都做出插件,以后开发系统就像搭积木一样堆积就好啦,呵呵。
开发个Rails插件其实不难,或者说很简单,流程基本如下:
1、生成骨架
在Rails的script下有个generate,可以直接生成骨架,执行:
# ruby script/generate plugin foo
就会在$RAILS_ROOT/vendor/plugins下生成一个foo目录,如下:

?

Ruby代码
  1. /1stlog$ruby?script/generate?plugin?foo??
  2. ??????create??vendor/plugins/foo/lib??
  3. ??????create??vendor/plugins/foo/tasks??
  4. ??????create??vendor/plugins/foo/test??
  5. ??????create??vendor/plugins/foo/README??
  6. ??????create??vendor/plugins/foo/MIT-LICENSE??
  7. ??????create??vendor/plugins/foo/Rakefile??
  8. ??????create??vendor/plugins/foo/init.rb??
  9. ??????create??vendor/plugins/foo/install.rb??
  10. ??????create??vendor/plugins/foo/uninstall.rb??
  11. ??????create??vendor/plugins/foo/lib/foo.rb??
  12. ??????create??vendor/plugins/foo/tasks/foo_tasks.rake??
  13. ??????create??vendor/plugins/foo/test/foo_test.rb?

?

? 2、编写相关代码
?在lib/foo.rb 下编写你的插件代码即可。

Ruby代码

?

  1. 在lib/foo.rb 下编写你的插件代码即可。

    Ruby代码
    1. module?Foo??
    2. ??def?say_hello?name??
    3. ????“hello?#{name}”??
    4. ??end??
    5. end??

    3、混入到core中去
    在init.rb中编写混入代码即可。

    Ruby代码
    1. require?‘foo’????
    2. ActionController::Base.send?:include,?Foo

?

读书人网 >网络基础

热点推荐