{"id":2552,"date":"2013-05-09T18:52:10","date_gmt":"2013-05-09T09:52:10","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2552"},"modified":"2018-11-14T16:33:55","modified_gmt":"2018-11-14T07:33:55","slug":"active_modularity","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/05\/09\/active_modularity\/","title":{"rendered":"ActiveRecord\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u5206\u5272\u3092\u652f\u63f4\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea active_modularity \u3092\u4f5c\u3063\u3066\u307f\u305f"},"content":{"rendered":"
\u5148\u65e5 activerecord-blockwhere<\/a> \u306e\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001 active_modularity\u306f\u4e00\u3064\u306eRails\u30a2\u30d7\u30ea\u3067\u7ba1\u7406\u6a5f\u80fd\u3068\u516c\u958b\u6a5f\u80fd\u306a\u3069\u3001\u8907\u6570\u306e\u6a5f\u80fd\u3067\u540c\u3058\u30e2\u30c7\u30eb\uff08\u30c6\u30fc\u30d6\u30eb\uff09\u3092\u64cd\u4f5c\u3057\u305f\u3044\u5834\u5408\u306b\u5229\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n \u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4ee5\u4e0b\u306e\u69cb\u9020\u306b\u3059\u308b\u3053\u3068\u304c\u524d\u63d0\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n active_modularity\u3092\u5229\u7528\u3057\u306a\u304f\u3066\u3082\u3001\u4e0a\u8a18\u69cb\u6210\u306eRails\u30a2\u30d7\u30ea\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u306f\u53ef\u80fd\u3067\u3059\u304c\u3001\u4f55\u70b9\u304b\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3059\u3002 \u5b9f\u969b\u306b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3001\u30e2\u30c7\u30eb\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u5206\u5272\u3092\u884c\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u307e\u305a\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u5358\u4f4d\u3067\u5206\u5272\u3057\u307e\u3059\u3002 \u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u7d99\u627f\u3059\u308b\u57fa\u5e95\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306f\u3001\u300cApplicationController\u300d\u3067\u306f\u306a\u304f\u300cPublic::ApplicationController\u300d\u306e\u3088\u3046\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u540d\u3092\u7701\u7565\u305b\u305a\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 namespace\u3068\u3057\u3066public,admin\u3092\u6307\u5b9a\u3057\u3001\u305d\u306e\u4e2d\u3067\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002 \u30e2\u30c7\u30eb\u306fmodels\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u57fa\u5e95\u30e2\u30c7\u30eb\u3092\u4f5c\u308a\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306b\u7d99\u627f\u3057\u305f\u30e2\u30c7\u30eb\u3092\u4f5c\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002 \u5404\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u30d3\u30e5\u30fc\u306b\u3064\u3044\u3066\u306f\u7279\u306b\u610f\u8b58\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002 \u30e2\u30c7\u30eb\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306b\u4f5c\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5229\u70b9\u304c\u751f\u307e\u308c\u307e\u3059\u3002<\/p>\n \u5404\u30e2\u30b8\u30e5\u30fc\u30eb\u3054\u3068\u306b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304c\u308f\u304b\u308c\u308b\u305f\u3081\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3001attr_accessible\u306a\u3069\u3001 form_for\u3092\u5229\u7528\u3057\u3066\u30d5\u30a9\u30fc\u30e0\u3092\u4f5c\u3063\u305f\u5834\u5408\u3001form\u30bf\u30b0\u306eaction\u5c5e\u6027\u306b\u8a2d\u5b9a\u3055\u308c\u308bURL\u306f\u30e2\u30c7\u30eb\u306e\u30af\u30e9\u30b9\u540d\u3092\u5143\u306b\u69cb\u7bc9\u3055\u308c\u307e\u3059\u3002 \u4ee5\u4e0b\u306e3\u3064\u306eform_for\u306eaction\u5c5e\u6027\u306f\u5168\u3066\u540c\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u30c7\u30b3\u30ec\u30fc\u30bf\u3092\u5b9a\u7fa9\u3067\u304d\u308b active_decorator<\/a> \u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308a\u307e\u3059\u3002 active_decorator\u306f\u524d\u56de\u3061\u3087\u3063\u3068\u89e6\u308c\u305factiverecord-refinements<\/a>\u3068\u540c\u3058\u4f5c\u8005amatsuda\u3055\u3093\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u306d\u3002 Rails\u3092development\u30e2\u30fc\u30c9\u3067\u8d77\u52d5\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u306e\u307e\u307e\u3067\u306f\u4e0a\u624b\u304f\u52d5\u304b\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n Public::EntriesController#index \u30a2\u30af\u30b7\u30e7\u30f3\u3067\u306f Entry.all \u3067\u5168\u30a8\u30f3\u30c8\u30ea\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002 development\u30e2\u30fc\u30c9(config.cache_class=false\u306e\u5834\u5408)\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306fconst_missing\u306b\u3088\u308a\u9045\u5ef6\u8aad\u307f\u8fbc\u307f\u3055\u308c\u307e\u3059\u304c\u3001Admin::Entry\u30af\u30e9\u30b9\u304c\u5148\u306b\u8aad\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u3068\u7d99\u627f\u5143\u306eEntry\u30af\u30e9\u30b9\u304c\u5b9a\u7fa9\u3055\u308c\u308b\u305f\u3081\u3001Public\u30e2\u30b8\u30e5\u30fc\u30eb\u5185\u3067\u306eEntry\u304c\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306eEntry\u3068\u3057\u3066\u89e3\u91c8\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002<\/p>\n \u3053\u306e\u554f\u984c\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5185\u3067Admin::Entry\u306e\u3088\u3046\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u540d\u3092\u7701\u7565\u305b\u305a\u306b\u66f8\u3051\u3070\u89e3\u6d88\u3055\u308c\u307e\u3059\u3002<\/p>\n \u3042\u308b\u3044\u306finitializer\u306b\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3082\u5bfe\u5fdc\u3067\u304d\u307e\u3059\u3002 \u8d77\u52d5\u6642\u306b\u5168\u3066\u306e\u30e2\u30c7\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3057\u307e\u3046\u3053\u3068\u3067\u3001\u3053\u306e\u554f\u984c\u3092\u56de\u907f\u3057\u307e\u3059\u3002 \u5148\u307b\u3069\u306e\u30d6\u30ed\u30b0\u30b5\u30a4\u30c8\u306b\u30b3\u30e1\u30f3\u30c8\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 \u3057\u304b\u3057\u3001\u95a2\u9023\u5148\u306e\u30e2\u30c7\u30eb\u3092build\u3084create\u3057\u3088\u3046\u3068\u3057\u305f\u5834\u5408\u3001\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306b\u3042\u308b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u751f\u6210\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n \u6b21\u306f\u30d6\u30ed\u30b0\u3092\u7de8\u96c6\u3059\u308b\u305f\u3081\u306e\u30e6\u30fc\u30b6\u3092\u4f5c\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 \u4ee5\u4e0b\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n \u3057\u304b\u3057\u3001Admin::Author\u30af\u30e9\u30b9\u3092create\u3059\u308b\u3068\u3001type\u30ab\u30e9\u30e0\u306b\u306f\u300cAdmin::Author\u300d\u306e\u3088\u3046\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u4ed8\u304d\u306e\u30af\u30e9\u30b9\u540d\u304c\u767b\u9332\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002 active_modularity\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u300c\u95a2\u9023\uff08Association\uff09\u5148\u30af\u30e9\u30b9\u306e\u554f\u984c\u300d\u300c\u5358\u4e00\u30c6\u30fc\u30d6\u30eb\u7d99\u627f\uff08Single Table Inheritance\uff09\u30af\u30e9\u30b9\u306e\u554f\u984c\u300d\u304c\u89e3\u6d88\u3055\u308c\u307e\u3059\u3002<\/p>\n \u3067\u306factive_modularity\u3092\u5c0e\u5165\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306fbundler\u3067\u884c\u3044\u307e\u3059<\/p>\n \u6b21\u306b\u5148\u307b\u3069\u4f5c\u3063\u305finitializer\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n \u8a2d\u5b9a\u306f\u3053\u308c\u3067\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n \u3082\u3046\u4e00\u5ea6\u95a2\u9023\u30e2\u30c7\u30eb\u306ebuild\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n \u671f\u5f85\u901a\u308a\u306e\u7d50\u679c\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n \u3067\u306f\u3001\u6b21\u306b\u5358\u4e00\u30c6\u30fc\u30d6\u30eb\u7d99\u627f\u306e\u4f8b\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n \u5c11\u3057\u8907\u96d1\u3067\u3059\u304c\u3001\u5358\u7d14\u306b\u8868\u73fe\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n active_modularity\u306f\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u5206\u5272\u3092\u307b\u3093\u306e\u3061\u3087\u3063\u3068\u3060\u3051\u624b\u52a9\u3051\u3057\u3066\u304f\u308c\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 \u5148\u65e5 activerecord-blockwhere \u306e\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001
\n\u5b9f\u306f\u540c\u3058\u65e5\u306b active_modularity<\/a> \u3068\u3044\u3046Gem\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u516c\u958b\u3057\u307e\u3057\u305f\u3002<\/p>\n\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u7d39\u4ecb<\/h2>\n
\n
\n\u3053\u306e\u554f\u984c\u70b9\u3092\u89e3\u6d88\u3057\u3066\u304f\u308c\u308b\u306e\u304cactive_modularity\u3067\u3059\u3002<\/p>\n\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3001\u30e2\u30c7\u30eb\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u5206\u5272\u3059\u308b<\/h2>\n
\n\u4f8b\u3068\u3057\u3066\u3001\u7c21\u5358\u306a\u30d6\u30ed\u30b0\u30b5\u30a4\u30c8\u306e\u7ba1\u7406\u6a5f\u80fd\u3068\u516c\u958b\u6a5f\u80fd\u3092\u4f5c\u308b\u5834\u5408\u3092\u8003\u3048\u307e\u3059\u3002<\/p>\n\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u5206\u5272<\/h3>\n
\nPublic::ApplicationController\u306e\u3088\u3046\u306b\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u4ee5\u4e0b\u3067\u57fa\u5e95\u3068\u306a\u308b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f5c\u3063\u3066\u5171\u901a\u306e\u30d5\u30a3\u30eb\u30bf\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n# app\/controllers\/public\/application_controller.rb\r\nmodule Public\r\n class ApplicationController < ::ApplicationController\r\n # \u5171\u901a\u6a5f\u80fd\u7b49\u3092\u5b9f\u88c5...\r\n end\r\nend\r\n\r\n# app\/controllers\/public\/entries_controller.rb\r\nmodule Public\r\n class EntriesController < Public::ApplicationController\r\n def index\r\n @entries = Entry.all\r\n end\r\n\r\n def show\r\n @entry = Entry.find(params[:id])\r\n end\r\n end\r\nend\r\n\r\n# app\/controllers\/admin\/application_controller.rb\r\nmodule Admin\r\n class ApplicationController < ::ApplicationController\r\n # \u8a8d\u8a3c\u7528\u306e\u30d5\u30a3\u30eb\u30bf\u3084\u5171\u901a\u6a5f\u80fd\u7b49\u3092\u5b9f\u88c5...\r\n end\r\nend\r\n\r\n# app\/controllers\/admin\/entries_controller.rb\r\nmodule Admin\r\n class EntriesController < Admin::ApplicationController\r\n def index\r\n @entries = Entry.all\r\n end\r\n\r\n def create\r\n # params\u306e\u30ad\u30fc\u304cadmin_entry\u306b\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\r\n @entry = Entry.create(params[:admin_entry])\r\n respond_with(@entry)\r\n end\r\n\r\n # \u305d\u306e\u4ed6RUD\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u5b9f\u88c5...\r\n end\r\nend<\/pre>\n
\n\u30e2\u30b8\u30e5\u30fc\u30eb\u540d\u3092\u6307\u5b9a\u3057\u306a\u3044\u5834\u5408\u3001\u8aad\u307f\u8fbc\u307f\u306e\u9806\u5e8f\u6b21\u7b2c\u3067\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306eApplicationController\u3092\u7d99\u627f\u3057\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u8a18\u8ff0<\/h3>\n
\npublic\u5074\u306eurl\u306f\/\u3092\u8d77\u70b9\u306b\u3059\u308b\u305f\u3081\u3001path\u30aa\u30d7\u30b7\u30e7\u30f3\u3067nil\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n# config\/routes.rb\r\nMyBlog::Application.routes.draw do\r\n namespace :public, path: nil do\r\n root to: 'entries#index'\r\n resources :entries\r\n end\r\n\r\n namespace :admin do\r\n root to: 'entries#index'\r\n resources :entries\r\n end\r\nend<\/pre>\n
\u30e2\u30c7\u30eb\u306e\u5206\u5272<\/h3>\n
\n\u57fa\u5e95\u30e2\u30c7\u30eb\u306b\u306f\u5171\u901a\u3067\u5229\u7528\u3059\u308b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3084\u30e1\u30bd\u30c3\u30c9\u306a\u3069\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n# app\/models\/entry.rb\r\nclass Entry < ActiveRecord::Base\r\n # \u5171\u901a\u3067\u5229\u7528\u3057\u305f\u3044\u3082\u306e\u3092\u66f8\u304f\r\n validates :title, presence: true\r\n validates :content, presence: true\r\nend\r\n\r\n# app\/models\/public\/entry.rb\r\nmodule Public\r\n class Entry < ::Entry\r\n # \u516c\u958b\u6a5f\u80fd\u306e\u307f\u3067\u5229\u7528\u3057\u305f\u3044\u3082\u306e\u3092\u66f8\u304f\r\n end\r\nend\r\n\r\n# app\/models\/admin\/entry.rb\r\nmodule Admin\r\n class Entry < ::Entry\r\n # \u7ba1\u7406\u6a5f\u80fd\u306e\u307f\u3067\u5229\u7528\u3057\u305f\u3044\u3082\u306e\u3092\u66f8\u304f\r\n attr_accessible :title, :content\r\n end\r\nend<\/pre>\n
\u30d3\u30e5\u30fc\u3092\u4f5c\u308b<\/h3>\n
\n\u30ec\u30a4\u30a2\u30a6\u30c8\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u5404\u30e2\u30b8\u30e5\u30fc\u30eb\u5185\u306eApplicationController\u306b\u5bfe\u3057\u3066\u6307\u5b9a\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n# app\/views\/layouts\/public\/application.html.erb<\/pre>\n
\u516c\u958b\u6a5f\u80fd<\/h1>\n
<%= yield %>\r\n\r\n# app\/views\/layouts\/admin\/application.html.erb<\/pre>\n
\u7ba1\u7406\u6a5f\u80fd<\/h1>\n
<%= yield %><\/pre>\n
\u30e2\u30b8\u30e5\u30fc\u30eb\u5206\u5272\u306b\u3088\u308b\u5229\u70b9<\/h2>\n
\u6a5f\u80fd\u5358\u4f4d\u3067\u5fc5\u8981\u306a\u6a5f\u80fd\u3092\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b<\/h3>\n
\n\u5171\u901a\u3067\u5b9a\u7fa9\u3057\u305f\u3044\u3082\u306e\u3001\u6a5f\u80fd\u3054\u3068\u306b\u5207\u308a\u66ff\u3048\u305f\u3044\u3082\u306e\u3092\u7d20\u76f4\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306b\u30e2\u30fc\u30c9\u3092\u6301\u305f\u305b\u3066if\u3067\u5207\u308a\u66ff\u3048\u308b\u3001\u306a\u3069\u306e\u5f37\u5f15\u306a\u30b3\u30fc\u30c9\u304c\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\nform_for\u3067url\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b<\/h3>\n
\n\u30e2\u30c7\u30eb\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306b\u5b9a\u7fa9\u3057\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30681\u5bfe1\u3067\u5bfe\u5fdc\u3055\u305b\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u3053\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u6307\u5b9a\u306e\u5fc5\u8981\u304c\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<%\r\n @entry = ::Entry.new\r\n @admin_entry = Admin::Entry.new\r\n%>\r\n\r\n<%= form_for(@entry, url: admin_entries_path) do %>\r\n<% end %>\r\n\r\n\r\n<%= form_for([:admin, @entry]) do %>\r\n<% end %>\r\n\r\n\r\n<%= form_for(@admin_entry) do %>\r\n<% end %><\/pre>\n
active_decorator\u3068\u76f8\u6027\u304c\u826f\u3044\uff01<\/h3>\n
\n\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3068\u3001\u30e2\u30c7\u30eb\u30681\u5bfe1\u3067\u5bfe\u5fdc\u3059\u308b\u30c7\u30b3\u30fc\u30ec\u30fc\u30bf\uff08\u30e2\u30b8\u30e5\u30fc\u30eb\uff09\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3001\u30d3\u30e5\u30fc\u5074\u306e\u30ed\u30b8\u30c3\u30af\u3092\u5207\u308a\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\u30e2\u30c7\u30eb\u3092\u6a5f\u80fd\u5358\u4f4d\u3067\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u7ba1\u7406\u6a5f\u80fd\u3068\u516c\u958b\u6a5f\u80fd\u3067\u5225\u3005\u306e\u30c7\u30b3\u30fc\u30ec\u30fc\u30bf\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\nkaminari<\/a>\u3092\u306f\u3058\u3081\u3001\u3044\u3064\u3082\u4f7f\u3044\u3084\u3059\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002<\/p>\n\u30e2\u30b8\u30e5\u30fc\u30eb\u5206\u5272\u306b\u3088\u308b\u554f\u984c\u70b9<\/h2>\n
development\u30e2\u30fc\u30c9\u3067\u306e\u554f\u984c<\/h3>\n
\n\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306fPublic\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u4e2d\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001Entry\u306fPublic::Entry\u30af\u30e9\u30b9\u3068\u3057\u3066\u89e3\u6c7a\u3055\u308c\u308b\u3053\u3068\u3092\u671f\u5f85\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\n\u203b\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u5f8c\u3005active_modularity\u306e\u521d\u671f\u5316\u306b\u5229\u7528\u3059\u308b\u306e\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306factive_modularity.rb\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n# config\/initializers\/active_modularity.rb\r\n\r\n# load all files (Must be loaded the models and controllers.)\r\nMyBlog::Application.eager_load! unless Rails.configuration.cache_classes<\/pre>\n
\n\u3057\u304b\u3057\u3001\u8d77\u52d5\u4e2d\u306b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u8ffd\u52a0\u3057\u305f\u5834\u5408\u306f\u554f\u984c\u304c\u767a\u751f\u3059\u308b\u4e8b\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u5834\u5408\u306fRails\u3092\u518d\u8d77\u52d5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\u95a2\u9023\uff08Association\uff09\u5148\u30af\u30e9\u30b9\u306e\u554f\u984c<\/h3>\n
\nhas_many\u3084belongs_to\u306e\u95a2\u9023\u306f\u6a5f\u80fd\u9593\u3067\u7570\u306a\u308b\u3053\u3068\u304c\u7121\u3044\u305f\u3081\u3001\u57fa\u5e95\u30e2\u30c7\u30eb\u306b\u8a18\u8ff0\u3059\u308b\u306e\u304c\u6b63\u3057\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n# app\/models\/entry.rb\r\nclass Entry < ActiveRecord::Base\r\n has_many :comments\r\nend\r\n\r\n# app\/models\/comment.rb\r\nclass Comment < ActiveRecord::Base\r\n belongs_to :entry\r\nend\r\n\r\n# app\/models\/public\/comment.rb\r\nmodule Public\r\n class Comment < ::Comment\r\n end\r\nend\r\n\r\n# app\/models\/admin\/comment.rb\r\nmodule Admin\r\n class Comment < ::Comment\r\n end\r\nend<\/pre>\n
> admin_entry = Admin::Entry.first\r\n> comment = admin_entry.comments.build\r\n> comment.class\r\n=> Comment(id: integer, ...) # \u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306eComment\u30af\u30e9\u30b9\u306b\u306a\u3063\u3066\u3057\u307e\u3046<\/pre>\n
\u5358\u4e00\u30c6\u30fc\u30d6\u30eb\u7d99\u627f\uff08Single Table Inheritance\uff09\u30af\u30e9\u30b9\u306e\u554f\u984c<\/h3>\n
\n\u30e6\u30fc\u30b6\u306b\u306f\u30ec\u30d3\u30e5\u30fc\u30ef\u3068\u8457\u8005\u3068\u3044\u3046\u7a2e\u985e\u304c\u3042\u308a\u3001\u5358\u4e00\u30c6\u30fc\u30d6\u30eb\u7d99\u627f\u3092\u5229\u7528\u3057\u3066\u5b9f\u88c5\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002<\/p>\nclass User < ActiveRecord::Base\r\nend\r\n\r\nclass Reviewer < User\r\nend\r\n\r\nclass Author < User\r\nend\r\n\r\nmodule Public\r\n class User < ::User\r\n end\r\n\r\n class Reviewer < ::Reviewer\r\n end\r\n\r\n class Author < ::Author\r\n end\r\nend\r\n\r\nmodule Admin\r\n class User < ::User\r\n end\r\n\r\n class Reviewer < ::Reviewer\r\n end\r\n\r\n class Author < ::Author\r\n end\r\nend<\/pre>\n
\n\u307e\u305f\u3001Public::User\u30af\u30e9\u30b9\u3067\u306efind\u306f\u5168\u3066\u306etype\u3092\u5bfe\u8c61\u306b\u3057\u305f\u3044\u306e\u3067\u3059\u304c\u3001type\u5c5e\u6027\u304cPublic::User\u306e\u30ec\u30b3\u30fc\u30c9\u3060\u3051\u3092\u691c\u7d22\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n> Admin::Author.new.type\r\n=> \"Admin::Author\"\r\n> Public::User.scoped.to_sql\r\n=> SELECT \"users\".* FROM \"users\" WHERE \"users\".\"type\" IN ('Public::User')<\/pre>\n
active_modularity\u306e\u52b9\u679c<\/h2>\n
active_modularity\u306e\u5c0e\u5165<\/h3>\n
# Gemfile\u306b\u4ee5\u4e0b\u306e\u4e00\u884c\u3092\u8ffd\u52a0\r\ngem 'active_modularity'\r\nbundle install<\/pre>\n
# config\/initializers\/active_modularity.rb\r\n\r\n# enable active modurality\r\nActiveRecord::Base.acts_as_modurality\r\n\r\n# load all files (Must be loaded the models and controllers.)\r\nMyBlog::Application.eager_load! unless Rails.configuration.cache_classes<\/pre>\n
Association\u5148\u30af\u30e9\u30b9\u304c\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306b\u306a\u308b<\/h3>\n
> admin_entry = Admin::Entry.first\r\n> comment = admin_entry.comments.build\r\n> comment.class\r\n=> Admin::Comment(id: integer, ...) # \u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u4e0b\u306e\u30af\u30e9\u30b9\u306b\u306a\u3063\u305f\uff01<\/pre>\n
\u5358\u4e00\u30c6\u30fc\u30d6\u30eb\u7d99\u627f\u306e\u30af\u30e9\u30b9\u304c\u5909\u308f\u308b<\/h3>\n
# \u4f5c\u6210\u3057\u305f\u30ec\u30b3\u30fc\u30c9\u306etype\u306b\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u4ed8\u304b\u306a\u3044\r\n> author = Admin::Author.create(name: \"taro\")\r\n> author.type\r\n=> \"Author\"\r\n> author.class\r\n=> Admin::Author\r\n\r\n# \u691c\u7d22\u5f8c\u306e\u30af\u30e9\u30b9\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u4ee5\u4e0b\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u306a\u308b\r\n> Public::User.scoped.to_sql\r\n=> SELECT \"users\".* FROM \"users\" WHERE \"users\".\"type\"\r\n> user = Public::User.last\r\n> user.type\r\n=> \"Author\"\r\n> user.class\r\n=> Public::Author\r\n> user.id = author.id\r\n=> true\r\n\r\n# \u30d9\u30fc\u30b9\u306e\u30af\u30e9\u30b9\u540d\u3068\u540c\u3058\u540d\u524d\u306e\u30af\u30e9\u30b9\u3067\u306ftype\u304cnil\u3068\u306a\u308b\r\n> Public::User.new.type\r\n=> nil<\/pre>\n
\n
\u307e\u3068\u3081<\/h2>\n
\n\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u5206\u5272\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u306b\u3088\u3063\u3066\u3001\u5b9f\u88c5\u3092\u6a5f\u80fd\u3054\u3068\u306b\u5206\u96e2\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u305c\u3072\u4e00\u5ea6\u304a\u8a66\u3057\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\n\u5b9f\u306f\u540c\u3058\u65e5\u306b active_modularity \u3068\u3044\u3046Gem\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u516c\u958b\u3057\u307e\u3057\u305f\u3002
\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[96,22,24,95],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2552"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2552"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2552\/revisions"}],"predecessor-version":[{"id":17936,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2552\/revisions\/17936"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}