Skip to content

Commit

Permalink
Trace version authors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcasals committed Nov 17, 2017
1 parent 214197b commit 5a6dec7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
Expand Up @@ -30,7 +30,9 @@ def call
attr_reader :result

def create_result
@result = Result.create!(
@result = Decidim.traceability.create!(
Result,
@form.current_user,
feature: @form.current_feature,
scope: @form.scope,
category: @form.category,
Expand Down
Expand Up @@ -35,7 +35,9 @@ def call
attr_reader :result, :form

def update_result
result.update_attributes!(
Decidim.traceability.update!(
result,
form.current_user,
scope: @form.scope,
category: @form.category,
parent_id: @form.parent_id,
Expand Down
8 changes: 8 additions & 0 deletions decidim-accountability/spec/commands/create_result_spec.rb
Expand Up @@ -4,6 +4,7 @@

describe Decidim::Accountability::Admin::CreateResult do
let(:organization) { create :organization, available_locales: [:en] }
let(:user) { create :user, organization: organization }
let(:participatory_process) { create :participatory_process, organization: organization }
let(:current_feature) { create :accountability_feature, participatory_space: participatory_process }
let(:scope) { create :scope, organization: organization }
Expand Down Expand Up @@ -36,6 +37,7 @@
let(:form) do
double(
invalid?: invalid,
current_user: user,
current_feature: current_feature,
title: { en: "title" },
description: { en: "description" },
Expand Down Expand Up @@ -73,6 +75,12 @@
expect(result.scope).to eq scope
end

it "creates a new version for the result", versioning: true do
subject.call
expect(result.versions.count).to eq 1
expect(result.versions.last.whodunnit).to eq user.to_gid.to_s
end

it "sets the category" do
subject.call
expect(result.category).to eq category
Expand Down
9 changes: 9 additions & 0 deletions decidim-accountability/spec/commands/update_result_spec.rb
Expand Up @@ -4,6 +4,7 @@

describe Decidim::Accountability::Admin::UpdateResult do
let(:result) { create :result }
let(:user) { create :user, organization: organization }
let(:organization) { result.feature.organization }
let(:scope) { create :scope, organization: organization }
let(:category) { create :category, participatory_space: participatory_process }
Expand Down Expand Up @@ -36,6 +37,7 @@
let(:form) do
double(
invalid?: invalid,
current_user: user,
title: { en: "title" },
description: { en: "description" },
proposal_ids: proposals.map(&:id),
Expand Down Expand Up @@ -66,6 +68,13 @@
expect(translated(result.title)).to eq "title"
end

it "creates a new version for the result", versioning: true do
expect do
subject.call
end.to change { result.versions.count }.by(1)
expect(result.versions.last.whodunnit).to eq user.to_gid.to_s
end

it "sets the scope" do
subject.call
expect(result.scope).to eq scope
Expand Down
9 changes: 7 additions & 2 deletions decidim-core/lib/decidim/has_reference.rb
Expand Up @@ -28,12 +28,17 @@ def calculate_reference
Decidim.resource_reference_generator.call(self, feature)
end

# Internal: Sets the unique reference to the model.
# Internal: Sets the unique reference to the model. Note that if the resource
# implements `Decidim::Traceable` this method will not create a version.
#
# Returns nothing.
def store_reference
self[:reference] ||= calculate_reference
save if changed?
return unless changed?

# rubocop:disable Rails/SkipsModelValidations
update_column(:reference, self[:reference])
# rubocop:enable Rails/SkipsModelValidations
end
end
end
Expand Down

0 comments on commit 5a6dec7

Please sign in to comment.