form_configurable_agent_presenter.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'delegate'
  2. class Decorator < SimpleDelegator
  3. def class
  4. __getobj__.class
  5. end
  6. end
  7. class FormConfigurableAgentPresenter < Decorator
  8. def initialize(agent, view)
  9. @agent = agent
  10. @view = view
  11. super(agent)
  12. end
  13. def option_field_for(attribute)
  14. data = @agent.form_configurable_fields[attribute]
  15. value = @agent.options[attribute.to_s] || @agent.default_options[attribute.to_s]
  16. html_options = data.fetch(:html_options, {}).deep_merge({
  17. role: (data[:roles] + ['form-configurable']).join(' '),
  18. data: { attribute: },
  19. })
  20. case data[:type]
  21. when :text
  22. @view.content_tag 'div' do
  23. @view.concat @view.text_area_tag("agent[options][#{attribute}]", value,
  24. html_options.merge(class: 'form-control', rows: 3))
  25. if data[:ace].present?
  26. ace_options = { source: "[name='agent[options][#{attribute}]']", mode: '', theme: '' }.deep_symbolize_keys!
  27. ace_options.deep_merge!(data[:ace].deep_symbolize_keys) if data[:ace].is_a?(Hash)
  28. @view.concat @view.content_tag('div', '', class: 'ace-editor', data: ace_options)
  29. end
  30. end
  31. when :boolean
  32. @view.content_tag 'div' do
  33. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  34. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true',
  35. @agent.send(:boolify, value) == true, html_options
  36. @view.concat "True"
  37. end)
  38. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  39. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false',
  40. @agent.send(:boolify, value) == false, html_options
  41. @view.concat "False"
  42. end)
  43. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  44. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual',
  45. @agent.send(:boolify, value).nil?, html_options
  46. @view.concat "Manual Input"
  47. end)
  48. @view.concat(@view.text_field_tag("agent[options][#{attribute}]", value,
  49. html_options.merge(class: "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}")))
  50. end
  51. when :array
  52. @view.select_tag "agent[options][#{attribute}]", nil,
  53. html_options.deep_merge(class: 'form-control',
  54. data: { value:, cache_response: data[:cache_response] != false })
  55. when :string
  56. @view.text_field_tag "agent[options][#{attribute}]", value,
  57. html_options.deep_merge(class: 'form-control', data: { cache_response: data[:cache_response] != false })
  58. when :number
  59. @view.number_field_tag "agent[options][#{attribute}]", value,
  60. html_options.deep_merge(class: 'form-control', data: { cache_response: data[:cache_response] != false })
  61. when :json
  62. @view.text_area_tag "agent[options][#{attribute}]", value,
  63. html_options.deep_merge(class: 'form-control live-json-editor', rows: 10)
  64. end
  65. end
  66. end