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

Admin menu and controller sections #9

Merged
merged 59 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
93c6567
create admin_engine
ElviaBth Apr 3, 2024
9afcf35
add menu to admin
ElviaBth Apr 3, 2024
115b8f4
add admin menu translation
ElviaBth Apr 3, 2024
4dd27b8
add code_groups_controller
ElviaBth Apr 3, 2024
894f752
add admin template for view
ElviaBth Apr 3, 2024
183332c
add table to index
ElviaBth Apr 3, 2024
d0dd529
Merge branch 'main' into feature/admin_menu_and_controllers
microstudi Apr 3, 2024
306d829
update private method gruops
ElviaBth Apr 4, 2024
d8f307f
fix lint errors
ElviaBth Apr 4, 2024
583133a
change datetime format in index table
ElviaBth Apr 4, 2024
ed399c4
add action icons
ElviaBth Apr 4, 2024
6e8844a
refactor index table body content
ElviaBth Apr 4, 2024
4242352
fix index new_access_code_button
ElviaBth Apr 4, 2024
3385e98
add styles to icon_links
ElviaBth Apr 4, 2024
4973c30
add condition when migration is empty
ElviaBth Apr 4, 2024
212188e
add card title to edit and new views
ElviaBth Apr 4, 2024
eede624
update edit, form and new views
ElviaBth Apr 4, 2024
3978cc2
add edit and new method to controller
ElviaBth Apr 5, 2024
d0ee388
change action delete icon
ElviaBth Apr 5, 2024
5175578
add command and create method
ElviaBth Apr 5, 2024
a6a0238
add update command and update controller
ElviaBth Apr 5, 2024
3e6d477
add destroy method
ElviaBth Apr 5, 2024
c781002
refactor i18n variables
ElviaBth Apr 5, 2024
533bfb4
configure action preview
ElviaBth Apr 8, 2024
8cebf58
add pagination
ElviaBth Apr 8, 2024
f7f32f7
add command specs
ElviaBth Apr 8, 2024
057b69d
add controller spec
ElviaBth Apr 9, 2024
a61a1cb
test create, edit and update controller methods
ElviaBth Apr 9, 2024
e71bb33
add system access_code_group_spec
ElviaBth Apr 9, 2024
84ebcfc
replace Date.today for Time.zone.today in spec
ElviaBth Apr 9, 2024
8e31f4f
Merge branch 'main' into feature/admin_menu_and_controllers
ElviaBth Apr 9, 2024
5d99afa
update num of tockens in view
ElviaBth Apr 9, 2024
92a0e77
add validations to codegroupform
ElviaBth Apr 9, 2024
472559a
refactor create_code_group_spec
ElviaBth Apr 9, 2024
8195029
save refactored command test
ElviaBth Apr 9, 2024
510ce62
refactor update_code_group_spec
ElviaBth Apr 9, 2024
d510add
test edit case added to system spec
ElviaBth Apr 9, 2024
aecbe7a
refactor create_code_group command
ElviaBth Apr 10, 2024
5cd407d
refactor update_code_group_spec
ElviaBth Apr 10, 2024
8d81df7
add checks to access_code_groups_spec
ElviaBth Apr 10, 2024
eef32b9
refactor call command method
ElviaBth Apr 10, 2024
e810bbf
add code_group_form_spec
ElviaBth Apr 11, 2024
569f79e
refactor index view
ElviaBth Apr 11, 2024
38003ab
use factory in code_groups_controller_spec
ElviaBth Apr 11, 2024
9de5771
add i18n key
ElviaBth Apr 11, 2024
97b1b5e
add validations checks
ElviaBth Apr 11, 2024
274f99f
adds a new access_code_group with nil expiration date
ElviaBth Apr 11, 2024
4d7b8d9
add surveys helper_method
ElviaBth Apr 11, 2024
5f90ecd
add select to system test
ElviaBth Apr 11, 2024
bbfe588
put dates at the past
ElviaBth Apr 15, 2024
e64c355
refactor code_group_form_spec
ElviaBth Apr 15, 2024
b3fc479
add more checks to code_group_form_spec
ElviaBth Apr 15, 2024
25f03ff
use TimeWithZone instead LocalizeDate
ElviaBth Apr 15, 2024
4ffbcc2
fix form specs
microstudi Apr 15, 2024
f32fe24
Merge branch 'main' into feature/admin_menu_and_controllers
microstudi Apr 15, 2024
f73c77e
add screenshot
microstudi Apr 15, 2024
a89a969
add screenshot
microstudi Apr 15, 2024
df0e5b1
add usage
microstudi Apr 16, 2024
e67c8f2
new screenshot
microstudi Apr 16, 2024
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
40 changes: 40 additions & 0 deletions app/commands/decidim/anonymous_codes/admin/create_code_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
class CreateCodeGroup < Decidim::Command
def initialize(form, organization)
@form = form
@organization = organization
end

