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:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user