superuser 0.2.2 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca5f9b99d69ab6be91c64b8b59d81a8c3e6c4b09d6a339371e3fa8c30db8175c
4
- data.tar.gz: c006c02556167bf7271cf35419f9715fdcc26c75ac94860678cc3e3ec50870e9
3
+ metadata.gz: 9a1c31d05b650f4b02d719b16ac734f688688cd851a6385c501f6f690808f7ec
4
+ data.tar.gz: e6d0c6a36beb2545af0aade3a658dd547477a5a579ce909d6d33aaf231fb02af
5
5
  SHA512:
6
- metadata.gz: b5af5cc4a9b5bfcf1e162fd6e3d52f1fa2b50b873c20d306dabae5e11d7758d788e7fc745f0741ff7995b968ac88141e2b2713c186fb18f422d09240facde2f9
7
- data.tar.gz: a766b2a731841dad2bbbffc95e779543322f88fa0172e5c1c79f5a213f7d816f398ed589cf44dad5286d0f3703a42ad3f6e2b4ddd3ac96878cdc70dc1fb2b296
6
+ metadata.gz: '08455a1986f43b3ce7223ca6d65922ba577ae9a89ac4d734c6841833522ee8cf4cbab8d88b34fe2ba1ef759a94cb019733eb5b78baf8105c71abcd79946aea48'
7
+ data.tar.gz: a146ab5ac8596eedea709007a93b59e584f394ba22e32c244b6dcef84369cd021efc6107f319d083dc522de08380522078a0e3ffcd6c55bc435e519ab028c8a6
@@ -1,8 +1,21 @@
1
1
  Description:
2
- Explain the generator
2
+ Generate scaffolding for you rails admin area using the command "rails g superuser name_of_your_resource"
3
3
 
4
4
  Example:
5
- rails generate superuser Thing
5
+ rails generate superuser users
6
6
 
