When defining abilities, scopes cover more cases because they can be used to check permissions for a record and to filter a collection. Ruby blocks can only be used to check permissions for a record. Note the `Budget::Phase.kind_or_later` name sounds funny, probably because we use the word "phase" for both an an attribute in the budgets table and an object associated with the budget, and so naming methods for a budget phase is a bit tricky.
28 lines
629 B
Ruby
28 lines
629 B
Ruby
module Reportable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
has_one :report, as: :process, inverse_of: :process, dependent: :destroy
|
|
accepts_nested_attributes_for :report
|
|
|
|
Report::KINDS.each do |kind|
|
|
scope "#{kind}_enabled", -> { joins(:report).where("reports.#{kind}": true) }
|
|
end
|
|
end
|
|
|
|
def report
|
|
super || build_report
|
|
end
|
|
|
|
Report::KINDS.each do |kind|
|
|
define_method "#{kind}_enabled?" do
|
|
report.send(kind)
|
|
end
|
|
alias_method "#{kind}_enabled", "#{kind}_enabled?"
|
|
|
|
define_method "#{kind}_enabled=" do |enabled|
|
|
report.send("#{kind}=", enabled)
|
|
end
|
|
end
|
|
end
|