decidim-generators 0.11.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +26 -0
  3. data/Gemfile.lock +671 -0
  4. data/README.md +76 -0
  5. data/Rakefile +32 -0
  6. data/exe/decidim +14 -0
  7. data/lib/decidim/generators/app_generator.rb +161 -0
  8. data/lib/decidim/generators/app_templates/Dockerfile.erb +1 -0
  9. data/lib/decidim/generators/app_templates/README.md.erb +22 -0
  10. data/lib/decidim/generators/app_templates/cable.yml.erb +9 -0
  11. data/lib/decidim/generators/app_templates/carrierwave.rb +28 -0
  12. data/lib/decidim/generators/app_templates/database.yml.erb +87 -0
  13. data/lib/decidim/generators/app_templates/decidim.scss.erb +3 -0
  14. data/lib/decidim/generators/app_templates/decidim_controller.rb.erb +5 -0
  15. data/lib/decidim/generators/app_templates/docker-compose.yml.erb +26 -0
  16. data/lib/decidim/generators/app_templates/example_authorization_handler.rb +55 -0
  17. data/lib/decidim/generators/app_templates/initializer.rb +47 -0
  18. data/lib/decidim/generators/app_templates/secrets.yml.erb +61 -0
  19. data/lib/decidim/generators/app_templates/social_share_button.rb +8 -0
  20. data/lib/decidim/generators/component_generator.rb +74 -0
  21. data/lib/decidim/generators/component_templates/Gemfile.erb +26 -0
  22. data/lib/decidim/generators/component_templates/LICENSE-AGPLv3.txt +661 -0
  23. data/lib/decidim/generators/component_templates/README.md.erb +30 -0
  24. data/lib/decidim/generators/component_templates/Rakefile +9 -0
  25. data/lib/decidim/generators/component_templates/app/assets/config/component_manifest.js +0 -0
  26. data/lib/decidim/generators/component_templates/app/assets/images/decidim/component/icon.svg +1 -0
  27. data/lib/decidim/generators/component_templates/app/controllers/decidim/component/admin/application_controller.rb.erb +15 -0
  28. data/lib/decidim/generators/component_templates/app/controllers/decidim/component/application_controller.rb.erb +13 -0
  29. data/lib/decidim/generators/component_templates/app/helpers/decidim/component/application_helper.rb.erb +10 -0
  30. data/lib/decidim/generators/component_templates/app/models/decidim/component/abilities/admin/admin_ability.rb.erb +17 -0
  31. data/lib/decidim/generators/component_templates/app/models/decidim/component/abilities/current_user_ability.rb.erb +47 -0
  32. data/lib/decidim/generators/component_templates/app/models/decidim/component/application_record.rb.erb +10 -0
  33. data/lib/decidim/generators/component_templates/bin/rails.erb +20 -0
  34. data/lib/decidim/generators/component_templates/config/i18n-tasks.yml.erb +10 -0
  35. data/lib/decidim/generators/component_templates/config/locales/en.yml.erb +6 -0
  36. data/lib/decidim/generators/component_templates/decidim-component.gemspec.erb +22 -0
  37. data/lib/decidim/generators/component_templates/lib/decidim/component.rb.erb +13 -0
  38. data/lib/decidim/generators/component_templates/lib/decidim/component/admin.rb.erb +10 -0
  39. data/lib/decidim/generators/component_templates/lib/decidim/component/admin_engine.rb.erb +32 -0
  40. data/lib/decidim/generators/component_templates/lib/decidim/component/component.rb.erb +40 -0
  41. data/lib/decidim/generators/component_templates/lib/decidim/component/engine.rb.erb +29 -0
  42. data/lib/decidim/generators/component_templates/lib/decidim/component/test/factories.rb.erb +13 -0
  43. data/lib/decidim/generators/component_templates/lib/decidim/component/version.rb.erb +10 -0
  44. data/lib/decidim/generators/component_templates/spec/factories.rb.erb +3 -0
  45. data/lib/decidim/generators/component_templates/spec/spec_helper.rb.erb +13 -0
  46. data/lib/decidim/generators/install_generator.rb +170 -0
  47. data/lib/decidim/generators/version.rb +10 -0
  48. metadata +123 -0
