如何以编程方式覆盖 Ruport 数据表的格式化程序?

发布于 2024-12-04 08:24:58 字数 608 浏览 0 评论 0原文

我在 Rails 应用程序中有一个非常简单的 Ruport 设置,其中 Ruport 控制器传递一个 Report::Data::Table 实例:

class Reporter < Ruport::Controller
  stage :headline, :data, :footer
  required_option :report

def setup
    report_klass = options.report.report_model
    report_klass ||= Report
    self.data = report_klass.send(:report_table_by_sql, options.report.query)
  end
end

Data::Table 实例当要求渲染时,存储在 data 中的数据使用 Ruport::Data::Table 作为其委托控制器,因此当我稍后调用

output << data.to_html

How can I Tell data 将其渲染方法委托给记者类,所以我所有的钩子覆盖都可以存在在一个地方?

I have a pretty straight-forward Ruport setup in my Rails app, where the Ruport controller is passed a Report::Data::Table instance:

class Reporter < Ruport::Controller
  stage :headline, :data, :footer
  required_option :report

def setup
    report_klass = options.report.report_model
    report_klass ||= Report
    self.data = report_klass.send(:report_table_by_sql, options.report.query)
  end
end

The Data::Table instance that is stored in data uses Ruport::Data::Table as its delegated controller when asked to render, so that's what gets called when I later call

output << data.to_html

How can I tell data to delegate its rendering methods to the Reporter class, so all my hook overrides can live in one place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2024-12-11 08:24:58

Ruport 格式化程序被设计为封装在继承自 Formatter 的单独类中。
我相信类似的东西会实现你想要的:

# Inherits all the default build methods from the HTML Formatter
class ReporterTableHTMLFormatter < Ruport::Formatter::HTML
  # Sets this class to render Ruport Tables
  # (Data instead of Controller may also work, the code path is a bit unclear/overloaded)
  renders :html, :for => Ruport::Controller::Table

  # Build table row
  def build_row(data)
    ...
  end
end

Ruport formatters are designed to be encapsulated in a separate class that inherits from Formatter.
I believe something similar to this will achieve what you want:

# Inherits all the default build methods from the HTML Formatter
class ReporterTableHTMLFormatter < Ruport::Formatter::HTML
  # Sets this class to render Ruport Tables
  # (Data instead of Controller may also work, the code path is a bit unclear/overloaded)
  renders :html, :for => Ruport::Controller::Table

  # Build table row
  def build_row(data)
    ...
  end
end
昔梦 2024-12-11 08:24:58

Ruport 的 API 文档 明确表示您可以注册单个 Formatter 后代具有多个 Controller,因此,如果您想要一个实现所有挂钩的格式化程序,您可以简单地说:

class DualPurposeFormatter < Ruport::Formatter::HTML
  renders :html, :for => [Reporter, Ruport::Controller::Table]

  #for Reporter controller
  def build_headline
    #...
  end

  #for Ruport::Controller::Table
  def build_row(row_data)
    #...
  end
end

Ruport's API documentation makes it clear that you can register a single Formatter descendant with multiple Controllers, so if you want to have a single formatter that implements all the hooks, you can simply say as much:

class DualPurposeFormatter < Ruport::Formatter::HTML
  renders :html, :for => [Reporter, Ruport::Controller::Table]

  #for Reporter controller
  def build_headline
    #...
  end

  #for Ruport::Controller::Table
  def build_row(row_data)
    #...
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文