scaffold_pico 0.3.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -47
- data/lib/scaffold/cli.rb +16 -4
- data/lib/scaffold/erb_context.rb +11 -0
- data/lib/scaffold/generators/base_generator.rb +74 -0
- data/lib/scaffold/generators/controller_generator.rb +27 -0
- data/lib/scaffold/generators/fabricator_generator.rb +26 -0
- data/lib/scaffold/generators/locales_generator.rb +42 -0
- data/lib/scaffold/generators/models_generator.rb +53 -0
- data/lib/scaffold/generators/routes_generator.rb +48 -0
- data/lib/scaffold/generators/views_generator.rb +57 -0
- data/lib/scaffold/main.rb +15 -6
- data/lib/scaffold/models/resource.rb +66 -0
- data/lib/scaffold/rails.rb +54 -0
- data/lib/scaffold/services/controller.rb +112 -0
- data/lib/scaffold/services/nested_in_resources.rb +42 -0
- data/lib/scaffold/services/path.rb +72 -0
- data/lib/scaffold/services/resource.rb +73 -0
- data/lib/scaffold/services/route.rb +20 -0
- data/lib/scaffold/services/view.rb +14 -0
- data/lib/scaffold_pico.rb +6 -7
- data/lib/templates/pico/controller.rb.erb +34 -26
- data/lib/templates/pico/fabricators/fabrication.rb.erb +4 -4
- data/lib/templates/pico/locales/bg.scaffold_pico.yml.erb +35 -0
- data/lib/templates/pico/locales/en.scaffold_pico.yml.erb +35 -0
- data/lib/templates/pico/model.rb.erb +2 -2
- data/lib/templates/pico/search.rb.erb +12 -12
- data/lib/templates/pico/views/materialize/slim/_form.html.slim.erb +1 -4
- data/lib/templates/pico/views/materialize/slim/edit.html.slim.erb +9 -7
- data/lib/templates/pico/views/materialize/slim/index.html.slim.erb +15 -15
- data/lib/templates/pico/views/materialize/slim/new.html.slim.erb +7 -5
- data/lib/templates/pico/views/materialize/slim/show.html.slim.erb +10 -10
- data/lib/templates/pico/views/zurb/slim/_form.html.slim.erb +2 -5
- data/lib/templates/pico/views/zurb/slim/edit.html.slim.erb +8 -6
- data/lib/templates/pico/views/zurb/slim/index.html.slim.erb +14 -14
- data/lib/templates/pico/views/zurb/slim/new.html.slim.erb +6 -4
- data/lib/templates/pico/views/zurb/slim/show.html.slim.erb +7 -7
- metadata +22 -10
- data/lib/scaffold/base_generator.rb +0 -60
- data/lib/scaffold/controller_generator.rb +0 -24
- data/lib/scaffold/fabricator_generator.rb +0 -25
- data/lib/scaffold/models_generator.rb +0 -51
- data/lib/scaffold/params.rb +0 -174
- data/lib/scaffold/routes_generator.rb +0 -29
- data/lib/scaffold/views_generator.rb +0 -52
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51f562ce19e922d879648cf3db894dea6a96a9c6
|
4
|
+
data.tar.gz: 1321074674bc39be2a893ff9bbcd4dda70259d5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bfcbd09575b717f2720f3ffdf1f1c45e7c40f904ff8607d333f4f7859a2a7bc5cc862b5a9ea40b002f4ea8ed694829abbf40e1d659f989a1ee0c72ffc37ed09
|
7
|
+
data.tar.gz: 220ba11acff7b3cd7a610eef5edaaf1add7e2b94530ee2e5fddaef3dc8b9403261db18c2c96b5381e39c4cd82771f3079df0f10eba7e44fc58caa21e6c55e3f8
|
data/README.md
CHANGED
@@ -15,9 +15,14 @@ No need to learn DSLs or support 3rd party gems.
|
|
15
15
|
* You can specify includes/joins for the #index action
|
16
16
|
* Includes search
|
17
17
|
|
18
|
+
![alt tag](https://raw.githubusercontent.com/gudata/scaffold_pico/master/doc/screenshot_index.jpg)
|
19
|
+
|
20
|
+
|
18
21
|
# SYNOPSIS
|
19
22
|
|
20
|
-
scaffold_pico.rb
|
23
|
+
scaffold_pico.rb \
|
24
|
+
-m Admin::Vector -n administration -b AdminController \
|
25
|
+
--nested_in_resources Assets::VectorFrame \
|
21
26
|
--fields name: description:text featured:boolean license:belongs_to group_id:integer svg:file \
|
22
27
|
--index-fields id name imported featured \
|
23
28
|
--search-fields name aspect_ratio license created_at \
|
@@ -25,54 +30,27 @@ No need to learn DSLs or support 3rd party gems.
|
|
25
30
|
--services_folder=services \
|
26
31
|
--debug --css_framework=materialize
|
27
32
|
|
33
|
+
|
28
34
|
# Install
|
35
|
+
The gem assume you have already in your Gemfile
|
36
|
+
|
37
|
+
gem 'kaminari'
|
38
|
+
gem 'slim-rails'
|
39
|
+
gem 'simple_form'
|
40
|
+
|
41
|
+
# And one of those
|
42
|
+
#
|
43
|
+
# gem 'materialize-sass'
|
44
|
+
# gem 'foundation-rails'
|
29
45
|
|
30
46
|
There is no need to have it in your gem file.
|
31
47
|
|
32
48
|
gem install scaffold_pico
|
33
49
|
|
34
|
-
Set some default I18n options
|
35
|
-
|
36
|
-
en.yml:
|
37
|
-
|
38
|
-
scaffold:
|
39
|
-
confirm: 'Are you sure?'
|
40
|
-
notices:
|
41
|
-
success:
|
42
|
-
create: Was successfully created %{model}
|
43
|
-
update: Was successfully updated %{model}
|
44
|
-
destroy: Was successfully destroyed %{model}
|
45
|
-
failed:
|
46
|
-
create: Fail creating %{model}
|
47
|
-
update: Fail updating %{model}
|
48
|
-
destroy: Fail destroing %{model}
|
49
|
-
new:
|
50
|
-
title: "New %{model}"
|
51
|
-
show:
|
52
|
-
title: "%{model}"
|
53
|
-
edit:
|
54
|
-
title: "Edit %{model}"
|
55
|
-
index:
|
56
|
-
title: "%{model}"
|
57
|
-
edit: Edit
|
58
|
-
show: Show
|
59
|
-
destroy: Delete
|
60
|
-
actions: Action
|
61
|
-
search:
|
62
|
-
header: Search
|
63
|
-
button: Search
|
64
|
-
reset: New
|
65
|
-
|
66
|
-
actions:
|
67
|
-
index: List %{model}
|
68
|
-
actions: 'Actions'
|
69
|
-
new: New %{model}
|
70
|
-
edit: Edit %{model}
|
71
|
-
|
72
50
|
# Overriding
|
73
|
-
If you want to change something you can
|
74
|
-
|
51
|
+
If you want to change something you can override/change
|
75
52
|
|
53
|
+
RAILS_ROOT/config/locales/en.scaffold_pico.yml
|
76
54
|
RAILS_ROOT/lib/templates/pico
|
77
55
|
/controller.rb
|
78
56
|
/search.rb
|
@@ -104,17 +82,15 @@ Clone the repo in ~/scaffold_pico or whatever, then in some rails project run th
|
|
104
82
|
--fields name:string
|
105
83
|
|
106
84
|
# Known Issues
|
107
|
-
|
85
|
+
If you can't start scaffold_pico you might need to update your bundler
|
108
86
|
|
109
|
-
|
87
|
+
gem update bundler
|
110
88
|
|
111
|
-
|
89
|
+
Help please: For some of the generated models/views there are extra blank links. It would be great if someone knows how to parse the erb and skip the new lines
|
112
90
|
|
113
91
|
You can't generate scaffolds for a mounted named engines. I don't know if we need this feature.
|
114
92
|
|
115
|
-
The Param class has become very messy because of all those variables, so it should be split in small helpers.
|
116
|
-
|
117
93
|
|
118
94
|
## Copyright
|
119
95
|
|
120
|
-
Copyright (c)
|
96
|
+
Copyright (c) 2017 gudata. See LICENSE.txt for further details.
|
data/lib/scaffold/cli.rb
CHANGED
@@ -16,16 +16,28 @@ module Scaffold
|
|
16
16
|
desc 'print some debug info'
|
17
17
|
end
|
18
18
|
|
19
|
+
option :force do
|
20
|
+
long '--force'
|
21
|
+
desc 'Overwrite without asking questions'
|
22
|
+
end
|
23
|
+
|
19
24
|
option :fabrication do
|
20
25
|
long '--fabrication'
|
21
26
|
desc 'Generate fabrication fabricator https://github.com/paulelliott/fabrication'
|
22
27
|
end
|
23
28
|
|
24
|
-
option :
|
29
|
+
option :controller_namespaces do
|
25
30
|
short '-n'
|
26
|
-
long '--
|
31
|
+
long '--controller_namespaces=namespace1/namespace2'
|
27
32
|
validate /\A(\w+(?:\/\w+)*)\z/
|
28
|
-
desc 'Optional
|
33
|
+
desc 'Optional namespaces for the controllers. Example: -n admin/secret_area. '
|
34
|
+
end
|
35
|
+
|
36
|
+
option :nested_in_resources do
|
37
|
+
short '-nr'
|
38
|
+
long '--nested_in_resources=Reports::Client/Building'
|
39
|
+
validate /\A(?:::)?(?:(?:[A-Z]\w*(?:::[A-Z]\w*)*)\/?)*\z/
|
40
|
+
desc 'Optional nest resource in other resources. Example: -nr Reports::Client/Building -m Room'
|
29
41
|
end
|
30
42
|
|
31
43
|
option :base_controller do
|
@@ -57,7 +69,7 @@ module Scaffold
|
|
57
69
|
option :fields do
|
58
70
|
long '-f'
|
59
71
|
long '--fields *FIELDS'
|
60
|
-
desc 'title:string body:text published:boolean amount:decimal attachment:file tracking_id:integer:uniq '
|
72
|
+
desc 'title:string library:belongs_to body:text published:boolean amount:decimal attachment:file tracking_id:integer:uniq '
|
61
73
|
end
|
62
74
|
|
63
75
|
option :index_fields do
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'readline'
|
2
|
+
|
3
|
+
module Scaffold
|
4
|
+
module Generators
|
5
|
+
class BaseGenerator
|
6
|
+
@@aways_ovewrite = false
|
7
|
+
|
8
|
+
def initialize rails
|
9
|
+
@rails = rails
|
10
|
+
end
|
11
|
+
|
12
|
+
def objectify
|
13
|
+
OpenStruct.new(vars).instance_eval { binding }
|
14
|
+
end
|
15
|
+
|
16
|
+
# where is the root of the gem
|
17
|
+
def root
|
18
|
+
Scaffold.root
|
19
|
+
end
|
20
|
+
|
21
|
+
def project_root
|
22
|
+
Dir.pwd
|
23
|
+
end
|
24
|
+
|
25
|
+
# Check to see if it is overridden and use it
|
26
|
+
def find_root *segments
|
27
|
+
original = File.join(root, segments)
|
28
|
+
overridden = File.join(project_root, segments)
|
29
|
+
File.exists?(overridden) ? overridden : original
|
30
|
+
end
|
31
|
+
|
32
|
+
# the root of the templates
|
33
|
+
def templates
|
34
|
+
'lib/templates/pico/'
|
35
|
+
end
|
36
|
+
|
37
|
+
def ask(prompt="", newline=false)
|
38
|
+
prompt += "\n" if newline
|
39
|
+
Readline.readline(prompt, true).squeeze(" ").strip
|
40
|
+
end
|
41
|
+
|
42
|
+
def write_with_confirmation(target_file_path, content)
|
43
|
+
unless File.exists?(target_file_path)
|
44
|
+
IO.write(target_file_path, content)
|
45
|
+
return
|
46
|
+
end
|
47
|
+
|
48
|
+
@@aways_ovewrite = 'a' if @rails.choice[:force].present?
|
49
|
+
|
50
|
+
answer = if @@aways_ovewrite
|
51
|
+
if @rails.choice[:force].blank?
|
52
|
+
puts "#{target_file_path} exists, overwrite? [yaN] y"
|
53
|
+
end
|
54
|
+
'y'
|
55
|
+
else
|
56
|
+
ask("#{target_file_path} exists, overwrite? [yaN]").downcase
|
57
|
+
end
|
58
|
+
|
59
|
+
@@aways_ovewrite = true if answer == 'a'
|
60
|
+
|
61
|
+
IO.write(target_file_path, content) if answer == 'y'
|
62
|
+
end
|
63
|
+
|
64
|
+
def parse_template(content, context_hash)
|
65
|
+
# http://www.stuartellis.eu/articles/erb/
|
66
|
+
# content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })#.gsub(/\s+\n$/, "")
|
67
|
+
|
68
|
+
# content = ::ERB.new(content, 0, '>').result(ErbContext.new(context_hash).get_binding)
|
69
|
+
content = ::ERB.new(content, 0, '-').result(ErbContext.new(context_hash).get_binding)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Scaffold
|
2
|
+
module Generators
|
3
|
+
class ControllerGenerator < Scaffold::Generators::BaseGenerator
|
4
|
+
|
5
|
+
def generate
|
6
|
+
controller_file_path = create_path(@rails.controller.file_name)
|
7
|
+
|
8
|
+
# puts "Creating #{controller_file_path}"
|
9
|
+
|
10
|
+
filepath = find_root(templates, 'controller.rb.erb')
|
11
|
+
content = File.read(filepath)
|
12
|
+
|
13
|
+
content = parse_template(content, {rails: @rails})
|
14
|
+
|
15
|
+
# puts content
|
16
|
+
write_with_confirmation(controller_file_path, content)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_path file_name
|
20
|
+
controller_path = File.join(Dir.pwd, 'app', 'controllers', @rails.controller.namespaces_as_path)
|
21
|
+
controller_file_path = File.join(controller_path, file_name)
|
22
|
+
FileUtils.mkpath(controller_path)
|
23
|
+
controller_file_path
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Scaffold
|
2
|
+
module Generators
|
3
|
+
class FabricatorGenerator < Scaffold::Generators::BaseGenerator
|
4
|
+
def generate
|
5
|
+
fabricators_path = create_fabricators_path
|
6
|
+
source_file_name = "fabrication.rb.erb"
|
7
|
+
target_file_name = "#{@rails.resource.name}_fabricator.rb"
|
8
|
+
source_file_path = find_root(templates, 'fabricators', source_file_name)
|
9
|
+
content = File.read(source_file_path)
|
10
|
+
|
11
|
+
content = parse_template(content, {rails: @rails})
|
12
|
+
|
13
|
+
target_file_path = File.join(fabricators_path, target_file_name)
|
14
|
+
|
15
|
+
write_with_confirmation(target_file_path, content)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_fabricators_path
|
19
|
+
fabricators_path = File.join(Dir.pwd, 'spec', 'fabricators')
|
20
|
+
FileUtils.mkpath(fabricators_path)
|
21
|
+
fabricators_path
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Scaffold
|
2
|
+
module Generators
|
3
|
+
class LocalesGenerator < Scaffold::Generators::BaseGenerator
|
4
|
+
def generate
|
5
|
+
return if existing_locale?
|
6
|
+
generate_locale('en')
|
7
|
+
generate_locale('bg')
|
8
|
+
end
|
9
|
+
|
10
|
+
def existing_locale?
|
11
|
+
Dir.glob(File.join(locales_path, '**')).each do |name|
|
12
|
+
return true if File.basename(name) =~ /scaffold_pico.yml/
|
13
|
+
end
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
def generate_locale(locale)
|
18
|
+
locales_path = create_locales_path
|
19
|
+
source_file_name = "#{locale}.scaffold_pico.yml.erb"
|
20
|
+
target_file_name = "#{locale}.scaffold_pico.yml"
|
21
|
+
source_file_path = find_root(templates, 'locales', source_file_name)
|
22
|
+
content = File.read(source_file_path)
|
23
|
+
|
24
|
+
content = parse_template(content, {rails: @rails})
|
25
|
+
|
26
|
+
target_file_path = File.join(locales_path, target_file_name)
|
27
|
+
|
28
|
+
write_with_confirmation(target_file_path, content)
|
29
|
+
end
|
30
|
+
|
31
|
+
def locales_path
|
32
|
+
locales_path = File.join(Dir.pwd, 'config', 'locales')
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_locales_path
|
36
|
+
FileUtils.mkpath(locales_path)
|
37
|
+
locales_path
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Scaffold
|
2
|
+
module Generators
|
3
|
+
class ModelsGenerator < Scaffold::Generators::BaseGenerator
|
4
|
+
def generate
|
5
|
+
searches_path = create_searches_path
|
6
|
+
puts "Add '#{@rails.resource.services_folder}' folder in your autoload_paths (application.rb)"
|
7
|
+
create_search_object searches_path
|
8
|
+
create_model
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_model
|
12
|
+
source_file_name = "model.rb.erb"
|
13
|
+
target_file_name = @rails.resource.file_name
|
14
|
+
|
15
|
+
source_file_path = find_root(templates, source_file_name)
|
16
|
+
content = File.read(source_file_path)
|
17
|
+
|
18
|
+
content = parse_template(content, {rails: @rails})
|
19
|
+
|
20
|
+
if @rails.resource.modules?
|
21
|
+
model_path = File.join(Dir.pwd, 'app', 'models')
|
22
|
+
else
|
23
|
+
model_path = File.join(Dir.pwd, 'app', 'models', @rails.resource.modules.map(&:underscore))
|
24
|
+
FileUtils.mkdir_p model_path
|
25
|
+
end
|
26
|
+
|
27
|
+
target_file_path = File.join(model_path, target_file_name)
|
28
|
+
|
29
|
+
write_with_confirmation(target_file_path, content)
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_search_object searches_path
|
33
|
+
source_file_name = "search.rb.erb"
|
34
|
+
target_file_name = "#{@rails.resource.name.pluralize}_search.rb"
|
35
|
+
|
36
|
+
source_file_path = find_root(templates, source_file_name)
|
37
|
+
content = File.read(source_file_path)
|
38
|
+
|
39
|
+
content = parse_template(content, {rails: @rails})
|
40
|
+
|
41
|
+
target_file_path = File.join(searches_path, target_file_name)
|
42
|
+
write_with_confirmation(target_file_path, content)
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_searches_path
|
46
|
+
searches_path = File.join(Dir.pwd, 'app', @rails.resource.services_folder, 'search', @rails.controller.namespaces_as_path)
|
47
|
+
FileUtils.mkpath(searches_path)
|
48
|
+
searches_path
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Scaffold
|
2
|
+
module Generators
|
3
|
+
class RoutesGenerator < Scaffold::Generators::BaseGenerator
|
4
|
+
|
5
|
+
def generate
|
6
|
+
puts "Insert something like this in routes.rb"
|
7
|
+
|
8
|
+
nested_resources = @rails.nested_in_resources.resources
|
9
|
+
nested_resources_count = nested_resources.count
|
10
|
+
|
11
|
+
namespaces_count = @rails.controller.namespaces_as_path.count
|
12
|
+
|
13
|
+
route_string = "#{spaces(nested_resources_count + namespaces_count + 2)}resources :#{@rails.route.resource_name}"
|
14
|
+
|
15
|
+
nested_resources.reverse.each_with_index do |resource, index|
|
16
|
+
route_string = resources_block(resource.name, namespaces_count + nested_resources_count - index, route_string)
|
17
|
+
end
|
18
|
+
|
19
|
+
@rails.controller.namespaces_as_path.reverse.each_with_index do |namespace, index|
|
20
|
+
index
|
21
|
+
route_string = namespace_block(namespace, namespaces_count - index, route_string)
|
22
|
+
end
|
23
|
+
puts route_string
|
24
|
+
end
|
25
|
+
|
26
|
+
def spaces(count)
|
27
|
+
" "*2*(count)
|
28
|
+
end
|
29
|
+
|
30
|
+
def resources_block resource_name, index, route_string
|
31
|
+
ident = spaces(index+1)
|
32
|
+
|
33
|
+
begin_resource_name_line = "#{ident}resources :#{resource_name.pluralize} do"
|
34
|
+
end_resource_name_line = "#{ident}end"
|
35
|
+
"#{begin_resource_name_line}\n#{route_string}\n#{end_resource_name_line}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def namespace_block namespace, index, route_string
|
39
|
+
ident = spaces(index+1)
|
40
|
+
|
41
|
+
begin_namespace_line = "#{ident}namespace :#{namespace} do"
|
42
|
+
end_namespace_line = "#{ident}end"
|
43
|
+
"#{begin_namespace_line}\n#{route_string}\n#{end_namespace_line}"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|