官术网_书友最值得收藏!

Model hooks

These hooks are used even less frequently than controller hooks but are being included here for completeness.

Model extension is better handled through the use of new methods or encapsulation of existing methods by means of the alias_method_chain pattern. For a summary of alias_method_chain see http://stackoverflow.com/a/3697391.

A common use-case for model hooks is the :model_project_copy_before_save hook as this can be used to replicate content from our plugin that belonged to a specific project if that project is copied:

module RedmineKnowledgebase
  class Hooks < Redmine::Hook::ViewListener
    def model_project_copy_before_save(context = {})
      source = context[:source_project]
      destination = context[:destination_project]

      if source.module_enabled?(:redmine_knowledgebase)
        # TODO: clone all categories
        # TODO: clone all articles
        # TODO: ensure cloned articles refer to cloned categories 
      end
    end
  end
end

The actual implementation has been left out in the preceding snippet, but placeholders have been left intact to illustrate what actions we could be taking.

主站蜘蛛池模板: 岚皋县| 泰顺县| 新余市| 通渭县| 皮山县| 鹤山市| 分宜县| 新营市| 香格里拉县| 山阳县| 朝阳县| 聂拉木县| 常宁市| 弋阳县| 礼泉县| 郯城县| 克拉玛依市| 四川省| 长子县| 阳信县| 宁乡县| 玉田县| 黄骅市| 亳州市| 苗栗市| 玉环县| 黄石市| 黄梅县| 青河县| 温泉县| 襄汾县| 盘锦市| 泗阳县| 浮梁县| 大悟县| 平罗县| 阳城县| 南涧| 盘锦市| 黎城县| 泾阳县|