Guest User

Untitled

a guest
Feb 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. module VueHelper
  2. def vue_component(component_name, **props, &block)
  3. component_name = component_name.underscore.tr('_', '-')
  4. raw = props.delete(:raw)
  5. if block_given?
  6. "<#{component_name} #{attribute_options(props)} #{raw}>#{capture(&block)}</#{component_name}>".html_safe
  7. else
  8. "<#{component_name} #{attribute_options(props)} #{raw}></#{component_name}>".html_safe
  9. end
  10. end
  11.  
  12. private
  13.  
  14. def attribute_options(attributes = {})
  15. binded_props = attributes.delete(:binded) || {}
  16. event_props = attributes.delete(:events) || {}
  17. result_attrs = ''
  18.  
  19. attributes.each do |key, value|
  20. normalized_key = key.to_s.underscore.tr('_', '-')
  21. normalized_value = value
  22. if value.is_a?(Array)
  23. normalized_value.to_s.tr('"', '\'')
  24. elsif value =~ /"/
  25.  
  26. normalized_value.tr('"', '\'')
  27. end
  28. result_attrs += "#{normalized_key}=\"#{normalized_value}\" "
  29. end
  30.  
  31. binded_props.each do |key, value|
  32. normalized_key = key.to_s.underscore.tr('_', '-')
  33. normalized_value = value
  34. unless value.is_a?(String)
  35. # if there are some single quotes in the text, make them in ASCII
  36. # so HTML doesn't brake
  37. normalized_value = value.to_json.gsub("'", "&#39")
  38. end
  39. result_attrs += "v-bind:#{normalized_key}='#{normalized_value}' "
  40. end
  41.  
  42. event_props.each do |key, value|
  43. normalized_key = key.to_s.underscore.tr('_', '-')
  44. result_attrs += "v-on:#{normalized_key}='#{value}' "
  45. end
  46. result_attrs
  47. end
  48. end
Add Comment
Please, Sign In to add comment