weapon 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/support/model_from_sql/model_from_sql_generator.rb +50 -0
- data/lib/support/util.rb +10 -0
- data/lib/weapon.rb +4 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a488fb189fbefd59f4d2bb80b4688e575a2b8eb1
|
4
|
+
data.tar.gz: d3b80d39dd07a7a136df60c11c2b5de5b65c06c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0391f8b3b33ad961172ec9b0d1d2af2513f5ef3a0942c2c38ac6c826efe341e1ae9e16df32848f149dfd4fad2a36807bdc6a51f8ac4dd6972a37e4ce4a329d03
|
7
|
+
data.tar.gz: be475562fee373f5d89796aa15c5adfbe84cc316cd518bf62feb43a38f820965c3a904ecf2698a30891aaacb5931278cd23c6ef4bfac45103d65ee5a09ceace6
|
@@ -2,22 +2,72 @@ class ModelFromSqlGenerator < Rails::Generators::Base
|
|
2
2
|
source_root File.expand_path("../templates", __FILE__)
|
3
3
|
|
4
4
|
def build_models
|
5
|
+
ap 'args is '
|
6
|
+
ap args[0]
|
5
7
|
tables = ActiveRecord::Base.connection.tables
|
6
8
|
|
7
9
|
Rails.application.eager_load!
|
8
10
|
in_models = ActiveRecord::Base.descendants.map(&:table_name)
|
11
|
+
ap 'tables from database'
|
12
|
+
ap tables
|
9
13
|
ap in_models
|
10
14
|
new_tables = tables - in_models - ['schema_migrations', 'ar_internal_metadata']
|
15
|
+
if args[0]
|
16
|
+
new_tables = new_tables.select {|table| table.include?(args[0])}
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
ap 'new tables'
|
21
|
+
ap new_tables
|
11
22
|
|
12
23
|
new_tables.each do |table_name|
|
13
24
|
generate "model #{table_name} --skip-migration" # or whatever you want here
|
14
25
|
model_file = "app/models/#{table_name.singularize}.rb"
|
26
|
+
ap 'new model file'
|
27
|
+
ap 'output columns'
|
28
|
+
self.generate_migrate(table_name)
|
29
|
+
ap model_file
|
15
30
|
inject_into_file model_file, before: "end\n" do <<-'RUBY'
|
16
31
|
self.table_name = "table_name_replace"
|
17
32
|
RUBY
|
18
33
|
end
|
19
34
|
gsub_file model_file, 'table_name_replace', table_name
|
20
35
|
end
|
36
|
+
end
|
21
37
|
|
38
|
+
def generate_migrate(table_name)
|
39
|
+
valid_columns = ActiveRecord::Base.connection.columns(table_name).select {|column| !['id', 'created_at', 'updated_at'].include?(column.name)}
|
40
|
+
ap valid_columns
|
41
|
+
add_columns = valid_columns.map do |column|
|
42
|
+
ap column
|
43
|
+
ap column.name
|
44
|
+
ap column.default
|
45
|
+
ap column.comment
|
46
|
+
ap column.sql_type_metadata
|
47
|
+
sql = "add_column :#{column.table_name}, :#{column.name}, :#{column.sql_type_metadata.type}"
|
48
|
+
sql += ", default: #{column.default}" if column.default && column.default.is_a?(Integer)
|
49
|
+
sql += ", default: '#{column.default}'" if column.default && column.default.is_a?(String)
|
50
|
+
sql += ", comment: '#{column.comment}'" if column.comment
|
51
|
+
sql += ", null: #{column.null}"
|
52
|
+
sql
|
53
|
+
end
|
54
|
+
ap add_columns
|
55
|
+
migrate_file = "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{table_name}.rb"
|
56
|
+
run "touch #{migrate_file}"
|
57
|
+
run "echo 'replace\n' >> #{migrate_file}"
|
58
|
+
inject_into_file migrate_file, before: "replace\n" do <<-'RUBY'
|
59
|
+
class CreateModelReplace < ActiveRecord::Migration
|
60
|
+
def change
|
61
|
+
create_table :table_replace
|
62
|
+
add_columns
|
63
|
+
end
|
64
|
+
end
|
65
|
+
RUBY
|
66
|
+
end
|
67
|
+
gsub_file migrate_file, 'ModelReplace', table_name.camelize
|
68
|
+
gsub_file migrate_file, 'add_columns', add_columns.join("\n ")
|
69
|
+
gsub_file migrate_file, 'table_replace', table_name.pluralize
|
70
|
+
gsub_file migrate_file, 'replace', ''
|
22
71
|
end
|
72
|
+
|
23
73
|
end
|
data/lib/support/util.rb
ADDED
data/lib/weapon.rb
CHANGED
@@ -131,17 +131,20 @@ class Weapon < Thor
|
|
131
131
|
end
|
132
132
|
|
133
133
|
desc "model_from_sql", "build model from sql"
|
134
|
+
method_option :contain, :type => :string, :default => '', :required => false
|
134
135
|
def model_from_sql
|
135
136
|
makesure_in_git
|
136
137
|
FileUtils.mkdir_p "lib/generators/model_from_sql"
|
137
138
|
copy_file 'support/model_from_sql/model_from_sql_generator.rb', 'lib/generators/model_from_sql/model_from_sql_generator.rb'
|
138
139
|
run 'git add lib/'
|
139
|
-
run
|
140
|
+
run "rails g model_from_sql #{options.contain}"
|
140
141
|
run 'git add app/'
|
141
142
|
run 'git add spec/'
|
142
143
|
run 'git add test/'
|
143
144
|
run 'git add lib/'
|
145
|
+
run 'git add db/migrate'
|
144
146
|
run 'git commit -a -m "add model_from_sql generator and build models from sql"'
|
147
|
+
run 'annonate'
|
145
148
|
end
|
146
149
|
|
147
150
|
desc "build all activeadmin page ", "build all activeadmin page"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weapon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- seaify
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- lib/support/model_from_sql/model_from_sql_generator.rb
|
115
115
|
- lib/support/rails_settings_ui/rails_settings_ui.rb
|
116
116
|
- lib/support/rails_settings_ui/setting.rb
|
117
|
+
- lib/support/util.rb
|
117
118
|
- lib/weapon.rb
|
118
119
|
homepage: https://github.com/seaify/weapon
|
119
120
|
licenses:
|
@@ -135,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
136
|
version: '0'
|
136
137
|
requirements: []
|
137
138
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.4.
|
139
|
+
rubygems_version: 2.4.8
|
139
140
|
signing_key:
|
140
141
|
specification_version: 4
|
141
142
|
summary: weapon for rails application!
|