superuser 0.2.3 → 0.3.3

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: 37ba17de48a89db27bfd4cc1a10b09224f357abe34af8fd460b67b16335ed6e8
4
- data.tar.gz: 819e3904403516ea5e17dc57262a4feef66c55bee5c97df0c4e23e855330ebc0
3
+ metadata.gz: 487d1ecfa49de1fdbe1f4b6de87d465f163087f78f2b6f938c36a290eeb1f457
4
+ data.tar.gz: 2ccb0b6c9deadff8e05068a0fab17eb650a0cc7e9969b44bd0870d2b0e8d12c9
5
5
  SHA512:
6
- metadata.gz: 2916b5fb284d9d7d7a0564a6cbf795dcfdce93bcff8f0d2bc2c8da34d5ca44de67770c1c33a5b3ce34fb66821cb4320afd44d7970213bafd8e464e5da40bbdca
7
- data.tar.gz: 2df66f5f8edb5d244d6d0a069559f7a42d14f52f77f51e8d0d62b8157698bf090a271e73add5abf2524df0c5449b78356f355c5fde6b3e6ee7db5c180e2a2fd8
6
+ metadata.gz: 904adf9db5e4eef4b417f2fb66ff449ba44acb2b3d3449aca0785adfb4f4a2b2973a15cf2c2a68a9012f0bac35b5df1214c137879ac576e90b5dbebb14dc97a8
7
+ data.tar.gz: 0a0d7d7dcc1ff3d145c4e2e12a3eb2a40e7f9621ab4625570ca4b95012ae1b4c04b8ce91b61d079e3eab3ac9e6fb973e5b9c1b3036640d9a8e80380834ed74ad
@@ -1,21 +1,21 @@
1
1
  Description:
2
- Generate scaffolding for you rails admin area using the command "rails g superuser name_of_your_resource"
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 users
5
+ rails generate superuser users
6
6
 
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
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
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
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,127 @@
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
+ if for_webpack_native
17
+ copy_file "webpack_native_base.js", superuser_js_file
18
+ else
19
+ copy_file "superuser_base.js", superuser_js_file
20
+ end
21
+ end
22
+ end
23
+
24
+ def generate_layout
25
+ superuser_layout_file = "app/views/layouts/superuser/application.html.erb"
26
+ if !File.exist?(superuser_layout_file)
27
+ template "views/layouts/application.html.erb", superuser_layout_file
28
+ end
29
+ end
30
+
31
+ def generate_base_controller
32
+ superuser_base_controller = "app/controllers/superuser/base_controller.rb"
33
+ if !File.exist?(superuser_base_controller)
34
+ copy_file "base_controller.rb", superuser_base_controller
35
+ end
36
+ end
37
+
38
+ def generate_dashboard_controller
39
+ superuser_dashboard_controller = "app/controllers/superuser/dashboard_controller.rb"
40
+ if !File.exist?(superuser_dashboard_controller)
41
+ copy_file "dashboard_controller.rb", superuser_dashboard_controller
42
+
43
+ copy_file "views/dashboard_index.html.erb", "app/views/superuser/dashboard/index.html.erb"
44
+
45
+ add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_dashboard_link">', "<%= link_to 'dashboard', [:superuser, :root] %>"
46
+ end
47
+ end
48
+
49
+ def add_base_route
50
+ path = File.join(destination_root, 'config/routes.rb')
51
+ file_content = File.read(path)
52
+ # if namespace for :superuser don't exists then create it
53
+ unless file_content.include? 'namespace :superuser do'
54
+ route "\tnamespace :superuser do\n\t\troot to: 'dashboard#index'\n\tend\n"
55
+ end
56
+ end
57
+
58
+ def generate_search_form
59
+ template "views/_search.html.erb", "app/views/shared/superuser/_search.html.erb"
60
+ end
61
+
62
+ # in case of user is using webpack_native gem then add an entry pointing to superuser "application" javascript file
63
+ def add_entry_to_webpack_native_config
64
+ if for_webpack_native
65
+
66
+ webpack_config_file = "#{Rails.root}/app/webpack_native/webpack.config.js"
67
+
68
+ entry_line = "\n\t\t\tsuperuser: './src/javascripts/superuser.js',"
69
+
70
+ path = File.join(destination_root, 'app/webpack_native/webpack.config.js')
71
+ file_content = File.read(path)
72
+
73
+ if file_content.include? 'entry: {'
74
+ inject_into_file webpack_config_file, entry_line, :after => 'entry: {'
75
+ puts separator_line
76
+ note = "Restart rails server (in case it's running) for updates to take place in webpack.config.js"
77
+ puts "\e[33m#{note}\e[0m"
78
+ puts separator_line
79
+ else
80
+ puts separator_line
81
+ puts "You need to add the following entry to your webpack.config.js, i.e:\n\n"
82
+ entry = "entry: { \n superuser: './src/javascripts/superuser/application.js',\n // ...\n}"
83
+ puts "\e[32m#{entry}\e[0m"
84
+ note = "\nNote: do not forget to restart your server after that!"
85
+ puts "\e[33m#{note}\e[0m"
86
+ puts separator_line
87
+ end
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def separator_line
94
+ "\n"+ ("~" * 60) +"\n\n"
95
+ end
96
+
97
+ def for_webpack_native
98
+ frontend.include?('webpack_native') || frontend.include?('webpackNative')
99
+ end
100
+
101
+ def stylesheets_folder
102
+ if for_webpack_native
103
+ 'app/webpack_native/src/stylesheets'
104
+ else
105
+ 'app/assets/stylesheets'
106
+ end
107
+ end
108
+
109
+ def javascripts_folder
110
+ if for_webpack_native
111
+ 'app/webpack_native/src/javascripts'
112
+ else
113
+ 'app/javascript/packs'
114
+ end
115
+ end
116
+
117
+ def add_layout_links(relative_file, search_text, replace_text)
118
+ path = File.join(destination_root, relative_file)
119
+ file_content = File.read(path)
120
+
121
+ unless file_content.include? replace_text
122
+ content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t\t\t#{replace_text}")
123
+ File.open(path, 'wb') { |file| file.write(content) }
124
+ end
125
+ end
126
+
127
+ 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/superuser/_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