def call
return broadcast(:invalid) if @form.invalid?

transaction do
create_code_group
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
end

broadcast(:ok)
end

private

attr_reader :code_group, :form

def create_code_group
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
@code_group = Decidim.traceability.create!(
Group,
@form.current_user,
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
title: form.title,
expires_at: form.expires_at,
active: form.active,
max_reuses: form.max_reuses,
organization: @organization
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
)
end
end
end
end
end
39 changes: 39 additions & 0 deletions app/commands/decidim/anonymous_codes/admin/update_code_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
class UpdateCodeGroup < Decidim::Command
def initialize(form, code_group)
@form = form
@code_group = code_group
end

def call
return broadcast(:invalid) if @form.invalid?

transaction do
update_code_group
end

broadcast(:ok, @code_group)
end

private

attr_reader :code_group, :form

def update_code_group
Decidim.traceability.update!(
code_group,
form.current_user,
title: form.title,
expires_at: form.expires_at,
active: form.active,
max_reuses: form.max_reuses
)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
class ApplicationController < Decidim::Admin::ApplicationController
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
class CodeGroupsController < ApplicationController
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
helper_method :groups
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved

def index; end
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved

def new
@form = form(CodeGroupForm).instance
end

def create
@form = form(CodeGroupForm).from_params(params)

CreateCodeGroup.call(@form, current_organization) do
on(:ok) do
flash[:notice] = I18n.t("code_groups.create.success", scope: "decidim.anonymous_codes.admin")
redirect_to code_groups_path
end

on(:invalid) do
flash.now[:alert] = I18n.t("code_groups.create.invalid", scope: "decidim.anonymous_codes.admin")
render action: "new"
end
end
end

def edit
@form = form(CodeGroupForm).from_model(code_group)
end

def update
@form = form(CodeGroupForm).from_params(params)

UpdateCodeGroup.call(@form, code_group) do
on(:ok) do
flash[:notice] = I18n.t("code_groups.update.success", scope: "decidim.anonymous_codes.admin")
redirect_to code_groups_path
end

on(:invalid) do
flash.now[:alert] = I18n.t("code_groups.update.invalid", scope: "decidim.anonymous_codes.admin")
render action: "edit"
end
end
end

def destroy
Decidim.traceability.perform_action!("delete", code_group, current_user) do
code_group.destroy!
end

flash[:notice] = I18n.t("code_groups.destroy.success", scope: "decidim.anonymous_codes.admin")

redirect_to code_groups_path
end

private

def groups
AnonymousCodes::Group.for(current_organization)
end

def code_group
@code_group ||= groups.find(params[:id])
end
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
19 changes: 19 additions & 0 deletions app/forms/decidim/anonymous_codes/admin/code_group_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
class CodeGroupForm < Decidim::Form
include TranslatableAttributes

translatable_attribute :title, String
attribute :expires_at, Decidim::Attributes::LocalizedDate
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
attribute :active, Boolean, default: true
attribute :max_reuses, Integer, default: 1

validates :max_reuses, presence: true
validates :title, translatable_presence: true
end
end
end
end
23 changes: 23 additions & 0 deletions app/views/decidim/anonymous_codes/admin/code_groups/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="card">
<div class="card-divider">
<h2 class="card-title"><%= title %></h2>
</div>

<div class="card-section">
<div class="row column">
<%= form.translated :text_field, :title, label: t(".title") %>
</div>

<div class="row column">
<%= form.datetime_field :expires_at, label: t(".expires_at") %>
</div>

<div class="row column">
<%= form.check_box :active, label: t(".active") %>
</div>

<div class="row column">
<%= form.number_field :max_reuses, label: t(".max_reuses") %>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= decidim_form_for(@form, html: { class: "form edit_code_group" }) do |f| %>
<%= render partial: "form", object: f, locals: { title: t(".title") } %>

