談談 Module 跟 ActiveSupport 的 Concern

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
1.03K   0  
·
2018/12/23
·
3 mins read


Biryanni Dish on Round Stainless Steel Tray
DRY = 分享

Ruby on Rails 就是 Ruby,(只是,為什麼叫“Rails",我真的找不到說法?有大師可以賜教嗎?), Ruby 是一個物件導向的語言,所以,就有 class,也就是可以繼承,那為什麼又會有 module 跟 ActiveSupport 的 Concern?

關於 ruby 的 class 跟 module,我覺得高見龍先生的網上大作,寫的很好,不用我再寫,看他寫的就好:

類別(Class)與模組(Module) | 高見龍: 學習不需要為公司、不需要為長官、同事、不需要為別人,只為你自己。

簡單來說,Module(模組)跟 Class(類別)沒什麼太大的差別,差在,module 沒辦法 new 一個新的 module 出來,Module 沒辦法繼承別的 Module。 基本上,module 的用處就是分享功能,把要分享的功能寫在 module 裡,需要的時候再 include 進來,主要就是為了 DRY!,不要一個 code 寫得到處都是,真的很難維護。

在 Rails 中,要分享功能一樣要用到 module,但是還要加上 ActiveSupport 的 Concern 了,話說這 ActiveSupport 真是個包山包海的東西,搞到懂都天黑了,我都是一邊用一般看,想知道到底有多少東西,看這篇,介紹的很好:Ruby on Rails 實戰聖經 | ActiveSupport - 工具函式庫

本質上,Rails 的 module concern 跟 ruby 的 module 很像,主要的不同就是,Rails 的 Concern 是個讓 model 跟 controller 共用程式功能的支援,所以當你想要寫一段在 Rails 內的分享功能時,是在 controller 的,就寫在 controllers/concerns 內,model 內要分享的,就寫在 models/concerns 內,所以配合上 view 內的 helper,MVC 就都有各自對應的 DRY 分享方式了。

Concern 的寫法都很像,多說無益啦,看程式碼最快,我們就來寫一個在 controllers 中,可以共用的碼,來,直接上碼:

module ConvertDateParams
  extend ActiveSupport::Concern

  def date_select_convert(x_year, x_month, x_day, x_timezone)
    # convert to Time.
    v_at = ActiveSupport::TimeZone.new(x_timezone).local_to_utc( Time.new(x_year.to_i, x_month.to_i, x_day.to_i,0,0,0) )
    return v_at
  end
  # end of date_select_convert

end

 是的,就這樣,這個讓你可以直接轉換 form 內 datetime_select 的 date_select_convert() 就可以在你的任何一個 controller 中呼叫了,這功能只是個範例,沒寫例外及錯誤處理,可別就直接 copy 了,一笑~

concern 很好用,不過,我可以確定的是,各位用了一會後,很快就會有碰到 uninitialized constant 錯誤,因為,命名太方便了,我想很多人也會跟我一樣,copy 一個相似的過來改,結果擋名改錯,很煩的,所以要記住,Concerns 的 檔名要跟 module,但是還是要照 Rails 的命名法,比如上例:

module 名:ConvertDateParams = 檔名:convert_date_params.rb

Rails 對命名很挑惕的,大小寫,底線在那裡,很重要的!

後記

如果要詳細了解 concern 的用法,中文介紹的好像不多,這有一篇還不錯:Concerns in Rails,更清楚的我覺得要看官方介紹了:點 Ruby on Rails API 內的 ActiveSupport > Concern,其中有關於 include,extend,等... 的 Ruby hock (掛鉤) methods,我覺得這一篇寫得很好:

Ruby's Important Hook Methods — SitePoint — Imran Latif explores Ruby's most important hook methods. Learn what these methods are and how Ruby's favorite gems are using them.
SitePoint


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

This article is part of:
Categories:
Tags:
Date:
Published: 2018/12/23 - Updated: 2018/12/25
Total: 786 words


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.