惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
Last Week in AI
Last Week in AI
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
The Cloudflare Blog
罗磊的独立博客
月光博客
月光博客
N
Netflix TechBlog - Medium
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Jina AI
Jina AI
J
Java Code Geeks

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 5 設備器材篇 如何經營工程師 Youtube 頻道 - Part 4 後製剪輯篇 如何經營工程師 Youtube 頻道 - Part 3 文案企劃篇 如何經營工程師 Youtube 頻道 - Part 2 設備器材篇 如何經營工程師 Youtube 頻道 - Part 1 制訂頻道方向篇 如何經營工程師 Youtube 頻道 - Part 0 Rails 中避免 race condition 的最佳實踐(二) Rails 中避免 race condition 的最佳實踐(一) 10 分鐘整合 google sheet 做自動化開發功能週報 經營 Side Project 300 天所帶來的收穫及挑戰 我的 Youtube 影片製作流程 API 設計時必須注意的 HTTP header 底線問題 如何提升你的程式可讀性之實務技巧(三) 如何提升你的程式可讀性之實務技巧(二) 如何提升你的程式可讀性之實務技巧(一) Ruby 中使用 freeze 優化效能的時機 避免 React 中的 useEffect 無限 render 在 Rails 內輕量使用 Vue Component 的最佳實踐 如何在區域網路用 Docker 架設有 SSL 的 Gitlab 從被問到問人,那些我常問的面試問題 [Rails] 如何漂亮寫出可維護的 query (Maintainable Rails Query) 在已知長度情況下優化 slice 的性能
學習如何包gem
Nic Lin · 2015-12-13 · via Nic Lin's Blog

這是一個新增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"

就可以直接使用了!!