Class: Recipes::VueAdmin
- Inherits:
-
Rails::AppBuilder
- Object
- Rails::AppBuilder
- Recipes::VueAdmin
- Defined in:
- lib/potassium/recipes/vue_admin.rb
Instance Method Summary collapse
- #active_admin_js ⇒ Object
- #add_component_integration ⇒ Object
- #add_vue_admin ⇒ Object
- #add_vue_component_library ⇒ Object
- #ask ⇒ Object
- #component_builder ⇒ Object
- #create ⇒ Object
- #install ⇒ Object
- #installed? ⇒ Boolean
- #vue_component ⇒ Object
Instance Method Details
#active_admin_js ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/potassium/recipes/vue_admin.rb', line 122 def active_admin_js " import { createApp } from 'vue';\n import AdminComponent from '../components/admin-component.vue';\n\n function onLoad() {\n if (document.getElementById('wrapper') !== null) {\n const app = createApp({\n mounted() {\n // We need to re-trigger DOMContentLoaded for ArcticAdmin after Vue replaces DOM elements\n window.document.dispatchEvent(new Event('DOMContentLoaded', {\n bubbles: true,\n cancelable: true,\n }));\n },\n });\n app.component('admin_component', AdminComponent);\n\n // Avoid using '#wrapper' as the mount point, as that includes the entire admin page,\n // which could be used for Client-Side Template Injection (CSTI) attacks. Limit the\n // mount point to specific areas where you need Vue components.\n\n // DO NOT mount Vue in elements that contain user input rendered by\n // ActiveAdmin.\n // By default ActiveAdmin doesn't escape {{ }} in user input, so it's\n // possible to inject arbitrary JavaScript code into the page.\n\n // app.mount('#wrapper');\n }\n\n return null;\n }\n\n document.addEventListener('DOMContentLoaded', onLoad, { once: true });\n HERE\nend\n" |
#add_component_integration ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/potassium/recipes/vue_admin.rb', line 46 def add_component_integration line = "class CustomFooter < ActiveAdmin::Component" initializer = "config/initializers/active_admin.rb" gsub_file initializer, /(#{Regexp.escape(line)})/mi do |_match| " require \"vue_component.rb\"\n AUTO_BUILD_ELEMENTS=[:admin_component, :template, :slot]\n component_creator(AUTO_BUILD_ELEMENTS)\n\n \#{line}\n HERE\n end\nend\n" |
#add_vue_admin ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/potassium/recipes/vue_admin.rb', line 34 def add_vue_admin add_vue_component_library add_component_integration insert_into_file( 'app/frontend/entrypoints/active_admin.js', active_admin_js ) copy_file '../assets/active_admin/admin-component.vue', 'app/frontend/components/admin-component.vue', force: true end |
#add_vue_component_library ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/potassium/recipes/vue_admin.rb', line 60 def add_vue_component_library lib_component_path = "lib/vue_component.rb" class_definitions = " \#{vue_component}\n \#{component_builder}\n HERE\n File.open(lib_component_path, \"w\") { |file| file.write(class_definitions) }\nend\n" |
#ask ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/potassium/recipes/vue_admin.rb', line 2 def ask if selected?(:admin_mode) vue_admin = answer(:vue_admin) do Ask.confirm "Do you want Vue support for ActiveAdmin?" end set(:vue_admin, vue_admin) end end |
#component_builder ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/potassium/recipes/vue_admin.rb', line 104 def component_builder " def component_creator(auto_build_elements)\n auto_build_elements.each do |element|\n as_string=element.to_s\n camelized_element = as_string.camelize\n Object.const_set(camelized_element,Class.new(VueComponent))\n Object.const_get(camelized_element).class_eval do\n builder_method as_string.to_sym\n def tag_name\n self.class.to_s.underscore\n end\n end\n end\n end\n HERE\nend\n" |
#create ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/potassium/recipes/vue_admin.rb', line 11 def create recipe = self if selected?(:vue_admin) after(:admin_install) do recipe.add_vue_admin end end end |
#install ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/potassium/recipes/vue_admin.rb', line 20 def install active_admin = load_recipe(:admin) if active_admin.installed? add_vue_admin info "VueAdmin installed" else info "VueAdmin can't be installed because Active Admin isn't installed." end end |
#installed? ⇒ Boolean
30 31 32 |
# File 'lib/potassium/recipes/vue_admin.rb', line 30 def installed? file_exist?("lib/vue_component.rb") end |
#vue_component ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/potassium/recipes/vue_admin.rb', line 70 def vue_component " class VueComponent < Arbre::Component\n builder_method :root\n def tag_name\n :root\n end\n\n def initialize(*)\n super\n end\n\n def build(attributes = {})\n super(process_attributes(attributes))\n end\n\n def process_attributes(attributes)\n vue_attributes = {}\n attributes.each do |key, value|\n dasherized_key = key.to_s.dasherize\n if value.is_a?(String)\n vue_attributes[dasherized_key] = value\n elsif !dasherized_key.index(':').nil? && dasherized_key.index(':').zero?\n vue_attributes[dasherized_key] = value.to_json\n else\n vue_attributes[\":\" + dasherized_key] = value.to_json\n end\n end\n vue_attributes\n end\n end\n HERE\nend\n" |