Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generalize extra fields table for future uses #247

Merged
merged 12 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tests-legacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
features: enabled
- rspec: system/*_spec.rb
features: enabled
- rspec: system/awesome_map
features: enabled
fail-fast: false

services:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
features: enabled
- rspec: system/*_spec.rb
features: enabled
- rspec: system/awesome_map
features: enabled
fail-fast: false

services:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,10 @@ And then execute:
```bash
bundle
bundle exec rails decidim_decidim_awesome:install:migrations
bundle exec rails decidim_decidim_awesome:webpacker:install
bundle exec rails decidim:upgrade
bundle exec rails db:migrate
```

> NOTE: the `decidim_decidim_awesome:webpacker:install` is only necessary for Decidim versions starting at 0.25.

If you are upgrading from a version prior to 0.8, make sure to visit the URL `/admin/decidim_awesome/checks` and run image migrations for the old images:

![Check image migrations](examples/check_image_migrations.png)
Expand All @@ -316,12 +314,13 @@ RAILS_ENV=production bin/rails decidim_awesome:active_storage_migrations:check_m
```

The correct version of Decidim Awesome should resolved automatically by the Bundler.
However you can force some specific version using `gem "decidim-decidim_awesome", "~> 0.8.0"` in the Gemfile.
However you can force some specific version using `gem "decidim-decidim_awesome", "~> 0.10.0"` in the Gemfile.

Depending on your Decidim version, choose the corresponding Awesome version to ensure compatibility:

| Awesome version | Compatible Decidim versions |
|---|---|
| 0.10.0 | >= 0.26.7, >= 0.27.3 |
| 0.9.2 | >= 0.26.7, >= 0.27.3 |
| 0.9.x | 0.26.x, 0.27.x |
| 0.8.x | 0.25.x, 0.26.x |
Expand All @@ -330,6 +329,7 @@ Depending on your Decidim version, choose the corresponding Awesome version to e
| 0.5.x | 0.21.x, 0.22.x |

> *Heads up!*
> * version 0.10.0 requires database migrations! Don't forget the migrations step when updating.
> * version 0.8.0 removes CSS Themes for tenants. If you have been using them you will have to manually migrate them to custom styles.
> * version 0.8.0 uses ActiveStorage, same as Decidim 0.25. 2 new rake task have been introduced to facilitate the migration: `bin/rails decidim_awesome:active_storage_migrations:check_migration_from_carrierwave` and
`bin/rails decidim_awesome:active_storage_migrations:migrate_from_carrierwave`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def cache_hash
hash << I18n.locale.to_s
hash << model.cache_key_with_version
hash << model.proposal_votes_count
hash << model.weight_cache&.totals
hash << model.extra_fields&.vote_weight_totals
hash << model.endorsements_count
hash << model.comments_count
hash << Digest::MD5.hexdigest(model.component.cache_key_with_version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module Decidim
module DecidimAwesome
module HasWeightCache
module HasProposalExtraFields
extend ActiveSupport::Concern

included do
has_one :weight_cache, foreign_key: "decidim_proposal_id", class_name: "Decidim::DecidimAwesome::WeightCache", dependent: :destroy
has_one :extra_fields, foreign_key: "decidim_proposal_id", class_name: "Decidim::DecidimAwesome::ProposalExtraField", dependent: :destroy

def weight_count(weight)
(weight_cache && weight_cache.totals[weight.to_s]) || 0
(extra_fields && extra_fields.vote_weight_totals[weight.to_s]) || 0
end

def vote_weights
Expand All @@ -25,14 +25,14 @@ def all_vote_weights
end

def update_vote_weights!
weight_cache ||= Decidim::DecidimAwesome::WeightCache.find_or_initialize_by(proposal: self)
weight_cache.totals = {}
extra_fields ||= Decidim::DecidimAwesome::ProposalExtraField.find_or_initialize_by(proposal: self)
extra_fields.vote_weight_totals = {}
votes.each do |vote|
weight_cache.totals[vote.weight] ||= 0
weight_cache.totals[vote.weight] += 1
extra_fields.vote_weight_totals[vote.weight] ||= 0
extra_fields.vote_weight_totals[vote.weight] += 1
end
weight_cache.save!
self.weight_cache = weight_cache
extra_fields.save!
self.extra_fields = extra_fields
@vote_weights = nil
@all_vote_weights = nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Decidim
module DecidimAwesome
class WeightCache < ApplicationRecord
self.table_name = "decidim_awesome_weight_caches"
class ProposalExtraField < ApplicationRecord
self.table_name = "decidim_awesome_proposal_extra_fields"

belongs_to :proposal, foreign_key: "decidim_proposal_id", class_name: "Decidim::Proposals::Proposal"
end
Expand Down
16 changes: 8 additions & 8 deletions app/models/decidim/decidim_awesome/vote_weight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class VoteWeight < ApplicationRecord
after_save :update_vote_weight_totals!

def update_vote_weight_totals!
cache = Decidim::DecidimAwesome::WeightCache.find_or_initialize_by(proposal: proposal)
cache.totals = cache.totals || {}
extra = Decidim::DecidimAwesome::ProposalExtraField.find_or_initialize_by(proposal: proposal)
extra.vote_weight_totals = extra.vote_weight_totals || {}

prev = weight_previous_change&.first
if prev.present?
cache.totals[prev.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight: prev).count
cache.totals.delete(prev.to_s) if cache.totals[prev.to_s].zero?
extra.vote_weight_totals[prev.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight: prev).count
extra.vote_weight_totals.delete(prev.to_s) if extra.vote_weight_totals[prev.to_s].zero?
end
cache.totals[weight.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight: weight).count
cache.totals.delete(weight.to_s) if cache.totals[weight.to_s].zero?
cache.weight_total = cache.totals.inject(0) { |sum, (weight, count)| sum + (weight.to_i * count) }
cache.save!
extra.vote_weight_totals[weight.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight: weight).count
extra.vote_weight_totals.delete(weight.to_s) if extra.vote_weight_totals[weight.to_s].zero?
extra.weight_total = extra.vote_weight_totals.inject(0) { |sum, (weight, count)| sum + (weight.to_i * count) }
extra.save!
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

class CreateDecidimAwesomeWeightCaches < ActiveRecord::Migration[6.0]
class CreateDecidimAwesomeProposalExtraFields < ActiveRecord::Migration[6.0]
def change
create_table :decidim_awesome_weight_caches do |t|
create_table :decidim_awesome_proposal_extra_fields do |t|
# this might be polymorphic in the future (if other types of votes are supported)
t.references :decidim_proposal, null: false, index: { name: "decidim_awesome_proposals_weights_cache" }
t.references :decidim_proposal, null: false, index: { name: "decidim_awesome_extra_fields_on_proposal" }

t.jsonb :totals
t.jsonb :vote_weight_totals
t.integer :weight_total, default: 0
t.timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/decidim_awesome/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Engine < ::Rails::Engine
# add vote weight to proposal vote
Decidim::Proposals::ProposalVote.include(Decidim::DecidimAwesome::HasVoteWeight)
# add vote weight cache to proposal
Decidim::Proposals::Proposal.include(Decidim::DecidimAwesome::HasWeightCache)
Decidim::Proposals::Proposal.include(Decidim::DecidimAwesome::HasProposalExtraFields)
Decidim::Proposals::ProposalSerializer.include(Decidim::DecidimAwesome::ProposalSerializerOverride)
Decidim::Proposals::ProposalType.include(Decidim::DecidimAwesome::ProposalTypeOverride)
Decidim::Proposals::ProposalMCell.include(Decidim::DecidimAwesome::ProposalMCellOverride)
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/decidim_awesome/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
sequence(:weight) { |n| n }
end

factory :awesome_weight_cache, class: "Decidim::DecidimAwesome::WeightCache" do
factory :awesome_proposal_extra_fields, class: "Decidim::DecidimAwesome::ProposalExtraField" do
proposal { create :proposal }

trait :with_votes do
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/decidim_awesome_upgrade_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Rake::Task["decidim:webpacker:install"].enhance do
Rake::Task["decidim_decidim_awesome:webpacker:install"].invoke
end

Rake::Task["decidim:webpacker:upgrade"].enhance do
Rake::Task["decidim_decidim_awesome:webpacker:install"].invoke
end
2 changes: 1 addition & 1 deletion spec/cells/voting/voting_cards_base_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Voting
let(:user) { create(:user, :confirmed, organization: organization) }
let!(:component) { create :proposal_component, :with_votes_enabled, organization: organization, settings: { awesome_voting_manifest: manifest } }
let(:proposal) { create(:proposal, component: component) }
let!(:weight_cache) { create(:awesome_weight_cache, proposal: proposal) }
let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: proposal) }
let!(:vote_weights) do
[
create_list(:awesome_vote_weight, 3, vote: create(:proposal_vote, proposal: proposal), weight: 1),
Expand Down
2 changes: 1 addition & 1 deletion spec/cells/voting/voting_cards_counter_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Voting
let(:user) { create(:user, :confirmed, organization: organization) }
let!(:component) { create :proposal_component, :with_votes_enabled, organization: organization, settings: { awesome_voting_manifest: manifest } }
let(:proposal) { create(:proposal, component: component) }
let!(:weight_cache) { create(:awesome_weight_cache, proposal: proposal) }
let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: proposal) }
let!(:vote_weights) do
[
create_list(:awesome_vote_weight, 3, vote: create(:proposal_vote, proposal: proposal), weight: 1),
Expand Down
2 changes: 1 addition & 1 deletion spec/cells/voting/voting_cards_proposal_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Voting
let(:user) { create(:user, :confirmed, organization: organization) }
let!(:component) { create :proposal_component, :with_votes_enabled, organization: organization, settings: { awesome_voting_manifest: manifest } }
let(:proposal) { create(:proposal, component: component) }
let!(:weight_cache) { create(:awesome_weight_cache, proposal: proposal) }
let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: proposal) }
let!(:vote_weights) do
[
create_list(:awesome_vote_weight, 3, vote: create(:proposal_vote, proposal: proposal), weight: 1),
Expand Down
Loading
Loading