Class: Admin::Tramway::Event::ParticipantFormFieldForm

Inherits:
Tramway::ExtendedApplicationForm
  • Object
show all
Defined in:
app/forms/admin/tramway/event/participant_form_field_form.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ParticipantFormFieldForm

Returns a new instance of ParticipantFormFieldForm.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 7

def initialize(object)
  super(object).tap do
    form_properties event: :association,
                    title: :string,
                    description: :string,
                    field_type: :default,
                    options: :text,
                    list_field: :boolean,
                    presence_field: :boolean,
                    position: :numeric,
                    select_options: {
                      type: :string,
                      input_options: {
                        hint: I18n.t('hints.tramway.event.participant_form_field.select_options')
                      }
                    }
  end
end

Instance Method Details

#list_fieldObject



36
37
38
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 36

def list_field
  model.options.present? && model.options.dig('list_field') == 'true'
end

#optionsObject



48
49
50
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 48

def options
  model.options&.to_json
end

#options=(value) ⇒ Object



52
53
54
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 52

def options=(value)
  model.options = value == '' ? value : JSON.parse(value)
end

#presence_fieldObject



40
41
42
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 40

def presence_field
  model.options.present? && model.options.dig('validations', 'presence') == 'true'
end

#select_optionsObject



44
45
46
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 44

def select_options
  model.options.present? && model.options.dig('collection', 'array')&.join(',')
end

#submit(params) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/forms/admin/tramway/event/participant_form_field_form.rb', line 26

def submit(params)
  super(params).tap do
    model.options = {} if model.options == ''
    model.options&.merge! list_field: (params[:list_field] == '1').to_s
    model.options&.deep_merge! collection: { array: params[:select_options].split(',') || [] }
    model.options&.deep_merge! validations: { presence: (params[:presence_field] == '1').to_s }
    model.save
  end
end