bullet_train-super_scaffolding 1.0.5 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bullet_train/super_scaffolding/engine.rb +8 -0
- data/lib/bullet_train/super_scaffolding/scaffolders/crud_field_scaffolder.rb +1 -1
- data/lib/bullet_train/super_scaffolding/scaffolders/crud_scaffolder.rb +1 -3
- data/lib/bullet_train/super_scaffolding/version.rb +1 -1
- data/lib/bullet_train/super_scaffolding.rb +1 -1
- data/lib/scaffolding/block_manipulator.rb +140 -0
- data/lib/scaffolding/script.rb +1 -0
- data/lib/scaffolding/transformer.rb +13 -12
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2cb314ed84525cc6dafce8c9c1ddeec7b51486c8e759b1ab32b176791730e4f
|
4
|
+
data.tar.gz: 3d72bc268faeb84f61bec331bc5fda61e2d298e539ed184ce273d614d18303eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12fc83283e2bd199994e47457e09ad18f097cc61c80d10ec67b55e72971e198f76616fad859539ad2be547683a77b3a7abcbd725bb69cdc005566c679cb80324
|
7
|
+
data.tar.gz: 4e3ae7e9b86a6ae8341384081ca05b87aa71e0c0de4d4af9277c554a05d6ac5765064e840fa274ae428c9e016ada8d1e3e180f3f048f363c0d5eb2d06cbc17cb
|
@@ -1,6 +1,14 @@
|
|
1
1
|
module BulletTrain
|
2
2
|
module SuperScaffolding
|
3
3
|
class Engine < ::Rails::Engine
|
4
|
+
initializer "bullet_train.super_scaffolding.register_template_path" do |app|
|
5
|
+
# Templates from the application itself should always be highest priority.
|
6
|
+
# This allows application developers to locally overload any template from any package.
|
7
|
+
BulletTrain::SuperScaffolding.template_paths << Rails.root.to_s
|
8
|
+
|
9
|
+
# Register the base path of this package with the Super Scaffolding engine.
|
10
|
+
BulletTrain::SuperScaffolding.template_paths << File.expand_path("../../../..", __FILE__)
|
11
|
+
end
|
4
12
|
end
|
5
13
|
end
|
6
14
|
end
|
@@ -26,7 +26,7 @@ module BulletTrain
|
|
26
26
|
# get all the attributes.
|
27
27
|
attributes = argv[1..-1]
|
28
28
|
|
29
|
-
check_required_options_for_attributes(
|
29
|
+
check_required_options_for_attributes("crud-field", attributes, child)
|
30
30
|
|
31
31
|
transformer = Scaffolding::Transformer.new(child, parents, @options)
|
32
32
|
transformer.add_attributes_to_various_views(attributes, type: :crud_field)
|
@@ -45,9 +45,7 @@ module BulletTrain
|
|
45
45
|
# get all the attributes.
|
46
46
|
attributes = argv[2..-1]
|
47
47
|
|
48
|
-
check_required_options_for_attributes(
|
49
|
-
|
50
|
-
binding.pry
|
48
|
+
check_required_options_for_attributes("crud", attributes, child, parent)
|
51
49
|
|
52
50
|
transformer = Scaffolding::Transformer.new(child, parents, @options)
|
53
51
|
transformer.scaffold_crud(attributes)
|
@@ -13,7 +13,7 @@ module BulletTrain
|
|
13
13
|
"crud" => "BulletTrain::SuperScaffolding::Scaffolders::CrudScaffolder",
|
14
14
|
"crud-field" => "BulletTrain::SuperScaffolding::Scaffolders::CrudFieldScaffolder",
|
15
15
|
"join-model" => "BulletTrain::SuperScaffolding::Scaffolders::JoinModelScaffolder",
|
16
|
-
"oauth-provider" => "BulletTrain::SuperScaffolding::Scaffolders::OauthProviderScaffolder"
|
16
|
+
"oauth-provider" => "BulletTrain::SuperScaffolding::Scaffolders::OauthProviderScaffolder"
|
17
17
|
}
|
18
18
|
|
19
19
|
class Runner
|
@@ -0,0 +1,140 @@
|
|
1
|
+
class Scaffolding::BlockManipulator
|
2
|
+
attr_accessor :lines
|
3
|
+
|
4
|
+
def initialize(filepath)
|
5
|
+
@filepath = filepath
|
6
|
+
@lines = File.readlines(filepath)
|
7
|
+
end
|
8
|
+
|
9
|
+
#
|
10
|
+
# Wrap a block of ruby code inside another block
|
11
|
+
#
|
12
|
+
# @param [String] starting A string to search for at the start of the block. Eg "<%= updates_for context, collection do"
|
13
|
+
# @param [Array] with An array with two String elements. The text that should wrap the block. Eg ["<%= action_model_select_controller do %>", "<% end %>"]
|
14
|
+
#
|
15
|
+
def wrap_block(starting:, with:)
|
16
|
+
with[0] += "\n" unless with[0].match?(/\n$/)
|
17
|
+
with[1] += "\n" unless with[1].match?(/\n$/)
|
18
|
+
starting_line = find_block_start(starting)
|
19
|
+
end_line = find_block_end(starting_from: starting_line)
|
20
|
+
|
21
|
+
final = []
|
22
|
+
block_indent = ""
|
23
|
+
spacer = " "
|
24
|
+
@lines.each_with_index do |line, index|
|
25
|
+
line += "\n" unless line.match?(/\n$/)
|
26
|
+
if index < starting_line
|
27
|
+
final << line
|
28
|
+
elsif index == starting_line
|
29
|
+
block_indent = line.match(/^\s*/).to_s
|
30
|
+
final << block_indent + with[0]
|
31
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
32
|
+
elsif index > starting_line && index < end_line
|
33
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
34
|
+
elsif index == end_line
|
35
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
36
|
+
final << block_indent + with[1]
|
37
|
+
else
|
38
|
+
final << line
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
@lines = final
|
43
|
+
unless @lines.last.match?(/\n$/)
|
44
|
+
@lines.last += "\n"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def insert(content, within: nil, after: nil, before: nil, after_block: nil, append: false)
|
49
|
+
# Search for before like we do after, we'll just inject before it.
|
50
|
+
after ||= before
|
51
|
+
|
52
|
+
# If within is given, find the start and end lines of the block
|
53
|
+
content += "\n" unless content.match?(/\n$/)
|
54
|
+
start_line = 0
|
55
|
+
end_line = @lines.count - 1
|
56
|
+
if within.present?
|
57
|
+
start_line = find_block_start(within)
|
58
|
+
end_line = find_block_end(starting_from: start_line)
|
59
|
+
# start_line += 1 # ensure we actually insert the content _within_ the given block
|
60
|
+
# end_line += 1 if end_line == start_line
|
61
|
+
end
|
62
|
+
if after_block.present?
|
63
|
+
block_start = find_block_start(after_block)
|
64
|
+
block_end = find_block_end(starting_from: block_start)
|
65
|
+
start_line = block_end
|
66
|
+
end_line = @lines.count - 1
|
67
|
+
end
|
68
|
+
index = start_line
|
69
|
+
match = false
|
70
|
+
while index < end_line && !match
|
71
|
+
line = @lines[index]
|
72
|
+
if after.nil? || line.match?(after)
|
73
|
+
unless append
|
74
|
+
match = true
|
75
|
+
# We adjust the injection point if we really wanted to insert before.
|
76
|
+
insert_line(content, index - (before ? 1 : 0))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
index += 1
|
80
|
+
end
|
81
|
+
|
82
|
+
return if match
|
83
|
+
|
84
|
+
# Match should always be false here.
|
85
|
+
if append && !match
|
86
|
+
insert_line(content, index - 1)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def insert_line(content, insert_at_index)
|
91
|
+
content += "\n" unless content.match?(/\n$/)
|
92
|
+
final = []
|
93
|
+
@lines.each_with_index do |line, index|
|
94
|
+
indent = line.match(/^\s*/).to_s
|
95
|
+
final << line
|
96
|
+
if index == insert_at_index
|
97
|
+
final << indent + content
|
98
|
+
end
|
99
|
+
end
|
100
|
+
@lines = final
|
101
|
+
end
|
102
|
+
|
103
|
+
def insert_block(block_content, after_block:)
|
104
|
+
block_start = find_block_start(after_block)
|
105
|
+
block_end = find_block_end(starting_from: block_start)
|
106
|
+
insert_line(block_content[0], block_end)
|
107
|
+
insert_line(block_content[1], block_end + 1)
|
108
|
+
end
|
109
|
+
|
110
|
+
def write
|
111
|
+
File.write(@filepath, @lines.join)
|
112
|
+
end
|
113
|
+
|
114
|
+
def find_block_start(starting_string)
|
115
|
+
matcher = Regexp.escape(starting_string)
|
116
|
+
starting_line = 0
|
117
|
+
|
118
|
+
@lines.each_with_index do |line, index|
|
119
|
+
if line.match?(matcher)
|
120
|
+
starting_line = index
|
121
|
+
break
|
122
|
+
end
|
123
|
+
end
|
124
|
+
starting_line
|
125
|
+
end
|
126
|
+
|
127
|
+
def find_block_end(starting_from:)
|
128
|
+
depth = 0
|
129
|
+
current_line = starting_from
|
130
|
+
@lines[starting_from..@lines.count].each_with_index do |line, index|
|
131
|
+
current_line = starting_from + index
|
132
|
+
depth += 1 if line.match?(/\s*<%.+ do .*%>/)
|
133
|
+
depth += 1 if line.match?(/\s*<% if .*%>/)
|
134
|
+
depth += 1 if line.match?(/\s*<% unless .*%>/)
|
135
|
+
depth -= 1 if line.match?(/\s*<%.* end .*%>/)
|
136
|
+
break current_line if depth == 0
|
137
|
+
end
|
138
|
+
current_line
|
139
|
+
end
|
140
|
+
end
|
data/lib/scaffolding/script.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "indefinite_article"
|
2
2
|
require "yaml"
|
3
|
+
require "scaffolding/class_names_transformer"
|
3
4
|
|
4
5
|
class Scaffolding::Transformer
|
5
6
|
attr_accessor :child, :parent, :parents, :class_names_transformer, :cli_options, :additional_steps, :namespace, :suppress_could_not_find
|
@@ -111,11 +112,11 @@ class Scaffolding::Transformer
|
|
111
112
|
# Originally all the potential source files were in the repository alongside the application.
|
112
113
|
# Now the files could be provided by an included Ruby gem, so we allow those Ruby gems to register their base
|
113
114
|
# path and then we check them in order to see which template we should use.
|
114
|
-
BulletTrain::SuperScaffolding.template_paths.
|
115
|
+
BulletTrain::SuperScaffolding.template_paths.map do |base_path|
|
115
116
|
base_path = Pathname.new(base_path)
|
116
117
|
resolved_path = base_path.join(file).to_s
|
117
|
-
File.
|
118
|
-
end.compact.first
|
118
|
+
File.exist?(resolved_path) ? resolved_path : nil
|
119
|
+
end.compact.first || raise("Couldn't find the Super Scaffolding template for `#{file}` in any of the following locations:\n\n#{BulletTrain::SuperScaffolding.template_paths.join("\n")}")
|
119
120
|
end
|
120
121
|
|
121
122
|
def get_transformed_file_content(file)
|
@@ -275,7 +276,7 @@ class Scaffolding::Transformer
|
|
275
276
|
begin
|
276
277
|
target_file_content = File.read(transformed_file_name)
|
277
278
|
rescue Errno::ENOENT => _
|
278
|
-
puts "Couldn't find '#{transformed_file_name}'".red unless suppress_could_not_find
|
279
|
+
puts "Couldn't find '#{transformed_file_name}'".red unless (suppress_could_not_find || options[:suppress_could_not_find])
|
279
280
|
return false
|
280
281
|
end
|
281
282
|
|
@@ -755,7 +756,6 @@ class Scaffolding::Transformer
|
|
755
756
|
end
|
756
757
|
|
757
758
|
# field on the form.
|
758
|
-
file_name = "./app/views/account/scaffolding/completely_concrete/tangible_things/_form.html.erb"
|
759
759
|
field_attributes = {method: ":#{name}"}
|
760
760
|
field_options = {}
|
761
761
|
|
@@ -799,7 +799,12 @@ class Scaffolding::Transformer
|
|
799
799
|
end
|
800
800
|
|
801
801
|
field_content = "<%= render 'shared/fields/#{type}'#{", " if field_attributes.any?}#{field_attributes.map { |key, value| "#{key}: #{value}" }.join(", ")} %>"
|
802
|
-
|
802
|
+
|
803
|
+
# TODO Add more of these from other packages?
|
804
|
+
is_core_model = ["Team", "User", "Membership"].include?(child)
|
805
|
+
|
806
|
+
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/_form.html.erb", field_content, ERB_NEW_FIELDS_HOOK, prepend: true, suppress_could_not_find: is_core_model)
|
807
|
+
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/_fields.html.erb", field_content, ERB_NEW_FIELDS_HOOK, prepend: true, suppress_could_not_find: !is_core_model)
|
803
808
|
end
|
804
809
|
|
805
810
|
#
|
@@ -1000,12 +1005,8 @@ class Scaffolding::Transformer
|
|
1000
1005
|
|
1001
1006
|
# TODO The serializers can't handle these `has_rich_text` attributes.
|
1002
1007
|
unless type == "trix_editor"
|
1003
|
-
|
1004
|
-
|
1005
|
-
"./app/serializers/api/v1/scaffolding/completely_concrete/tangible_thing_serializer.rb"
|
1006
|
-
].each do |file|
|
1007
|
-
scaffold_add_line_to_file(file, ":#{name},", RUBY_NEW_FIELDS_HOOK, prepend: true)
|
1008
|
-
end
|
1008
|
+
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/_tangible_thing.json.jbuilder", ":#{name},", RUBY_NEW_FIELDS_HOOK, prepend: true, suppress_could_not_find: true)
|
1009
|
+
scaffold_add_line_to_file("./app/serializers/api/v1/scaffolding/completely_concrete/tangible_thing_serializer.rb", ":#{name},", RUBY_NEW_FIELDS_HOOK, prepend: true)
|
1009
1010
|
|
1010
1011
|
assertion = if type == "date_field"
|
1011
1012
|
"assert_equal Date.parse(tangible_thing_data['#{name}']), tangible_thing.#{name}"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-super_scaffolding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/bullet_train/super_scaffolding/scaffolders/oauth_provider_scaffolder.rb
|
46
46
|
- lib/bullet_train/super_scaffolding/version.rb
|
47
47
|
- lib/scaffolding.rb
|
48
|
+
- lib/scaffolding/block_manipulator.rb
|
48
49
|
- lib/scaffolding/class_names_transformer.rb
|
49
50
|
- lib/scaffolding/oauth_providers.rb
|
50
51
|
- lib/scaffolding/routes_file_manipulator.rb
|