Use metaprogramming for report methods

We're going to add an `advanced_stats` report, and having 3 identical
sets of methods would be too much duplication.
This commit is contained in:
Javi Martín
2019-04-29 14:21:38 +02:00
parent 354b183e17
commit 1c2e38ea00
2 changed files with 10 additions and 15 deletions

View File

@@ -10,21 +10,14 @@ module Reportable
super || build_report super || build_report
end end
def results_enabled? Report::KINDS.each do |kind|
report&.results? define_method "#{kind}_enabled?" do
report.send(kind)
end end
alias_method :results_enabled, :results_enabled? alias_method "#{kind}_enabled", "#{kind}_enabled?"
def stats_enabled? define_method "#{kind}_enabled=" do |enabled|
report&.stats? report.send("#{kind}=", enabled)
end end
alias_method :stats_enabled, :stats_enabled?
def results_enabled=(enabled)
report.results = enabled
end
def stats_enabled=(enabled)
report.stats = enabled
end end
end end

View File

@@ -1,3 +1,5 @@
class Report < ApplicationRecord class Report < ApplicationRecord
KINDS = %i[results stats]
belongs_to :process, polymorphic: true belongs_to :process, polymorphic: true
end end