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

feat(logos): manage departments and organisations logos with scaleway #1845

Merged
merged 25 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Style/ClassAndModuleChildren:
Style/ParallelAssignment:
Enabled: false

Style/Alias:
Enabled: false
qblanc marked this conversation as resolved.
Show resolved Hide resolved

Style/RegexpLiteral:
Enabled: false

Expand Down
9 changes: 6 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ gem "blueprinter"
# A Rails engine for creating super-flexible admin dashboards
gem "administrate", git: "https://github.com/thoughtbot/administrate.git"

# Required for ActiveStorage for S3 compatible storage
gem "aws-sdk-s3", require: false
# manage attachments from administrate dashboards
gem "administrate-field-active_storage"
aminedhobb marked this conversation as resolved.
Show resolved Hide resolved

# Use Active Storage variant
gem "image_processing", "~> 1.2"
gem "image_processing"

# Required for ActiveStorage for S3 compatible storage
gem "aws-sdk-s3", require: false

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", ">= 1.4.2", require: false
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
administrate-field-active_storage (1.0.1)
administrate (>= 0.2.2)
rails (>= 7.0)
afm (0.2.2)
ast (2.4.2)
aws-eventstream (1.3.0)
Expand Down Expand Up @@ -526,6 +529,7 @@ PLATFORMS
DEPENDENCIES
addressable
administrate!
administrate-field-active_storage
aws-sdk-s3
blueprinter
bootsnap (>= 1.4.2)
Expand All @@ -536,7 +540,7 @@ DEPENDENCIES
factory_bot_rails
faraday
groupdate
image_processing (~> 1.2)
image_processing
jbuilder (~> 2.7)
jsbundling-rails
jwt
Expand Down
Binary file added app/assets/images/logos/pie-pantin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
682 changes: 0 additions & 682 deletions app/assets/images/logos/pie-pantin.svg

This file was deleted.

4 changes: 1 addition & 3 deletions app/controllers/previews/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def mailer_instance_variables
mandatory_warning: @invitation.mandatory_warning,
punishable_warning: @invitation.punishable_warning,
custom_sentence: @invitation.custom_sentence,
signature_lines: @organisation.messages_configuration&.signature_lines,
organisation_logo_path: @organisation.logo_path(%w[png jpg]),
department_logo_path: @department.logo_path(%w[png jpg])
signature_lines: @organisation.messages_configuration&.signature_lines
}
end

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/previews/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def mailer_instance_variables
punishable_warning: @notification.punishable_warning,
custom_sentence: @notification.custom_sentence,
signature_lines: @notification.signature_lines,
instruction_for_rdv: @notification.instruction_for_rdv,
organisation_logo_path: @organisation.logo_path(%w[png jpg]),
department_logo_path: @department.logo_path(%w[png jpg])
instruction_for_rdv: @notification.instruction_for_rdv
}
end

Expand Down
3 changes: 3 additions & 0 deletions app/controllers/super_admins/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module SuperAdmins
class ApplicationController < Administrate::ApplicationController
include AuthenticatedControllerConcern
include SuperAdmins::RedirectAndRenderConcern
# Needed to generate ActiveStorage urls locally, it sets the host and protocol
include ActiveStorage::SetCurrent unless Rails.env.production?

before_action :authenticate_super_admin!

def authenticate_super_admin!
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/super_admins/departments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class DepartmentsController < SuperAdmins::ApplicationController

private

def scoped_resource
resource_class.with_attached_logo
end

def default_sorting_attribute
:number
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/super_admins/organisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def update

private

def scoped_resource
resource_class.with_attached_logo
end

def resource
@resource ||= Organisation.new(resource_params)
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/teleprocedure_landings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ class TeleprocedureLandingsController < ApplicationController

def show
@department = Department.find_by!(number: params[:department_number])
@department_logo_path = @department.logo_path
end
end
3 changes: 3 additions & 0 deletions app/dashboards/department_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DepartmentDashboard < Administrate::BaseDashboard
email: Field::String,
file_configurations: Field::HasMany,
invitations: Field::HasMany,
logo: Field::ActiveStorage.with_options(show_preview_variant: false),
motif_categories: Field::HasMany,
name: Field::String,
number: Field::String,
Expand Down Expand Up @@ -64,6 +65,7 @@ class DepartmentDashboard < Administrate::BaseDashboard
organisations
created_at
updated_at
logo
].freeze

