Skip to content

Commit

Permalink
Allow capacity,name edit when registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Xander authored and TomNaessens committed Mar 17, 2024
1 parent 20347c9 commit 964f3a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion app/controllers/access_levels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def update
authorize! :update, @event
@access_level = @event.access_levels.find(params.require(:id))

flash.now[:error] = "Something went wrong updating the ticket" unless @access_level.update(access_level_params)
flash.now[:error] = "Something went wrong updating the ticket" unless @access_level.update(
@access_level.registrations.any? ? access_level_params_strict : access_level_params
)

respond_with @access_level
end
Expand Down Expand Up @@ -68,4 +70,8 @@ def toggle_visibility
def access_level_params
params.require(:access_level).permit(:name, :capacity, :price, :has_comment, :hidden, :permit)
end

def access_level_params_strict
params.require(:access_level).permit(:name, :capacity)
end
end
6 changes: 6 additions & 0 deletions app/models/access_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class AccessLevel < ApplicationRecord
validates :price, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :capacity, numericality: { allow_nil: true, only_integer: true, greater_than: 0 }

validate do |access_level|
if access_level.capacity && access_level.registrations.size > access_level.capacity
access_level.errors.add "Registration", "count exceeds capacity."
end
end

validate do |access_level|
access_level.errors.add :event_id, "has no bank number." if access_level.price.positive? && access_level.event.bank_number.blank?
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/access_levels/_access_level.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<td>
<div class="nowrap">
<%= link_to raw(visibility_icon(access_level)), toggle_visibility_event_access_level_path(access_level.event, access_level), class: 'btn btn-xs btn-default', remote: true %>
<%= link_to raw('<i class="glyphicon glyphicon-edit"></i> Edit'), edit_event_access_level_path(access_level.event, access_level), class: 'btn btn-xs btn-default', remote: true %>
<% unless access_level.registrations.any? or access_level.partners.any? %>
<%= link_to raw('<i class="glyphicon glyphicon-edit"></i> Edit'), edit_event_access_level_path(access_level.event, access_level), class: 'btn btn-xs btn-default', remote: true %>
<%= link_to raw('<i class="glyphicon glyphicon-trash"></i> Delete'), event_access_level_path(access_level.event, access_level), class: 'btn btn-xs btn-danger', data: {confirm: 'Are you sure?'}, remote: true, method: :delete %>
<% end %>
</div>
Expand Down
8 changes: 4 additions & 4 deletions app/views/access_levels/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<%= form_for [object.event, object], remote: true do |f| %>
<td><%= f.text_field :name, class: 'form-control' %></td>
<td><%= f.number_field :capacity, class: 'form-control', placeholder: 0 %></td>
<td><%= f.text_field :price, value: nice_amount(f.object.price), class: 'form-control', placeholder: "0.00" %></td>
<td><%= f.text_field :price, value: nice_amount(f.object.price), class: 'form-control', placeholder: "0.00", disabled: f.object.registrations.any? %></td>
<td width="130px">
<%= f.collection_select :permit, AccessLevel.permits.keys, :itself, lambda {|k| translate(k)}, {}, class: 'form-control' %>
<%= f.collection_select :permit, AccessLevel.permits.keys, :itself, lambda {|k| translate(k)}, {}, class: 'form-control', disabled: f.object.registrations.any? %>
</td>
<td><div class="checkbox"><%= f.check_box :has_comment %></div></td>
<td><div class="checkbox"><%= f.check_box :hidden %></div></td>
<td><div class="checkbox"><%= f.check_box :has_comment, disabled: f.object.registrations.any? %></div></td>
<td><div class="checkbox"><%= f.check_box :hidden, disabled: f.object.registrations.any? %></div></td>
<td><%= f.button button_name, class: 'btn btn-sm btn-primary' %></td>
<%= javascript_tag do %>
var id = "#<% if button_name == 'Save' %>edit_<% end %><%= dom_id(f.object) %>";
Expand Down

0 comments on commit 964f3a1

Please sign in to comment.