1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

fix ability to edit custom prices

This commit is contained in:
Sylvain 2016-08-25 14:13:30 +02:00
parent 5d91756ace
commit 05b010b83f
3 changed files with 11 additions and 0 deletions

View File

@ -239,6 +239,7 @@
</div>
<div class="form-group" ng-repeat="price in event.prices">
<div class="col-sm-5">
<input type="hidden" name="event[event_price_categories_attributes][][id]" ng-value="price.id">
<select class="form-control"
ng-model="price.category"
name="event[event_price_categories_attributes][][price_category_id]"

View File

@ -62,6 +62,7 @@ class API::EventsController < API::ApiController
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
# handle general properties
event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time,
:amount, :nb_total_places, :availability_id,
:all_day, :recurrence, :recurrence_end_at, :category_id, :event_theme_ids,
@ -70,6 +71,7 @@ class API::EventsController < API::ApiController
event_files_attributes: [:id, :attachment, :_destroy],
event_price_categories_attributes: [:id, :price_category_id, :amount]
)
# handle dates & times (whole-day events or not, maybe during many days)
start_date = Time.zone.parse(event_preparams[:start_date])
end_date = Time.zone.parse(event_preparams[:end_date])
start_time = Time.parse(event_preparams[:start_time]) if event_preparams[:start_time]
@ -83,10 +85,14 @@ class API::EventsController < API::ApiController
end
event_preparams.merge!(availability_attributes: {id: event_preparams[:availability_id], start_at: start_at, end_at: end_at, available_type: 'event'})
.except!(:start_date, :end_date, :start_time, :end_time, :all_day)
# convert main price to centimes
event_preparams.merge!(amount: (event_preparams[:amount].to_i * 100 if event_preparams[:amount].present?))
# delete non-complete "other" prices and convert them to centimes
event_preparams[:event_price_categories_attributes].delete_if { |price_cat| price_cat[:price_category_id].empty? or price_cat[:amount].empty? }
event_preparams[:event_price_categories_attributes].each do |price_cat|
price_cat[:amount] = price_cat[:amount].to_i * 100
end
# return the resulting params object
event_preparams
end

View File

@ -1,4 +1,8 @@
class EventPriceCategory < ActiveRecord::Base
belongs_to :event
belongs_to :price_category
validates :event_id, presence: true
validates :price_category_id, presence: true
validates :amount, presence: true
end