diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3db115d58..0b17a1c82 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,7 @@
# Changelog Fab Manager
## next release (v2.5.0)
+- Ability to remove an unused custom price for an event (#61)
- Prevent polling notifications when the application is in background
- Ability to export the availabilities and their reservation rate from the admin calendar
- Ability to create, manage and reserve spaces
diff --git a/app/assets/javascripts/controllers/admin/events.coffee.erb b/app/assets/javascripts/controllers/admin/events.coffee.erb
index 9ef91e1c5..728a58cbb 100644
--- a/app/assets/javascripts/controllers/admin/events.coffee.erb
+++ b/app/assets/javascripts/controllers/admin/events.coffee.erb
@@ -16,6 +16,8 @@
# - $scope.toggleStartDatePicker($event)
# - $scope.toggleEndDatePicker($event)
# - $scope.toggleRecurrenceEnd(e)
+# - $scope.addPrice()
+# - $scope.removePrice(price, $event)
#
# Requires :
# - $scope.event.event_files_attributes = []
@@ -137,6 +139,21 @@ class EventsController
+ ##
+ # Remove the price or mark it as 'to delete'
+ ##
+ $scope.removePrice = (price, event) ->
+ event.preventDefault()
+ event.stopPropagation()
+ if price.id
+ price._destroy = true
+ else
+ index = $scope.event.prices.indexOf(price)
+ $scope.event.prices.splice(index, 1)
+
+
+
+
##
# Controller used in the events listing page (admin view)
##
diff --git a/app/assets/templates/events/_form.html.erb b/app/assets/templates/events/_form.html.erb
index b146fffac..b99fbf639 100644
--- a/app/assets/templates/events/_form.html.erb
+++ b/app/assets/templates/events/_form.html.erb
@@ -246,7 +246,7 @@
{{ '0_=_free' }}
-
diff --git a/app/controllers/api/events_controller.rb b/app/controllers/api/events_controller.rb
index ef4144d6f..7ce75a376 100644
--- a/app/controllers/api/events_controller.rb
+++ b/app/controllers/api/events_controller.rb
@@ -12,7 +12,6 @@ class API::EventsController < API::ApiController
# paginate
@events = @events.page(@page).per(12)
-
end
# GET /events/upcoming/:limit
@@ -38,11 +37,20 @@ class API::EventsController < API::ApiController
def update
authorize Event
- if @event.update(event_params.permit!)
- render :show, status: :ok, location: @event
- else
- render json: @event.errors, status: :unprocessable_entity
+ begin
+ if @event.update(event_params.permit!)
+ render :show, status: :ok, location: @event
+ else
+ render json: @event.errors, status: :unprocessable_entity
+ end
+ rescue ActiveRecord::RecordNotDestroyed => e
+ if e.record.class.name == 'EventPriceCategory'
+ render json: {error: ["#{e.record.price_category.name}: #{t('events.error_deleting_reserved_price')}"]}, status: :unprocessable_entity
+ else
+ render json: {error: [t('events.other_error')]}, status: :unprocessable_entity
+ end
end
+
end
def destroy
@@ -69,7 +77,7 @@ class API::EventsController < API::ApiController
:age_range_id, event_theme_ids: [],
event_image_attributes: [:attachment],
event_files_attributes: [:id, :attachment, :_destroy],
- event_price_categories_attributes: [:id, :price_category_id, :amount]
+ event_price_categories_attributes: [:id, :price_category_id, :amount, :_destroy]
)
# handle dates & times (whole-day events or not, maybe during many days)
start_date = Time.zone.parse(event_preparams[:start_date])
diff --git a/app/models/event.rb b/app/models/event.rb
index 9ddfd617d..4c9e28395 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -12,7 +12,7 @@ class Event < ActiveRecord::Base
has_many :event_price_categories, dependent: :destroy
has_many :price_categories, through: :event_price_categories
- accepts_nested_attributes_for :event_price_categories, allow_destroy: false
+ accepts_nested_attributes_for :event_price_categories, allow_destroy: true
belongs_to :age_range
diff --git a/app/models/event_price_category.rb b/app/models/event_price_category.rb
index 843418d87..623c4013f 100644
--- a/app/models/event_price_category.rb
+++ b/app/models/event_price_category.rb
@@ -6,4 +6,12 @@ class EventPriceCategory < ActiveRecord::Base
validates :price_category_id, presence: true
validates :amount, presence: true
+
+ before_destroy :verify_no_associated_tickets
+
+ protected
+ def verify_no_associated_tickets
+ tickets.count == 0
+ end
+
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 69950ff3d..910fc6d6e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -124,6 +124,11 @@ en:
i_ve_reserved: "I've reserved"
completed: "Full"
+ events:
+ # error messages when updating an event
+ error_deleting_reserved_price: "Unable to delete the requested price because it is associated with some reservations"
+ other_error: "An unexpected error occurred while updating the event"
+
export_members:
# members list export to EXCEL format
members: "Members"
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 2455e3775..2efae5864 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -124,6 +124,11 @@ fr:
i_ve_reserved: "J'ai réservé"
completed: "Complet"
+ events:
+ # messages d'erreur lors de la mise à jour d'un évènement
+ error_deleting_reserved_price: "Impossible de supprimer le tarif demandé car il est associé à des réservations"
+ other_error: "Une erreur inattendue est survenue lors de la mise à jour de l'évènement"
+
export_members:
# export de la liste des members au format EXCEL
members: "Membres"