7
- This will create:
8
- what/will/it/create
7
+ This will create:
8
+ controllers/superuser/users_controller.rb
9
+ views/superuser/index.html.erb
10
+ views/superuser/new.html.erb
11
+ views/superuser/show.html.erb
12
+ views/superuser/edit.html.erb
13
+ views/superuser/_form.html.erb
14
+
15
+ + will create some base/shared files if they don't exists:
16
+ assets/javascripts/superuser/application.js
17
+ assets/stylesheets/superuser/application.scss
18
+ controllers/superuser/base_controller.rb
19
+ controllers/superuser/dashboard_controller.rb
20
+ views/layouts/superuser/application.html.erb
21
+ views/shared/_search.html.erb
@@ -0,0 +1,123 @@
1
+ class Superuser::InitGenerator < Rails::Generators::Base
2
+
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :frontend, type: :string, default: 'webpacker'
5
+
6
+ def generate_css_file
7
+ superuser_css_file = "#{stylesheets_folder()}/superuser.scss"
8
+ if !File.exist?(superuser_css_file)
9
+ copy_file "superuser_base.scss", superuser_css_file
10
+ end
11
+ end
12
+
13
+ def generate_js_file
14
+ superuser_js_file = "#{javascripts_folder()}/superuser.js"
15
+ if !File.exist?(superuser_js_file)
16
+ copy_file "superuser_base.js", superuser_js_file
17
+ end
18
+ end
19
+
20
+ def generate_layout
21
+ superuser_layout_file = "app/views/layouts/superuser/application.html.erb"
22
+ if !File.exist?(superuser_layout_file)
23
+ template "views/layouts/application.html.erb", superuser_layout_file
24
+ end
25
+ end
26
+
27
+ def generate_base_controller
28
+ superuser_base_controller = "app/controllers/superuser/base_controller.rb"
29
+ if !File.exist?(superuser_base_controller)
30
+ copy_file "base_controller.rb", superuser_base_controller
31
+ end
32
+ end
33
+
34
+ def generate_dashboard_controller
35
+ superuser_dashboard_controller = "app/controllers/superuser/dashboard_controller.rb"
36
+ if !File.exist?(superuser_dashboard_controller)
37
+ copy_file "dashboard_controller.rb", superuser_dashboard_controller
38
+
39
+ copy_file "views/dashboard_index.html.erb", "app/views/superuser/dashboard/index.html.erb"
40
+
41
+ add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_dashboard_link">', "<%= link_to 'dashboard', [:superuser, :root] %>"
42
+ end
43
+ end
44
+
45
+ def add_base_route
46
+ path = File.join(destination_root, 'config/routes.rb')
47
+ file_content = File.read(path)
48
+ # if namespace for :superuser don't exists then create it
49
+ unless file_content.include? 'namespace :superuser do'
50
+ route "\tnamespace :superuser do\n\t\troot to: 'dashboard#index'\n\tend\n"
51
+ end
52
+ end
53
+
54
+ def generate_search_form
55
+ template "views/_search.html.erb", "app/views/shared/superuser/_search.html.erb"
56
+ end
57
+
58
+ # in case of user is using webpack_native gem then add an entry pointing to superuser "application" javascript file
59
+ def add_entry_to_webpack_native_config
60
+ if for_webpack_native
61
+
62
+ webpack_config_file = "#{Rails.root}/app/webpack_native/webpack.config.js"
63
+
64
+ entry_line = "\n\t\t\tsuperuser: './src/javascripts/superuser/application.js',"
65
+
66
+ path = File.join(destination_root, 'app/webpack_native/webpack.config.js')
67
+ file_content = File.read(path)
68
+
69
+ if file_content.include? 'entry: {'
70
+ inject_into_file webpack_config_file, entry_line, :after => 'entry: {'
71
+ puts separator_line
72
+ note = "Restart rails server (in case it's running) for updates to take place in webpack.config.js"
73
+ puts "\e[33m#{note}\e[0m"
74
+ puts separator_line
75
+ else
76
+ puts separator_line
77
+ puts "You need to add the following entry to your webpack.config.js, i.e:\n\n"
78
+ entry = "entry: { \n superuser: './src/javascripts/superuser/application.js',\n // ...\n}"
79
+ puts "\e[32m#{entry}\e[0m"
80
+ note = "\nNote: do not forget to restart your server after that!"
81
+ puts "\e[33m#{note}\e[0m"
82
+ puts separator_line
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def separator_line
90
+ "\n"+ ("~" * 60) +"\n\n"
91
+ end
92
+
93
+ def for_webpack_native
94
+ frontend.include?('webpack_native') || frontend.include?('webpackNative')
95
+ end
96
+
97
+ def stylesheets_folder
98
+ if for_webpack_native
99
+ 'app/webpack_native/src/stylesheets'
100
+ else
101
+ 'app/assets/stylesheets'
102
+ end
103
+ end
104
+
105
+ def javascripts_folder
106
+ if for_webpack_native
107
+ 'app/webpack_native/src/javascripts'
108
+ else
109
+ 'app/javascript/packs'
110
+ end
111
+ end
112
+
113
+ def add_layout_links(relative_file, search_text, replace_text)
114
+ path = File.join(destination_root, relative_file)
115
+ file_content = File.read(path)
116
+
117
+ unless file_content.include? replace_text
118
+ content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t\t\t#{replace_text}")
119
+ File.open(path, 'wb') { |file| file.write(content) }
120
+ end
121
+ end
122
+
123
+ end
@@ -1,205 +1,136 @@
1
1
  class SuperuserGenerator < Rails::Generators::Base
2
2
 
