Skip to content

Commit

Permalink
add command and create method
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed Apr 5, 2024
1 parent d0ee388 commit 5175578
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
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
end

broadcast(:ok)
end

private

attr_reader :code_group, :form

def create_code_group
@code_group = Decidim.traceability.create!(
Group,
@form.current_user,
title: form.title,
expires_at: form.expires_at,
active: form.active,
max_reuses: form.max_reuses,
organization: @organization
)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ class CodeGroupsController < ApplicationController
def index; end

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

def create; 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(CodeGroupsForm).from_model(code_group)
@form = form(CodeGroupForm).from_model(code_group)
end

def update; end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Decidim
module AnonymousCodes
module Admin
class CodeGroupsForm < Decidim::Form
class CodeGroupForm < Decidim::Form
include TranslatableAttributes

translatable_attribute :title, String
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ en:
anonymous_codes:
admin:
code_groups:
create:
invalid: There was a problem creating this access code group
success: Access code group successfully created
edit:
title: Edit access code group
form:
Expand Down

0 comments on commit 5175578

Please sign in to comment.