Skip to content

Commit d83f99f

Browse files
authoredJul 27, 2019
DEV: improves rails plugin generator (discourse#7949)
Fixes bugs, simplifies code, more default files. General idea, more is more here as it's easier to just delete things than reading and passing all the options.
1 parent 3324747 commit d83f99f

21 files changed

+193
-95
lines changed
 

‎lib/generators/plugin/plugin_generator.rb

+64-40
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,99 @@
33
require 'rails/generators/named_base'
44

55
class PluginGenerator < Rails::Generators::NamedBase
6+
attr_writer :about
67
attr_writer :github_username
78

89
desc 'This generator creates a Discourse plugin skeleton'
910

1011
source_root File.expand_path('templates', __dir__)
1112

12-
class_option :controller, type: :boolean, desc: "Generate controller", default: true
13-
class_option :spec, type: :boolean, desc: "Generate spec", default: true
14-
class_option :acceptance, type: :boolean, desc: "Generate acceptance test", default: true
15-
class_option :stylesheet, type: :boolean, desc: "Generate Stylesheet", default: true
16-
class_option :javascript, type: :boolean, desc: "Generate Javascript initializer", default: true
17-
class_option :scheduled_job, type: :boolean, desc: "Generate scheduled job", default: false
18-
class_option :help, type: :boolean, desc: "Adds help comments in generated files", default: true
13+
class_option :no_license, type: :boolean, desc: "No license", default: false
1914

20-
def create_acceptance_file
21-
return unless @options['acceptance']
15+
def create_plugin
16+
@about ||= ask("What is the purpose of your plugin?")
17+
@github_username ||= ask("Github username?")
2218

23-
template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6")
19+
readme_file
20+
routes_file
21+
engine_file
22+
plugin_file
23+
controller_file
24+
license_file
25+
stylesheet_file
26+
javascript_file
27+
settings_file
28+
locales_file
2429
end
2530

26-
def create_spec_file
27-
return if !@options['spec'] || !@options['controller']
28-
31+
def controller_file
32+
template 'plugin_controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{dasherized_name}/#{underscored_name}_controller.rb")
33+
template 'controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{dasherized_name}/actions_controller.rb")
2934
template 'controller_spec.rb.erb', File.join('plugins', dasherized_name, "spec/requests/actions_controller_spec.rb")
3035
end
3136

32-
def create_scheduled_job_file
33-
return unless @options['scheduled_job']
34-
35-
path = File.join('plugins', dasherized_name, 'jobs/scheduled', "check_#{underscored_name}.rb")
36-
template 'scheduled_job.rb.erb', path
37-
end
38-
39-
def create_readme_file
40-
ensure_github_username
41-
37+
def readme_file
4238
template 'README.md.erb', File.join('plugins', dasherized_name, "README.md")
4339
end
4440

45-
def create_license_file
46-
ensure_github_username
41+
def license_file
42+
return if @options['no_license']
4743

4844
template 'LICENSE.erb', File.join('plugins', dasherized_name, "LICENSE")
4945
end
5046

51-
def create_plugin_file
52-
ensure_github_username
47+
def engine_file
48+
template 'engine.rb.erb', File.join('plugins', dasherized_name, "lib", dasherized_name, "engine.rb")
49+
end
5350

54-
template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
51+
def routes_file
52+
template 'routes.rb.erb', File.join('plugins', dasherized_name, "config", "routes.rb")
53+
template 'route_constraint.rb.erb', File.join('plugins', dasherized_name, "lib", "#{underscored_name}_constraint.rb")
5554
end
5655

57-
def create_stylesheet_file
58-
return unless @options['stylesheet']
56+
def plugin_file
57+
template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
58+
end
5959

60+
def stylesheet_file
6061
template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/common', "#{dasherized_name}.scss")
62+
template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/desktop', "#{dasherized_name}.scss")
63+
template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/mobile', "#{dasherized_name}.scss")
6164
end
6265

63-
def create_javascript_file
64-
return unless @options['javascript']
66+
def javascript_file
67+
template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6")
68+
template 'javascript.js.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/initializers', "#{dasherized_name}.js.es6")
69+
template 'route-map.js.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/discourse', "#{dasherized_name}-route-map.js.es6")
70+
71+
folder = 'assets/javascripts/discourse/templates'
72+
template "#{folder}/template.hbs.erb", path(folder, "actions.hbs")
73+
template "#{folder}/template-show.hbs.erb", path(folder, "actions-show.hbs")
74+
template "#{folder}/template-index.hbs.erb", path(folder, "actions-index.hbs")
75+
76+
folder = 'assets/javascripts/discourse/routes'
77+
template "#{folder}/route.js.es6.erb", path(folder, "#{dasherized_name}-actions.js.es6")
78+
template "#{folder}/route-show.js.es6.erb", path(folder, "#{dasherized_name}-actions-show.js.es6")
79+
template "#{folder}/route-index.js.es6.erb", path(folder, "#{dasherized_name}-actions-index.js.es6")
80+
81+
folder = 'assets/javascripts/discourse/controllers'
82+
template "#{folder}/controller.js.es6.erb", path(folder, "actions.js.es6")
83+
template "#{folder}/controller-show.js.es6.erb", path(folder, "actions-show.js.es6")
84+
template "#{folder}/controller-index.js.es6.erb", path(folder, "actions-index.js.es6")
85+
86+
folder = 'assets/javascripts/discourse/models'
87+
template "#{folder}/model.js.es6.erb", path(folder, "action.js.es6")
6588