3
- source_root File.expand_path('../templates', __FILE__)
4
- argument :resources_name, type: :string
5
- attr_accessor :attributes
6
-
7
- # NOTE: the order of the following methods is important!
8
-
9
- def generate_css_file
10
-
11
- if !File.exist?("app/assets/stylesheets/superuser/application.scss")
12
- copy_file "superuser_base.scss", "app/assets/stylesheets/superuser/application.scss"
13
- end
14
-
15
- end
16
-
17
- def generate_js_file
18
-
19
- if !File.exist?("app/assets/javascripts/superuser/application.js")
20
- copy_file "superuser_base.js", "app/assets/javascripts/superuser/application.js"
21
- end
22
-
23
- end
24
-
25
- def generate_layout
26
-
27
- if !File.exist?("app/views/layouts/superuser/application.html.erb")
28
- template "views/layouts/application.html.erb", "app/views/layouts/superuser/application.html.erb"
29
- end
30
-
31
- end
32
-
33
- def generate_controller
34
-
35
- if !File.exist?("app/controllers/superuser/base_controller.rb")
36
- copy_file "base_controller.rb", "app/controllers/superuser/base_controller.rb"
37
- end
38
- if !File.exist?("app/controllers/superuser/dashboard_controller.rb")
39
- copy_file "dashboard_controller.rb", "app/controllers/superuser/dashboard_controller.rb"
40
- copy_file "views/dashboard_index.html.erb", "app/views/superuser/dashboard/index.html.erb"
41
- route "\tnamespace :superuser do\n\t\troot to: 'dashboard#index'\n\tend"
42
- add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_dashboard_link">', "<%= link_to 'dashboard', [:superuser, :root] %>"
43
- end
44
- template "controller_template.rb", "app/controllers/superuser/#{naming(:resources)}_controller.rb"
45
-
46
- end
47
-
48
- def generate_route_and_link
49
-
50
- # add resources to route if not exists
51
- route_replacement = "resources :#{resources}"
52
- r = add_resources_route 'config/routes.rb', search = 'namespace :superuser do', route_replacement
53
-
54
- # add link to resources in the layout if not exists
55
- link = "<%= link_to '#{resources}', [:superuser, :#{resources}] %>"
56
- add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_item">', link
57
-
58
- end
59
-
60
- def generate_views
61
-
62
- template "views/_form.html.erb", "app/views/superuser/#{naming(:resources)}/_form.html.erb"
63
- template "views/index.html.erb", "app/views/superuser/#{naming(:resources)}/index.html.erb"
64
- template "views/show.html.erb", "app/views/superuser/#{naming(:resources)}/show.html.erb"
65
- template "views/new.html.erb", "app/views/superuser/#{naming(:resources)}/new.html.erb"
66
- template "views/edit.html.erb", "app/views/superuser/#{naming(:resources)}/edit.html.erb"
67
-
68
- end
69
-
70
- def generate_search_form
71
-
72
- template "views/_search.html.erb", "app/views/shared/_search.html.erb"
73
-
74
- end
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :resources_name, type: :string
5
+ attr_accessor :attributes
6
+
7
+ # NOTE: the order of the following methods is important!
8
+
9
+ def generate_controller
10
+ template "controller_template.rb", "app/controllers/superuser/#{naming(:resources)}_controller.rb"
11
+ end
12
+
13
+ def generate_route_and_link
14
+ # add resources to route if not exists
15
+ route_replacement = "resources :#{resources}"
16
+ r = add_resources_route 'config/routes.rb', search = 'namespace :superuser do', route_replacement
17
+
18
+ # add link to resources in the layout if not exists
19
+ link = "<%= link_to '#{resources}', [:superuser, :#{resources}] %>"
20
+ add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_item">', link
21
+ end
22
+
23
+ def generate_views
24
+ template "views/_form.html.erb", "app/views/superuser/#{naming(:resources)}/_form.html.erb"
25
+ template "views/index.html.erb", "app/views/superuser/#{naming(:resources)}/index.html.erb"
26
+ template "views/show.html.erb", "app/views/superuser/#{naming(:resources)}/show.html.erb"
27
+ template "views/new.html.erb", "app/views/superuser/#{naming(:resources)}/new.html.erb"
28
+ template "views/edit.html.erb", "app/views/superuser/#{naming(:resources)}/edit.html.erb"
29
+ end
75
30
 
76
31
  private
77
32
 
78
33
  def replace(file_path)
79
-
80
- gsub_file file_path, 'resources', "#{naming(:resources)}"
81
- gsub_file file_path, 'resource', "#{naming(:resource)}"
82
- gsub_file file_path, 'ControllerName', "#{naming(:controller_name)}"
83
- gsub_file file_path, 'ModelName', "#{naming(:model_name)}"
84
-
34
+ gsub_file file_path, 'resources', "#{naming(:resources)}"
35
+ gsub_file file_path, 'resource', "#{naming(:resource)}"
36
+ gsub_file file_path, 'ControllerName', "#{naming(:controller_name)}"
37
+ gsub_file file_path, 'ModelName', "#{naming(:model_name)}"
85
38
  end
86
39
 
87
40
  def resources
88
-
89
- resources_name.underscore
90
-
41
+ resources_name.underscore
91
42
  end
92
43
 
93
44
  def resource
94
-
95
- resources_name.singularize.underscore
96
-
45
+ resources_name.singularize.underscore
97
46
  end
98
47
 
99
48
  def get_controller_name
100
-
101
- resources_name.camelize
102
-
49
+ resources_name.camelize
103
50
  end
104
51
 
105
52
  def naming(key)
106
-
107
- map = {
108
- resources: resources_name.underscore,
109
- resource: resources_name.singularize.underscore,
110
- controller_name: resources_name.camelize,
111
- model_name: resources_name.classify
112
- }
113
- return map[key]
114
-
53
+ map = {
54
+ resources: resources_name.underscore,
55
+ resource: resources_name.singularize.underscore,
56
+ controller_name: resources_name.camelize,
57
+ model_name: resources_name.classify
58
+ }
59
+ return map[key]
115
60
  end
116
61
 
117
62
  def model_columns_for_attributes
