Class: Recipes::VueAdmin

Inherits:
Rails::AppBuilder
  • Object
show all
Defined in:
lib/potassium/recipes/vue_admin.rb

Instance Method Summary collapse

Instance Method Details

#active_admin_jsObject



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
# File 'lib/potassium/recipes/vue_admin.rb', line 128

def active_admin_js
  <<~HERE
    import { createApp } from 'vue';
    import AdminComponent from './components/admin-component.vue';

    function onLoad() {
      if (document.getElementById('wrapper') !== null) {
        const app = createApp({
          mounted() {
            // We need to re-trigger DOMContentLoaded for ArcticAdmin after Vue replaces DOM elements
            window.document.dispatchEvent(new Event('DOMContentLoaded', {
              bubbles: true,
              cancelable: true,
            }));
          },
        });
        app.component('AdminComponent', AdminComponent);
        app.mount('#wrapper');
      }

      return null;
    }

    document.addEventListener('DOMContentLoaded', onLoad, { once: true });
  HERE
end

#add_component_integrationObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/potassium/recipes/vue_admin.rb', line 52

def add_component_integration
  line = "class CustomFooter < ActiveAdmin::Component"
  initializer = "config/initializers/active_admin.rb"
  gsub_file initializer, /(#{Regexp.escape(line)})/mi do |_match|
    <<~HERE
      require "vue_component.rb"
      AUTO_BUILD_ELEMENTS=[:admin_component, :template, :slot]
      component_creator(AUTO_BUILD_ELEMENTS)

      #{line}
    HERE
  end
end

#add_vue_adminObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/potassium/recipes/vue_admin.rb', line 35

def add_vue_admin
  add_vue_component_library
  add_component_integration
  js_line = 'import "activeadmin_addons"'
  gsub_file(
    'app/javascript/active_admin.js',
    js_line,
    <<~HERE
      #{js_line}
      #{active_admin_js}
    HERE
  )
  copy_file '../assets/active_admin/admin-component.vue',
            'app/javascript/components/admin-component.vue',
            force: true
end

#add_vue_component_libraryObject



66
67
68
69
70
71
72
73
74
# File 'lib/potassium/recipes/vue_admin.rb', line 66

def add_vue_component_library
  lib_component_path = "lib/vue_component.rb"
  class_definitions =
    <<~HERE
      #{vue_component}
      #{component_builder}
    HERE
  File.open(lib_component_path, "w") { |file| file.write(class_definitions) }
end

#askObject



2
3
4
5
6
7
8
9
10
# 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)
    set(:front_end, :vue) if vue_admin
  end
end

#component_builderObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/potassium/recipes/vue_admin.rb', line 110

def component_builder
  <<~HERE
    def component_creator(auto_build_elements)
      auto_build_elements.each do |element|
        as_string=element.to_s
        camelized_element = as_string.camelize
        Object.const_set(camelized_element,Class.new(VueComponent))
        Object.const_get(camelized_element).class_eval do
          builder_method as_string.to_sym
          def tag_name
            self.class.to_s.underscore
          end
        end
      end
    end
  HERE
end

#createObject



12
13
14
15
16
17
18
19
# File 'lib/potassium/recipes/vue_admin.rb', line 12

def create
  recipe = self
  if selected?(:vue_admin)
    after(:admin_install) do
      recipe.add_vue_admin
    end
  end
end

#installObject



21
22
23
24
25
26
27
28
29
# File 'lib/potassium/recipes/vue_admin.rb', line 21

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/potassium/recipes/vue_admin.rb', line 31

def installed?
  dir_exist?("app/assets/javascripts/admin")
end

#vue_componentObject



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
103
104
105
106
107
108
# File 'lib/potassium/recipes/vue_admin.rb', line 76

def vue_component
  <<~HERE
    class VueComponent < Arbre::Component
      builder_method :root
      def tag_name
        :root
      end

      def initialize(*)
        super
      end

      def build(attributes = {})
        super(process_attributes(attributes))
      end

      def process_attributes(attributes)
        vue_attributes = {}
        attributes.each do |key, value|
          dasherized_key = key.to_s.dasherize
          if value.is_a?(String)
            vue_attributes[dasherized_key] = value
          elsif !dasherized_key.index(':').nil? && dasherized_key.index(':').zero?
            vue_attributes[dasherized_key] = value.to_json
          else
            vue_attributes[":" + dasherized_key] = value.to_json
          end
        end
        vue_attributes
      end
    end
  HERE
end