# FORM_ATTRIBUTES
Expand All @@ -79,6 +81,7 @@ class DepartmentDashboard < Administrate::BaseDashboard
phone_number
carnet_de_bord_deploiement_id
display_in_stats
logo
].freeze

# COLLECTION_FILTERS
Expand Down
6 changes: 4 additions & 2 deletions app/dashboards/organisation_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class OrganisationDashboard < Administrate::BaseDashboard
invitations: Field::HasMany,
last_webhook_update_received_at: Field::DateTime,
lieux: Field::HasMany,
logo: Field::ActiveStorage.with_options(show_preview_variant: false),
logo_filename: Field::String,
messages_configuration: Field::HasOne,
motif_categories: Field::HasMany,
Expand Down Expand Up @@ -63,13 +64,13 @@ class OrganisationDashboard < Administrate::BaseDashboard
email
safir_code
independent_from_cd
logo_filename
agent_roles
lieux
motif_categories
created_at
updated_at
last_webhook_update_received_at
logo
].freeze

# FORM_ATTRIBUTES
Expand All @@ -84,12 +85,13 @@ class OrganisationDashboard < Administrate::BaseDashboard
email
safir_code
independent_from_cd
logo_filename
logo
].freeze

FORM_ATTRIBUTES_NEW = %i[
rdv_solidarites_organisation_id
department
logo
].freeze

# COLLECTION_FILTERS
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module PdfHelper
def logo_path(name, formats = [])
Logo.new(name).path(formats)
end

qblanc marked this conversation as resolved.
Show resolved Hide resolved
def pdf_stylesheet_link_tag(source)
if Rails.env.test?
# wicked_pdf_stylesheet_link_tag produces prevents from forming a pdf in test
Expand Down
6 changes: 0 additions & 6 deletions app/javascript/controllers/tooltip_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ export default class extends Controller {
});
}

logoFilenameAttribute() {
tippy(this.element, {
content: "Renseigner le nom du logo sans l'extension du fichier (.png, .svg, .jpg)",
});
}

