Skip to content

Commit

Permalink
Add a callout to the survey component management (#11)
Browse files Browse the repository at this point in the history
* add views

* remove google chrome

* remove google chrome
  • Loading branch information
microstudi authored Apr 3, 2024
1 parent dc3b94f commit 4dd10fc
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ jobs:
- run: bundle exec rubocop -P
name: Lint Ruby files

# - run: bundle exec erblint app/**/*.erb
# name: Lint ERB files
- run: bundle exec erblint app/**/*.erb
name: Lint ERB files
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ jobs:
with:
chromedriver-version: 119.0.6045.105

- name: List Chrome
shell: "bash"
run: apt list --installed | grep chrome

- name: Remove Chrome
shell: "bash"
run: sudo apt remove google-chrome-stable

- uses: browser-actions/setup-chrome@v1
with:
chrome-version: 119.0.6045.105

- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
Expand Down
1 change: 1 addition & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SimpleCov.start do
root ENV.fetch("ENGINE_ROOT", nil)

add_filter "lib/decidim/anonymous_codes/version.rb"
add_filter "lib/decidim/anonymous_codes/test"
add_filter "/spec"
end

Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PATH
decidim-admin (>= 0.27.0, < 0.28)
decidim-core (>= 0.27.0, < 0.28)
decidim-surveys (>= 0.27.0, < 0.28)
deface (>= 1.9.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -331,6 +332,12 @@ GEM
declarative-builder (0.1.0)
declarative-option (< 0.2.0)
declarative-option (0.1.0)
deface (1.9.0)
actionview (>= 5.2)
nokogiri (>= 1.6)
polyglot
railties (>= 5.2)
rainbow (>= 2.1.0)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
devise (4.9.3)
Expand Down Expand Up @@ -564,6 +571,7 @@ GEM
pg_search (2.3.6)
activerecord (>= 5.2)
activesupport (>= 5.2)
polyglot (0.3.5)
premailer (1.21.0)
addressable
css_parser (>= 1.12.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Decidim::AnonymousCodes.configure do |config|
# The generator to use for the codes (defaults to an alphanumeric uppercase string of length "default_token_length")
# if you customize this, the default_token_length will be ignored
def self.token_generator
SecureRandom.hex(25)
SecureRandom.hex(25)
end
end
```
Expand Down
5 changes: 5 additions & 0 deletions app/models/decidim/anonymous_codes/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ class Group < ApplicationRecord
throw(:abort) if tokens.where(usage_count: 1..Float::INFINITY).count.positive?
end

belongs_to :organization, class_name: "Decidim::Organization", foreign_key: "decidim_organization_id"
has_many :tokens, class_name: "Decidim::AnonymousCodes::Token", dependent: :destroy
belongs_to :resource, polymorphic: true, optional: true

def self.for(organization)
where(organization: organization)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- insert_after "erb[loud]:contains('settings_attribute_input')" -->

<% if manifest.name == :surveys && field_name == :allow_unregistered %>
<%= render "decidim/anonymous_codes/admin/surveys_component_settings/callout", resource: Decidim::Surveys::Survey.find_by(component: @component) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% groups = Decidim::AnonymousCodes::Group.for(@component.organization).where(resource: resource) %>
<% if groups.any? %>
<div class="callout alert">
<p><strong><%= t(".group_locked") %></strong></p>
<ul>
<% groups.each do |group| %>
<li><%= link_to translated_attribute(group.title), "##{group.id}" %></li>
<% end %>
</ul>
<p class="help-text"><%= t(".group_locked_desc") %></p>
</div>
<% else %>
<div class="callout warning">
<p><strong><%= t(".group_unlocked") %></strong></p>
<p><%= link_to t(".new_group"), "#" %></p>
<p class="help-text"><%= t(".group_unlocked_desc") %></p>
</div>
<% end %>
15 changes: 15 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
---
en:
decidim:
anonymous_codes:
admin:
surveys_component_settings:
callout:
group_locked: This survey can only be answered by using a valid code.
group_locked_desc: Note that, unless "Allow unregistered users to answer"
is enabled, only registered users can answer this survey (and they also
will have to provide the code).
group_unlocked: Anyone can answer this survey, but you can choose to restrict
this by using a answer codes.
group_unlocked_desc: Note that "Allow unregistered users to answer" will
still apply if you choose to use this option.
groups: 'Edit groups:'
new_group: "\U0001F449 Create answer codes here"
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def change
t.boolean :active, default: true, null: false
t.integer :max_reuses, default: 1, null: false
t.references :resource, polymorphic: true, null: true, index: { name: "decidim_anonymous_codes_groups_on_resource" }
t.references :decidim_organization, null: false, foreign_key: true, index: { name: "decidim_anonymous_codes_groups_on_organization" }

t.timestamps
end
end
Expand Down
33 changes: 33 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

if !Rails.env.production? || ENV.fetch("SEED", nil)
print "Creating seeds for decidim_anonymous_codes...\n" unless Rails.env.test?

organization = Decidim::Organization.first

group1 = Decidim::AnonymousCodes::Group.create!(
title: { en: "First Group" },
organization: organization,
expires_at: 1.week.from_now,
active: true,
max_reuses: 1,
resource: Decidim::Surveys::Survey.first
)

group2 = Decidim::AnonymousCodes::Group.create!(
title: { en: "Second Group" },
organization: organization,
expires_at: 1.week.from_now,
active: true,
max_reuses: 1,
resource: Decidim::Surveys::Survey.second
)

5.times do
Decidim::AnonymousCodes::Token.create!(token: Decidim::AnonymousCodes.token_generator, usage_count: 0, group: group1)
end

25.times do
Decidim::AnonymousCodes::Token.create!(token: Decidim::AnonymousCodes.token_generator, usage_count: 0, group: group2)
end
end
1 change: 1 addition & 0 deletions decidim-anonymous_codes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "decidim-admin", Decidim::AnonymousCodes::COMPAT_DECIDIM_VERSION
spec.add_dependency "decidim-core", Decidim::AnonymousCodes::COMPAT_DECIDIM_VERSION
spec.add_dependency "decidim-surveys", Decidim::AnonymousCodes::COMPAT_DECIDIM_VERSION
spec.add_dependency "deface", ">= 1.9.0"

spec.add_development_dependency "decidim-dev", Decidim::AnonymousCodes::COMPAT_DECIDIM_VERSION
spec.metadata["rubygems_mfa_required"] = "true"
Expand Down
2 changes: 2 additions & 0 deletions lib/decidim/anonymous_codes/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "deface"

module Decidim
module AnonymousCodes
# This is the engine that runs on the public interface of decidim-anonymous_codes.
Expand Down
3 changes: 2 additions & 1 deletion lib/decidim/anonymous_codes/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
FactoryBot.define do
factory :anonymous_codes_group, class: "Decidim::AnonymousCodes::Group" do
title { { en: "Group title" } }
organization { create(:organization) }
expires_at { 1.week.from_now }
active { true }
max_reuses { 1 }
resource { create(:survey) }
end

factory :anonymous_codes_token, class: "Decidim::AnonymousCodes::Token" do
token { SecureRandom.uuid }
token { Decidim::AnonymousCodes.token_generator }
usage_count { 0 }
group { create(:anonymous_codes_group) }
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

require "decidim/core/test/factories"
require "decidim/surveys/test/factories"
require "decidim/proposals/test/factories"
require "decidim/anonymous_codes/test/factories"
5 changes: 5 additions & 0 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ module AnonymousCodes

describe "associations" do
it { expect(described_class.reflect_on_association(:tokens).macro).to eq(:has_many) }
it { expect(described_class.reflect_on_association(:organization).macro).to eq(:belongs_to) }
end

context "when tokens exist" do
let!(:token) { create(:anonymous_codes_token, group: group) }

it "has an organization" do
expect(group.organization).to be_a(Decidim::Organization)
end

it "destroys the tokens when destroyed" do
expect { group.destroy }.to change(Token, :count).by(-1)
end
Expand Down
47 changes: 47 additions & 0 deletions spec/system/admin/surveys_component_settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "spec_helper"

describe "Surveys Component Settings", type: :system do
let(:organization) { component.organization }
let(:component) { survey.component }
let(:user) { create :user, :admin, :confirmed, organization: organization }
let(:group) { create :anonymous_codes_group, organization: organization, resource: resource }
let!(:token) { create :anonymous_codes_token, group: group }
let!(:survey) { create(:survey) }
let(:resource) { nil }

def visit_component
visit Decidim::EngineRouter.admin_proxy(component.participatory_space).edit_component_path(component.id)
end

before do
switch_to_host(organization.host)
login_as user, scope: :user
visit_component
end

it "has callout" do
within ".callout.warning" do
expect(page).to have_content("Create answer codes here")
end
end

context "when codes are available" do
let(:resource) { survey }

it "has callout" do
within ".callout.alert" do
expect(page).to have_content("This survey can only be answered by using a valid code")
end
end
end

context "when not a surveys component" do
let(:component) { create :proposal_component }

it "has no callout" do
expect(page).not_to have_css(".callout")
end
end
end

0 comments on commit 4dd10fc

Please sign in to comment.