rails开发利器:视频播放插件plugin(如何开发一个简单的插件)

  1.  For: rails2.3.8  
    因为我的项目是基于rails2.3.8的,以后在做rails3.0的
  2. plugin的名称是 video_player, 新建plugin
    ruby  script/generate plugin video_player --with-generator

  3. 重新组织我的文件目录
    新建video_player和rails文件夹,新建rails/init.rb文件
    mkdir rails
    mkdir lib/video_player
    touch rails/init.rb

    修改 rails/init.rb
    require 'video_player'

    删除多余的文件
     rm -rf init.rb install.rb
    
  4. 整理一下我的思路:
    4.1 我需要新建一个helper,用于显示flash video, 而且能分直播和非直播的模式
    4.2 我需要新建一个rake task,用于资源的复制
  5. 开发helper方法
    新建lib/app/helpers/video_player_helper.rb文件
    module VideoPlayerHelper
    
    
       def video_player(url_bad='',live=true,width=500, height=400)
        flash_url_bad = "http://#{request.env["HTTP_HOST"]}/player.swf"
        flash_url = flash_url_bad
        player = ""
        player += ''
        player +=  ""
        url = URI.escape(url_bad, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
        if live
          player += "  "
        else
          player += " "
        end
    
        player += "
                
                
    修改video_player.rb文件,将helper加入application中
    %w{ helpers }.each do |dir|
    
      path = File.join(File.dirname(__FILE__), 'app', dir)
      $LOAD_PATH << path
      ActiveSupport::Dependencies.load_paths << path
      ActiveSupport::Dependencies.load_once_paths.delete(path)
    end 
    
    if defined?(ActionView::Base)
      ActionView::Base.send :include, VideoPlayerHelper
    end


  6. 新建一个rake,用于复制player.swf到Public目录
    require 'fileutils'
    include FileUtils::Verbose
    desc "copy player.swf to public dir"
    task :copy_video_player do
      play_swf = File.join(File.dirname(__FILE__),"player.swf")
      cp(play_swf, File.join(RAILS_ROOT, "public"))
    end
  7. 代码地址
    https://github.com/chucai/videoplayer


  8. 你可能感兴趣的:(ruby,on,rails)