1
0

form_configurable_agent_presenter.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. ace_options = { source: "[name='agent[options][#{attribute}]']", mode: '', theme: ''}.deep_symbolize_keys!
  23. ace_options.deep_merge!(data[:ace].deep_symbolize_keys) if data[:ace].is_a?(Hash)
  24. @view.concat @view.content_tag('div', '', class: 'ace-editor', data: ace_options)
  25. end
  26. end
  27. when :boolean
  28. @view.content_tag 'div' do
  29. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  30. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true', @agent.send(:boolify, value) == true, html_options
  31. @view.concat "True"
  32. end)
  33. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  34. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false', @agent.send(:boolify, value) == false, html_options
  35. @view.concat "False"
  36. end)
  37. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  38. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual', @agent.send(:boolify, value) == nil, html_options
  39. @view.concat "Manual Input"
  40. end)
  41. @view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
  42. end
  43. when :array, :string
  44. @view.text_field_tag "agent[options][#{attribute}]", value, html_options.deep_merge(:class => 'form-control', data: {cache_response: data[:cache_response] != false})
  45. end
  46. end
  47. end