Files
grecia/spec/models/concerns/reportable.rb
Javi Martín f8e6e98d3a Define stats and result permissions with scopes
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.
2019-11-09 19:34:21 +01:00

106 lines
3.2 KiB
Ruby

shared_examples "reportable" do
let(:reportable) { create(model_name(described_class)) }
describe "scopes" do
describe ".results_enabled" do
it "includes records with results enabled" do
reportable.update!(results_enabled: true)
expect(described_class.results_enabled).to eq [reportable]
end
it "does not include records without results enabled" do
reportable.update!(results_enabled: false)
expect(described_class.results_enabled).to be_empty
end
end
describe ".stats_enabled" do
it "includes records with stats enabled" do
reportable.update!(stats_enabled: true)
expect(described_class.stats_enabled).to eq [reportable]
end
it "does not include records without stats enabled" do
reportable.update!(stats_enabled: false)
expect(described_class.stats_enabled).to be_empty
end
end
end
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
skip "there's no original column" unless reportable.has_attribute?(:results_enabled)
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
skip "there's no original column" unless reportable.has_attribute?(:stats_enabled)
reportable.update!(stats_enabled: true)
expect(reportable.read_attribute(:stats_enabled)).to be false
end
end
end