@@ -0,0 +1,30 @@
1
+ # Decidim::<%= component_module_name %>
2
+
3
+ <%= component_description %>.
4
+
5
+ ## Usage
6
+
7
+ <%= component_module_name %> will be available as a Component for a Participatory
8
+ Space.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'decidim-<%= component_name %>
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```bash
21
+ bundle
22
+ ```
23
+
24
+ ## Contributing
25
+
26
+ See [Decidim](https://github.com/decidim/decidim).
27
+
28
+ ## License
29
+
30
+ This engine is distributed under the GNU AFFERO GENERAL PUBLIC LICENSE.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/dev/common_rake"
4
+
5
+ desc "Generates a dummy app for testing"
6
+ task test_app: "decidim:generate_external_test_app"
7
+
8
+ desc "Generates a development app."
9
+ task development_app: "decidim:generate_external_development_app"
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 35 35"><path d="M17.5 35A17.5 17.5 0 1 1 35 17.5 17.52 17.52 0 0 1 17.5 35zm0-33.06A15.56 15.56 0 1 0 33.06 17.5 15.57 15.57 0 0 0 17.5 1.94zm9.5 13.7H8a1 1 0 0 1 0-1.94h19a1 1 0 0 1 0 1.94zm0 3.68H8a1 1 0 0 1 0-1.94h19a1 1 0 0 1 0 1.94zM22.26 23H8a1 1 0 0 1 0-1.94h14.26a1 1 0 0 1 0 1.94z"/></svg>
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ module Admin
6
+ # This controller is the abstract class from which all other controllers of
7
+ # this engine inherit.
8
+ #
9
+ # Note that it inherits from `Decidim::Admin::Components::BaseController`, which
10
+ # override its layout and provide all kinds of useful methods.
11
+ class ApplicationController < Decidim::Admin::Components::BaseController
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ # This controller is the abstract class from which all other controllers of
6
+ # this engine inherit.
7
+ #
8
+ # Note that it inherits from `Decidim::Components::BaseController`, which
9
+ # override its layout and provide all kinds of useful methods.
10
+ class ApplicationController < Decidim::Components::BaseController
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ # Custom helpers, scoped to the <%= component_name %> engine.
6
+ #
7
+ module ApplicationHelper
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ module Abilities
6
+ module Admin
7
+ # Defines the abilities related to <%= component_name %> for a logged in admin user.
8
+ # Intended to be used with `cancancan`.
9
+ class AdminAbility < Decidim::Abilities::AdminAbility
10
+ def define_abilities
11
+ # can :manage, SomeResource
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ module Abilities
6
+ # Defines the abilities related to <%= component_name %> for a logged in user.
7
+ # Intended to be used with `cancancan`.
8
+ class CurrentUserAbility
9
+ include CanCan::Ability
10
+
11
+ attr_reader :user, :context
12
+
13
+ def initialize(user, context)
14
+ return unless user
15
+
16
+ @user = user
17
+ @context = context
18
+
19
+ # can :manage, SomeResource if authorized?(:some_action)
20
+ end
21
+
22
+ private
23
+
24
+ def authorized?(action)
25
+ return unless component
26
+
27
+ ActionAuthorizer.new(user, component, action).authorize.ok?
28
+ end
29
+
30
+ def current_settings
31
+ context.fetch(:current_settings, nil)
32
+ end
33
+
34
+ def component_settings
35
+ context.fetch(:component_settings, nil)
36
+ end
37
+
38
+ def component
39
+ component = context.fetch(:current_component, nil)
40
+ return nil unless component && component.manifest.name == :<%= component_name %>
41
+
42
+ component
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ # Abstract class from which all models in this engine inherit.
6
+ class ApplicationRecord < ActiveRecord::Base
7
+ self.abstract_class = true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This command will automatically be run when you run "rails" with Rails gems
5
+ # installed from the root of your application.
6
+
7
+ ENGINE_ROOT = File.expand_path("..", __dir__)
8
+ ENGINE_PATH = File.expand_path("../lib/decidim/<%= component_name %>/engine", __dir__)
9
+
10
+ # Set up gems listed in the Gemfile.
11
+ <%- if options[:external] -%>
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
13
+ <% else %>
14
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __dir__)
15
+ <%- end -%>
16
+
17
+ require "bundler/setup"
18
+
19
+ require "rails/all"
20
+ require "rails/engine/commands"
@@ -0,0 +1,10 @@
1
+ ---
2
+
3
+ base_locale: en
4
+ locales: [en]
5
+
6
+ ignore_unused:
7
+ - "decidim.components.<%= component_name %>.name"
8
+
9
+ ignore_missing:
10
+ - decidim.participatory_processes.scopes.global
@@ -0,0 +1,6 @@
1
+ ---
2
+ en:
3
+ decidim:
4
+ components:
5
+ <%= component_name %>:
6
+ name: <%= component_module_name %>
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
4
+
5
+ require "decidim/<%= component_name %>/version"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.version = Decidim::<%= component_module_name %>.version
9
+ s.authors = ["<%= %x[git config user.name].chomp %>"]
10
+ s.email = ["<%= %x[git config user.email].chomp %>"]
11
+ s.license = "AGPL-3.0"
12
+ s.homepage = "https://github.com/decidim/decidim-module-<%= component_name %>"
13
+ s.required_ruby_version = ">= 2.3.1"
14
+
15
+ s.name = "decidim-<%= component_name %>"
16
+ s.summary = "A decidim <%= component_name %> module"
17
+ s.description = "<%= component_description %>."
18
+
19
+ s.files = Dir["{app,config,lib}/**/*", "LICENSE-AGPLv3.txt", "Rakefile", "README.md"]
20
+
21
+ s.add_dependency "decidim-core", Decidim::<%= component_module_name %>.version
22
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/<%= component_name %>/admin"
4
+ require "decidim/<%= component_name %>/engine"
5
+ require "decidim/<%= component_name %>/admin_engine"
6
+ require "decidim/<%= component_name %>/component"
7
+
8
+ module Decidim
9
+ # This namespace holds the logic of the `<%= component_module_name %>` component. This component
10
+ # allows users to create <%= component_name %> in a participatory space.
11
+ module <%= component_module_name %>
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ # This module contains all the domain logic associated to Decidim's <%= component_module_name %>
6
+ # component admin panel.
7
+ module Admin
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module <%= component_module_name %>
5
+ # This is the engine that runs on the public interface of `<%= component_module_name %>`.
6
+ class AdminEngine < ::Rails::Engine
7
+ isolate_namespace Decidim::<%= component_module_name %>::Admin
8
+
9
+ paths["db/migrate"] = nil
10
+
11
+ routes do
12
+ # Add admin engine routes here
13
+ # resources :<%= component_name %> do
14
+ # collection do
15
+ # resources :exports, only: [:create]
16
+ # end
17
+ # end
18
+ # root to: "<%= component_name %>#index"
19
+ end
20
+
21
+ initializer "decidim_<%= component_name %>.inject_abilities_to_user" do |_app|
22
+ Decidim.configure do |config|
23
+ config.admin_abilities += ["Decidim::<%= component_module_name %>::Abilities::Admin::AdminAbility"]
24
+ end
25
+ end
26
+
27
+ def load_seed
28
+ nil
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency "decidim/components/namer"
4
+
5
+ Decidim.register_component(:<%= component_name %>) do |component|
6
+ component.engine = Decidim::<%= component_module_name %>::Engine
7
+ component.admin_engine = Decidim::<%= component_module_name %>::AdminEngine
8
+ component.icon = "decidim/<%= component_name %>/icon.svg"
9
+
10
+ # component.on(:before_destroy) do |instance|
11
+ # # Code executed before removing the component
12
+ # end
13
+
14
+ # These actions permissions can be configured in the admin panel
15
+ # component.actions = %w()
16
+
17
+ # component.settings(:global) do |settings|
18
+ # # Add your global settings
19
+ # # Available types: :integer, :boolean
20
+ # # settings.attribute :vote_limit, type: :integer, default: 0
21
+ # end
22
+
23
+ # component.settings(:step) do |settings|
24
+ # # Add your settings per step
25
+ # end
26
+
27
+ # component.register_resource do |resource|
28
+ # # Register a optional resource that can be references from other resources.
29
+ # resource.model_class_name = "Decidim::<%= component_module_name %>::SomeResource"
30
+ # resource.template = "decidim/<%= component_name %>/some_resources/linked_some_resources"
31
+ # end
32
+
33
+ # component.register_stat :some_stat do |context, start_at, end_at|
34
+ # # Register some stat number to the application
35
+ # end
36
+
37
+ # component.seeds do |participatory_space|
38
+ # # Add some seeds for this component
39
+ # end
40
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "decidim/core"
5
+
6
+ module Decidim
7
+ module <%= component_module_name %>
8
+ # This is the engine that runs on the public interface of <%= component_name %>.
9
+ class Engine < ::Rails::Engine
10
+ isolate_namespace Decidim::<%= component_module_name %>
11
+
12
+ routes do
13
+ # Add engine routes here
14
+ # resources :<%= component_name %>
15
+ # root to: "<%= component_name %>#index"
16
+ end
17
+
18
+ initializer "decidim_<%= component_name %>.assets" do |app|
19
+ app.config.assets.precompile += %w[decidim_<%= component_name %>_manifest.js decidim_<%= component_name %>_manifest.css]
20
+ end
21
+
22
+ initializer "decidim_<%= component_name %>.inject_abilities_to_user" do |_app|
23
+ Decidim.configure do |config|
24
+ config.abilities += ["Decidim::<%= component_module_name %>::Abilities::CurrentUserAbility"]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/core/test/factories"
4
+
5
+ FactoryBot.define do
6
+ factory :<%= component_name %>_component, parent: :component do
7
+ name { Decidim::Components::Namer.new(participatory_space.organization.available_locales, :<%= component_name %>).i18n_name }
8
+ manifest_name :<%= component_name %>
9
+ participatory_space { create(:participatory_process, :with_steps) }
10
+ end
11
+
12
+ # Add engine factories here
13
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ # This holds the decidim-meetings version.
5
+ module <%= component_module_name %>
6
+ def self.version
7
+ "<%= core_version %>"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/<%= component_name %>/test/factories"
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/dev"
4
+
5
+ ENV["ENGINE_NAME"] = File.dirname(__dir__).split("/").last
6
+
7
+ <%- if options[:external] -%>
8
+ Decidim::Dev.dummy_app_path = File.expand_path(File.join("spec", "decidim_dummy_app"))
9
+ <%- else -%>
10
+ Decidim::Dev.dummy_app_path = File.expand_path(File.join("..", "spec", "decidim_dummy_app"))
11
+ <%- end -%>
12
+
13
+ require "decidim/dev/test/base_spec_helper"
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+ require "securerandom"
5
+
6
+ module Decidim
7
+ module Generators
8
+ # Installs `decidim` to a Rails app by adding the needed lines of code
9
+ # automatically to important files in the Rails app.
10
+ #
11
+ # Remember that, for how generators work, actions are executed based on the
12
+ # definition order of the public methods.
13
+ class InstallGenerator < Rails::Generators::Base
14
+ desc "Install decidim"
15
+ source_root File.expand_path("app_templates", __dir__)
16
+
17
+ class_option :app_name, type: :string,
18
+ default: nil,
19
+ desc: "The name of the app"
20
+
21
+ class_option :recreate_db, type: :boolean,
22
+ default: false,
23
+ desc: "Recreate db after installing decidim"
24
+
25
+ class_option :seed_db, type: :boolean,
26
+ default: false,
27
+ desc: "Seed db after installing decidim"
28
+
29
+ def install
30
+ route "mount Decidim::Core::Engine => '/'"
31
+ end
32
+
33
+ def add_seeds
34
+ append_file "db/seeds.rb", <<~RUBY
35
+ # You can remove the 'faker' gem if you don't want Decidim seeds.
36
+ Decidim.seed!
37
+ RUBY
38
+ end
39
+
40
+ def copy_initializer
41
+ copy_file "carrierwave.rb", "config/initializers/carrierwave.rb"
42
+ copy_file "social_share_button.rb", "config/initializers/social_share_button.rb"
43
+ end
44
+
45
+ def secrets
46
+ template "secrets.yml.erb", "config/secrets.yml", force: true
47
+ end
48
+
49
+ def remove_layout
50
+ remove_file "app/views/layouts/application.html.erb"
51
+ remove_file "app/views/layouts/mailer.text.erb"
52
+ end
53
+
54
+ def append_assets
55
+ append_file "app/assets/javascripts/application.js", "//= require decidim"
56
+ gsub_file "app/assets/javascripts/application.js", %r{//= require turbolinks\n}, ""
57
+ inject_into_file "app/assets/stylesheets/application.css",
58
+ before: "*= require_tree ." do
59
+ "*= require decidim\n "
60
+ end
61
+
62
+ template "decidim.scss.erb", "app/assets/stylesheets/decidim.scss", force: true
63
+ end
64
+
65
+ def disable_precompilation_on_demand
66
+ %w(development test).each do |environment|
67
+ inject_into_file "config/environments/#{environment}.rb",
68
+ before: /^end$/ do
69
+ cut <<~RUBY, strip: false
70
+ |
71
+ | # No precompilation on demand on first request
72
+ | config.assets.check_precompiled_asset = false
73
+ RUBY
74
+ end
75
+ end
76
+ end
77
+
78
+ def configure_js_compressor
79
+ gsub_file "config/environments/production.rb", "config.assets.js_compressor = :uglifier", "config.assets.js_compressor = Uglifier.new(:harmony => true)"
80
+ end
81
+
82
+ def smtp_environment
83
+ inject_into_file "config/environments/production.rb",
84
+ after: "config.log_formatter = ::Logger::Formatter.new" do
85
+ cut <<~RUBY
86
+ |
87
+ | config.action_mailer.smtp_settings = {
88
+ | :address => Rails.application.secrets.smtp_address,
89
+ | :port => Rails.application.secrets.smtp_port,
90
+ | :authentication => Rails.application.secrets.smtp_authentication,
91
+ | :user_name => Rails.application.secrets.smtp_username,
92
+ | :password => Rails.application.secrets.smtp_password,
93
+ | :domain => Rails.application.secrets.smtp_domain,
94
+ | :enable_starttls_auto => Rails.application.secrets.smtp_starttls_auto,
95
+ | :openssl_verify_mode => 'none'
96
+ | }
97
+ RUBY
98
+ end
99
+ end
100
+
101
+ def copy_migrations
102
+ rails "railties:install:migrations"
103
+ recreate_db if options[:recreate_db]
104
+ end
105
+
106
+ def letter_opener_web
107
+ letter_opener_route = cut <<~RUBY
108
+ |
109
+ | if Rails.env.development?
110
+ | mount LetterOpenerWeb::Engine, at: "/letter_opener"
111
+ | end
112
+ RUBY
113
+
114
+ route letter_opener_route
115
+
116
+ inject_into_file "config/environments/development.rb",
117
+ after: "config.action_mailer.raise_delivery_errors = false" do
118
+ cut <<~RUBY
119
+ |
120
+ | config.action_mailer.delivery_method = :letter_opener_web
121
+ | config.action_mailer.default_url_options = { port: 3000 }
122
+ RUBY
123
+ end
124
+ end
125
+
126
+ private
127
+
128
+ def recreate_db
129
+ soft_rails "db:environment:set", "db:drop"
130
+ rails "db:create"
131
+
132
+ # In order to ensure that migrations don't eager load models with not
133
+ # yet fully populated schemas (which could break commands which load
134
+ # migrations and seeds in the same process, such as `rails db:migrate
135
+ # db:seed`), we make sure to run them in the same process if seeds are
136
+ # requested so that we can catch these situations earlier than end
137
+ # users.
138
+ if options[:seed_db]
139
+ rails "db:migrate", "db:seed"
140
+ else
141
+ rails "db:migrate"
142
+ end
143
+
144
+ rails "db:test:prepare"
145
+ end
146
+
147
+ # Runs rails commands in a subprocess, and aborts if it doesn't suceeed
148
+ def rails(*args)
149
+ abort unless system("bin/rails", *args)
150
+ end
151
+
152
+ # Runs rails commands in a subprocess silencing errors, and ignores status
153
+ def soft_rails(*args)
154
+ system("bin/rails", *args, err: File::NULL)
155
+ end
156
+
157
+ def scss_variables
158
+ variables = File.join(Gem.loaded_specs["decidim-core"].full_gem_path, "app", "assets", "stylesheets", "decidim", "_variables.scss")
159
+ File.read(variables).split("\n").map { |line| "// #{line}".gsub(" !default", "") }.join("\n")
160
+ end
161
+
162
+ def cut(text, strip: true)
163
+ cutted = text.gsub(/^ *\|/, "")
164
+ return cutted unless strip
165
+
166
+ cutted.rstrip
167
+ end
168
+ end
169
+ end
170
+ end