Sha256: 53974aba3de72668a319c6289d4ffb111715ef683ad75ce5b8ef39380cc72f3b

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Decidim
  module Meetings
    module Admin
      # This class holds a Form to update meeting registrations from Decidim's admin panel.
      class MeetingRegistrationsForm < Decidim::Form
        include TranslatableAttributes

        mimic :meeting

        attribute :registrations_enabled, Boolean
        attribute :available_slots, Integer
        translatable_attribute :registration_terms, String

        validates :registration_terms, translatable_presence: true, if: ->(form) { form.registrations_enabled? }
        validates :available_slots, numericality: { greater_than_or_equal_to: 0 }, if: ->(form) { form.registrations_enabled? }
        validate :available_slots_greater_than_or_equal_to_registrations_count, if: ->(form) { form.registrations_enabled? && form.available_slots.positive? }

        # We need this method to ensure the form object will always have an ID,
        # and thus its `to_param` method will always return a significant value.
        # If we remove this method, get an error onn the `update` action and try
        # to resubmit the form, the form will not hold an ID, so the `to_param`
        # method will return an empty string and Rails will treat this as a
        # `create` action, thus raising an error since this action is not defined
        # for the controller we're using.
        #
        # TL;DR: if you remove this method, we'll get errors, so don't.
        def id
          return super if super.present?
          meeting.id
        end

        private

        def available_slots_greater_than_or_equal_to_registrations_count
          errors.add(:available_slots, :invalid) if available_slots < meeting.registrations.count
        end

        def meeting
          @meeting ||= context[:meeting]
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-meetings-0.10.1 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.10.0 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.9.3 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.9.2 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.9.1 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.9.0 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.8.4 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.8.3 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.8.2 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.8.1 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.8.0 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.7.4 app/forms/decidim/meetings/admin/meeting_registrations_form.rb
decidim-meetings-0.7.3 app/forms/decidim/meetings/admin/meeting_registrations_form.rb