12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- require 'delegate'
- class Decorator < SimpleDelegator
- def class
- __getobj__.class
- end
- end
- class FormConfigurableAgentPresenter < Decorator
- def initialize(agent, view)
- @agent = agent
- @view = view
- super(agent)
- end
- def option_field_for(attribute)
- data = @agent.form_configurable_fields[attribute]
- value = @agent.options[attribute.to_s] || @agent.default_options[attribute.to_s]
- html_options = data.fetch(:html_options, {}).deep_merge({
- role: (data[:roles] + ['form-configurable']).join(' '),
- data: { attribute: },
- })
- case data[:type]
- when :text
- @view.content_tag 'div' do
- @view.concat @view.text_area_tag("agent[options][#{attribute}]", value,
- html_options.merge(class: 'form-control', rows: 3))
- if data[:ace].present?
- ace_options = { source: "[name='agent[options][#{attribute}]']", mode: '', theme: '' }.deep_symbolize_keys!
- ace_options.deep_merge!(data[:ace].deep_symbolize_keys) if data[:ace].is_a?(Hash)
- @view.concat @view.content_tag('div', '', class: 'ace-editor', data: ace_options)
- end
- end
- when :boolean
- @view.content_tag 'div' do
- @view.concat(@view.content_tag('label', class: 'radio-inline') do
- @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true',
- @agent.send(:boolify, value) == true, html_options
- @view.concat "True"
- end)
- @view.concat(@view.content_tag('label', class: 'radio-inline') do
- @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false',
- @agent.send(:boolify, value) == false, html_options
- @view.concat "False"
- end)
- @view.concat(@view.content_tag('label', class: 'radio-inline') do
- @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual',
- @agent.send(:boolify, value).nil?, html_options
- @view.concat "Manual Input"
- end)
- @view.concat(@view.text_field_tag("agent[options][#{attribute}]", value,
- html_options.merge(class: "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}")))
- end
- when :array
- @view.select_tag "agent[options][#{attribute}]", nil,
- html_options.deep_merge(class: 'form-control',
- data: { value:, cache_response: data[:cache_response] != false })
- when :string
- @view.text_field_tag "agent[options][#{attribute}]", value,
- html_options.deep_merge(class: 'form-control', data: { cache_response: data[:cache_response] != false })
- when :number
- @view.number_field_tag "agent[options][#{attribute}]", value,
- html_options.deep_merge(class: 'form-control', data: { cache_response: data[:cache_response] != false })
- when :json
- @view.text_area_tag "agent[options][#{attribute}]", value,
- html_options.deep_merge(class: 'form-control live-json-editor', rows: 10)
- end
- end
- end
|