diff --git a/app/controllers/letters_controller.rb b/app/controllers/letters_controller.rb index 7b878d4..321ba3f 100644 --- a/app/controllers/letters_controller.rb +++ b/app/controllers/letters_controller.rb @@ -1,5 +1,12 @@ 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 @letter = Letter.new end @@ -8,7 +15,7 @@ class LettersController < ApplicationController @letter = Letter.new(letter_params) respond_to do |format| 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 } else format.html { render :new } diff --git a/app/helpers/form_helper.rb b/app/helpers/form_helper.rb new file mode 100644 index 0000000..4495fef --- /dev/null +++ b/app/helpers/form_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/letter.rb b/app/models/letter.rb index b7f6603..3c0dd58 100644 --- a/app/models/letter.rb +++ b/app/models/letter.rb @@ -1,3 +1,4 @@ class Letter < ApplicationRecord has_one_attached :document + has_many :signatories end diff --git a/app/views/letters/_form.html.erb b/app/views/letters/_form.html.erb new file mode 100644 index 0000000..92f24ac --- /dev/null +++ b/app/views/letters/_form.html.erb @@ -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 n’apparaî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 %> + +
+ <%= form.submit 'Déposer ma lettre ouverte', class: %i[btn btn-dark] %> +
+<% end %> diff --git a/app/views/letters/new.html.erb b/app/views/letters/new.html.erb index e69de29..61de114 100644 --- a/app/views/letters/new.html.erb +++ b/app/views/letters/new.html.erb @@ -0,0 +1,3 @@ +

Nouvelle lettre ouverte

+ +<%= render 'form', letter: @letter %> \ No newline at end of file diff --git a/app/views/letters/show.html.erb b/app/views/letters/show.html.erb new file mode 100644 index 0000000..6dddd76 --- /dev/null +++ b/app/views/letters/show.html.erb @@ -0,0 +1,8 @@ +

<%= @letter.title %>

+ +Rédigée par : <%= @letter.author %> <% unless @letter.private_email %>(<%= @letter.email %>) <% end %> + +
+ <%= @letter.document %> +
+ diff --git a/config/environments/development.rb b/config/environments/development.rb index 58610a5..59d64f9 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -62,4 +62,7 @@ Rails.application.configure do # Raise error when a before_action's only/except options reference missing actions 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 diff --git a/config/environments/production.rb b/config/environments/production.rb index 309e0f5..8178cfc 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -74,6 +74,9 @@ Rails.application.configure do # Do not dump schema after migrations. 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. # config.hosts = [ # "example.com", # Allow requests from example.com diff --git a/config/storage.yml b/config/storage.yml new file mode 100644 index 0000000..e900ef0 --- /dev/null +++ b/config/storage.yml @@ -0,0 +1,7 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> \ No newline at end of file diff --git a/db/migrate/20240608194425_create_active_storage_tables.active_storage.rb b/db/migrate/20240608194425_create_active_storage_tables.active_storage.rb new file mode 100644 index 0000000..e4706aa --- /dev/null +++ b/db/migrate/20240608194425_create_active_storage_tables.active_storage.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..e6427a1 --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/test/controllers/letters_controller_test.rb b/test/controllers/letters_controller_test.rb new file mode 100644 index 0000000..6a46e47 --- /dev/null +++ b/test/controllers/letters_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class LettersControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/signatories_controller_test.rb b/test/controllers/signatories_controller_test.rb new file mode 100644 index 0000000..603853c --- /dev/null +++ b/test/controllers/signatories_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SignatoriesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/letters.yml b/test/fixtures/letters.yml new file mode 100644 index 0000000..64d88ef --- /dev/null +++ b/test/fixtures/letters.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + +two: + title: MyString diff --git a/test/fixtures/signatories.yml b/test/fixtures/signatories.yml new file mode 100644 index 0000000..fde8d5a --- /dev/null +++ b/test/fixtures/signatories.yml @@ -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 diff --git a/test/models/letter_test.rb b/test/models/letter_test.rb new file mode 100644 index 0000000..d2fcfec --- /dev/null +++ b/test/models/letter_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class LetterTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/signatory_test.rb b/test/models/signatory_test.rb new file mode 100644 index 0000000..4642eb3 --- /dev/null +++ b/test/models/signatory_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SignatoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end