rails_vue_generator 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 845ede3c6dc62f3710389fc8e9ec87d70849388a18102ae4c0a676dcb84a6a32
4
+ data.tar.gz: 29dd83376ecd343ea0a24df9f247955400ef66dd503815e6ccc5098e6d1f5767
5
+ SHA512:
6
+ metadata.gz: a7c2f93ee0151486fa17c00f72c9f00ba248a6c478fb03700ea653a2ec26e30070a398e9c74812a6b0428feb6214bb2627aa82118eb4c63fd7fcfcce0304d9b6
7
+ data.tar.gz: 00c2cb6ef9e8ca3f9e2b9a2b94ebd74850f73062994255b56d2751ba37bfcad12d1ecbc713c9bc43aca5667a5acd0e51b143e968876be8fa470db7398edabc15
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1 @@
1
+ rails_vue_generator
@@ -0,0 +1 @@
1
+ ruby-2.7.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_vue_generator.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_vue_generator (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (13.0.1)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 2.0)
16
+ rails_vue_generator!
17
+ rake (~> 13.0)
18
+
19
+ BUNDLED WITH
20
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Stewart McKee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # RailsVueGenerator
2
+
3
+ The Rails Vue Generator is intended to build a starter Vue application, or build a sample application you can copy and paste from. The contents of the application are based on your rails application. Its loosely based on the idea for building templates for views in rails when you build a scaffold.
4
+
5
+ Install by adding to your Gemfile
6
+
7
+ gem 'rails_vue_generator'
8
+
9
+ Usage:
10
+
11
+ Start a rails console and run
12
+
13
+ RailsVueGenerator::Generator.generate
14
+
15
+ This will generate a `vue_example` folder that contains the vue application sample code.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_vue_generator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ require "rails_vue_generator/version"
2
+ require "rails_vue_generator/handlers/base_handler"
3
+ require "rails_vue_generator/handlers/index_handler"
4
+ require "rails_vue_generator/handlers/show_handler"
5
+ require "rails_vue_generator/handlers/edit_handler"
6
+ require "rails_vue_generator/handlers/router_handler"
7
+ require "rails_vue_generator/handlers/navigation_handler"
8
+ require "rails_vue_generator/handlers/api_handler"
9
+ require "rails_vue_generator/handlers/store_handler"
10
+
11
+ require "rails_vue_generator/generator"
12
+
13
+ require "rails_vue_generator/base_generator"
14
+ require "rails_vue_generator/model_generator"
15
+ require "rails_vue_generator/navigation_generator"
16
+ require "rails_vue_generator/router_generator"
17
+ require "rails_vue_generator/api_generator"
18
+ require "rails_vue_generator/store_generator"
19
+
20
+ module RailsVueGenerator
21
+ class Error < StandardError; end
22
+ end
@@ -0,0 +1,27 @@
1
+ module RailsVueGenerator
2
+ class ApiGenerator < BaseGenerator
3
+
4
+ def initialize(options = {})
5
+ options[:path] = File.join(Generator::ROOT_PATH, "src", "api") unless options.key?(:path)
6
+ @options = options
7
+ end
8
+
9
+ def generate
10
+ FileUtils.mkdir_p(@options[:path])
11
+ index_lines = []
12
+ models.each do |model|
13
+ index_lines << "import #{model.to_s.downcase} from '@/api/#{model.to_s.downcase}'"
14
+ end
15
+ index_lines << ""
16
+ index_lines << "export default {"
17
+ models.each do |model|
18
+ index_lines << " #{model.to_s.downcase}: #{model.to_s.downcase},"
19
+ end
20
+ index_lines << "}"
21
+ File.write(File.join(@options[:path], "index.js"), index_lines.join("\n") )
22
+
23
+ models.map{|model| File.write(File.join(@options[:path], "#{model.to_s.downcase}.js"), Handlers::ApiHandler.new(model).generate) }
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ module RailsVueGenerator
2
+ class BaseGenerator
3
+ IGNORED_CONSTANTS = %i[Fixnum Bignum SourceAnnotationExtractor NIL Data TRUE FALSE TimeoutError].freeze
4
+
5
+ def models
6
+ Module.constants.reject { |c| IGNORED_CONSTANTS.include?(c) }.select do |constant_name|
7
+ constant = eval constant_name.to_s
8
+ if !constant.nil? && constant.is_a?(Class) && (constant.superclass == ApplicationRecord)
9
+ constant
10
+ end
11
+ end.map { |c| eval c.to_s }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+
2
+
3
+ module RailsVueGenerator
4
+ class Generator
5
+ ROOT_PATH = File.join(Dir.getwd, "vue_example")
6
+
7
+ def generate
8
+ ModelGenerator.new.generate
9
+
10
+ RouterGenerator.new.generate
11
+ NavigationGenerator.new.generate
12
+ ApiGenerator.new.generate
13
+ StoreGenerator.new.generate
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,47 @@
1
+
2
+ module RailsVueGenerator
3
+ module Handlers
4
+ class ApiHandler < BaseHandler
5
+
6
+ def generate
7
+ lines = []
8
+ lines << "import axios from 'axios';"
9
+ lines << ""
10
+ lines << "export const HTTP = axios.create({"
11
+ lines << " baseURL: process.env.VUE_APP_API_ENDPOINT || 'http://localhost:3000'"
12
+ lines << "})"
13
+ lines << ""
14
+ lines << "export default {"
15
+
16
+ lines << " async getAll() {"
17
+ lines << " const response = await HTTP.get(`/#{model_name.pluralize}`)"
18
+ lines << " return response"
19
+ lines << " },"
20
+
21
+ lines << " async get(id) {"
22
+ lines << " const response = await HTTP.get(`/#{model_name.pluralize}/${id}`)"
23
+ lines << " return response"
24
+ lines << " },"
25
+
26
+ lines << " async create(data) {"
27
+ lines << " const response = await HTTP.post(`/#{model_name.pluralize}`, {#{model_name}: data})"
28
+ lines << " return response"
29
+ lines << " },"
30
+
31
+ lines << " async update(id, data) {"
32
+ lines << " const response = await HTTP.put(`/#{model_name.pluralize}/${id}`, {#{model_name}: data})"
33
+ lines << " return response"
34
+ lines << " },"
35
+
36
+ lines << " async delete(id) {"
37
+ lines << " const response = await HTTP.delete(`/#{model_name.pluralize}/${id}`)"
38
+ lines << " return response"
39
+ lines << " }"
40
+
41
+ lines << "}"
42
+
43
+ lines.join("\n")
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ module RailsVueGenerator
2
+ module Handlers
3
+ class BaseHandler
4
+ IGNORED_COLUMNS = [:id].freeze
5
+
6
+ def initialize(model)
7
+ @model = model
8
+ end
9
+
10
+ def model_name
11
+ @model.to_s.downcase
12
+ end
13
+
14
+ def generate
15
+ lines = []
16
+ lines << template
17
+ lines << script
18
+ lines << style
19
+ lines.join("\n\n")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsVueGenerator
4
+ module Handlers
5
+ class EditHandler < BaseHandler
6
+ def template
7
+ edit_form_lines = []
8
+ edit_form_lines << '<template>'
9
+ edit_form_lines << '<div>'
10
+ edit_form_lines << "<div v-if='loading' class=\"spinner\">Loading #{model_name.titleize}</div>"
11
+ edit_form_lines << "<div v-else>"
12
+ ignored_columns = [:id]
13
+
14
+ @model.columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }.each do |column|
15
+ name = column.name
16
+ type = column.type
17
+ edit_form_lines << "<div class='form-group'>"
18
+ case column.type
19
+ when :boolean
20
+ edit_form_lines << "<input type='checkbox' id='checkbox' v-model='#{model_name}.#{name}'>"
21
+ edit_form_lines << "<label for='checkbox'>#{name.titleize}</label>"
22
+ when :string
23
+ input_type = if name.downcase == 'email'
24
+ 'email'
25
+ else
26
+ 'text'
27
+ end
28
+ edit_form_lines << "<label for='#{name}'>#{name.titleize}</label>"
29
+ edit_form_lines << "<input type='#{input_type}' id='#{name}' v-model='#{model_name}.#{name}'>"
30
+ when :integer
31
+ edit_form_lines << "<label for='#{name}'>#{name.titleize}</label>"
32
+ edit_form_lines << "<input type='number' id='#{name}' v-model='#{model_name}.#{name}'>"
33
+ when :datetime
34
+ edit_form_lines << "<label for='#{name}'>#{name.titleize}</label>"
35
+ edit_form_lines << "<input type='datetime' id='#{name}' v-model='#{model_name}.#{name}'>"
36
+ when :json
37
+ when :text
38
+ edit_form_lines << "<label for='#{name}'>#{name.titleize}</label>"
39
+ edit_form_lines << "<textarea id='#{name}' v-model='#{model_name}.#{name}'/>"
40
+ when :date
41
+ edit_form_lines << "<label for='#{name}'>#{name.titleize}</label>"
42
+ edit_form_lines << "<input type='date' id='#{name}' v-model='#{model_name}.#{name}'>"
43
+ end
44
+ edit_form_lines << '</div>'
45
+ end
46
+ edit_form_lines << "<div>"
47
+ edit_form_lines << "<a href='#' class='btn btn-primary' @click=\"save\">Save Changes</a> "
48
+ edit_form_lines << "<router-link :to=\"{name: '#{model_name.pluralize}'}\">Back</router-link>"
49
+ edit_form_lines << '</div>'
50
+ edit_form_lines << '</div>'
51
+ edit_form_lines << '</div>'
52
+ edit_form_lines << '</template>'
53
+ edit_form_lines.join("\n")
54
+ end
55
+
56
+ def script
57
+ lines = []
58
+
59
+ lines << '<script>'
60
+ lines << " import {mapState} from 'vuex'"
61
+ lines << ' export default {'
62
+ lines << " name: '#{model_name}-edit',"
63
+ lines << ' computed:{'
64
+ lines << ' ...mapState({'
65
+ lines << " #{model_name}: state => state.#{model_name}.current,"
66
+ lines << " loading: state => state.#{model_name}.loading.current"
67
+ lines << ' }),'
68
+ lines << ' },'
69
+ lines << ' methods: {'
70
+ lines << ' save(){'
71
+ lines << " const data = this.#{model_name}"
72
+ lines << " if (this.#{model_name}.id !== undefined) {"
73
+ lines << " const id = this.#{model_name}.id"
74
+ lines << " console.log(\"updating \", this.#{model_name})"
75
+ lines << " this.$store.dispatch('#{model_name}/update', {id, data}).then(function(response) {this.$router.push({name: '#{model_name}', params: {id: response.id}})}.bind(this))"
76
+ lines << ' } else {'
77
+ lines << " console.log(\"updating \", this.#{model_name})"
78
+ lines << " this.$store.dispatch('#{model_name}/create', this.#{model_name}).then(function(response) {this.$router.push({name: '#{model_name}', params: {id: response.id}})}.bind(this))"
79
+ lines << ' }'
80
+ lines << ' }'
81
+ lines << ' },'
82
+ lines << ' created() {'
83
+ lines << ' if (this.$route.params.id !== undefined) {'
84
+ lines << " this.$store.dispatch('#{model_name}/get', this.$route.params.id)"
85
+ lines << " } else {"
86
+ lines << " this.$store.dispatch('#{model_name}/clearCurrent', this.$route.params.id)"
87
+ lines << " }"
88
+ lines << ' }'
89
+ lines << '};'
90
+ lines << '</script>'
91
+ lines.join("\n")
92
+ end
93
+
94
+ def style
95
+ lines = []
96
+ lines << "<style lang='scss' scoped>"
97
+ lines << '</style>'
98
+ lines.join("\n")
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,62 @@
1
+ module RailsVueGenerator
2
+ module Handlers
3
+ class IndexHandler < BaseHandler
4
+ def template
5
+ index_page_lines = []
6
+ index_page_lines << "<template><div><h1>#{model_name.titleize}</h1>"
7
+ index_page_lines << "<div class=\"spinner\" v-if='loading'>Loading #{model_name.pluralize.titleize}</div>"
8
+ index_page_lines << "<div v-else>"
9
+ index_page_lines << "<table>"
10
+ index_page_lines << "<tr>"
11
+ @model.columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }.each do |column|
12
+ index_page_lines << "<th>#{column.name.titleize}</th>"
13
+ end
14
+ index_page_lines << "<th></th>"
15
+ index_page_lines << "</tr>"
16
+
17
+ index_page_lines << "<tr v-for='#{model_name.downcase} in #{model_name.downcase.pluralize}' :key='#{model_name.downcase}.id'>"
18
+ @model.columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }.each do |column|
19
+ index_page_lines << "<td>{{#{model_name.downcase}.#{column.name}}}</td>"
20
+ end
21
+ index_page_lines << "<td><router-link :to=\"{name: '#{model_name}', params: {id: #{model_name.downcase}.id}}\">Show</router-link> | "
22
+ index_page_lines << "<router-link :to=\"{name: 'edit-#{model_name}', params: {id: #{model_name.downcase}.id}}\">Edit</router-link></td>"
23
+
24
+ index_page_lines << "</tr>"
25
+ index_page_lines << "</table>"
26
+ index_page_lines << "<router-link :to=\"{name: 'new-#{model_name}'}\">Create a new #{model_name.titleize}</router-link>"
27
+ index_page_lines << "</div>"
28
+ index_page_lines << "</div>"
29
+ index_page_lines << "</template>"
30
+ index_page_lines.join("\n")
31
+ end
32
+
33
+ def script
34
+ lines = []
35
+
36
+ lines << "<script>"
37
+ lines << "import {mapState} from 'vuex'"
38
+ lines << "export default {"
39
+ lines << "name: '#{model_name}-edit',"
40
+ lines << "computed:{"
41
+ lines << "...mapState({"
42
+ lines << "#{model_name.pluralize}: state => state.#{model_name}.all,"
43
+ lines << "loading: state => state.#{model_name}.loading.all"
44
+ lines << "}),"
45
+ lines << "},"
46
+ lines << "created() {"
47
+ lines << "this.$store.dispatch('#{model_name}/getAll')"
48
+ lines << "}"
49
+ lines << "};"
50
+ lines << "</script>"
51
+ lines.join("\n")
52
+ end
53
+
54
+ def style
55
+ lines = []
56
+ lines << "<style lang='scss' scoped>"
57
+ lines << "</style>"
58
+ lines.join("\n")
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module RailsVueGenerator
2
+ module Handlers
3
+ class NavigationHandler < BaseHandler
4
+ def generate
5
+ "<router-link :to=\"{name: '#{model_name.pluralize}'}\">#{model_name.titleize}</router-link> | "
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module RailsVueGenerator
2
+ module Handlers
3
+ class RouterHandler < BaseHandler
4
+ def generate
5
+ routes = []
6
+ routes << " {\n path: '/#{model_name.pluralize}',\n name: '#{model_name.pluralize}',\n component: () => import( /* webpackChunkName: \"#{model_name.pluralize}\" */ '@/views/#{model_name}/index.vue')\n },\n"
7
+ routes << " {\n path: '/#{model_name}/new',\n name: 'new-#{model_name}',\n component: () => import( /* webpackChunkName: \"#{model_name.pluralize}\" */ '@/views/#{model_name}/edit.vue')\n },\n"
8
+ routes << " {\n path: '/#{model_name}/:id',\n name: '#{model_name}',\n component: () => import( /* webpackChunkName: \"#{model_name.pluralize}\" */ '@/views/#{model_name}/show.vue')\n },\n"
9
+ routes << " {\n path: '/#{model_name}/:id/edit',\n name: 'edit-#{model_name}',\n component: () => import( /* webpackChunkName: \"#{model_name.pluralize}\" */ '@/views/#{model_name}/edit.vue')\n },\n"
10
+ routes
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ module RailsVueGenerator
2
+ module Handlers
3
+ class ShowHandler < BaseHandler
4
+ def template
5
+ show_page_lines = []
6
+ show_page_lines << "<template><div>"
7
+ ignored_columns = [:id]
8
+ show_page_lines << "<div v-if='loading' class=\"spinner\">Loading #{model_name.titleize}</div>"
9
+ show_page_lines << "<div v-else>"
10
+ show_page_lines << "<table>"
11
+ @model.columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }.each do |column|
12
+ show_page_lines << "<tr>"
13
+ show_page_lines << "<th>#{column.name.titleize}</th><td>{{#{model_name}.#{column.name}}}</td>"
14
+ show_page_lines << "</tr>"
15
+ end
16
+ show_page_lines << "</table>"
17
+ show_page_lines << "<div>"
18
+ show_page_lines << "<router-link :to=\"{name: 'edit-#{model_name}', params: {id: #{model_name}.id}}\">Edit</router-link>"
19
+ show_page_lines << "<a href='#' @click='deleteRecord'>Delete</a>"
20
+ show_page_lines << "</div>"
21
+ show_page_lines << "</div>"
22
+ show_page_lines << "</div></template>"
23
+ show_page_lines.join("\n")
24
+ end
25
+
26
+ def script
27
+ lines = []
28
+
29
+ lines << "<script>"
30
+ lines << "import {mapState} from 'vuex'"
31
+ lines << "export default {"
32
+ lines << " name: '#{model_name}-show',"
33
+ lines << " computed:{"
34
+ lines << " ...mapState({"
35
+ lines << " #{model_name}: state => state.#{model_name}.current,"
36
+ lines << " loading: state => state.#{model_name}.loading.current"
37
+ lines << " }),"
38
+ lines << " },"
39
+ lines << " created() {"
40
+ lines << " this.$store.dispatch('#{model_name}/get', this.$route.params.id)"
41
+ lines << " },"
42
+ lines << " methods: {"
43
+ lines << " deleteRecord() {"
44
+ lines << " this.$store.dispatch('#{model_name}/delete', this.#{model_name}.id).then(function(response) {this.$router.push({name: '#{model_name.pluralize}'})}.bind(this))"
45
+ lines << " }"
46
+ lines << " }"
47
+ lines << "};"
48
+ lines << "</script>"
49
+ lines.join("\n")
50
+ end
51
+
52
+ def style
53
+ lines = []
54
+ lines << "<style lang='scss' scoped>"
55
+ lines << "</style>"
56
+ lines.join("\n")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsVueGenerator
4
+ module Handlers
5
+ class StoreHandler < BaseHandler
6
+ def generate
7
+ lines = []
8
+ lines << "import api from '@/api'"
9
+ lines << "import router from '@/router'"
10
+ lines << ''
11
+ lines << 'const state = {'
12
+ lines << ' current: {},'
13
+ lines << ' all: [],'
14
+ lines << ' loading: {'
15
+ lines << ' current: false,'
16
+ lines << ' all: false'
17
+ lines << ' }'
18
+ lines << '}'
19
+ lines << ''
20
+ lines << 'const actions = {'
21
+ lines << ' async getAll({'
22
+ lines << ' commit'
23
+ lines << ' }, code) {'
24
+ lines << " commit('startLoading', 'all')"
25
+ lines << " const result = await api.#{model_name}.getAll()"
26
+ lines << ' const responseData = result.data'
27
+ lines << " commit('store#{model_name.pluralize.titleize}', responseData)"
28
+ lines << " commit('endLoading', 'all')"
29
+ lines << ' return responseData'
30
+ lines << ' },'
31
+ lines << ' async get({'
32
+ lines << ' commit'
33
+ lines << ' }, id) {'
34
+ lines << " commit('startLoading', 'current')"
35
+ lines << " const result = await api.#{model_name}.get(id)"
36
+ lines << ' const responseData = result.data'
37
+ lines << " commit('store#{model_name.titleize}', responseData)"
38
+ lines << " commit('endLoading', 'current')"
39
+ lines << ' return responseData'
40
+ lines << ' },'
41
+ lines << ' async create({commit}, data) {'
42
+ lines << " const result = await api.#{model_name}.create(data)"
43
+ lines << ' const responseData = result.data'
44
+ lines << " commit('store#{model_name.titleize}', responseData)"
45
+ lines << ' return responseData'
46
+ lines << ' },'
47
+ lines << ' async update({commit}, {id, data}) {'
48
+ lines << " commit('startLoading', 'current')"
49
+ lines << " const result = await api.#{model_name}.update(id, data)"
50
+ lines << ' const responseData = result.data'
51
+ lines << " commit('store#{model_name.titleize}', responseData)"
52
+ lines << " commit('endLoading', 'current')"
53
+ lines << ' return responseData'
54
+ lines << ' },'
55
+ lines << ' async delete({commit}, id) {'
56
+ lines << " const result = await api.#{model_name}.delete(id)"
57
+ lines << ' const responseData = result.data'
58
+ lines << " commit('clearCurrent')"
59
+ lines << ' return responseData'
60
+ lines << ' },'
61
+ lines << ' clearCurrent({commit}) {'
62
+ lines << ' commit("clearCurrent")'
63
+ lines << ' }'
64
+ lines << ''
65
+ lines << '}'
66
+ lines << 'const mutations = {'
67
+ lines << " store#{model_name.titleize}(state, #{model_name}) {"
68
+ lines << " state.current = #{model_name}"
69
+ lines << ' },'
70
+ lines << " store#{model_name.pluralize.titleize}(state, data) {"
71
+ lines << ' state.all = data'
72
+ lines << ' },'
73
+ lines << ' clearCurrent(state) {'
74
+ lines << ' state.current = {}'
75
+ lines << ' },'
76
+ lines << ' startLoading(state, key) {'
77
+ lines << ' state.loading[key] = true'
78
+ lines << ' },'
79
+ lines << ' endLoading(state, key) {'
80
+ lines << ' state.loading[key] = false'
81
+ lines << ' }'
82
+ lines << '}'
83
+ lines << ''
84
+ lines << 'export default {'
85
+ lines << ' namespaced: true,'
86
+ lines << ' state,'
87
+ lines << ' actions,'
88
+ lines << ' mutations'
89
+ lines << '}'
90
+ lines << ''
91
+
92
+ lines.join("\n")
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,20 @@
1
+ module RailsVueGenerator
2
+ class ModelGenerator < BaseGenerator
3
+ def initialize(options = {})
4
+ options[:path] = File.join(Generator::ROOT_PATH, "src", "views") unless options.key?(:path)
5
+ @options = options
6
+ end
7
+
8
+ def generate
9
+ models.map do |model|
10
+ FileUtils.mkdir_p(File.join(@options[:path], model.to_s.downcase))
11
+ puts "Writing #{File.join(@options[:path], model.to_s.downcase, "index.vue")}"
12
+ puts File.write(File.join(@options[:path], model.to_s.downcase, "index.vue"), Handlers::IndexHandler.new(model).generate)
13
+ puts "Writing #{File.join(@options[:path], model.to_s.downcase, "show.vue")}"
14
+ puts File.write(File.join(@options[:path], model.to_s.downcase, "show.vue"), Handlers::ShowHandler.new(model).generate)
15
+ puts "Writing #{File.join(@options[:path], model.to_s.downcase, "edit.vue")}"
16
+ puts File.write(File.join(@options[:path], model.to_s.downcase, "edit.vue"), Handlers::EditHandler.new(model).generate)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module RailsVueGenerator
2
+ class NavigationGenerator < BaseGenerator
3
+ def initialize(options = {})
4
+ options[:path] = File.join(Generator::ROOT_PATH, "src", "components") unless options.key?(:path)
5
+ options[:filename] = "navigation.vue" unless options.key?(:filename)
6
+ @options = options
7
+ end
8
+
9
+ def generate
10
+ FileUtils.mkdir_p(@options[:path])
11
+ File.write(File.join(@options[:path], @options[:filename]), "<template><div>" + models.map { |model| Handlers::NavigationHandler.new(model).generate }.join("\n") + "</div></template>")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module RailsVueGenerator
2
+ class RouterGenerator < BaseGenerator
3
+ def initialize(options = {})
4
+ options[:path] = File.join(Generator::ROOT_PATH, "src", "router") unless options.key?(:path)
5
+ options[:filename] = "index.js" unless options.key?(:filename)
6
+ @options = options
7
+ end
8
+
9
+ def generate
10
+ FileUtils.mkdir_p(@options[:path])
11
+ routes = []
12
+ models.each do |model|
13
+ routes += Handlers::RouterHandler.new(model).generate
14
+ end
15
+ File.write(File.join(@options[:path], @options[:filename]), "import Vue from 'vue'\nimport VueRouter from 'vue-router'\nVue.use(VueRouter)\nconst routes = [" + routes.join + "]\nconst router = new VueRouter({\nmode: 'history',\nbase: process.env.BASE_URL,\nroutes\n})\nexport default router\n")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ module RailsVueGenerator
2
+ class StoreGenerator < BaseGenerator
3
+
4
+ def initialize(options = {})
5
+ options[:path] = File.join(Generator::ROOT_PATH, "src", "store") unless options.key?(:path)
6
+ @options = options
7
+ end
8
+
9
+ def generate
10
+ FileUtils.mkdir_p(@options[:path])
11
+ FileUtils.mkdir_p(File.join(@options[:path], "modules"))
12
+ index_lines = []
13
+
14
+ index_lines << "import Vue from 'vue'"
15
+ index_lines << "import Vuex from 'vuex'"
16
+ models.each do |model|
17
+ index_lines << "import #{model.to_s.downcase} from '@/store/modules/#{model.to_s.downcase}'"
18
+ end
19
+ index_lines << ""
20
+ index_lines << "Vue.use(Vuex)"
21
+ index_lines << ""
22
+ index_lines << "export default new Vuex.Store({"
23
+ index_lines << " modules: {"
24
+ index_lines << " #{models.map{|model| model.to_s.downcase}.join(", ")}"
25
+ index_lines << " }"
26
+ index_lines << "})"
27
+ File.write(File.join(@options[:path], "index.js"), index_lines.join("\n") )
28
+
29
+ models.map{|model| File.write(File.join(@options[:path], "modules", "#{model.to_s.downcase}.js"), Handlers::StoreHandler.new(model).generate) }
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module RailsVueGenerator
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rails_vue_generator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_vue_generator"
8
+ spec.version = RailsVueGenerator::VERSION
9
+ spec.authors = ["Stewart McKee"]
10
+ spec.email = ["stewart@theizone.co.uk"]
11
+
12
+ spec.summary = "Generates VueJS code based no models in rails"
13
+ spec.description = "Generates VueJS code based no models in rails"
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 13.0"
29
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_vue_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Stewart McKee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ description: Generates VueJS code based no models in rails
42
+ email:
43
+ - stewart@theizone.co.uk
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".ruby-gemset"
50
+ - ".ruby-version"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/rails_vue_generator.rb
59
+ - lib/rails_vue_generator/api_generator.rb
60
+ - lib/rails_vue_generator/base_generator.rb
61
+ - lib/rails_vue_generator/generator.rb
62
+ - lib/rails_vue_generator/handlers/api_handler.rb
63
+ - lib/rails_vue_generator/handlers/base_handler.rb
64
+ - lib/rails_vue_generator/handlers/edit_handler.rb
65
+ - lib/rails_vue_generator/handlers/index_handler.rb
66
+ - lib/rails_vue_generator/handlers/navigation_handler.rb
67
+ - lib/rails_vue_generator/handlers/router_handler.rb
68
+ - lib/rails_vue_generator/handlers/show_handler.rb
69
+ - lib/rails_vue_generator/handlers/store_handler.rb
70
+ - lib/rails_vue_generator/model_generator.rb
71
+ - lib/rails_vue_generator/navigation_generator.rb
72
+ - lib/rails_vue_generator/router_generator.rb
73
+ - lib/rails_vue_generator/store_generator.rb
74
+ - lib/rails_vue_generator/version.rb
75
+ - rails_vue_generator.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.1.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Generates VueJS code based no models in rails
99
+ test_files: []