118
-
119
- resources_name.classify.constantize.columns.reject do |column|
120
- column.name.to_s =~ /^(id|user_id|created_at|updated_at)$/
121
- end
122
-
63
+ resources_name.classify.constantize.columns.reject do |column|
64
+ column.name.to_s =~ /^(id|user_id|created_at|updated_at)$/
65
+ end
123
66
  end
124
67
 
125
68
  def editable_attributes
126
-
127
- attributes ||= model_columns_for_attributes.map do |column|
128
- {name: column.name.to_s, type: column.type.to_s}
129
- end
130
-
69
+ attributes ||= model_columns_for_attributes.map do |column|
70
+ {name: column.name.to_s, type: column.type.to_s}
71
+ end
131
72
  end
132
73
 
133
74
  def get_model
134
-
135
- resources_name.classify.constantize
136
-
75
+ resources_name.classify.constantize
137
76
  end
138
77
 
139
78
  def get_resource_attributes
140
-
141
- editable_attributes.map { |a| a.name.prepend(':') }.join(', ')
142
-
79
+ editable_attributes.map {
80
+ |a| a.name.prepend(':')
81
+ }.join(', ')
143
82
  end
144
83
 
145
84
  def field_type(db_type)
146
-
147
- matching_type = {
148
- decimal: "text_field",
149
- float: "text_field",
150
- datetime: "text_field",
151
- string: "text_field",
152
- integer: "text_field",
153
- text: "text_area",
154
- json: "text_area",
155
- jsonb: "text_area"
156
- }
157
- matching_type[db_type.to_sym] || "text_field"
158
-
85
+ matching_type = {
86
+ decimal: "text_field",
87
+ float: "text_field",
88
+ datetime: "text_field",
89
+ string: "text_field",
90
+ integer: "text_field",
91
+ text: "text_area",
92
+ json: "text_area",
93
+ jsonb: "text_area"
94
+ }
95
+ matching_type[db_type.to_sym] || "text_field"
159
96
  end
160
97
 
161
98
  def destination_path(path)
162
-
163
- File.join(destination_root, path)
164
-
99
+ File.join(destination_root, path)
165
100
  end
166
101
 
167
102
  # sub_file modified
168
103
  def add_resources_route(relative_file, search_text, replace_text)
104
+ path = destination_path(relative_file)
105
+ file_content = File.read(path)
169
106
 
170
- path = destination_path(relative_file)
171
- file_content = File.read(path)
172
-
173
- # if namespace for :superuser don't exists then create it
174
- unless file_content.include? 'namespace :superuser do'
175
- route "\tnamespace :superuser do\n\t\tresources :#{resources}\n\tend"
176
- return
177
- end
107
+ # if namespace for :superuser don't exists then create it
108
+ unless file_content.include? 'namespace :superuser do'
109
+ route "\tnamespace :superuser do\n\t\tresources :#{resources}\n\tend"
110
+ return
111
+ end
178
112
 
179
- # the regular expression string should be between single quotes not double quotes
180
- # the regular expression string should not include delimiters
181
- # the matching will stop when find the first occurence of 'end'
182
- regex_string = 'namespace \:superuser do[^end]*' + replace_text
183
- regex = Regexp.new(regex_string)
184
-
185
- #unless file_content.include? replace_text
186
- unless regex.match file_content
187
- content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t#{replace_text}")
188
- File.open(path, 'wb') { |file| file.write(content) }
189
- end
113
+ # the regular expression string should be between single quotes not double quotes
114
+ # the regular expression string should not include delimiters
115
+ # the matching will stop when find the first occurence of 'end'
116
+ regex_string = 'namespace \:superuser do[^end]*' + replace_text
117
+ regex = Regexp.new(regex_string)
190
118
 
119
+ #unless file_content.include? replace_text
120
+ unless regex.match file_content
121
+ content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t#{replace_text}")
122
+ File.open(path, 'wb') { |file| file.write(content) }
123
+ end
191
124
  end
192
125
 
193
126
  def add_layout_links(relative_file, search_text, replace_text)
127
+ path = destination_path(relative_file)
128
+ file_content = File.read(path)
194
129
 
195
- path = destination_path(relative_file)
196
- file_content = File.read(path)
197
-
198
- unless file_content.include? replace_text
199
- content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t\t\t#{replace_text}")
200
- File.open(path, 'wb') { |file| file.write(content) }
201
- end
202
-
130
+ unless file_content.include? replace_text
131
+ content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t\t\t#{replace_text}")
132
+ File.open(path, 'wb') { |file| file.write(content) }
133
+ end
203
134
  end
204
135
 
205
136
  end