Nic Lin's Blog

喜歡在地上打滾的 Rails Developer

學習如何包gem

這是一個新增Facebook讚與留言的功能 我們將他實作在


def social_plugin(plugin_name, options)
  width  = options.delete(:width)
  height = options.delete(:height)
  style  = "border:none; overflow:hidden; width:#{width}px; height:#{height}px;"
  src    = "http://www.facebook.com/plugins/#{plugin_name}.php?#{options.to_param}"

  content_tag(:iframe, "", src: src, scrolling: "no", frameborder: "0", style: style, 
              allowtransparency: "true")
end

def fb_like(like_url, custom_options={})
  options = {
    href: like_url,
    send: false,
    layout: "button_count",
    show_faces: false,
    width: 90
  }

  options.merge! custom_options

  social_plugin("like", options)
end

def fb_comments(url, custom_options={})
  options = {
    href: url,
    num_posts: 10
  }

  options.merge! custom_options

  social_plugin("comments", options)
end

然後把這二個功能放入到app/views/products/show.html.erb 螢幕快照 2015-12-14 上午12.10.05 1.png

before

螢幕快照 2015-12-14 上午12.11.39.png

after

螢幕快照 2015-12-14 上午12.13.03.png

現在我們想把這個 helper 拆出來,包到 Gem 裡面,讓我以後不用再做這些設定,直接在 view 去用它就好

bundle fb_like_comment

螢幕快照 2015-12-14 上午12.17.32.png

找出這個gem的路徑

螢幕快照 2015-12-14 上午12.18.22.png

安裝在你的 Rails 專案裡面, 設定 path: xxx <== 只抓取你本機端的資料夾

螢幕快照 2015-12-14 上午12.19.58.png

在跑bundel install之前 必須先把你創建的gem裡面的xxx.gemspec檔案作一些小修改 不然直接跑bundle install會跳錯誤

錯誤如下

螢幕快照 2015-12-14 上午12.42.21.png

這裡的gem以fb_like_comment作示範

...
spec.summary       = "修改為任意內容"
spec.description   = "修改為任意內容"
...

接下來跑bundle install可以看到已經將我們自己製作的gem加入了

螢幕快照 2015-12-14 上午12.48.32.png

設定 dependency ‘railries’

gem的根目錄裡面的xxx.gemspec必須加入

spec.add_dependency “railties”

...
+  spec.add_development_dependency "railties"
...

Gem 的資料夾跑 bundle install, 把這個 gem 讀進去

設定 railties.rb

建一個新檔案: lib/fb_like_comment/railtie.rb

module SampleGemHelper
  class Railtie < Rails::Railtie
    initializer "SampleGemHelper.view_helpers" do
      ActionView::Base.send :include, FbLikeComment
    end
  end
end

把一開始在 Rails 新做的功能丟進去


require "fb_like_comment/version"
require "fb_like_comment/railtie" if defined?(Rails)

module FbLikeComment
def social_plugin(plugin_name, options)
    width  = options.delete(:width)
    height = options.delete(:height)
    style  = "border:none; overflow:hidden; width:#{width}px; height:#{height}px;"
    src    = "http://www.facebook.com/plugins/#{plugin_name}.php?#{options.to_param}"

    content_tag(:iframe, "", src: src, scrolling: "no", frameborder: "0", style: style,
                allowtransparency: "true")
  end

  def fb_like(like_url, custom_options={})
    options = {
      href: like_url,
      send: false,
      layout: "button_count",
      show_faces: false,
      width: 90
    }

    options.merge! custom_options

    social_plugin("like", options)
  end

  def fb_comments(url, custom_options={})
    options = {
      href: url,
      num_posts: 10
    }

    options.merge! custom_options

    social_plugin("comments", options)
  end
end

把原本寫在 rails 的 application_helper 移除掉, 並把 rails 重新跑 bundle install + rails s

若設定有錯誤,就會在 bundle install 這邊出現錯誤

這樣基本的 Gem 就完成了

把寫好的gem push到github上面就可以給別人使用囉

以後要給別人用,直接請它安裝

gem "fb_like_comment", github: "niclin/fb_like_comment"

就可以直接使用了!!

Share Comments
comments powered by Disqus