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
@@ -1,17 +1,17 @@
1
- h1 = t('scaffold.show.title', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
1
+ h1 = t('scaffold.show.title', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
2
2
 
3
3
  .section
4
4
  .left
5
- = link_to <%= @collection_path %>, class: 'waves-effect waves-light btn' do
5
+ = link_to <%= @rails.path.collection %>, class: 'waves-effect waves-light btn' do
6
6
  i.material-icons.right view_list
7
- = t('scaffold.actions.index', model: <%= @resource_class_name %>.model_name.human(count: 2).mb_chars.downcase)
7
+ = t('scaffold.actions.index', model: <%= @rails.resource.class_name %>.model_name.human(count: 2).mb_chars.downcase)
8
8
  .right
9
- = link_to <%= @instance_edit_resource_path %>, class: 'btn waves-effect waves-light' do
9
+ = link_to <%= @rails.path.edit_instance_resource %>, class: 'btn waves-effect waves-light' do
10
10
  i.material-icons.right mode_edit
11
- = t('scaffold.actions.edit', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
12
- = link_to <%= @new_resource_path %>, class: 'btn waves-effect waves-light' do
11
+ = t('scaffold.actions.edit', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
12
+ = link_to <%= @rails.path.new_resource %>, class: 'btn waves-effect waves-light' do
13
13
  i.material-icons.right add
14
- = t('scaffold.actions.new', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
14
+ = t('scaffold.actions.new', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
15
15
 
16
16
  .clearfix
17
17
 
@@ -19,8 +19,8 @@ h1 = t('scaffold.show.title', model: <%= @resource_class_name %>.model_name.huma
19
19
  .card-content
20
20
  table.striped.highlight.responsive-table
21
21
  tbody
22
- <% @fields.keys.each do |field_name| %>
22
+ <% @rails.resource.fields.keys.each do |field_name| %>
23
23
  tr
24
- td = <%= @resource_class_name -%>.human_attribute_name(:<%= field_name -%>)
25
- td = @<%= @resource_name %>.<%= field_name -%>
24
+ td = <%= @rails.resource.class_name -%>.human_attribute_name(:<%= field_name -%>)
25
+ td = @<%= @rails.resource.name %>.<%= field_name -%>
26
26
  <% end %>
@@ -1,6 +1,6 @@
1
1
  = form.error_notification
2
2
  .form-inputs
3
- <% @fields.each_pair do |field_name, kind| -%>
3
+ <% @rails.resource.fields.each_pair do |field_name, kind| -%>
4
4
  <% if kind == 'file' -%>
5
5
  = form.input :<%= field_name -%>, as: :file
6
6
  <% elsif kind == 'belongs_to' -%>
@@ -12,7 +12,4 @@
12
12
  <% else -%>
13
13
  = form.input :<%= field_name %>
14
14
  <% end -%>
15
- <% end -%>
16
-
17
- .form-actions
18
- = form.submit class: 'waves-effect waves btn-large'
15
+ <% end -%>
@@ -1,11 +1,13 @@
1
- h1 = t('scaffold.edit.title', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
1
+ h1 = t('scaffold.edit.title', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
2
2
 
3
3
  .callout.clearfix
4
4
  .float-left
5
- = link_to t('scaffold.show.actions.index', model: <%= @resource_class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @collection_path %>, class: 'button small primary'
5
+ = link_to t('scaffold.show.actions.index', model: <%= @rails.resource.class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @rails.path.collection %>, class: 'button small primary'
6
6
  .float-right
7
- = link_to t('scaffold.actions.edit', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @instance_edit_resource_path %>, class: 'button small warning'
8
- = link_to t('scaffold.actions.new', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @new_resource_path %>, class: 'button small secondary'
7
+ = link_to t('scaffold.actions.edit', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @rails.path.edit_instance_resource %>, class: 'button small warning'
8
+ = link_to t('scaffold.actions.new', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @rails.path.new_resource %>, class: 'button small secondary'
9
9
 
10
- = simple_form_for <%= @instance_resource_path %> do |form|
11
- = render 'form', form: form
10
+ = simple_form_for <%= @rails.path.instance_resource %> do |form|
11
+ = render 'form', form: form
12
+ .form-actions
13
+ = form.submit class: 'waves-effect waves btn-large'
@@ -1,37 +1,37 @@
1
- h1 = t('scaffold.index.title', model: <%= @modulized_resource_class_name %>.model_name.human(count: 2).mb_chars.downcase)
1
+ h1 = t('scaffold.index.title', model: <%= @rails.resource.class_name_with_modules %>.model_name.human(count: 2).mb_chars.downcase)
2
2
 
3
3
  .row
4
4
  .small-12.medium-10.columns
5
5
  table
6
6
  thead
7
7
  tr
8
- <% @fields.select {|k,v| @index_fields.include?(k) }.keys.each do |field_name| %>
9
- th = <%= @modulized_resource_class_name -%>.human_attribute_name(:<%= field_name -%>)
8
+ <% @rails.resource.fields.select {|k,v| @rails.controller.index_fields.include?(k) }.keys.each do |field_name| %>
9
+ th = <%= @rails.resource.class_name_with_modules -%>.human_attribute_name(:<%= field_name -%>)
10
10
  <% end %>
11
11
  th colspan=3 = t('scaffold.index.actions')
12
12
 
13
13
  tbody
14
- - @<%= @collection_name %>.each do |<%= @resource_name %>|
14
+ - @<%= @rails.resource.collection_name %>.each do |<%= @rails.resource.name %>|
15
15
  tr
16
- <% @fields.select {|k,v| @index_fields.include?(k) }.keys.each do |field_name| %>
17
- td = <%= @resource_name %>.<%= field_name -%>
16
+ <% @rails.resource.fields.select {|k,v| @rails.controller.index_fields.include?(k) }.keys.each do |field_name| %>
17
+ td = <%= @rails.resource.name %>.<%= field_name -%>
18
18
  <% end %>
19
- td = link_to t('scaffold.index.show'), <%= @resource_path %>
20
- td = link_to t('scaffold.index.edit'), <%= @edit_resource_path %>
21
- td = link_to t('scaffold.index.destroy'), <%= @resource_path %>, data: {:confirm => t('scaffold.confirm')}, :method => :delete
22
- = paginate @<%= @collection_name %>
19
+ td = link_to t('scaffold.index.show'), <%= @rails.path.resource %>
20
+ td = link_to t('scaffold.index.edit'), <%= @rails.path.edit_resource %>
21
+ td = link_to t('scaffold.index.destroy'), <%= @rails.path.resource %>, data: {:confirm => t('scaffold.confirm')}, :method => :delete
22
+ = paginate @<%= @rails.resource.collection_name %>
23
23
 
24
24
  .small-12.medium-2.columns
25
25
  aside.callout.clearfix.actions
26
- = link_to t('scaffold.actions.new', model: <%= @modulized_resource_class_name %>.model_name.human.mb_chars.downcase), <%= @new_resource_path %>, class: 'button small secondary'
26
+ = link_to t('scaffold.actions.new', model: <%= @rails.resource.class_name_with_modules %>.model_name.human.mb_chars.downcase), <%= @rails.path.new_resource %>, class: 'button small secondary'
27
27
 
28
28
  aside.search.callout.clearfix
29
29
  h5 = t('scaffold.index.search.header')
30
30
 
31
- = simple_form_for <%= "@#{@collection_name}_search" %>, as: :<%= @collection_name %>_search, url: <%= @collection_path %>, method: :get do |form|
31
+ = simple_form_for <%= "@#{@rails.resource.collection_name}_search" %>, as: :<%= @rails.resource.collection_name %>_search, url: <%= @rails.path.collection %>, method: :get do |form|
32
32
  = form.error_notification
33
33
  .form-inputs
34
- <% @fields.select {|k,v| @search_fields.include?(k) }.each_pair do |field_name, field_type| %>
34
+ <% @rails.resource.fields.select {|k,v| @rails.controller.search_fields.include?(k) }.each_pair do |field_name, field_type| %>
35
35
  <% if field_type == 'belongs_to' -%>
36
36
  = form.input :<%= field_name -%>_id
37
37
  <% elsif ['date', 'datetime'].include?(field_type) -%>
@@ -45,4 +45,4 @@ h1 = t('scaffold.index.title', model: <%= @modulized_resource_class_name %>.mode
45
45
 
46
46
  .form-actions
47
47
  = form.submit t('scaffold.index.search.button'), class: 'button'
48
- = link_to t('scaffold.index.search.reset'), url_for(<%= @collection_path %>), class: 'button small secondary'
48
+ = link_to t('scaffold.index.search.reset'), url_for(<%= @rails.path.collection %>), class: 'button small secondary'
@@ -1,8 +1,10 @@
1
- h1 = t('scaffold.new.title', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
1
+ h1 = t('scaffold.new.title', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
2
2
 
3
3
  .callout.clearfix
4
4
  .float-left
5
- = link_to t('scaffold.show.actions.index', model: <%= @resource_class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @collection_path %>, class: 'button small primary'
5
+ = link_to t('scaffold.show.actions.index', model: <%= @rails.resource.class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @rails.path.collection %>, class: 'button small primary'
6
6
 
7
- = simple_form_for <%= @instance_resource_path %> do |form|
8
- = render 'form', form: form
7
+ = simple_form_for <%= @rails.path.instance_resource %> do |form|
8
+ = render 'form', form: form
9
+ .form-actions
10
+ = form.submit class: 'waves-effect waves btn-large'
@@ -1,16 +1,16 @@
1
- h1 = t('scaffold.show.title', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase)
1
+ h1 = t('scaffold.show.title', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase)
2
2
 
3
3
  .callout.clearfix
4
4
  .float-left
5
- = link_to t('scaffold.show.actions.index', model: <%= @resource_class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @collection_path %>, class: 'button small primary'
5
+ = link_to t('scaffold.show.actions.index', model: <%= @rails.resource.class_name %>.model_name.human(count: 2).mb_chars.downcase), <%= @rails.path.collection %>, class: 'button small primary'
6
6
  .float-right
7
- = link_to t('scaffold.actions.edit', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @instance_edit_resource_path %>, class: 'button small warning'
8
- = link_to t('scaffold.actions.new', model: <%= @resource_class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @new_resource_path %>, class: 'button small secondary'
7
+ = link_to t('scaffold.actions.edit', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @rails.path.edit_instance_resource %>, class: 'button small warning'
8
+ = link_to t('scaffold.actions.new', model: <%= @rails.resource.class_name %>.model_name.human(count: 1).mb_chars.downcase), <%= @rails.path.new_resource %>, class: 'button small secondary'
9
9
 
10
10
  table
11
11
  tbody
12
- <% @fields.keys.each do |field_name| %>
12
+ <% @rails.resource.fields.keys.each do |field_name| %>
13
13
  tr
14
- td = <%= @resource_class_name -%>.human_attribute_name(:<%= field_name -%>)
15
- td = @<%= @resource_name %>.<%= field_name -%>
14
+ td = <%= @rails.resource.class_name -%>.human_attribute_name(:<%= field_name -%>)
15
+ td = @<%= @rails.resource.name %>.<%= field_name -%>
16
16
  <% end %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaffold_pico
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gudata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-26 00:00:00.000000000 Z
11
+ date: 2017-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -92,19 +92,30 @@ files:
92
92
  - LICENSE.txt
93
93
  - README.md
94
94
  - bin/scaffold_pico
95
- - lib/scaffold/base_generator.rb
96
95
  - lib/scaffold/cli.rb
97
- - lib/scaffold/controller_generator.rb
98
- - lib/scaffold/fabricator_generator.rb
96
+ - lib/scaffold/erb_context.rb
97
+ - lib/scaffold/generators/base_generator.rb
98
+ - lib/scaffold/generators/controller_generator.rb
99
+ - lib/scaffold/generators/fabricator_generator.rb
100
+ - lib/scaffold/generators/locales_generator.rb
101
+ - lib/scaffold/generators/models_generator.rb
102
+ - lib/scaffold/generators/routes_generator.rb
103
+ - lib/scaffold/generators/views_generator.rb
99
104
  - lib/scaffold/main.rb
100
- - lib/scaffold/models_generator.rb
101
- - lib/scaffold/params.rb
102
- - lib/scaffold/routes_generator.rb
105
+ - lib/scaffold/models/resource.rb
106
+ - lib/scaffold/rails.rb
107
+ - lib/scaffold/services/controller.rb
108
+ - lib/scaffold/services/nested_in_resources.rb
109
+ - lib/scaffold/services/path.rb
110
+ - lib/scaffold/services/resource.rb
111
+ - lib/scaffold/services/route.rb
112
+ - lib/scaffold/services/view.rb
103
113
  - lib/scaffold/template_engines/slim.rb
104
- - lib/scaffold/views_generator.rb
105
114
  - lib/scaffold_pico.rb
106
115
  - lib/templates/pico/controller.rb.erb
107
116
  - lib/templates/pico/fabricators/fabrication.rb.erb
117
+ - lib/templates/pico/locales/bg.scaffold_pico.yml.erb
118
+ - lib/templates/pico/locales/en.scaffold_pico.yml.erb
108
119
  - lib/templates/pico/model.rb.erb
109
120
  - lib/templates/pico/search.rb.erb
110
121
  - lib/templates/pico/views/materialize/slim/_form.html.slim.erb
@@ -137,8 +148,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
148
  version: '0'
138
149
  requirements: []
139
150
  rubyforge_project:
140
- rubygems_version: 2.6.8
151
+ rubygems_version: 2.5.2
141
152
  signing_key:
142
153
  specification_version: 4
143
154
  summary: Scaffold should be simple
144
155
  test_files: []
156
+ has_rdoc:
@@ -1,60 +0,0 @@
1
- require 'readline'
2
-
3
- module Scaffold
4
- class BaseGenerator
5
- @@aways_ovewrite = false
6
-
7
- def initialize params
8
- @params = params
9
- end
10
-
11
- def objectify
12
- OpenStruct.new(vars).instance_eval { binding }
13
- end
14
-
15
- # where is the root of the gem
16
- def root
17
- Scaffold.root
18
- end
19
-
20
- def project_root
21
- Dir.pwd
22
- end
23
-
24
- # Check to see if it is overridden and use it
25
- def find_root *segments
26
- original = File.join(root, segments)
27
- overridden = File.join(project_root, segments)
28
- File.exists?(overridden) ? overridden : original
29
- end
30
-
31
- # the root of the templates
32
- def templates
33
- 'lib/templates/pico/'
34
- end
35
-
36
- def ask(prompt="", newline=false)
37
- prompt += "\n" if newline
38
- Readline.readline(prompt, true).squeeze(" ").strip
39
- end
40
-
41
- def write_with_confirmation(target_file_path, content)
42
- unless File.exists?(target_file_path)
43
- IO.write(target_file_path, content)
44
- return
45
- end
46
-
47
- answer = if @@aways_ovewrite
48
- puts "#{target_file_path} exists, overwrite? [yaN] y"
49
- 'y'
50
- else
51
- ask("#{target_file_path} exists, overwrite? [yaN]").downcase
52
- end
53
-
54
- @@aways_ovewrite = true if answer == 'a'
55
-
56
- IO.write(target_file_path, content) if answer == 'y'
57
- end
58
-
59
- end
60
- end
@@ -1,24 +0,0 @@
1
- module Scaffold
2
- class ControllerGenerator < Scaffold::BaseGenerator
3
-
4
- def generate
5
- controller_file_path = create_path(@params.controller_file_name)
6
-
7
- # puts "Creating #{controller_file_path}"
8
-
9
- filepath = find_root(templates, 'controller.rb.erb')
10
- content = File.read(filepath)
11
- # http://www.stuartellis.eu/articles/erb/
12
- content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })
13
- # puts content
14
- write_with_confirmation(controller_file_path, content)
15
- end
16
-
17
- def create_path file_name
18
- controller_path = File.join(Dir.pwd, 'app', 'controllers', @params.namespaces_array)
19
- controller_file_path = File.join(controller_path, file_name)
20
- FileUtils.mkpath(controller_path)
21
- controller_file_path
22
- end
23
- end
24
- end
@@ -1,25 +0,0 @@
1
- module Scaffold
2
- class FabricatorGenerator < Scaffold::BaseGenerator
3
- def generate
4
- fabricators_path = create_fabricators_path
5
- source_file_name = "fabrication.rb.erb"
6
- target_file_name = "#{@params.resource_name}_fabricator.rb"
7
- source_file_path = find_root(templates, 'fabricators', source_file_name)
8
- content = File.read(source_file_path)
9
-
10
- # http://www.stuartellis.eu/articles/erb/
11
- content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })#.gsub(/\s+\n$/, "")
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
@@ -1,51 +0,0 @@
1
- module Scaffold
2
- class ModelsGenerator < Scaffold::BaseGenerator
3
- def generate
4
- searches_path = create_searches_path
5
- puts "Add '#{@params.services_folder}' folder in your autoload_paths (application.rb)"
6
- create_search_object searches_path
7
- create_model
8
- end
9
-
10
- def create_model
11
- source_file_name = "model.rb.erb"
12
- target_file_name = "#{@params.resource_name}.rb"
13
-
14
- source_file_path = find_root(templates, source_file_name)
15
- content = File.read(source_file_path)
16
-
17
- content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })
18
-
19
- if @params.modules.blank?
20
- model_path = File.join(Dir.pwd, 'app', 'models')
21
- else
22
- model_path = File.join(Dir.pwd, 'app', 'models', @params.modules.map(&:underscore))
23
- FileUtils.mkdir_p model_path
24
- end
25
-
26
- target_file_path = File.join(model_path, target_file_name)
27
-
28
- write_with_confirmation(target_file_path, content)
29
- end
30
-
31
- def create_search_object searches_path
32
- source_file_name = "search.rb.erb"
33
- target_file_name = "#{@params.resource_name.pluralize}_search.rb"
34
-
35
- source_file_path = find_root(templates, source_file_name)
36
- content = File.read(source_file_path)
37
-
38
- content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })
39
-
40
- target_file_path = File.join(searches_path, target_file_name)
41
- write_with_confirmation(target_file_path, content)
42
- end
43
-
44
- def create_searches_path
45
- searches_path = File.join(Dir.pwd, 'app', @params.services_folder, 'search', @params.namespaces_array)
46
- FileUtils.mkpath(searches_path)
47
- searches_path
48
- end
49
-
50
- end
51
- end
@@ -1,174 +0,0 @@
1
- module Scaffold
2
- class Params
3
- attr_reader :resource_name, # user for use in @user or filenames
4
- :resource_class_name, # CompanyOwnership
5
- :namespaces_array, # The namespaces for the controller
6
- :modules, # the modules for the model Admin::...
7
- :controller_file_name,
8
- :template,
9
- :css_framework,
10
- :views_folder_name,
11
- :route_resource_name,
12
- :services_folder
13
-
14
- def initialize choice
15
- (@modules, @model_name) = parse_model(choice[:model]) # what came from the params (it contain namespaces)
16
-
17
- @fields = expand_default_types(hasherize_fields(choice[:fields]))
18
- @index_fields = choice[:index_fields].blank? ? @fields.keys : choice[:index_fields]
19
- @search_fields = choice[:search_fields].blank? ? @fields.keys : choice[:search_fields]
20
- # permitted params
21
- @fields_permitted = associations_to_ids(@fields)
22
- @search_fields_permitted = search_fields_permitted(@search_fields, @fields)
23
-
24
- @joins = choice[:joins]
25
- @includes = choice[:includes]
26
- @template = choice[:template]
27
- @css_framework = choice[:css_framework]
28
-
29
- @resource_class_name = @model_name.singularize # CompanyOwnership
30
- # Admin::User or only User
31
- if @modules.empty?
32
- @modulized_resource_class_name = @resource_class_name
33
- else
34
- @modulized_resource_class_name = "#{@modules.join('::')}::#{@resource_class_name}"
35
- end
36
-
37
- # routes
38
- @route_resource_name = @model_name.tableize # resourse :users
39
- @namespace = choice[:namespace] # for the controllers
40
- @base_controller = choice[:base_controller] || 'ApplicationController'
41
- @namespaces_array = parse_namespaces_array(@namespace) # [:admin, ...?... ]
42
-
43
-
44
- # controller
45
- @resource_name = @model_name.tableize.singularize # user for use in @user or filenames
46
- @collection_name = @model_name.tableize # users for use in @users
47
-
48
- if @namespaces_array.blank?
49
- @controller_class_name = "#{@model_name.pluralize}Controller"
50
- @search_modulized_resource_class_name = "Search::#{@resource_class_name.pluralize}Search"
51
-
52
- # paths without namespace
53
- @new_resource_path = "[:new, :#{@resource_name}]"
54
-
55
- @resource_path = "[#{@resource_name}]"
56
- @edit_resource_path = "[:edit, #{@resource_name}]"
57
-
58
- @instance_resource_path = "[@#{@resource_name}]"
59
- @instance_edit_resource_path = "[:edit, @#{@resource_name}]"
60
-
61
- @collection_path = [@collection_name.to_sym]
62
-
63
- else
64
- @controller_namespaces = @namespace.camelize
65
- @controller_class_name = "#{@controller_namespaces}::#{@model_name.pluralize}Controller"
66
- @search_modulized_resource_class_name = "Search::#{@controller_namespaces}::#{@resource_class_name.pluralize}Search"
67
-
68
- # paths with namespace
69
- @path_segments = @namespaces_array.map{|segment| segment.to_sym }
70
- @new_resource_path = "[:new, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, :#{@resource_name}]"
71
-
72
- @resource_path = "[#{@path_segments.map{|item| ":#{item}"}.join(", ")}, #{@resource_name}]"
73
- @edit_resource_path = "[:edit, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, #{@resource_name}]"
74
-
75
- @instance_resource_path = "[#{@path_segments.map{|item| ":#{item}"}.join(", ")}, @#{@resource_name}]"
76
- @instance_edit_resource_path = "[:edit, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, @#{@resource_name}]"
77
-
78
- @collection_path = @path_segments.dup << @collection_name.to_sym
79
-
80
- end
81
- @controller_file_name = "#{@resource_class_name.tableize}_controller.rb"
82
-
83
-
84
- # view
85
- @views_folder_name = @model_name.tableize # users
86
-
87
-
88
-
89
- @services_folder = choice[:services_folder]
90
- debug_info if choice[:debug]
91
- end
92
-
93
- private
94
- def debug_info
95
- puts "\n"
96
- puts "Debug:"
97
- puts "resource_class_name: #{@resource_class_name}, resource_name: #{@resource_name}, collection_name: #{@collection_name}"
98
- puts "modulized_resource_class_name: #{@modulized_resource_class_name}"
99
-
100
- puts "\ncontroller:"
101
- puts "class: #{@controller_class_name}"
102
-
103
- puts "\nroutes:"
104
- puts "route_resource_name: #{@route_resource_name}"
105
- puts "namespaces_array: #{@namespaces_array} (for urls helpers)"
106
- puts "resource_path: #{@resource_path}, resource_name: #{@collection_path}"
107
- puts "collection_path: #{@collection_path}"
108
- puts "\n\n"
109
- end
110
-
111
- def parse_model model
112
- chunks = model.split('::').select{|chunk| !chunk.empty?}
113
- class_name = chunks.pop
114
- [chunks, class_name]
115
- end
116
-
117
- def parse_namespaces_array namespace
118
- (namespace || "").split('/').collect{|name| name.underscore}
119
- end
120
-
121
- def hasherize_fields fields_array
122
- fields = {}
123
- fields_array.each do |field_string|
124
- (key, value) = field_string.split(':')
125
- fields[key] = value
126
- end
127
- fields
128
- end
129
-
130
-
131
- def expand_default_types hash
132
- hash.each_pair do |key, value|
133
- hash[key] = 'string' if value.blank?
134
- end
135
- end
136
-
137
- # convert company => company_id
138
- def associations_to_ids hash
139
- to_ids = {}
140
- hash.each_pair do |key, type|
141
- key_name = ['references', 'belongs_to'].include?(type.downcase) ? "#{key}_id" : key
142
- to_ids[key_name] = type
143
- end
144
- to_ids
145
- end
146
-
147
- # convert company => company_id
148
- def search_fields_permitted search_fields, fields
149
- to_ids = []
150
-
151
- expanded_fields = expand_association_to_ids fields
152
-
153
- search_fields.each do |key|
154
- next unless expanded_fields.keys.include?(key)
155
- type = expanded_fields[key]
156
- to_ids << (['references', 'belongs_to'].include?(type.downcase) ? "#{key}_id" : key)
157
- end
158
- to_ids
159
- end
160
-
161
- # if the field is belongs_to
162
- # make so that fields contains the `field` and field_id
163
- def expand_association_to_ids fields
164
- expanded = {}
165
- fields.each_pair do |name, type|
166
- case type
167
- when 'belongs_to'
168
- expanded["#{name}_id"] = 'integer'
169
- end
170
- end
171
- fields.merge(expanded)
172
- end
173
- end
174
- end