Commit d5f1b884 authored by Alexandre Friquet's avatar Alexandre Friquet

Collaborating club can disbale and enable own performances: refs #88.

parent 4fa1e02d
......@@ -92,6 +92,8 @@ class MembersController < ApplicationController
member = Member.includes(:contacts).where(id: params[:id]).first
if !member.nil? && member.published? || member == current_member || admin?
@member = member
@enabled_performances = @member.performances.enabled
@disabled_performances = @member.performances.disabled
if @member.collaborating_club?
@internal_subscriptions = @member.subscriptions
@external_subscriptions = Subscription.for_performances(@member.performance_ids) - @internal_subscriptions
......
......@@ -24,7 +24,7 @@ class PerformancesController < ApplicationController
include AuthorizationsHelper
before_action :set_member
before_action :set_performance, only: [:edit, :update, :destroy]
before_action :set_performance, only: [:edit, :update, :toggle, :destroy]
before_action :authorize_member
before_action :set_available_days
before_action :set_activities
......@@ -57,6 +57,16 @@ class PerformancesController < ApplicationController
end
end
def toggle
@performance.toggle(:disabled)
done_action = (@performance.disabled? ? "désactivée" : "activée")
if @performance.save
redirect_to @member, notice: "Séance #{done_action} avec succès."
else
redirect_to @member, error: "La séance n'a pu être #{done_action}."
end
end
def destroy
@performance.destroy
redirect_to @member, notice: 'Séance supprimée avec succès.'
......
......@@ -122,4 +122,19 @@ module LinksHelper
custom_link 'fa-eur', '€', path, version, options
end
def custom_disable_link(path, version = :normal, options = {})
options[:class] << 'warning'
options[:method] = :patch
(options[:data] ||= {})[:confirm] = 'Etes-vous sûr de vouloir désactiver cet élément ?'
custom_link 'fa-toggle-off', 'Désactiver', path, version, options
end
def custom_enable_link(path, version = :normal, options = {})
options[:class] << 'warning'
options[:method] = :patch
(options[:data] ||= {})[:confirm] = 'Etes-vous sûr de vouloir activer cet élément ?'
custom_link 'fa-toggle-on', 'Activer', path, version, options
end
end
......@@ -34,6 +34,8 @@ class Performance < ApplicationRecord
validate :end_at_after_start_at
scope :for_member, ->(member) { where(member: member) }
scope :disabled, -> { where(disabled: true)}
scope :enabled, -> { where.not(id: disabled)}
def full_title
"[#{activity.title}] #{I18n.t('date.day_names')[Date::DAYNAMES.index(day.camelize)].camelize} #{human_readable_range}"
......
......@@ -26,16 +26,35 @@
<p class="AdminCard-globalActions">
<%= custom_add_link "Nouvelle séance", new_member_performance_path(@member) %>
</p>
<ul>
<% @member.performances.each do |perf| %>
<li>
<span class="no_print">
<%= custom_edit_link edit_member_performance_path(@member, perf), :short, class: %w(tight) %>
<%= custom_delete_link member_performance_path(@member, perf), :short, class: %w(tight) %>
</span>
<%= perf.full_title %>
</li>
<% end %>
</ul>
<%- if @enabled_performances.present? %>
<h2><%= toggle_link "Séances actives", '#enabled' %></h2>
<ul id="enabled">
<% @enabled_performances.each do |perf| %>
<li>
<span class="no_print">
<%= custom_edit_link edit_member_performance_path(@member, perf), :short, class: %w(tight) %>
<%= custom_disable_link toggle_member_performance_path(@member, perf), :short, class: %w(tight) %>
<%= custom_delete_link member_performance_path(@member, perf), :short, class: %w(tight) %>
</span>
<%= perf.full_title %>
</li>
<% end %>
</ul>
<%- end %>
<%- if @disabled_performances.present? %>
<h2><%= toggle_link "Séances inactives", '#disabled' %></h2>
<ul id="disabled" style="display:none">
<% @disabled_performances.each do |perf| %>
<li>
<span class="no_print">
<%= custom_edit_link edit_member_performance_path(@member, perf), :short, class: %w(tight) %>
<%= custom_enable_link toggle_member_performance_path(@member, perf), :short, class: %w(tight) %>
<%= custom_delete_link member_performance_path(@member, perf), :short, class: %w(tight) %>
</span>
<%= perf.full_title %>
</li>
<% end %>
</ul>
<%- end %>
</aside>
<%- end %>
......@@ -53,7 +53,9 @@ Rails.application.routes.draw do
get :raz_pass, :on => :member
post :search_contacts, on: :collection
get :map, on: :collection
resources :performances, except: [:index, :show]
resources :performances, except: [:index, :show] do
patch :toggle, on: :member
end
resources :subscriptions do
get :option, on: :collection
get :card, on: :member
......
class AddDisabledFlagToPerformances < ActiveRecord::Migration[5.1]
def change
add_column :performances, :disabled, :boolean, default: false
end
end
......@@ -32,7 +32,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180913142206) do
ActiveRecord::Schema.define(version: 20180917093307) do
create_table "activities", force: :cascade do |t|
t.string "title", limit: 255
......@@ -348,6 +348,7 @@ ActiveRecord::Schema.define(version: 20180913142206) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "activity_id"
t.boolean "disabled", default: false
end
create_table "phone_numbers", force: :cascade do |t|
......
......@@ -47,7 +47,7 @@ feature "Manage performances" do
expect(current_path).to eq member_path(@member)
expect(page).to have_content "La séance a bien été créée."
within "aside#performances ul" do
within "aside#performances ul#enabled" do
expect(page).to have_selector("li", text: "[Athlétisme] Mardi de 11h15 à 13h45")
end
end
......@@ -72,7 +72,7 @@ feature "Manage performances" do
expect(current_path).to eq member_path(@member)
expect(page).to have_content "Séance mise à jour avec succès."
within "aside#performances ul" do
within "aside#performances ul#enabled" do
expect(page).to have_selector("li", text: "[Natation] Mardi de 10h00 à 12h00")
end
end
......@@ -88,8 +88,37 @@ feature "Manage performances" do
expect(current_path).to eq member_path(@member)
expect(page).to have_content "Séance supprimée avec succès."
within "aside#performances ul" do
expect(page).not_to have_selector("li", text: "[Natation] Mardi de 10h00 à 12h00")
expect(page).not_to have_selector("aside#performances ul#enabled")
expect(page).not_to have_selector("aside#performances ul#disabled")
end
scenario "I can disable one and reenable it", :js do
@member.activities << [athle, swim]
perf = create :performance, day: :monday, start_at: "18:00", end_at: "20:00", activity: athle, member: @member
perf = create :performance, day: :monday, start_at: "18:00", end_at: "20:00", activity: swim, member: @member
visit member_path(@member)
expect(page).not_to have_selector("aside#performances ul#disabled")
within "aside#performances ul#enabled li", text: "[Natation] Lundi" do
click_link "Désactiver"
end
expect(current_path).to eq member_path(@member)
expect(page).to have_content "Séance désactivée avec succès."
within "aside#performances ul#enabled" do
expect(page).to have_selector("li", text: "[Athlétisme] Lundi")
end
click_link "Séances inactives"
within "aside#performances ul#disabled li", text: "[Natation] Lundi" do
click_link "Activer"
end
expect(current_path).to eq member_path(@member)
expect(page).to have_content "Séance activée avec succès."
expect(page).not_to have_selector("aside#performances ul#disabled")
within "aside#performances ul#enabled" do
expect(page).to have_selector("li", text: "[Athlétisme] Lundi")
expect(page).to have_selector("li", text: "[Natation] Lundi")
end
end
end
......
......@@ -73,5 +73,17 @@ RSpec.describe Performance, type: :model do
perf2 = create :performance, member: member2
expect(Performance.for_member(member1)).to eq [perf1]
end
it ".disabled returns only disabled performances" do
perf1 = create :performance, disabled: false
perf2 = create :performance, disabled: true
expect(Performance.disabled).to eq [perf2]
end
it ".enabled returns only enabled performances" do
perf1 = create :performance, disabled: false
perf2 = create :performance, disabled: true
expect(Performance.enabled).to eq [perf1]
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