senderCityAttribute() {
tippy(this.element, {
content:
Expand Down
8 changes: 5 additions & 3 deletions app/mailers/invitation_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ def set_signature_lines
end

def set_organisation_logo_path
return if @invitation.organisations.length != 1 || first_organisation.logo_path(%w[png jpg]).blank?
return if @invitation.organisations.length != 1 || first_organisation.logo.blank?

@organisation_logo_path = first_organisation.logo_path(%w[png jpg])
@organisation_logo_path = first_organisation.logo.url
end

def set_department_logo_path
@department_logo_path = @department.logo_path(%w[png jpg])
return if @department.logo.blank?

@department_logo_path = @department.logo.url
end

def set_rdv_title
Expand Down
6 changes: 4 additions & 2 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ def set_rdv_purpose
end

def set_organisation_logo_path
@organisation_logo_path = @rdv.organisation.logo_path(%w[png jpg])
return if @rdv.organisation.logo.blank?

@organisation_logo_path = @rdv.organisation.logo.url
end

def set_department_logo_path
@department_logo_path = @department.logo_path(%w[png jpg])
@department_logo_path = @department.logo.url
end

def rdv_by_phone?
Expand Down
16 changes: 10 additions & 6 deletions app/models/concerns/has_logo.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module HasLogo
delegate :path, to: :logo, prefix: true
extend ActiveSupport::Concern
ACCEPTED_FORMATS = %w[PNG JPG].freeze

def logo
Logo.new(logo_name)
end
MIME_TYPES = [
"image/png",
"image/jpeg"
].freeze

def logo_name
respond_to?(:logo_filename) && logo_filename.present? ? logo_filename : name.parameterize
included do
has_one_attached :logo
validates :logo, max_size: 2.megabytes,
accepted_formats: { formats: ACCEPTED_FORMATS, mime_types: MIME_TYPES }
end
end
2 changes: 1 addition & 1 deletion app/models/department.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Department < ApplicationRecord
include HasLogo

validates :name, :capital, :number, :pronoun, :region, presence: true
validates :name, :capital, :number, :pronoun, :region, :logo, presence: true

has_many :organisations, dependent: :nullify
has_many :invitations, dependent: :nullify
Expand Down
22 changes: 0 additions & 22 deletions app/models/logo.rb

This file was deleted.

30 changes: 8 additions & 22 deletions app/models/parcours_document.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
class ParcoursDocument < ApplicationRecord
belongs_to :department
belongs_to :agent
belongs_to :user
MAX_SIZE = 5.megabytes

has_one_attached :file

validates :file, presence: true

validate :file_size_validation
validate :file_format_validation
ACCEPTED_FORMATS = %w[PDF JPG PNG ODT DOC DOCX XLSX PPT ZIP].freeze

MIME_TYPES = [
"application/pdf",
Expand All @@ -27,19 +20,12 @@ class ParcoursDocument < ApplicationRecord
"application/zip"
].freeze

MAX_FILE_SIZE = 5.megabytes

private

def file_size_validation
return unless file.blob.byte_size > MAX_FILE_SIZE

errors.add(:base, "Le fichier est trop volumineux (5mo maximum)")
end
belongs_to :department
belongs_to :agent
belongs_to :user

def file_format_validation
return if MIME_TYPES.include?(file.blob.content_type)
has_one_attached :file

errors.add(:base, "Seuls les formats PDF, JPG, PNG, ODT, DOCX, XLSX, PPT, DOC et ZIP sont acceptés.")
end
validates :file, presence: true, max_size: MAX_SIZE,
accepted_formats: { formats: ACCEPTED_FORMATS, mime_types: MIME_TYPES }
end
20 changes: 20 additions & 0 deletions app/validators/accepted_formats_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AcceptedFormatsValidatorError < StandardError; end

class AcceptedFormatsValidator < ActiveModel::EachValidator
aminedhobb marked this conversation as resolved.
Show resolved Hide resolved
def validate_each(record, _attribute, value)
verify_options!(options)

return if value.blob.blank? || options[:mime_types].include?(value.blob.content_type)

record.errors.add(:base, "Seuls les formats #{options[:formats].join(', ')} sont acceptés.")
end

private

def verify_options!(options)
return if options.key?(:formats) && options[:formats].is_a?(Array) &&
options.key?(:mime_types) && options[:mime_types].is_a?(Array)

raise AcceptedFormatsValidatorError, "options[:formats] and options[:mime_types] must be defined and be arrays"
end
end
18 changes: 18 additions & 0 deletions app/validators/max_size_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class MaxSizeValidatorError < StandardError; end

class MaxSizeValidator < ActiveModel::EachValidator
def validate_each(record, _attribute, value)
verify_options!(options)

return if value.blob.blank? || value.blob.byte_size < options[:with]

# max_size must be in megabytes for the error message to be accurate
record.errors.add(:base, "Le fichier est trop volumineux (#{options[:with].to_s[0...-6]} Mo maximum)")
end

def verify_options!(options)
return if options.key?(:with) && options[:with].is_a?(Integer)
aminedhobb marked this conversation as resolved.
Show resolved Hide resolved

raise MaxSizeValidatorError, "max_size must be defined as an integer"
end
end
8 changes: 4 additions & 4 deletions app/views/letters/_footer.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<div class="letter-footer">
<% if display_europe_logos %>
<div id="europe-logos">
<%= image_tag(logo_path("europe-logos")) %>
<%= image_tag("logos/europe-logos.jpg") %>
</div>
<% end %>
<div>
<%= image_tag(logo_path("rdv-insertion", ["png"])) %>
<%= image_tag("logos/rdv-insertion.png") %>
</div>
<% if display_department_logo %>
<div id="department-logo">
<%= image_tag(department.logo_path) %>
<%= image_tag(department.logo.url) %>
</div>
<% end %>
<% if display_france_travail_logo %>
<div id="france-travail-logo">
<%= image_tag(logo_path("france-travail", ["png"])) %>
<%= image_tag("logos/france-travail.png") %>
</div>
<% end %>
</div>
Loading
Loading