Commit dadebb65 authored by Alexandre Friquet's avatar Alexandre Friquet
Browse files

Admin can manage images for a club novel: refs #100.

parent 4254fb45
......@@ -46,7 +46,8 @@ class ClubNovelsController < ApplicationController
private
def club_novel_params
params.require(:club_novel).permit(:title, :content, :published_at)
params.require(:club_novel).permit(:title, :content, :published_at,
images_attributes: image_permitted_attributes)
end
def sanitize_publication
......
class ClubNovel < ApplicationRecord
has_many :images, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
scope :published, -> { where.not(published_at: nil) }
scope :unpublished, -> { where(published_at: nil) }
scope :ordered, -> { order(published_at: :desc)}
......
......@@ -13,4 +13,17 @@
<span>Publique :</span> <%= club_novel.public_label %>
<%= textilize raw club_novel.content %>
<div class="item_aside">
<ul class="gallery">
<% club_novel.images.each do |img| %>
<li>
<%= link_to image_tag(img.img_file.url(:thumb), alt: img.title, class: "photo"), img.img_file.url(:large), rel: "prettyPhoto[gallery-#{club_novel.id.to_s}]" %>
</li>
<% end %>
</ul>
<% unless club_novel.images.empty? %>
<span class="images-number"><%= club_novel.images.length %> image(s)</span>
<% end %>
</div>
</li>
......@@ -27,12 +27,16 @@
<div class="tabs">
<ul>
<li><a href="#general">Général</a></li>
<li><a href="#images">Images</a></li>
</ul>
<div id="general">
<%= f.input :title, input_html: {class: "small_text"} %>
<%= f.input :content %>
<%= f.input :published, as: :boolean, checked_value: true, unchecked_value: false %>
</div>
<div id="images">
<%= render 'images_form', f: f %>
</div>
</div>
<p><%= f.button :submit %></p>
......
......@@ -215,6 +215,8 @@ fr:
title: "Intitulé"
content: "Contenu"
published: "Publiée"
images:
<<: *image_labels
helpers:
submit:
create: "Créer"
......
......@@ -97,6 +97,55 @@ feature "Manage club novels" do
expect(page).to have_content "Publique : NON"
end
end
context "regarding images", :js do
scenario "I can attach at least 2 images" do
image1 = build :image, title: "Image 1"
image2 = build :image, title: "Image 2"
visit edit_club_novel_path club_novel
within "form" do
click_link "Images"
click_link "Ajouter une image"
first_image_form = all("div.edit_image")[0]
first_image_form.fill_in("Titre", with: image1.title)
first_image_form.attach_file("Fichier", sample_image_path)
click_link "Ajouter une image"
second_image_form = all("div.edit_image")[1]
second_image_form.fill_in("Titre", with: image2.title)
second_image_form.attach_file("Fichier", sample_image_path)
click_update_button
end
expect(current_path).to eq club_novels_path
within "section#content li", text: club_novel.title do
within "div.item_aside" do
within "ul.gallery" do
expect(first('img')[:alt]).to eq(image1.title)
expect(page).to have_css('img', visible: false, count: 2)
end
within "span.images-number" do
expect(page).to have_content "2"
end
end
end
end
scenario "I can remove an image" do
image = create :image, imageable: club_novel
visit edit_club_novel_path(club_novel)
within "form" do
click_link "Images"
click_link "Supprimer l'image"
click_update_button
end
expect(current_path).to eq club_novels_path
within "section#content li", text: club_novel.title do
expect(page).not_to have_selector "img"
expect(page).not_to have_selector "span.images-number"
end
end
end
end
scenario "I can delete one" do
......
......@@ -2,6 +2,11 @@ require 'rails_helper'
RSpec.describe ClubNovel, type: :model do
context "associations" do
#it { is_expected.to have_many(:attached_files) }
it { is_expected.to have_many(:images) }
end
context "validations" do
it "default factory is valid" do
expect(build(:club_novel)).to be_valid
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment