Create reports

This table will store which reports (stats, results, ...) will be shown
for a certain process (polls, budgets, ...).

Note Rails fails to save a poll and its report when both are new records
if we add a `validate :process, presence: true` rule. Since it caused a
lot of trouble when creating records for tests during factories rule
completely. Instead, I've created the `results_enabled=` and
`stats_enabled=` methods, so tests are easier to set up, while also
automatically creating a report if it doesn't already exist. This also
decouples form structure and database implemenation.

Originally I named this table `enabled_reports` and instead of having
`stats` and `results` columns, it had an `enabled` column and a `kind`
column, which would be set to "stats" or "results". However, although
that table would allow us to add arbitrary reports easily, I found the
way we had to handle the `has_many` relationship was a bit too complex.
This commit is contained in:
Javi Martín
2019-04-29 14:13:46 +02:00
parent 2d29243a9e
commit 354b183e17
13 changed files with 136 additions and 11 deletions

View File

@@ -0,0 +1,71 @@
shared_examples "reportable" do
let(:reportable) { create(model_name(described_class)) }
describe "#results_enabled" do
it "can write and read the attribute" do
reportable.results_enabled = true
expect(reportable.results_enabled?).to be true
expect(reportable.results_enabled).to be true
reportable.results_enabled = false
expect(reportable.results_enabled?).to be false
expect(reportable.results_enabled).to be false
end
it "can save the value to the database" do
reportable.update(results_enabled: true)
saved_reportable = described_class.last
expect(saved_reportable.results_enabled?).to be true
expect(saved_reportable.results_enabled).to be true
reportable.update(results_enabled: false)
saved_reportable = described_class.last
expect(saved_reportable.results_enabled?).to be false
expect(saved_reportable.results_enabled).to be false
end
it "uses the `has_one` relation instead of the original column" do
reportable.update(results_enabled: true)
expect(reportable.read_attribute(:results_enabled)).to be false
end
end
describe "#stats_enabled" do
it "can write and read the attribute" do
reportable.stats_enabled = true
expect(reportable.stats_enabled?).to be true
expect(reportable.stats_enabled).to be true
reportable.stats_enabled = false
expect(reportable.stats_enabled?).to be false
expect(reportable.stats_enabled).to be false
end
it "can save the attribute to the database" do
reportable.update(stats_enabled: true)
saved_reportable = described_class.last
expect(saved_reportable.stats_enabled?).to be true
expect(saved_reportable.stats_enabled).to be true
reportable.update(stats_enabled: false)
saved_reportable = described_class.last
expect(saved_reportable.stats_enabled?).to be false
expect(saved_reportable.stats_enabled).to be false
end
it "uses the `has_one` relation instead of the original column" do
reportable.update(stats_enabled: true)
expect(reportable.read_attribute(:stats_enabled)).to be false
end
end
end