<div class="button--double form-general-submit">
<%= f.submit "update" %>
</div>
<% end %>
47 changes: 47 additions & 0 deletions app/views/decidim/anonymous_codes/admin/code_groups/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="card">
<div class="card-divider">
<h2 class="card-title">
Access code groups
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
<a class="button tiny button--title new-access" href="<%= new_code_group_path %>"><%= t("decidim.anonymous_codes.admin.code_groups.index.new_access_code_group_button") %></a>
</h2>
</div>
<div class="card-section">
<% if groups.any? %>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
<div class="table-scroll">
<table class="table-list">
<thead>
<tr>
<th><%= t("decidim.anonymous_codes.admin.code_groups.index.title") %></th>
<th><%= t("decidim.anonymous_codes.admin.code_groups.index.expires_at") %></th>
<th><%= t("decidim.anonymous_codes.admin.code_groups.index.active") %></th>
<th><%= t("decidim.anonymous_codes.admin.code_groups.index.num_of_tockens") %></th>
<th><%= t("decidim.anonymous_codes.admin.code_groups.index.max_reuses") %></th>
<th></th>
</tr>
</thead>
<tbody>
<% groups.each do |group| %>
<tr>
<td><%= translated_attribute(group.title) %></td>
<td><%= l(group.expires_at, format: :decidim_short) %></td>
<td><%= t("booleans.#{group.active}") %></td>
<td><%= group.tokens.count %></td>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
<td><%= group.max_reuses %></td>
<td class="table-list__actions">
<%= icon_link_to "circle-x", code_group_path(group.id), t("decidim.admin.actions.destroy"), method: :delete, class: "action-icon--remove", data: { confirm: t("decidim.admin.actions.confirm_destroy") } %>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
<%= icon_link_to "pencil", edit_code_group_path(group.id), t("decidim.admin.actions.edit"), class: "action-icon--edit" %>
<%= icon_link_to "eye", t("decidim.admin.actions.preview"), class: "action-icon--preview" %>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
<% end %>
</tbody>
</table>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
</div>
<% else %>
<div>
<p>No access code groups available.</p>
<p>Stert by adding an access code group from the "<a href="<%= new_code_group_path %>">New access code group</a>" button.</p>
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= decidim_form_for(@form, url: code_groups_path, html: { class: "form new_code_group" }) do |f| %>
<%= render partial: "form", object: f, locals: { title: t(".title") } %>

<div class="button--double form-general-submit">
<%= f.submit "create" %>
</div>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<p><strong><%= t(".group_locked") %></strong></p>
<ul>
<% groups.each do |group| %>
<li><%= link_to translated_attribute(group.title), "##{group.id}" %></li>
<li><%= link_to translated_attribute(group.title), decidim_admin_anonymous_codes.edit_code_group_path(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><%= link_to t(".new_group"), decidim_admin_anonymous_codes.code_groups_path %></p>
<p class="help-text"><%= t(".group_unlocked_desc") %></p>
</div>
<% end %>
33 changes: 33 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
---
en:
decidim:
admin:
actions:
confirm_destroy: Are you sure you want to delete this %{name}?
destroy: Delete
edit: Edit
preview: Preview
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
menu:
anonymous_codes: Access Codes
anonymous_codes:
admin:
code_groups:
create:
invalid: There was a problem creating this access code group
success: Access code group successfully created
destroy:
success: Access code group successfully destroyed
edit:
title: Edit access code group
form:
active: Active
expires_at: Expires At
max_reuses: Re-use max
title: Title
index:
active: Active
expires_at: Expires at
max_reuses: Re-use max
new_access_code_group_button: New access code group
num_of_tockens: Num. of Tockens
title: Title
new:
title: New access code group
update:
invalid: There was a problem updating this access code group
success: Access code group successfully updated
surveys_component_settings:
callout:
group_locked: This survey can only be answered by using a valid code.
Expand Down
2 changes: 2 additions & 0 deletions lib/decidim/anonymous_codes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "decidim/anonymous_codes/admin"
require "decidim/anonymous_codes/engine"
require "decidim/anonymous_codes/admin_engine"

module Decidim
# This namespace holds the logic of the `decidim-anonymous_codes` module.
Expand Down
8 changes: 8 additions & 0 deletions lib/decidim/anonymous_codes/admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module Admin
end
end
end
41 changes: 41 additions & 0 deletions lib/decidim/anonymous_codes/admin_engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
# This is the admin interface for `decidim-blogs`. It lets you edit and
# configure the blog associated to a participatory process.
class AdminEngine < ::Rails::Engine
isolate_namespace Decidim::AnonymousCodes::Admin

paths["db/migrate"] = nil
paths["lib/tasks"] = nil

routes do
resources :code_groups
root to: "code_groups#index"
end

def load_seed
nil
end

initializer "decidim_decidim-anonymous_codes.admin_mount_routes" do
Decidim::Core::Engine.routes do
mount Decidim::AnonymousCodes::AdminEngine, at: "/admin/anonymous_codes", as: "decidim_admin_anonymous_codes"
end
end

initializer "decidim-anonymous_codes.admin_menu" do
Decidim.menu :admin_menu do |menu|
menu.add_item :anonymous_codes_menu,
I18n.t("menu.anonymous_codes", scope: "decidim.admin"),
ElviaBth marked this conversation as resolved.
Show resolved Hide resolved
decidim_admin_anonymous_codes.code_groups_path,
icon_name: "hard-drive",
position: 7.5,
active: is_active_link?(decidim_admin_anonymous_codes.code_groups_path, :inclusive),
if: defined?(current_user) && current_user&.read_attribute("admin")
end
end
end
end
end
Loading