Can create new letter with attached document

This commit is contained in:
Norore 2024-06-09 00:24:48 +02:00
parent 1bcb99734a
commit e2e47eff46
17 changed files with 248 additions and 2 deletions

View file

@ -1,5 +1,12 @@
class LettersController < ApplicationController class LettersController < ApplicationController
before_action :set_letter, only: %i[show edit update delete] before_action :set_letter, only: %i[show]
def index
@letters = Letter.all
end
def show; end
def new def new
@letter = Letter.new @letter = Letter.new
end end
@ -8,7 +15,7 @@ class LettersController < ApplicationController
@letter = Letter.new(letter_params) @letter = Letter.new(letter_params)
respond_to do |format| respond_to do |format|
if @letter.save if @letter.save
format.html { redirect_to @laboratory, notice: 'Votre lettre ouverte a bien été soumise. Vous avez été redirigé sur votre lettre ouverte. Vous pouvez maintenant la partager.' } format.html { redirect_to @letter, notice: 'Votre lettre ouverte a bien été soumise. Vous avez été redirigé sur votre lettre ouverte. Vous pouvez maintenant la partager.' }
format.json { render :show, status: :created, location: @letter } format.json { render :show, status: :created, location: @letter }
else else
format.html { render :new } format.html { render :new }

View file

@ -0,0 +1,17 @@
module FormHelper
def errors_for(form, field)
pp form.object.errors[field].first
content_tag(:p, form.object.errors[field].try(:first), class: 'my-2 help-block')
end
def form_group_for(form, field, opts = {}, &block)
label = opts.fetch(:label, true)
has_errors = form.object.errors[field].present?
content_tag :div, class: "form-group col #{'alert alert-info' if has_errors}" do
concat form.label(field, class: 'control-label') if label
concat capture(&block)
concat errors_for(form, field)
end
end
end

View file

@ -1,3 +1,4 @@
class Letter < ApplicationRecord class Letter < ApplicationRecord
has_one_attached :document has_one_attached :document
has_many :signatories
end end

View file

@ -0,0 +1,32 @@
<%= form_with(model: letter, local: true, class: 'mam', multipart: true) do |form| %>
<%= form_group_for form, :title, label: false do %>
<%= form.label :title, 'Titre de votre lettre', class: %i[form-label required] %>
<%= form.text_field :title, class: %i[form-control], required: true %>
<% end %>
<%= form_group_for form, :document, label: false do %>
<%= form.label :document, 'Votre lettre au format PDF', class: %i[form-label] %>
<%= form.file_field :document, class: %i[form-control] %>
<% end %>
<%= form_group_for form, :author, label: false do %>
<%= form.label :author, 'Qui êtes-vous ?', class: %i[form-label required] %>
<%= form.text_field :author, class: %i[form-control], required: true %>
<% end %>
<%= form_group_for form, :email, label: false do %>
<%= form.label :email, 'Quelle est votre adresse courriel ?', class: %i[form-label required] %>
<%= form.email_field :email, class: %i[form-control], required: true %>
<% end %>
<%= form_group_for form, :private_email, label: false do %>
<%= form.label :private_email, 'Voulez-vous que votre adresse email soit privée ? Si oui, elle napparaîtra pas sur nos pages.', class: %i[form-label required] %>
<%= form.select :private_email, options_for_select(
[false, true].collect { |n| [(n) ? 'Oui' : 'Non', n] }, form.object.private_email
), {}, class: %i[form-control form-select] %>
<% end %>
<div class="mt-4 text-end">
<%= form.submit 'Déposer ma lettre ouverte', class: %i[btn btn-dark] %>
</div>
<% end %>

View file

@ -0,0 +1,3 @@
<h2 class="h3">Nouvelle lettre ouverte</h2>
<%= render 'form', letter: @letter %>

View file

@ -0,0 +1,8 @@
<h2><%= @letter.title %></h2>
Rédigée par : <%= @letter.author %> <% unless @letter.private_email %>(<%= @letter.email %>) <% end %>
<div>
<%= @letter.document %>
</div>

View file

@ -62,4 +62,7 @@ Rails.application.configure do
# Raise error when a before_action's only/except options reference missing actions # Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true config.action_controller.raise_on_missing_callback_actions = true
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
end end

View file

@ -74,6 +74,9 @@ Rails.application.configure do
# Do not dump schema after migrations. # Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false config.active_record.dump_schema_after_migration = false
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
# Enable DNS rebinding protection and other `Host` header attacks. # Enable DNS rebinding protection and other `Host` header attacks.
# config.hosts = [ # config.hosts = [
# "example.com", # Allow requests from example.com # "example.com", # Allow requests from example.com

7
config/storage.yml Normal file
View file

@ -0,0 +1,7 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>

View file

@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :key ], unique: true
end
create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end

62
db/schema.rb generated Normal file
View file

@ -0,0 +1,62 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_06_08_194425) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum"
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "active_storage_variant_records", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "letters", force: :cascade do |t|
t.string "title"
t.string "author"
t.string "email"
t.boolean "private_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "signatories", force: :cascade do |t|
t.string "name"
t.string "profession"
t.string "email"
t.boolean "private_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class LettersControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class SignatoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

7
test/fixtures/letters.yml vendored Normal file
View file

@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
two:
title: MyString

11
test/fixtures/signatories.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
profession: MyString
email: MyString
two:
name: MyString
profession: MyString
email: MyString

View file

@ -0,0 +1,7 @@
require "test_helper"
class LetterTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class SignatoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end