diff --git a/lib/generators/plugin/plugin_generator.rb b/lib/generators/plugin/plugin_generator.rb index 29568aa4003..94f9d2441dd 100644 --- a/lib/generators/plugin/plugin_generator.rb +++ b/lib/generators/plugin/plugin_generator.rb @@ -7,11 +7,26 @@ class PluginGenerator < Rails::Generators::NamedBase source_root File.expand_path('templates', __dir__) + class_option :controller, type: :boolean, desc: "Generate controller", default: true + class_option :spec, type: :boolean, desc: "Generate spec", default: true + class_option :acceptance, type: :boolean, desc: "Generate acceptance test", default: true class_option :stylesheet, type: :boolean, desc: "Generate Stylesheet", default: true class_option :javascript, type: :boolean, desc: "Generate Javascript initializer", default: true class_option :scheduled_job, type: :boolean, desc: "Generate scheduled job", default: false class_option :help, type: :boolean, desc: "Adds help comments in generated files", default: true + def create_acceptance_file + return unless @options['acceptance'] + + template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6") + end + + def create_spec_file + return if !@options['spec'] || !@options['controller'] + + template 'controller_spec.rb.erb', File.join('plugins', dasherized_name, "spec/requests/actions_controller_spec.rb") + end + def create_scheduled_job_file return unless @options['scheduled_job'] diff --git a/lib/generators/plugin/templates/acceptance-test.js.es6.erb b/lib/generators/plugin/templates/acceptance-test.js.es6.erb new file mode 100644 index 00000000000..85da8e9791f --- /dev/null +++ b/lib/generators/plugin/templates/acceptance-test.js.es6.erb @@ -0,0 +1,9 @@ +import { acceptance } from "helpers/qunit-helpers"; + +acceptance("<%= name %>", { loggedIn: true }); + +test("<%= name %> works", async assert => { + await visit("/admin/plugins/<%= dasherized_name %>"); + + assert.ok(false, "it shows the <%= name %> button"); +}); diff --git a/lib/generators/plugin/templates/controller_spec.rb.erb b/lib/generators/plugin/templates/controller_spec.rb.erb new file mode 100644 index 00000000000..88935a5dfe2 --- /dev/null +++ b/lib/generators/plugin/templates/controller_spec.rb.erb @@ -0,0 +1,13 @@ +require 'rails_helper' + +describe <%= name %>::ActionsController do + before do + SiteSetting.queue_jobs = false + end + + it 'can list' do + sign_in(Fabricate(:user)) + get "/<%= dasherized_name %>/list.json" + expect(response.status).to eq(200) + end +end diff --git a/lib/generators/plugin/templates/plugin.rb.erb b/lib/generators/plugin/templates/plugin.rb.erb index 031c366ddcb..4937d8dba72 100644 --- a/lib/generators/plugin/templates/plugin.rb.erb +++ b/lib/generators/plugin/templates/plugin.rb.erb @@ -27,4 +27,25 @@ after_initialize do <% if @options["scheduled_job"] %> require File.expand_path("../jobs/scheduled/check_<%= underscored_name %>.rb", __FILE__) <% end %> + + <% if @options["controller"] %> + require_dependency "application_controller" + class <%= name %>::ActionsController < ::ApplicationController + requires_plugin PLUGIN_NAME + + before_action :ensure_logged_in + + def list + render json: success_json + end + end + + <%= name %>::Engine.routes.draw do + get "/list" => "actions#list" + end + + Discourse::Application.routes.append do + mount ::<%= name %>::Engine, at: "/<%= dasherized_name %>" + end + <% end %> end