scaffold_pico 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 175c54b342e8d50e676e2bf54e99bdac1515960a
4
+ data.tar.gz: 103171fdcd24d32f815a669595c14844f226050c
5
+ SHA512:
6
+ metadata.gz: caae14c0f717591e44525c06fb141c84deab161967fbdfc444b1b25ffafb8df55e882440f50ffad0de0b907f83daed2e9e3a69f9b809db74364db52c76725948
7
+ data.tar.gz: 75e15aeb60a6209f25251f6fc68fe99a45695df11f7422b7feff236dde84d84e47bfb71c9cb627bba2a3e3d49dce1cc289abd5c904024615158def7d7ddb186e
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 gudata
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ = scaffold_pico
2
+
3
+ ruby -I lib bin/scaffold_pico.rb --css_framework=zurb --template=slim -m user -n Admin -i :roles :company -j :roles :company --fields name:string
4
+
5
+ scaffold_pico.rb -m Admin::User -n admin/gosho -i :roles :company -j :roles :company --fields name:string
6
+
7
+ ruby -I ~/scaffold_pico/lib ~/scaffold_pico/bin/scaffold_pico.rb -m Admin::Assistant -n administation/pencho --fields first_name:string last_name:string
8
+
9
+
10
+ ruby -I ~/scaffold_pico/lib ~/scaffold_pico/bin/scaffold_pico.rb -m Company -n super_admin --fields name:string website:string active:string subscription:string contact_person:string
11
+
12
+
13
+ ruby -I ~/scaffold_pico/lib ~/scaffold_pico/bin/scaffold_pico.rb -d --css_framework=materialize \
14
+ -m Vector -n manage -b AdminController \
15
+ --fields name: featured:boolean aspect_ratio: portrait:boolean landscape:boolean imported:boolean trending:boolean width:integer height:integer transperant:boolean bw:boolean license:belongs_to group_id:integer tool:belongs_to user_id: category_id: category_display_order:integer group_display_order:integer svg:file ai:file png:file emf:file wmf:file \
16
+ --index-fields id name imported featured \
17
+ --search-fields name aspect_ratio landscape portrait imported featured trending width height transperant bw license_id group_id tool_id user_id category_id
18
+
19
+
20
+ == I18n
21
+
22
+ en.yml:
23
+
24
+ activerecord:
25
+ notices:
26
+ success:
27
+ create: Was successfully created %{model}
28
+ update: Was successfully updated %{model}
29
+ destroy: Was successfully destroyed %{model}
30
+ failed:
31
+ create: Fail creating %{model}
32
+ update: Fail updating %{model}
33
+ destroy: Fail destroing %{model}
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2016 gudata. See LICENSE.txt for
38
+ further details.
39
+
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/scaffold/cli'
4
+
5
+ cli = Scaffold::CLI.new
6
+ cli.run
@@ -0,0 +1,22 @@
1
+ module Scaffold
2
+ class BaseGenerator
3
+
4
+ def initialize params
5
+ @params = params
6
+ end
7
+
8
+ def objectify
9
+ OpenStruct.new(vars).instance_eval { binding }
10
+ end
11
+
12
+ # where is the root of the gem
13
+ def root
14
+ Scaffold.root
15
+ end
16
+
17
+ # the root of the templates
18
+ def templates
19
+ 'lib/templates/pico/'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,110 @@
1
+ require 'scaffold_pico'
2
+ require 'choice'
3
+
4
+ module Scaffold
5
+ class CLI
6
+
7
+ def initialize
8
+ Choice.options do
9
+ header 'Synopsys: scaffold_pico.rb -m user -n Admin -i :roles :company -j :roles :company --fields name:string'
10
+ separator ''
11
+ header 'Specific options:'
12
+
13
+ option :debug do
14
+ short '-d'
15
+ long '--debug'
16
+ desc 'print some debug info'
17
+ end
18
+
19
+ option :namespace do
20
+ short '-n'
21
+ long '--namespace=namespace1/namespace2'
22
+ validate /\A(\w+(?:\/\w+)*)\z/
23
+ desc 'Optional namespace for the controllers. Example: -n admin/secret_area'
24
+ end
25
+
26
+ option :base_controller do
27
+ short '-b'
28
+ long '--base_controller=AdminController'
29
+ validate /((::)?([A-Z])+[a-z]*)*\z/
30
+ desc 'Optional base controller. Example: -b Admin::BaseController'
31
+ end
32
+
33
+ option :model do
34
+ short '-m'
35
+ long '--model=model'
36
+ desc 'The model. It could be with modules. Example: ModuleA::ModuleB::SomeClassName'
37
+ validate /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/
38
+ end
39
+
40
+ option :includes do
41
+ long '--includes=*INCLUDES'
42
+ desc 'add .includes(*includes) into the collection request, we have join also'
43
+ default []
44
+ end
45
+
46
+ option :joins do
47
+ long '--joins=*JOINS'
48
+ desc 'add .joins(*joins) into the collection request, we have includes also'
49
+ default []
50
+ end
51
+
52
+ option :fields do
53
+ long '-f'
54
+ long '--fields *FIELDS'
55
+ desc 'title:string body:text published:boolean amount:decimal tracking_id:integer:uniq '
56
+ end
57
+
58
+ option :index_fields do
59
+ long '-i'
60
+ long '--index-fields *FIELDS'
61
+ desc 'The fields you wish to see on the index page. Example: --index-fields title body published amount tracking_id'
62
+ end
63
+
64
+ option :search_fields do
65
+ long '-s'
66
+ long '--search-fields *FIELDS'
67
+ desc 'Fields on which the search will be done. Example: --search-fields title body published amount tracking_id'
68
+ end
69
+
70
+
71
+ separator ''
72
+ separator 'Defaults: '
73
+
74
+ option :template do
75
+ long '-t'
76
+ long '--template template'
77
+ default 'slim'
78
+ desc 'slim'
79
+ end
80
+
81
+ option :css_framework do
82
+ long '-c'
83
+ long '--css_framework css_framework'
84
+ default 'zurb'
85
+ desc 'zurb'
86
+ end
87
+
88
+ separator ''
89
+ separator 'Common options: '
90
+
91
+ option :help do
92
+ long '--help'
93
+ desc 'Show this message'
94
+ end
95
+
96
+ end
97
+
98
+
99
+ end
100
+
101
+ def run
102
+ if Choice[:model]
103
+ scaffold = Scaffold::Main.new(Choice.choices)
104
+ scaffold.run
105
+ else
106
+ puts "try #{$0} --help"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,24 @@
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
+ filename = File.join(root, templates, 'controller.rb.erb')
10
+ content = File.read(filename)
11
+ # http://www.stuartellis.eu/articles/erb/
12
+ content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })
13
+ # puts content
14
+ IO.write(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
@@ -0,0 +1,15 @@
1
+ module Scaffold
2
+ class Main
3
+ def initialize choice
4
+ @params = Params.new(choice)
5
+ end
6
+
7
+ def run
8
+ Scaffold::ControllerGenerator.new(@params).generate
9
+ Scaffold::ModelsGenerator.new(@params).generate
10
+ Scaffold::ViewsGenerator.new(@params).generate
11
+ Scaffold::RoutesGenerator.new(@params).generate
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module Scaffold
2
+ class ModelsGenerator < Scaffold::BaseGenerator
3
+ def generate
4
+ searches_path = create_searches_path
5
+ puts "Don't forget to add 'app/services' in your autoload_paths (application.rb)"
6
+ create_search_object searches_path
7
+ end
8
+
9
+ def create_search_object searches_path
10
+ source_file_name = "search.rb.erb"
11
+ target_file_name = "#{@params.resource_name.pluralize}_search.rb"
12
+
13
+ source_file_path = File.join(root, templates, source_file_name)
14
+ content = File.read(source_file_path)
15
+
16
+ content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })
17
+
18
+ target_file_path = File.join(searches_path, target_file_name)
19
+ IO.write(target_file_path, content)
20
+ end
21
+
22
+ def create_searches_path
23
+ searches_path = File.join(Dir.pwd, 'app', 'services', 'search', @params.namespaces_array)
24
+ FileUtils.mkpath(searches_path)
25
+ searches_path
26
+ end
27
+
28
+
29
+ end
30
+ end
@@ -0,0 +1,106 @@
1
+ module Scaffold
2
+ class Params
3
+ attr_reader :resource_name, :namespaces_array, :controller_file_name, :template, :css_framework, :views_folder_name, :route_resource_name
4
+
5
+ def expand_default_types hash
6
+ hash.each_pair do |key, value|
7
+ hash[key] = 'string' if value.blank?
8
+ end
9
+ end
10
+
11
+ def initialize choice
12
+ (@modules, @model_name) = parse_model(choice[:model]) # what came from the params (it contain namespaces)
13
+
14
+ @fields = expand_default_types(hasherize_fields(choice[:fields]))
15
+ @index_fields = choice[:index_fields].blank? ? @fields.keys : choice[:index_fields]
16
+ @search_fields = choice[:search_fields].blank? ? @fields.keys : choice[:search_fields]
17
+
18
+ @joins = choice[:joins]
19
+ @includes = choice[:includes]
20
+ @template = choice[:template]
21
+ @css_framework = choice[:css_framework]
22
+
23
+ @resource_class_name = @model_name.singularize # CompanyOwnership
24
+ # Admin::User or only User
25
+ if @modules.empty?
26
+ @modulized_resource_class_name = @resource_class_name
27
+ else
28
+ @modulized_resource_class_name = "#{@modules.join('::')}::#{@resource_class_name}"
29
+ end
30
+
31
+ # routes
32
+ @route_resource_name = @model_name.tableize # resoures :users
33
+ @namespace = choice[:namespace] # for the controllers
34
+ @base_controller = choice[:base_controller] || 'ApplicationController'
35
+ @namespaces_array = parse_namespaces_array(@namespace) # [:admin, ...?... ]
36
+
37
+ # controller
38
+ @controller_namespaces = @namespace.camelize
39
+
40
+ if @namespaces_array.blank?
41
+ @controller_class_name = "#{@model_name.pluralize}Controller"
42
+ @search_modulized_resource_class_name = "Search::#{@resource_class_name}Search"
43
+ else
44
+ @controller_class_name = "#{@controller_namespaces}::#{@model_name.pluralize}Controller"
45
+ @search_modulized_resource_class_name = "Search::#{@controller_namespaces}::#{@resource_class_name.pluralize}Search"
46
+ end
47
+ @controller_file_name = "#{@resource_class_name.tableize}_controller.rb"
48
+
49
+ @resource_name = @model_name.tableize.singularize # user for use in @user or filenames
50
+ @collection_name = @model_name.tableize # users for use in @users
51
+
52
+ # view
53
+ @views_folder_name = @model_name.tableize # users
54
+
55
+ @path_segments = @namespaces_array.map{|segment| segment.to_sym }
56
+ @new_resource_path = "[:new, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, :#{@resource_name}]"
57
+
58
+ @resource_path = "[#{@path_segments.map{|item| ":#{item}"}.join(", ")}, #{@resource_name}]"
59
+ @edit_resource_path = "[:edit, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, #{@resource_name}]"
60
+
61
+ @instance_resource_path = "[#{@path_segments.map{|item| ":#{item}"}.join(", ")}, @#{@resource_name}]"
62
+ @instance_edit_resource_path = "[:edit, #{@path_segments.map{|item| ":#{item}"}.join(", ")}, @#{@resource_name}]"
63
+
64
+ @collection_path = @path_segments.dup << @collection_name.to_sym
65
+
66
+ debug_info if choice[:debug]
67
+ end
68
+
69
+ def debug_info
70
+ puts "\n"
71
+ puts "Debug:"
72
+ puts "resource_class_name: #{@resource_class_name}, resource_name: #{@resource_name}, collection_name: #{@collection_name}"
73
+ puts "modulized_resource_class_name: #{@modulized_resource_class_name}"
74
+
75
+ puts "\ncontroller:"
76
+ puts "class: #{@controller_class_name}"
77
+
78
+ puts "\nroutes:"
79
+ puts "route_resource_name: #{@route_resource_name}"
80
+ puts "namespaces_array: #{@namespaces_array} (for urls helpers)"
81
+ puts "resource_path: #{@resource_path}, resource_name: #{@collection_path}"
82
+ puts "collection_path: #{@collection_path}"
83
+ puts "\n\n"
84
+ end
85
+
86
+ def parse_model model
87
+ chunks = model.split('::').select{|chunk| !chunk.empty?}
88
+ class_name = chunks.pop
89
+ [chunks, class_name]
90
+ end
91
+
92
+ def parse_namespaces_array namespace
93
+ namespace.split('/').collect{|name| name.underscore}
94
+ end
95
+
96
+ def hasherize_fields fields_array
97
+ fields = {}
98
+ fields_array.each do |field_string|
99
+ (key, value) = field_string.split(':')
100
+ fields[key] = value
101
+ end
102
+ fields
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,29 @@
1
+ module Scaffold
2
+ class RoutesGenerator < Scaffold::BaseGenerator
3
+
4
+ def generate
5
+ puts "Insert something like this in routes.rb"
6
+ namespaces_count = @params.namespaces_array.count
7
+ route_string = "#{spaces(namespaces_count+2)}resources :#{@params.route_resource_name}"
8
+
9
+ @params.namespaces_array.reverse.each_with_index do |namespace, index|
10
+ route_string = namespace_block(namespace, namespaces_count - index, route_string)
11
+ end
12
+ puts route_string
13
+ end
14
+
15
+ def spaces(count)
16
+ " "*2*(count)
17
+ end
18
+
19
+ def namespace_block namespace, index, route_string
20
+ ident = spaces(index+1)
21
+
22
+ begin_namespace_line = "#{ident}namespace :#{namespace} do"
23
+ namespace = "#{ident*2}#{namespace}"
24
+ end_namespace_line = "#{ident}end"
25
+ "#{begin_namespace_line}\n#{route_string}\n#{end_namespace_line}"
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module Scaffold
2
+ module TemplateEngines
3
+ class Slim
4
+
5
+ def extension
6
+ 'slim'
7
+ end
8
+
9
+ def source_folder_name
10
+ 'slim'
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ module Scaffold
2
+ class ViewsGenerator < Scaffold::BaseGenerator
3
+ def generate
4
+ views_path = create_views_path
5
+ templating_engine = choose_templating_engine
6
+ print "Creating view:"
7
+ %w(index new edit show _form).each do |view_name|
8
+ print view_name, ' '
9
+ create views_path, view_name, templating_engine, css_framework
10
+ end
11
+ print "\n"
12
+ end
13
+
14
+ def create views_path, view_name, templating_engine, css_framework
15
+ source_file_name = "#{view_name}.html.#{templating_engine.extension}.erb"
16
+ target_file_name = "#{view_name}.html.#{templating_engine.extension}"
17
+ source_file_path = File.join(root, templates, 'views',
18
+ css_framework,
19
+ templating_engine.source_folder_name,
20
+ source_file_name)
21
+ content = File.read(source_file_path)
22
+
23
+ # http://www.stuartellis.eu/articles/erb/
24
+ content = ::ERB.new(content, nil, '-').result(@params.instance_eval{ binding })#.gsub(/\s+\n$/, "")
25
+
26
+ target_file_path = File.join(create_views_path, target_file_name)
27
+ IO.write(target_file_path, content)
28
+ end
29
+
30
+ def create_views_path
31
+ views_path = File.join(Dir.pwd, 'app', 'views', @params.namespaces_array, @params.views_folder_name)
32
+ FileUtils.mkpath(views_path)
33
+ views_path
34
+ end
35
+
36
+
37
+ def choose_templating_engine
38
+ case @params.template
39
+ when 'slim'
40
+ ::Scaffold::TemplateEngines::Slim.new
41
+ end
42
+ end
43
+
44
+ def css_framework
45
+ @params.css_framework
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'ostruct'
4
+ require 'scaffold/main'
5
+ require 'scaffold/params'
6
+ require 'scaffold/base_generator'
7
+ require 'scaffold/controller_generator'
8
+ require 'scaffold/models_generator'
9
+ require 'scaffold/views_generator'
10
+ require 'scaffold/routes_generator'
11
+ require 'scaffold/template_engines/slim'
12
+ require 'erb'
13
+ require 'fileutils'
14
+
15
+ module Scaffold
16
+ def self.root
17
+ File.dirname __dir__
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scaffold_pico
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - gudata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: choice
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: require_all
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.1
83
+ description: Scaffolding
84
+ email: i.bardarov@gmail.com
85
+ executables:
86
+ - scaffold_pico.rb
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ files:
92
+ - LICENSE.txt
93
+ - README.rdoc
94
+ - bin/scaffold_pico.rb
95
+ - lib/scaffold/base_generator.rb
96
+ - lib/scaffold/cli.rb
97
+ - lib/scaffold/controller_generator.rb
98
+ - lib/scaffold/main.rb
99
+ - lib/scaffold/models_generator.rb
100
+ - lib/scaffold/params.rb
101
+ - lib/scaffold/routes_generator.rb
102
+ - lib/scaffold/template_engines/slim.rb
103
+ - lib/scaffold/views_generator.rb
104
+ - lib/scaffold_pico.rb
105
+ homepage: http://github.com/gudata/scaffold_pico
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Scaffold should be simple
129
+ test_files: []
130
+ has_rdoc: