# app/modles/concerns/common.rb module Common extend ActiveSupport::Concern # 这里的继承一定不要忘记了 # 可以将重复的代码都提取到这里 defmessage(content) "Message: #{content}" end end
重构原本的两个类,将 Common 包含进去。
1 2 3 4 5 6 7 8 9
# app/models/accout.rb classAccount < ActiveRecord::Base include Common end
# app/models/user.rb classUser < ActiveRecord::Base include Common end
测试一下代码,是正常运行的。把相同逻辑的代码抽取到 ActiveSupport::Concern 中,可以很大程度的减少 ActiveRecord Model 的复杂度。也符合代码的高内聚,低耦合。
1 2 3 4 5 6 7
$ rails c Running via Spring preloader in process 11485 Loading development environment (Rails 4.2.0) irb(main):001:0> Account.new.message "hello world!" => "hello world!" irb(main):001:0> User.new.message "hello world!" => "hello world!"