react_component_helper.rb 861 B

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. module ReactComponentHelper
  3. def react_component(name, props = {}, &block)
  4. data = { component: name.to_s.camelcase, props: Oj.dump(props) }
  5. if block.nil?
  6. div_tag_with_data(data)
  7. else
  8. content_tag(:div, data: data, &block)
  9. end
  10. end
  11. def react_admin_component(name, props = {})
  12. data = { 'admin-component': name.to_s.camelcase, props: Oj.dump(props) }
  13. div_tag_with_data(data)
  14. end
  15. def serialized_media_attachments(media_attachments)
  16. media_attachments.map { |attachment| serialized_attachment(attachment) }
  17. end
  18. private
  19. def div_tag_with_data(data)
  20. content_tag(:div, nil, data: data)
  21. end
  22. def serialized_attachment(attachment)
  23. ActiveModelSerializers::SerializableResource.new(
  24. attachment,
  25. serializer: REST::MediaAttachmentSerializer
  26. ).as_json
  27. end
  28. end