Skip to content

Commit

Permalink
Link collections and improvements to results view and API
Browse files Browse the repository at this point in the history
- Show category image if image is not defined
- Add possibility to define link collections for links
- Add link collection management views and API fields
- Destroy associated records on result destroy
- Order statuses by progress
- Disable general attachments display for results
- Add possibility to display attachments and links per collection
- Add statuses progress bar to the result sidebar
- Update timeline styling
- Add possibility to fetch the image blobs (main and list image)
- Fix issue updating main image or list image through the API
- Update locations module
- Add missing translations and modify existing translations
- Improve API specs
  • Loading branch information
ahukkanen committed Mar 27, 2024
1 parent 0544653 commit a0ac9cf
Show file tree
Hide file tree
Showing 45 changed files with 1,214 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DECIDIM_VERSION = Decidim::AccountabilitySimple.decidim_version
gem "decidim", DECIDIM_VERSION
gem "decidim-apifiles", github: "mainio/decidim-module-apifiles", branch: "main"
gem "decidim-favorites", github: "mainio/decidim-module-favorites", branch: "develop"
gem "decidim-locations", github: "mainio/decidim-module-locations", branch: "main"
gem "decidim-locations", github: "mainio/decidim-module-locations", branch: "develop"
gem "decidim-tags", github: "mainio/decidim-module-tags", branch: "main"

gem "decidim-accountability_simple", path: "."
Expand Down
7 changes: 4 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ GIT

GIT
remote: https://github.com/mainio/decidim-module-locations.git
revision: c3991933b8c6899e70d9faac57e862c11bd7e039
branch: main
revision: ce3b54b80f1502ef75acc662ca1decba70f76ff9
branch: develop
specs:
decidim-locations (0.27.4)
decidim-core (~> 0.27.4)
rgeo-geojson (~> 2.1, >= 2.1.1)
rgeo (= 3.0.1)
rgeo-geojson (= 2.1.1)

GIT
remote: https://github.com/mainio/decidim-module-tags.git
Expand Down
27 changes: 24 additions & 3 deletions app/cells/decidim/accountability/result_m_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,39 @@ def render_column?
!context[:no_column].presence
end

def has_image?
def has_list_image?
model.list_image && model.list_image.attached?
end

def has_main_image?
model.main_image && model.main_image.attached?
end

def has_category?
model.category.present?
end

def resource_image_path
return unless has_image?
return model.attached_uploader(:list_image).variant_url(resource_image_variant) if has_list_image?
return model.attached_uploader(:main_image).variant_url(resource_image_variant) if has_main_image?
return unless has_category?

category_image_path(model.category)
end

def category_image_path(cat)
return unless cat.respond_to?(:category_image_url)
return unless cat.category_image.attached?

cat.category_image_url(category_image_variant)
end

def resource_image_variant
:thumbnail
end

model.attached_uploader(:list_image).url
def category_image_variant
:card
end

def status_label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def create_result_links

