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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +23 -47
  3. data/lib/scaffold/cli.rb +16 -4
  4. data/lib/scaffold/erb_context.rb +11 -0
  5. data/lib/scaffold/generators/base_generator.rb +74 -0
  6. data/lib/scaffold/generators/controller_generator.rb +27 -0
  7. data/lib/scaffold/generators/fabricator_generator.rb +26 -0
  8. data/lib/scaffold/generators/locales_generator.rb +42 -0
  9. data/lib/scaffold/generators/models_generator.rb +53 -0
  10. data/lib/scaffold/generators/routes_generator.rb +48 -0
  11. data/lib/scaffold/generators/views_generator.rb +57 -0
  12. data/lib/scaffold/main.rb +15 -6
  13. data/lib/scaffold/models/resource.rb +66 -0
  14. data/lib/scaffold/rails.rb +54 -0
  15. data/lib/scaffold/services/controller.rb +112 -0
  16. data/lib/scaffold/services/nested_in_resources.rb +42 -0
  17. data/lib/scaffold/services/path.rb +72 -0
  18. data/lib/scaffold/services/resource.rb +73 -0
  19. data/lib/scaffold/services/route.rb +20 -0
  20. data/lib/scaffold/services/view.rb +14 -0
  21. data/lib/scaffold_pico.rb +6 -7
  22. data/lib/templates/pico/controller.rb.erb +34 -26
  23. data/lib/templates/pico/fabricators/fabrication.rb.erb +4 -4
  24. data/lib/templates/pico/locales/bg.scaffold_pico.yml.erb +35 -0
  25. data/lib/templates/pico/locales/en.scaffold_pico.yml.erb +35 -0
  26. data/lib/templates/pico/model.rb.erb +2 -2
  27. data/lib/templates/pico/search.rb.erb +12 -12
  28. data/lib/templates/pico/views/materialize/slim/_form.html.slim.erb +1 -4
  29. data/lib/templates/pico/views/materialize/slim/edit.html.slim.erb +9 -7
  30. data/lib/templates/pico/views/materialize/slim/index.html.slim.erb +15 -15
  31. data/lib/templates/pico/views/materialize/slim/new.html.slim.erb +7 -5
  32. data/lib/templates/pico/views/materialize/slim/show.html.slim.erb +10 -10
  33. data/lib/templates/pico/views/zurb/slim/_form.html.slim.erb +2 -5
  34. data/lib/templates/pico/views/zurb/slim/edit.html.slim.erb +8 -6
  35. data/lib/templates/pico/views/zurb/slim/index.html.slim.erb +14 -14
  36. data/lib/templates/pico/views/zurb/slim/new.html.slim.erb +6 -4
  37. data/lib/templates/pico/views/zurb/slim/show.html.slim.erb +7 -7
  38. metadata +22 -10
  39. data/lib/scaffold/base_generator.rb +0 -60
  40. data/lib/scaffold/controller_generator.rb +0 -24
  41. data/lib/scaffold/fabricator_generator.rb +0 -25
  42. data/lib/scaffold/models_generator.rb +0 -51
  43. data/lib/scaffold/params.rb +0 -174
  44. data/lib/scaffold/routes_generator.rb +0 -29
  45. data/lib/scaffold/views_generator.rb +0 -52
@@ -0,0 +1,57 @@
1
+ module Scaffold
2
+ module Generators
3
+ class ViewsGenerator < Scaffold::Generators::BaseGenerator
4
+ attr :css_framework, false
5
+ attr :template, false
6
+
7
+ def generate template, css_framework
8
+ @css_framework = css_framework
9
+ @template = template
10
+
11
+ views_path = create_views_path
12
+ templating_engine = choose_templating_engine
13
+
14
+ # print "Creating view:"
15
+ %w(index new edit show _form).each do |view_name|
16
+ # print view_name, ' '
17
+ create views_path, view_name, templating_engine, css_framework
18
+ end
19
+ # print "\n"
20
+ end
21
+
22
+ def create views_path, view_name, templating_engine, css_framework
23
+ source_file_name = "#{view_name}.html.#{templating_engine.extension}.erb"
24
+ target_file_name = "#{view_name}.html.#{templating_engine.extension}"
25
+ source_file_path = find_root(templates, 'views',
26
+ css_framework,
27
+ templating_engine.source_folder_name,
28
+ source_file_name)
29
+ content = File.read(source_file_path)
30
+
31
+ content = parse_template(content, {rails: @rails})
32
+
33
+
34
+ target_file_path = File.join(create_views_path, target_file_name)
35
+ write_with_confirmation(target_file_path, content)
36
+ end
37
+
38
+ def create_views_path
39
+ views_path = File.join(Dir.pwd, 'app', 'views', @rails.controller.namespaces_as_path, @rails.view.folder_name)
40
+ FileUtils.mkpath(views_path)
41
+ views_path
42
+ end
43
+
44
+
45
+ def choose_templating_engine
46
+ case template
47
+ when 'slim'
48
+ ::Scaffold::TemplateEngines::Slim.new
49
+ else
50
+ raise "I don't have defined templates for #{template}. However you can use [slim,]"
51
+ end
52
+ end
53
+
54
+
55
+ end
56
+ end
57
+ end
@@ -1,16 +1,25 @@
1
+ require_relative 'generators/base_generator'
2
+ require_relative 'generators/controller_generator'
3
+ require_relative 'generators/models_generator'
4
+ require_relative 'generators/views_generator'
5
+ require_relative 'generators/routes_generator'
6
+ require_relative 'generators/fabricator_generator'
7
+ require_relative 'generators/locales_generator'
8
+
1
9
  module Scaffold
