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

Admin can publish club novels: refs #100.

parent 797eb35d
......@@ -16,6 +16,7 @@ class ClubNovelsController < ApplicationController
def create
@club_novel = ClubNovel.new(club_novel_params)
sanitize_publication
if @club_novel.save
flash[:notice] = 'Info club créée avec succès.'
redirect_to @club_novel
......@@ -30,6 +31,7 @@ class ClubNovelsController < ApplicationController
def update
@club_novel = ClubNovel.find(params[:id])
sanitize_publication
if @club_novel.update_attributes(club_novel_params)
flash[:notice] = 'Info club modifiée avec succès.'
redirect_to @club_novel
......@@ -47,6 +49,11 @@ class ClubNovelsController < ApplicationController
private
def club_novel_params
params.require(:club_novel).permit(:title, :content)
params.require(:club_novel).permit(:title, :content, :published_at)
end
def sanitize_publication
@club_novel.published_at = (params[:club_novel][:published] == "true" ? Time.zone.now : nil)
params[:club_novel].delete(:published)
end
end
class ClubNovel < ApplicationRecord
def published
published_at.present?
end
def public_label
published ? "OUI" : "NON"
end
end
......@@ -31,6 +31,7 @@
<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>
......
......@@ -31,6 +31,9 @@
<%= custom_edit_link edit_club_novel_path(@club_novel), :short %>
<%= custom_list_link club_novels_path %>
</p>
<p>
<span>Publique :</span> <%= @club_novel.public_label %>
</p>
<% end %>
<%= textilize raw @club_novel.content %>
......
......@@ -214,6 +214,7 @@ fr:
club_novel:
title: "Intitulé"
content: "Contenu"
published: "Publiée"
helpers:
submit:
create: "Créer"
......
class AddPublicationDatetimeToClubNovels < ActiveRecord::Migration[5.1]
def change
add_column :club_novels, :published_at, :datetime
end
end
......@@ -32,7 +32,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190813084942) do
ActiveRecord::Schema.define(version: 20190813132459) do
create_table "activities", force: :cascade do |t|
t.string "title", limit: 255
......@@ -109,6 +109,7 @@ ActiveRecord::Schema.define(version: 20190813084942) do
t.text "content"
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "published_at"
end
create_table "contact_types", force: :cascade do |t|
......
......@@ -3,5 +3,10 @@ FactoryGirl.define do
title { "Item #{Faker::Lorem.unique.word}" }
content { |i| "Contenu de #{i.title}" }
updated_at nil
published_at nil
trait :published do
published_at { Time.zone.now }
end
end
end
......@@ -70,13 +70,33 @@ feature "Manage club novels" do
click_update_button
end
expect(current_path).to eq(club_novel_path(ClubNovel.first))
expect(current_path).to eq(club_novel_path(club_novel))
within "article" do
expect(page).to have_selector("h1", text: new_title)
end
end
scenario "I can publish one"
scenario "I can [un-]publish one" do
visit edit_club_novel_path(club_novel)
within "form" do
check "Publiée"
click_update_button
end
within "article" do
expect(page).to have_content "Publique : OUI"
end
visit edit_club_novel_path(club_novel)
within "form" do
uncheck "Publiée"
click_update_button
end
within "article" do
expect(page).to have_content "Publique : NON"
end
end
end
scenario "I can delete one" do
......
......@@ -7,4 +7,26 @@ RSpec.describe ClubNovel, type: :model do
expect(build(:club_novel)).to be_valid
end
end
context "behavior" do
describe "#published" do
it "returns true when published_at set" do
# Given
club_novel = create :club_novel, :published
# When
result = club_novel.published
# Then
expect(result).to be_truthy
end
it "returns false when published_at not set" do
# Given
club_novel = create :club_novel
# When
result = club_novel.published
# Then
expect(result).to be_falsy
end
end
end
end
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