form_configurable_agent_presenter.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.text_area_tag "agent[options][#{attribute}]", value, html_options.merge(class: 'form-control', rows: 3)
  20. when :boolean
  21. @view.content_tag 'div' do
  22. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  23. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true', @agent.send(:boolify, value) == true, html_options
  24. @view.concat "True"
  25. end)
  26. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  27. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false', @agent.send(:boolify, value) == false, html_options
  28. @view.concat "False"
  29. end)
  30. @view.concat(@view.content_tag('label', class: 'radio-inline') do
  31. @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual', @agent.send(:boolify, value) == nil, html_options
  32. @view.concat "Manual Input"
  33. end)
  34. @view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
  35. end
  36. when :array, :string
  37. @view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => 'form-control')
  38. end
  39. end
  40. end