2
10
  class Main
3
11
  def initialize choice
4
12
  @choice = choice
5
- @params = Params.new(@choice)
13
+ @rails = Rails.new(@choice)
6
14
  end
7
15
 
8
16
  def run
9
- Scaffold::FabricatorGenerator.new(@params).generate if @choice[:fabrication]
10
- Scaffold::ControllerGenerator.new(@params).generate
11
- Scaffold::ModelsGenerator.new(@params).generate
12
- Scaffold::ViewsGenerator.new(@params).generate
13
- Scaffold::RoutesGenerator.new(@params).generate
17
+ Scaffold::Generators::FabricatorGenerator.new(@rails).generate if @choice[:fabrication]
18
+ Scaffold::Generators::ControllerGenerator.new(@rails).generate
19
+ Scaffold::Generators::ModelsGenerator.new(@rails).generate
20
+ Scaffold::Generators::ViewsGenerator.new(@rails).generate(@choice[:template], @choice[:css_framework])
21
+ Scaffold::Generators::RoutesGenerator.new(@rails).generate
22
+ Scaffold::Generators::LocalesGenerator.new(@rails).generate
14
23
  end
15
24
 
16
25
  end
@@ -0,0 +1,66 @@
1
+ module Scaffold
2
+ module Models
3
+ class Resource
4
+
5
+ def initialize raw_input
6
+ (@modules, @class_name) = parse_model(raw_input)
7
+ end
8
+
9
+ # company_ownership
10
+ #
11
+ # Doesn't include modules
12
+ #
13
+ # for usage in controllers: company_ownership = CompanyOwnership.find...
14
+ # for usage in paths: link_to [company_ownership] do ...
15
+ def name
16
+ @class_name.tableize.singularize
17
+ end
18
+
19
+ def instance_name
20
+ "@#{name}"
21
+ end
22
+
23
+ def param_name
24
+ "#{name}_id"
25
+ end
26
+
27
+ # CompanyOwnership
28
+ def class_name
29
+ @class_name.singularize
30
+ end
31
+
32
+ def modules
33
+ @modules
34
+ end
35
+
36
+ # is the resource in modules Reports::Payment
37
+ def modules?
38
+ @modules.blank?
39
+ end
40
+
41
+ # company_ownerships
42
+ def collection_name
43
+ @class_name.tableize
44
+ end
45
+
46
+ # Admin::User or only User
47
+ def class_name_with_modules
48
+ if modules.empty?
49
+ class_name
50
+ else
51
+ "#{modules.join('::')}::#{class_name}"
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # model is something like Admin::User
58
+ def parse_model model
59
+ chunks = model.split('::').select{|chunk| !chunk.empty?}
60
+ class_name = chunks.pop
61
+ [chunks, class_name]
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'services/controller'
2
+ require_relative 'services/path'
3
+ require_relative 'services/resource'
4
+ require_relative 'services/nested_in_resources'
5
+ require_relative 'services/route'
6
+ require_relative 'services/view'
7
+
8
+ module Scaffold
9
+ class Rails
10
+ attr :controller, false
11
+ attr :path, false
12
+ attr :choice, false
13
+ attr :resource, false
14
+ attr :nested_in_resources, false
15
+ attr :route, false
16
+ attr :view, false
17
+
18
+ def initialize choice
19
+ @choice = choice
20
+
21
+ @resource = Services::Resource.new(self)
22
+ @nested_in_resources = Services::NestedInResources.new(self)
23
+ @controller = Services::Controller.new(self)
24
+ @route = Services::Route.new(self)
25
+ @path = Services::Path.new(self)
26
+ @view = Services::View.new(self)
27
+
28
+ debug_info if choice[:debug]
29
+ end
30
+
31
+ def debug_info
32
+ puts "\n"
33
+ puts "Debug:"
34
+ puts "resource_class_name: #{@resource.class_name}, resource_name: #{@resource.name}, collection_name: #{@resource.collection_name}"
35
+ puts "class_name_with_modules: #{@resource.class_name_with_modules}"
36
+
37
+ puts "\ncontroller:"
38
+ puts "class: #{@controller.class_name}"
39
+
40
+ puts "\nroutes:"
41
+ puts "route_resource_name: #{@route.resource_name}"
42
+ puts "controller_namespaces: #{@controller.namespaces_as_path} (for urls helpers)"
43
+ puts "resource_paths"
44
+ puts " new_resource_path: #{@path.new_resource}"
45
+ puts " resource_path: #{@path.resource}"
46
+ puts " edit_resource_path: #{@path.edit_resource}"
47
+ puts " instance_resource_path: #{@path.instance_resource}"
48
+ puts " edit_instance_resource: #{@path.edit_instance_resource}"
49
+ puts " collection_path: #{@path.collection}"
50
+ puts "\n\n"
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,112 @@
1
+ module Scaffold
2
+ module Services
3
+ class Controller
4
+ def initialize rails
5
+ @rails = rails
6
+ end
7
+
8
+ # example: Manage::BooksController
9
+ def class_name
10
+ if @rails.controller.namespaced?
11
+ "#{namespaces_as_modules}::#{@rails.resource.class_name.pluralize}Controller"
12
+ else
13
+ "#{@rails.resource.class_name.pluralize}Controller"
14
+ end
15
+ end
16
+
17
+ def file_name
18
+ "#{@rails.resource.class_name.tableize}_controller.rb"
19
+ end
20
+
21
+ def base_controller
22
+ @rails.choice[:base_controller] || 'ApplicationController'
23
+ end
24
+
25
+ # very bad idea to go so deep
26
+ def index_joins
27
+ @rails.choice[:joins]
28
+ end
29
+
30
+ def index_includes
31
+ @rails.choice[:includes]
32
+ end
33
+
34
+ def namespaced?
35
+ namespaces_as_string.present?
36
+ end
37
+
38
+ # Admin::ClassRooms::Etc
39
+ def namespaces_as_modules
40
+ namespaces_as_path.join.camelize
41
+ end
42
+
43
+ # ['admin', 'classrooms' ]
44
+ def namespaces_as_path
45
+ (namespaces_as_string || "").split('/').collect{|name| name.underscore}
46
+ end
47
+
48
+
49
+ # which fields will be visible on the index page
50
+ def index_fields
51
+ @rails.choice[:index_fields].blank? ? @rails.resource.fields.keys : @rails.choice[:index_fields]
52
+ end
53
+
54
+ def search_fields
55
+ @rails.choice[:search_fields].blank? ? @rails.resource.fields.keys : @rails.choice[:search_fields]
56
+ end
57
+
58
+ # convert company => company_id
59
+ # Used in the #index action
60
+ def permitted_search_fields
61
+ fields = @rails.resource.fields
62
+
63
+ to_ids = []
64
+
65
+ expanded_fields = expand_association_to_ids fields
66
+
67
+ search_fields.each do |key|
68
+ next unless expanded_fields.keys.include?(key)
69
+ type = expanded_fields[key]
70
+ to_ids << (['references', 'belongs_to'].include?(type.downcase) ? "#{key}_id" : key)
71
+ end
72
+ to_ids
73
+ end
74
+
75
+ def permitted_fields
76
+ associations_to_ids(@rails.resource.fields)
77
+ end
78
+
79
+
80
+ private
81
+
82
+ # admin/classrooms/etc
83
+ def namespaces_as_string
84
+ @namespaces_as_path ||= @rails.choice[:controller_namespaces]
85
+ end
86
+
87
+
88
+ # if the field is belongs_to
89
+ # make so that fields contains the `field` and field_id
90
+ def expand_association_to_ids fields
91
+ expanded = {}
92
+ fields.each_pair do |name, type|
93
+ case type
94
+ when 'belongs_to'
95
+ expanded["#{name}_id"] = 'integer'
96
+ end
97
+ end
98
+ fields.merge(expanded)
99
+ end
100
+
101
+ # convert company => company_id
102
+ def associations_to_ids hash
103
+ to_ids = {}
104
+ hash.each_pair do |key, type|
105
+ key_name = ['references', 'belongs_to'].include?(type.downcase) ? "#{key}_id" : key
106
+ to_ids[key_name] = type
107
+ end
108
+ to_ids
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,42 @@
1
+ module Scaffold
2
+ module Services
3
+ class NestedInResources
4
+ attr :resources
5
+
6
+ def initialize rails
7
+ @rails = rails
8
+
9
+ @resources = nested_in_resources_as_array.collect do |raw_input|
10
+ Scaffold::Models::Resource.new(raw_input)
11
+ end
12
+ end
13
+
14
+ def nested?
15
+ !resources.empty?
16
+ end
17
+
18
+ def each &block
19
+ resources.each do |resource|
20
+ yield resource
21
+ end
22
+ end
23
+
24
+ def collect &block
25
+ resources.collect do |resource|
26
+ yield resource
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def nested_in_resources_as_array
33
+ (nested_in_resources || "").split('/')
34
+ end
35
+
36
+ def nested_in_resources
37
+ @rails.choice[:nested_in_resources]
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ module Scaffold
2
+ module Services
3
+ class Path
4
+ def initialize rails
5
+ @rails = rails
6
+ end
7
+
8
+ def new_resource
9
+ segments = [':new']
10
+ segments += namespace_segments if @rails.controller.namespaced?
11
+ segments += nested_resource_segments if @rails.route.nested?
12
+ segments << ":#{@rails.resource.name}"
13
+ "[#{segments.join(', ')}]"
14
+ end
15
+
16
+ def resource
17
+ segments = []
18
+ segments += namespace_segments if @rails.controller.namespaced?
19
+ segments += nested_resource_segments if @rails.route.nested?
20
+ segments << @rails.resource.name
21
+ "[#{segments.join(', ')}]"
22
+ end
23
+
24
+ def edit_resource
25
+ segments = [':edit']
26
+ segments += namespace_segments if @rails.controller.namespaced?
27
+ segments += nested_resource_segments if @rails.route.nested?
28
+ segments << @rails.resource.name
29
+ "[#{segments.join(', ')}]"
30
+ end
31
+
32
+ def instance_resource
33
+ segments = []
34
+ segments += namespace_segments if @rails.controller.namespaced?
35
+ segments += nested_resource_segments if @rails.route.nested?
36
+ segments << "@#{@rails.resource.name}"
37
+ "[#{segments.join(', ')}]"
38
+ end
39
+
40
+ def edit_instance_resource
41
+ segments = [':edit']
42
+ segments += namespace_segments if @rails.controller.namespaced?
43
+ segments += nested_resource_segments if @rails.route.nested?
44
+ segments << "@#{@rails.resource.name}"
45
+ "[#{segments.join(', ')}]"
46
+ end
47
+
48
+ def collection
49
+ segments = []
50
+ segments += namespace_segments if @rails.controller.namespaced?
51
+ segments += nested_resource_segments if @rails.route.nested?
52
+ segments << ":#{@rails.resource.collection_name}"
53
+ "[#{segments.join(', ')}]"
54
+ end
55
+
56
+ private
57
+
58
+ # [":admin", ":companies"]
59
+ def namespace_segments
60
+ @rails.controller.namespaces_as_path.map{|segment| ":#{segment}".to_sym }
61
+ end
62
+
63
+ # [client]
64
+ def nested_resource_segments
65
+ @rails.nested_in_resources.collect do |nested_resource|
66
+ nested_resource.name
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,73 @@
1
+ module Scaffold
2
+ module Services
3
+ class Resource
4
+ attr :resource
5
+
6
+ def initialize rails
7
+ @rails = rails
8
+ @resource = Scaffold::Models::Resource.new(@rails.choice[:model])
9
+ end
10
+
11
+ def model_name
12
+ @resource.model_name
13
+ end
14
+ def modules
15
+ @resource.modules
16
+ end
17
+ def modules?
18
+ @resource.modules?
19
+ end
20
+ def name
21
+ @resource.name
22
+ end
23
+ def class_name
24
+ @resource.class_name
25
+ end
26
+ def collection_name
27
+ @resource.collection_name
28
+ end
29
+ def class_name_with_modules
30
+ @resource.class_name_with_modules
31
+ end
32
+
33
+ def file_name
34
+ "#{name}.rb"
35
+ end
36
+
37
+ # Search::Manage:BooksSearch
38
+ def search_class_name
39
+ if @rails.controller.namespaced?
40
+ "Search::#{@rails.controller.namespaces_as_modules}::#{class_name.pluralize}Search"
41
+ else
42
+ "Search::#{class_name.pluralize}Search"
43
+ end
44
+ end
45
+
46
+ def services_folder
47
+ @rails.choice[:services_folder]
48
+ end
49
+
50
+ def fields
51
+ expand_default_types(hasherize_fields(@rails.choice[:fields]))
52
+ end
53
+
54
+ private
55
+
56
+ def expand_default_types hash
57
+ hash.each_pair do |key, value|
58
+ hash[key] = 'string' if value.blank?
59
+ end
60
+ end
61
+
62
+ def hasherize_fields fields_array
63
+ fields = {}
64
+ fields_array.each do |field_string|
65
+ (key, value) = field_string.split(':')
66
+ fields[key] = value
67
+ end
68
+ fields
69
+ end
70
+
71
+ end
72
+ end
73
+ end