def create_result_link(form_result_link)
result_link_attributes = {
link_collection: @result.result_link_collections.find_by(id: form_result_link.collection_id),
label: form_result_link.label,
url: form_result_link.url,
position: form_result_link.position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def update_result_links

def update_result_link(form_result_link)
result_link_attributes = {
link_collection: result.result_link_collections.find_by(id: form_result_link.collection_id),
label: form_result_link.label,
url: form_result_link.url,
position: form_result_link.position
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Accountability
module Admin
# This command is executed when the user creates a ResultLinkCollection
# for a Result from the admin panel.
class CreateLinkCollection < Decidim::Command
def initialize(form)
@form = form
end

# Creates the link collection if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
return broadcast(:invalid) if @form.invalid?

transaction do
create_link_collection
end

broadcast(:ok, link_collection)
end

private

attr_reader :link_collection

def create_link_collection
@link_collection = Decidim.traceability.create!(
AccountabilitySimple::ResultLinkCollection,
@form.current_user,
params,
visibility: "all"
)
end

def params
{
decidim_accountability_result_id: @form.decidim_accountability_result_id,
key: @form.key,
name: @form.name,
position: @form.position
}
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Decidim
module Accountability
module Admin
# This command is executed when the user updates a ResultLinkCollection
# for a Result from the admin panel.
class UpdateLinkCollection < Decidim::Command
def initialize(form, link_collection)
@form = form
@link_collection = link_collection
end

# Updates the link collection if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
return broadcast(:invalid) if @form.invalid?

transaction do
update_link_collection
end

broadcast(:ok, link_collection)
end

private

attr_reader :link_collection

def update_link_collection
Decidim.traceability.update!(
link_collection,
@form.current_user,
params
)
end

def params
{
key: @form.key,
name: @form.name,
position: @form.position
}
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Decidim
module AccountabilitySimple
module Admin
module StatusesControllerExtensions
extend ActiveSupport::Concern

included do
private

# Order the statuses based on progress.
def statuses
@statuses ||= Decidim::Accountability::Status.where(component: current_component).order(:progress).page(params[:page]).per(15)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

module Decidim
module Accountability
module Admin
# Controller that allows managing all the link collections for a result.
class LinkCollectionsController < Admin::ApplicationController
helper_method :result

def index
enforce_permission_to :create, :result
end

def new
enforce_permission_to :create, :result
@form = form(AccountabilitySimple::Admin::ResultLinkCollectionForm).from_params({}, result: result)
end

def create
enforce_permission_to :create, :result
@form = form(AccountabilitySimple::Admin::ResultLinkCollectionForm).from_params(params, result: result)

CreateLinkCollection.call(@form) do
on(:ok) do
flash[:notice] = I18n.t("link_collections.create.success", scope: "decidim.accountability.admin")
redirect_to action: :index
end

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

def edit
@link_collection = collection.find(params[:id])
enforce_permission_to :update, :result, result: @link_collection.result
@form = form(AccountabilitySimple::Admin::ResultLinkCollectionForm).from_model(@link_collection, result: result)
end

def update
@link_collection = collection.find(params[:id])
enforce_permission_to :update, :result, result: @link_collection.result
@form = form(AccountabilitySimple::Admin::ResultLinkCollectionForm).from_params(params, result: result)

UpdateLinkCollection.call(@form, @link_collection) do
on(:ok) do
flash[:notice] = I18n.t("link_collections.update.success", scope: "decidim.accountability.admin")
redirect_to action: :index
end

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

def destroy
@link_collection = collection.find(params[:id])
enforce_permission_to :destroy, :result, result: @link_collection.result

Decidim.traceability.perform_action!("delete", @link_collection, current_user) do
@link_collection.destroy!
end

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

redirect_to action: :index
end

private

def result
@result ||= Result.where(component: current_component).find(params[:result_id])
end

def collection
@collection ||= result.result_link_collections
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def to_param
def map_model(model)
self.details = Decidim::AccountabilitySimple::ResultDetail.where(
accountability_result_detailable: model
).map do |detail|
).order(:position).map do |detail|
DefaultDetailsForm.from_model(detail)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Admin
class ResultDefaultDetailsForm < Decidim::Form
include TranslatableAttributes

mimic :result_detail

translatable_attribute :title, String
translatable_attribute :description, String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Admin
class ResultDetailsForm < Decidim::Form
include TranslatableAttributes

mimic :result_detail

translatable_attribute :title, String
translatable_attribute :description, String

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Decidim
module AccountabilitySimple
module Admin
# A form object to create or update attachment collections.
class ResultLinkCollectionForm < Form
include TranslatableAttributes

mimic :result_link_collection

attribute :key, String
attribute :position, Integer, default: 0
translatable_attribute :name, String

validates :name, translatable_presence: true
validates :decidim_accountability_result_id, presence: true

def decidim_accountability_result_id
context[:result]&.id
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ module Admin
class ResultLinkForm < Decidim::Form
include TranslatableAttributes

mimic :result_link

translatable_attribute :label, String
translatable_attribute :url, String

attribute :collection_id, Integer
attribute :position, Integer
attribute :deleted, Boolean, default: false

validates :label, translatable_presence: true, unless: :deleted
validates :url, translatable_presence: true, unless: :deleted
validates :position, numericality: { greater_than_or_equal_to: 0 }, unless: :deleted

def map_model(model)
self.collection_id = model.decidim_accountability_simple_result_link_collection_id
end

def to_param
return id if id.present?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ module ResultExtensions
has_many :result_details, -> { order(:position) }, as: :accountability_result_detailable,
class_name: "Decidim::AccountabilitySimple::ResultDetail"
has_many :result_detail_values, class_name: "Decidim::AccountabilitySimple::ResultDetailValue",
foreign_key: "decidim_accountability_result_id"
foreign_key: "decidim_accountability_result_id",
dependent: :destroy
has_many :result_link_collections,
-> { order(:position) },
class_name: "Decidim::AccountabilitySimple::ResultLinkCollection",
foreign_key: "decidim_accountability_result_id",
inverse_of: :result,
dependent: :destroy
has_many :result_links, -> { order(:position) }, class_name: "Decidim::AccountabilitySimple::ResultLink",
foreign_key: "decidim_accountability_result_id"
foreign_key: "decidim_accountability_result_id",
dependent: :destroy

def result_default_details
Decidim::AccountabilitySimple::ResultDetail.where(
Expand Down
Loading

0 comments on commit a0ac9cf

Please sign in to comment.