66-
template 'javascript.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/initializers', "#{dasherized_name}.es6")
89+
folder = 'assets/javascripts/discourse/adapters'
90+
template "#{folder}/adapter.js.es6.erb", path(folder, "action.js.es6")
6791
end
6892

69-
def create_settings_file
93+
def settings_file
7094
template 'settings.yml.erb', File.join('plugins', dasherized_name, 'config', 'settings.yml')
7195
end
7296

73-
def create_locales_file
74-
template 'client.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'client.en.yml')
97+
def locales_file
98+
template 'client.en.yml.erb', path('config/locales/client.en.yml')
7599
template 'server.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'server.en.yml')
76100
end
77101

@@ -83,10 +107,6 @@ def create_gitignore_entry
83107
end
84108
end
85109

86-
def ensure_github_username
87-
@github_username ||= ask("Github username?")
88-
end
89-
90110
def underscored_name
91111
name.underscore
92112
end
@@ -98,4 +118,8 @@ def dasherized_name
98118
def classified_name
99119
name.tableize.classify
100120
end
121+
122+
def path(*args)
123+
File.join('plugins', dasherized_name, args)
124+
end
101125
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import RestAdapter from "discourse/adapters/rest";
2+
3+
export default RestAdapter.extend({
4+
basePath() {
5+
return "/<%= dasherized_name %>/";
6+
}
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default Ember.Controller.extend({
2+
actions: {
3+
}
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default Ember.Controller.extend({
2+
actions: {
3+
}
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default Ember.Controller.extend({});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import RestModel from "discourse/models/rest";
2+
3+
export default RestModel.extend({});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default Discourse.Route.extend({
2+
controllerName: "actions-index",
3+
4+
model(params) {
5+
return this.store.findAll("action");
6+
},
7+
8+
renderTemplate() {
9+
this.render("actions-index");
10+
}
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default Discourse.Route.extend({
2+
controllerName: "actions-show",
3+
4+
model(params) {
5+
return this.store.find("action", params.id);
6+
},
7+
8+
renderTemplate() {
9+
this.render("actions-show");
10+
}
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default Discourse.Route.extend({
2+
controllerName: "actions",
3+
4+
renderTemplate() {
5+
this.render("actions");
6+
}
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
controller-index.hbs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
controller-show.hbs id: {{model.id}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
controller.hbs
2+
3+
{{outlet}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module <%= classified_name %>
2+
class ActionsController < ::ApplicationController
3+
requires_plugin <%= classified_name %>
4+
5+
before_action :ensure_logged_in
6+
7+
def index
8+
render_json_dump({ actions: [] })
9+
end
10+
11+
def show
12+
render_json_dump({ action: { id: params[:id] } })
13+
end
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module <%= classified_name %>
2+
class Engine < ::Rails::Engine
3+
engine_name "<%= classified_name %>".freeze
4+
isolate_namespace <%= classified_name %>
5+
6+
config.after_initialize do
7+
Discourse::Application.routes.append do
8+
mount ::<%= classified_name %>::Engine, at: "/<%= dasherized_name %>"
9+
end
10+
end
11+
end
12+
end

‎lib/generators/plugin/templates/javascript.es6.erb

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { withPluginApi } from "discourse/lib/plugin-api";
2+
3+
function initialize<%= classified_name %>(api) {
4+
// https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6
5+
}
6+
7+
export default {
8+
name: "<%= dasherized_name %>",
9+
10+
initialize() {
11+
withPluginApi("0.8.31", initialize<%= classified_name %>);
12+
}
13+
};
+7-39
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
11
# name: <%= name %>
2-
# about:
2+
# about: <%= @about %>
33
# version: 0.1
44
# authors: <%= @github_username %>
55
# url: https://github.com/<%= @github_username %>
66

7-
<% if @options["stylesheet"] %>
87
register_asset "stylesheets/common/<%= dasherized_name %>.scss"
9-
<% end %>
8+
register_asset "stylesheets/desktop/<%= dasherized_name %>.scss"
9+
register_asset "stylesheets/mobile/<%= dasherized_name %>.scss"
1010

1111
enabled_site_setting :<%= underscored_name %>_enabled
1212

13-
PLUGIN_NAME ||= "<%= name %>".freeze
13+
PLUGIN_NAME ||= "<%= classified_name %>".freeze
1414

15-
after_initialize do
16-
<% if @options['help'] %>
17-
# see lib/plugin/instance.rb for the methods available in this context
18-
<% end %>
19-
20-
module ::<%= classified_name %>
21-
class Engine < ::Rails::Engine
22-
engine_name PLUGIN_NAME
23-
isolate_namespace <%= classified_name %>
24-
end
25-
end
26-
27-
<% if @options["scheduled_job"] %>
28-
require File.expand_path("../jobs/scheduled/check_<%= underscored_name %>.rb", __FILE__)
29-
<% end %>
30-
31-
<% if @options["controller"] %>
32-
require_dependency "application_controller"
33-
class <%= name %>::ActionsController < ::ApplicationController
34-
requires_plugin PLUGIN_NAME
15+
load File.expand_path('../lib/<%= dasherized_name %>/engine.rb', __FILE__)
3516

36-
before_action :ensure_logged_in
37-
38-
def list
39-
render json: success_json
40-
end
41-
end
42-
43-
<%= name %>::Engine.routes.draw do
44-
get "/list" => "actions#list"
45-
end
46-
47-
Discourse::Application.routes.append do
48-
mount ::<%= name %>::Engine, at: "/<%= dasherized_name %>"
49-
end
50-
<% end %>
17+
after_initialize do
18+
# https://github.com/discourse/discourse/blob/master/lib/plugin/instance.rb
5119
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module <%= classified_name %>
2+
class <%= classified_name %>Controller < ::ApplicationController
3+
requires_plugin <%= classified_name %>
4+
5+
before_action :ensure_logged_in
6+
7+
def index
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function() {
2+
this.route("<%= dasherized_name %>", function() {
3+
this.route("actions", function() {
4+
this.route("show", { path: "/:id" });
5+
});
6+
});
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class <%= classified_name %>Constraint
2+
def matches?(request)
3+
SiteSetting.<%= underscored_name %>_enabled
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require_dependency "<%= underscored_name %>_constraint"
2+
3+
<%= classified_name %>::Engine.routes.draw do
4+
get "/" => "<%= dasherized_name %>#index", constraints: <%= classified_name %>Constraint.new
5+
get "/actions" => "actions#index", constraints: <%= classified_name %>Constraint.new
6+
get "/actions/:id" => "actions#show", constraints: <%= classified_name %>Constraint.new
7+
end

0 commit comments

Comments
 (0)
Please sign in to comment.