form_configurable_agent_presenter.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 = {role: (data[:roles] + ['form-configurable']).join(' '), data: {attribute: attribute}}
  17. case data[:type]
  18. when :text
  19. @view.content_tag 'div' do
  20. @view.concat @view.text_area_tag("agent[options][#{attribute}]", value, html_options.merge(class: 'form-control', rows: 3))
  21. if data[:ace].present?
  22. @view.concat @view.content_tag('div', '', class: 'ace-editor', data: { source: "[name='agent[options][#{attribute}]']" })
  23. end
  24. end
  25. when :boolean
  26. @view.content_tag 'div' do
  27. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  28. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true', @agent.send(:boolify, value) == true, html_options
  29. @view.concat "True"
  30. end)
  31. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  32. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false', @agent.send(:boolify, value) == false, html_options
  33. @view.concat "False"
  34. end)
  35. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  36. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual', @agent.send(:boolify, value) == nil, html_options
  37. @view.concat "Manual Input"
  38. end)
  39. @view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
  40. end
  41. when :array, :string
  42. @view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => 'form-control')
  43. end
  44. end
  45. end