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

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.

主站蜘蛛池模板: 菏泽市| 航空| 揭阳市| 新和县| 嘉禾县| 康定县| 葫芦岛市| 扶绥县| 喀什市| 台南市| 海林市| 旌德县| 通榆县| 永安市| 鄢陵县| 蓝山县| 昌邑市| 高安市| 望城县| 大足县| 交口县| 咸阳市| 蒙自县| 河源市| 商河县| 金寨县| 敖汉旗| 乌苏市| 新河县| 拉萨市| 溆浦县| 阿拉善左旗| 彭泽县| 阿巴嘎旗| 吴忠市| 东源县| 阜新| 吉木萨尔县| 洪雅县| 中山市| 渝中区|