moduler 0.5.0 → 0.6.0

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 (118) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/builds/moduler/tailwind.css +50 -1
  3. data/app/assets/javascripts/moduler/controllers/attachments_controller.js +30 -0
  4. data/app/assets/javascripts/moduler/controllers/dependent_select_controller.js +19 -0
  5. data/app/assets/javascripts/moduler/controllers/select_all_controller.js +15 -0
  6. data/app/components/moduler/filter_component.html.erb +9 -1
  7. data/app/components/moduler/filter_component.rb +12 -0
  8. data/app/components/moduler/form/attachments_component.html.erb +32 -0
  9. data/app/components/moduler/form/attachments_component.rb +28 -0
  10. data/app/components/moduler/form/checkboxes_component.html.erb +19 -0
  11. data/app/components/moduler/form/checkboxes_component.rb +13 -0
  12. data/app/components/moduler/form/select_component.html.erb +1 -1
  13. data/app/components/moduler/form/select_component.rb +5 -0
  14. data/app/components/moduler/form/wysiwyg_component.rb +1 -1
  15. data/app/controllers/concerns/moduler/authentication.rb +1 -1
  16. data/app/controllers/concerns/moduler/restrict_administrator_access.rb +32 -0
  17. data/app/controllers/moduler/administration/application_controller.rb +1 -0
  18. data/app/controllers/moduler/administration/articles_controller.rb +1 -1
  19. data/app/controllers/moduler/administration/emails/deliveries_controller.rb +42 -0
  20. data/app/controllers/moduler/administration/emails_controller.rb +68 -0
  21. data/app/controllers/moduler/administration/memberships_controller.rb +62 -0
  22. data/app/controllers/moduler/administration/organizations_controller.rb +51 -0
  23. data/app/controllers/moduler/administration/preferences_controller.rb +57 -0
  24. data/app/controllers/moduler/administration/roles_controller.rb +56 -0
  25. data/app/controllers/moduler/administration/users_controller.rb +2 -2
  26. data/app/controllers/moduler/application_controller.rb +2 -1
  27. data/app/controllers/moduler/articles_controller.rb +3 -2
  28. data/app/controllers/moduler/profile/sent_emails_controller.rb +18 -0
  29. data/app/controllers/moduler/profile/sessions/revocation_controller.rb +1 -0
  30. data/app/controllers/moduler/profile/sessions_controller.rb +1 -0
  31. data/app/controllers/moduler/profile_controller.rb +4 -1
  32. data/app/controllers/moduler/sessions_controller.rb +1 -0
  33. data/app/controllers/moduler/users_controller.rb +2 -0
  34. data/app/filters/email_delivery_filter.rb +35 -0
  35. data/app/filters/organization_query_filter.rb +23 -0
  36. data/app/jobs/moduler/email_delivery_job.rb +14 -0
  37. data/app/lib/moduler/form_builder.rb +17 -3
  38. data/app/mailers/moduler/application_mailer.rb +27 -1
  39. data/app/mailers/moduler/email_mailer.rb +14 -0
  40. data/app/models/moduler/article.rb +2 -0
  41. data/app/models/moduler/email.rb +59 -0
  42. data/app/models/moduler/membership.rb +25 -0
  43. data/app/models/moduler/organization.rb +13 -0
  44. data/app/models/moduler/preference.rb +44 -0
  45. data/app/models/moduler/role.rb +26 -0
  46. data/app/models/moduler/sent_email.rb +13 -0
  47. data/app/models/moduler/user.rb +13 -1
  48. data/app/models/moduler/user_email.rb +15 -0
  49. data/app/policies/moduler/administration/email_policy.rb +26 -0
  50. data/app/policies/moduler/administration/membership_policy.rb +29 -0
  51. data/app/policies/moduler/administration/organization_policy.rb +25 -0
  52. data/app/policies/moduler/administration/preference_policy.rb +29 -0
  53. data/app/policies/moduler/administration/role_policy.rb +25 -0
  54. data/app/views/layouts/moduler/mailer.html.erb +30 -0
  55. data/app/views/layouts/moduler/mailer.text.erb +1 -0
  56. data/app/views/moduler/administration/articles/_article.html.erb +2 -0
  57. data/app/views/moduler/administration/articles/_form.html.erb +1 -0
  58. data/app/views/moduler/administration/emails/_email.html.erb +33 -0
  59. data/app/views/moduler/administration/emails/_form.html.erb +23 -0
  60. data/app/views/moduler/administration/emails/deliveries/new.html.erb +58 -0
  61. data/app/views/moduler/administration/emails/edit.html.erb +1 -0
  62. data/app/views/moduler/administration/emails/index.html.erb +13 -0
  63. data/app/views/moduler/administration/emails/new.html.erb +1 -0
  64. data/app/views/moduler/administration/memberships/_form.html.erb +38 -0
  65. data/app/views/moduler/administration/memberships/_membership.html.erb +23 -0
  66. data/app/views/moduler/administration/memberships/edit.html.erb +1 -0
  67. data/app/views/moduler/administration/memberships/index.html.erb +14 -0
  68. data/app/views/moduler/administration/memberships/new.html.erb +1 -0
  69. data/app/views/moduler/administration/organizations/_form.html.erb +13 -0
  70. data/app/views/moduler/administration/organizations/_organization.html.erb +23 -0
  71. data/app/views/moduler/administration/organizations/edit.html.erb +1 -0
  72. data/app/views/moduler/administration/organizations/index.html.erb +13 -0
  73. data/app/views/moduler/administration/organizations/new.html.erb +1 -0
  74. data/app/views/moduler/administration/preferences/_form.html.erb +16 -0
  75. data/app/views/moduler/administration/preferences/_preference.html.erb +24 -0
  76. data/app/views/moduler/administration/preferences/edit.html.erb +1 -0
  77. data/app/views/moduler/administration/preferences/index.html.erb +13 -0
  78. data/app/views/moduler/administration/preferences/new.html.erb +1 -0
  79. data/app/views/moduler/administration/roles/_form.html.erb +24 -0
  80. data/app/views/moduler/administration/roles/_role.html.erb +15 -0
  81. data/app/views/moduler/administration/roles/edit.html.erb +1 -0
  82. data/app/views/moduler/administration/roles/index.html.erb +14 -0
  83. data/app/views/moduler/administration/roles/new.html.erb +1 -0
  84. data/app/views/moduler/administration/users/_user.html.erb +4 -0
  85. data/app/views/moduler/administration/users/edit.html.erb +1 -0
  86. data/app/views/moduler/administration/users/new.html.erb +1 -0
  87. data/app/views/moduler/common/_header.html.erb +2 -7
  88. data/app/views/moduler/common/administration/_sidebar.html.erb +9 -0
  89. data/app/views/moduler/common/profile/_sidebar.html.erb +1 -0
  90. data/app/views/moduler/email_mailer/custom.html.erb +3 -0
  91. data/app/views/moduler/email_mailer/custom.text.erb +1 -0
  92. data/app/views/moduler/profile/edit.html.erb +1 -0
  93. data/app/views/moduler/profile/sent_emails/index.html.erb +26 -0
  94. data/config/locales/moduler/administration/articles/en.yml +5 -0
  95. data/config/locales/moduler/administration/emails/en.yml +75 -0
  96. data/config/locales/moduler/administration/memberships/en.yml +35 -0
  97. data/config/locales/moduler/administration/organizations/en.yml +26 -0
  98. data/config/locales/moduler/administration/preferences/en.yml +44 -0
  99. data/config/locales/moduler/administration/roles/en.yml +32 -0
  100. data/config/locales/moduler/administration/users/en.yml +6 -1
  101. data/config/locales/moduler/common/en.yml +6 -1
  102. data/config/locales/moduler/components/en.yml +5 -0
  103. data/config/locales/moduler/models/en.yml +8 -0
  104. data/config/locales/moduler/profile/en.yml +2 -0
  105. data/config/locales/moduler/profile/sent_emails/en.yml +11 -0
  106. data/config/routes.rb +10 -0
  107. data/db/migrate/20261111113522_create_moduler_organizations.rb +11 -0
  108. data/db/migrate/20261111150000_create_moduler_roles.rb +13 -0
  109. data/db/migrate/20261111160000_create_moduler_memberships.rb +13 -0
  110. data/db/migrate/20261111180000_add_name_to_moduler_users.rb +5 -0
  111. data/db/migrate/20261111190000_add_status_to_moduler_articles.rb +8 -0
  112. data/db/migrate/20261111200000_create_moduler_preferences.rb +13 -0
  113. data/db/migrate/20261111210000_create_moduler_emails.rb +15 -0
  114. data/db/migrate/20261111210001_create_moduler_user_emails.rb +13 -0
  115. data/db/migrate/20261111210002_create_moduler_sent_emails.rb +12 -0
  116. data/lib/moduler/configuration.rb +6 -0
  117. data/lib/moduler/version.rb +1 -1
  118. metadata +80 -2
@@ -0,0 +1,51 @@
1
+ module Moduler
2
+ module Administration
3
+ class OrganizationsController < ApplicationController
4
+ before_action -> { authorize([ :moduler, :administration, :organization ]) }
5
+ before_action :set_organization, only: %i[ edit update ]
6
+
7
+ def index
8
+ @filter, @organizations = QueryFilter.new(Organization.all, permitted_filter_parameters).call
9
+ end
10
+
11
+ def new
12
+ @organization = Organization.new
13
+ end
14
+
15
+ def create
16
+ @organization = Organization.new(permitted_parameters)
17
+
18
+ if @organization.save
19
+ redirect_to moduler.administration_organizations_path, notice: t(".notice")
20
+ else
21
+ render :new, status: :unprocessable_entity
22
+ end
23
+ end
24
+
25
+ def edit; end
26
+
27
+ def update
28
+ if @organization.update(permitted_parameters)
29
+ redirect_to moduler.administration_organizations_path, notice: t(".notice")
30
+ else
31
+ render :edit, status: :unprocessable_entity
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def set_organization
38
+ @organization = Organization.find(params[:id])
39
+ authorize(@organization, policy_class: OrganizationPolicy)
40
+ end
41
+
42
+ def permitted_parameters
43
+ params.expect(organization: %i[ name ])
44
+ end
45
+
46
+ def permitted_filter_parameters
47
+ (params.permit(filter: %i[ query ])[:filter] || {}).merge(params.permit(:page))
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,57 @@
1
+ module Moduler
2
+ module Administration
3
+ class PreferencesController < ApplicationController
4
+ before_action -> { authorize([ :moduler, :administration, :preference ]) }
5
+ before_action :set_preference, only: %i[ edit update destroy ]
6
+
7
+ def index
8
+ @filter, @preferences = QueryFilter.new(Preference, permitted_filter_parameters).call
9
+ end
10
+
11
+ def new
12
+ @preference = Preference.new
13
+ end
14
+
15
+ def create
16
+ @preference = Preference.new(permitted_parameters)
17
+
18
+ if @preference.save
19
+ redirect_to moduler.administration_preferences_path, notice: t(".notice")
20
+ else
21
+ render :new, status: :unprocessable_entity
22
+ end
23
+ end
24
+
25
+ def edit; end
26
+
27
+ def update
28
+ if @preference.update(permitted_parameters)
29
+ redirect_to moduler.administration_preferences_path, notice: t(".notice")
30
+ else
31
+ render :edit, status: :unprocessable_entity
32
+ end
33
+ end
34
+
35
+ def destroy
36
+ @preference.destroy!
37
+
38
+ redirect_to moduler.administration_preferences_path, status: :see_other, notice: t(".notice")
39
+ end
40
+
41
+ private
42
+
43
+ def set_preference
44
+ @preference = Preference.find(params[:id])
45
+ authorize(@preference, policy_class: PreferencePolicy)
46
+ end
47
+
48
+ def permitted_parameters
49
+ params.expect(preference: %i[ kind name value ])
50
+ end
51
+
52
+ def permitted_filter_parameters
53
+ (params.permit(filter: %i[ query ])[:filter] || {}).merge(params.permit(:page))
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ module Moduler
2
+ module Administration
3
+ class RolesController < ApplicationController
4
+ before_action -> { authorize([ :moduler, :administration, :role ]) }
5
+ before_action :set_role, only: %i[ edit update ]
6
+
7
+ def index
8
+ @filter, @roles = OrganizationQueryFilter.new(Role.includes(:organization), permitted_filter_parameters).call
9
+ end
10
+
11
+ def new
12
+ @role = Role.new(moduler_organization_id: params[:organization_id])
13
+ end
14
+
15
+ def create
16
+ @role = Role.new(permitted_parameters)
17
+
18
+ if @role.save
19
+ redirect_to moduler.administration_roles_path, notice: t(".notice")
20
+ else
21
+ render :new, status: :unprocessable_entity
22
+ end
23
+ end
24
+
25
+ def edit; end
26
+
27
+ def update
28
+ if @role.update(permitted_update_parameters)
29
+ redirect_to moduler.administration_roles_path, notice: t(".notice")
30
+ else
31
+ render :edit, status: :unprocessable_entity
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def set_role
38
+ @role = Role.find(params[:id])
39
+ authorize(@role, policy_class: RolePolicy)
40
+ end
41
+
42
+ def permitted_parameters
43
+ params.expect(role: [ :moduler_organization_id, :name, permissions: [] ])
44
+ end
45
+
46
+ # The organization is set only when the role is created and cannot be changed.
47
+ def permitted_update_parameters
48
+ params.expect(role: [ :name, permissions: [] ])
49
+ end
50
+
51
+ def permitted_filter_parameters
52
+ (params.permit(filter: %i[ query organization_id ])[:filter] || {}).merge(params.permit(:page))
53
+ end
54
+ end
55
+ end
56
+ end
@@ -41,11 +41,11 @@ module Moduler
41
41
  end
42
42
 
43
43
  def permitted_parameters
44
- params.expect(user: %i[ email_address role status password ])
44
+ params.expect(user: %i[ email_address name role status password ])
45
45
  end
46
46
 
47
47
  def permitted_update_parameters
48
- params.expect(user: %i[ email_address role status ])
48
+ params.expect(user: %i[ email_address name role status ])
49
49
  end
50
50
 
51
51
  def permitted_filter_parameters
@@ -1,6 +1,7 @@
1
1
  module Moduler
2
2
  class ApplicationController < ActionController::Base
3
3
  include Authentication
4
+ include RestrictAdministratorAccess
4
5
  include RequireDocumentAcceptance
5
6
  include Pundit::Authorization
6
7
 
@@ -17,7 +18,7 @@ module Moduler
17
18
  end
18
19
 
19
20
  def not_authorized
20
- redirect_to moduler.root_path, alert: t("moduler.flash.not_authorized")
21
+ redirect_to current_home_path, alert: t("moduler.flash.not_authorized")
21
22
  end
22
23
  end
23
24
  end
@@ -4,13 +4,14 @@ module Moduler
4
4
 
5
5
  allow_unauthenticated_access
6
6
  skip_document_acceptance
7
+ allow_administrator_access
7
8
 
8
9
  def index
9
- @articles = Article.order(created_at: :desc).page(params[:page])
10
+ @articles = Article.published.order(created_at: :desc).page(params[:page])
10
11
  end
11
12
 
12
13
  def show
13
- @article = Article.find_by!(path: params[:path])
14
+ @article = Article.published.find_by!(path: params[:path])
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,18 @@
1
+ module Moduler
2
+ module Profile
3
+ # Emails delivered to the signed-in user.
4
+ class SentEmailsController < ApplicationController
5
+ skip_document_acceptance
6
+ allow_administrator_access
7
+
8
+ def index
9
+ @sent_emails = Current.user.sent_emails.with_attached_attachments.order(created_at: :desc).page(params[:page]).per(20)
10
+ end
11
+
12
+ # The email as it was delivered.
13
+ def show
14
+ render html: Current.user.sent_emails.find(params[:id]).body.html_safe, layout: false
15
+ end
16
+ end
17
+ end
18
+ end
@@ -3,6 +3,7 @@ module Moduler
3
3
  module Sessions
4
4
  class RevocationController < ApplicationController
5
5
  skip_document_acceptance
6
+ allow_administrator_access
6
7
 
7
8
  def destroy
8
9
  Current.user.sessions.active.update_all(signed_out_at: Time.current)
@@ -2,6 +2,7 @@ module Moduler
2
2
  module Profile
3
3
  class SessionsController < ApplicationController
4
4
  skip_document_acceptance
5
+ allow_administrator_access
5
6
 
6
7
  def index
7
8
  @sessions = Current.user.sessions.order(created_at: :desc).page(params[:page]).per(10)
@@ -1,6 +1,7 @@
1
1
  module Moduler
2
2
  class ProfileController < ApplicationController
3
3
  skip_document_acceptance
4
+ allow_administrator_access
4
5
 
5
6
  def edit
6
7
  @user = Current.user
@@ -22,8 +23,10 @@ module Moduler
22
23
 
23
24
  private
24
25
 
26
+ # The current password is required only to change the password.
25
27
  def permitted_parameters
26
- params.expect(user: %i[ password_challenge password password_confirmation ])
28
+ parameters = params.expect(user: %i[ name password_challenge password password_confirmation ])
29
+ parameters[:password].blank? ? parameters.except(:password_challenge, :password, :password_confirmation) : parameters
27
30
  end
28
31
  end
29
32
  end
@@ -2,6 +2,7 @@ module Moduler
2
2
  class SessionsController < ApplicationController
3
3
  allow_unauthenticated_access only: %i[ new create ]
4
4
  skip_document_acceptance
5
+ allow_administrator_access
5
6
 
6
7
  rate_limit to: 10, within: 3.minutes, only: :create,
7
8
  with: -> { redirect_to moduler.new_session_path, alert: t("moduler.sessions.create.rate_limited") }
@@ -2,6 +2,7 @@ module Moduler
2
2
  class UsersController < ApplicationController
3
3
  allow_unauthenticated_access only: %i[ new create ]
4
4
  skip_document_acceptance
5
+ allow_administrator_access
5
6
 
6
7
  rate_limit to: 10, within: 3.minutes, only: :create,
7
8
  with: -> { redirect_to moduler.new_user_path, alert: t("moduler.users.create.rate_limited") }
@@ -22,6 +23,7 @@ module Moduler
22
23
  @user = User.new(permitted_parameters)
23
24
 
24
25
  if @user.save
26
+ Email.deliver_event("user.signup", to: @user)
25
27
  redirect_to moduler.new_session_path, notice: t("moduler.users.create.success")
26
28
  else
27
29
  render :new
@@ -0,0 +1,35 @@
1
+ # Filters the users an email can be sent to, see
2
+ # Moduler::Administration::Emails::DeliveriesController.
3
+ class EmailDeliveryFilter < ApplicationFilter
4
+ PER_PAGE = [ 25, 50, 100 ].freeze
5
+
6
+ attribute :query, :string
7
+ attribute :role, :string
8
+ attribute :sent, :string
9
+ attribute :per_page, :integer
10
+ attribute :page, :integer
11
+
12
+ validates :query, length: { maximum: 100 }
13
+
14
+ modifiers << proc do |scope, filter|
15
+ scope.page(filter.page).per(PER_PAGE.include?(filter.per_page) ? filter.per_page : PER_PAGE.first)
16
+ end
17
+
18
+ modifiers << proc do |scope, filter|
19
+ if filter.query.present?
20
+ query = "%#{ActiveRecord::Base.sanitize_sql_like(filter.query)}%"
21
+ scope.where("moduler_users.email_address ILIKE :query OR moduler_users.name ILIKE :query", query: query)
22
+ end
23
+ end
24
+
25
+ modifiers << proc do |scope, filter|
26
+ scope.where(role: filter.role) if Moduler::User.roles.key?(filter.role)
27
+ end
28
+
29
+ modifiers << proc do |scope, filter|
30
+ case filter.sent
31
+ when "true" then scope.where.not(moduler_user_emails: { sent_at: nil })
32
+ when "false" then scope.where(moduler_user_emails: { sent_at: nil })
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ class OrganizationQueryFilter < ApplicationFilter
2
+ attribute :query, :string
3
+ attribute :organization_id, :string
4
+ attribute :page, :integer
5
+
6
+ validates :query, length: { maximum: 100 }
7
+
8
+ modifiers << proc do |scope, filter|
9
+ scope.page(filter.page).per(10)
10
+ end
11
+
12
+ modifiers << proc do |scope, _filter|
13
+ scope.order(created_at: :desc)
14
+ end
15
+
16
+ modifiers << proc do |scope, filter|
17
+ scope.search_by_query(filter.query) if filter.query.present?
18
+ end
19
+
20
+ modifiers << proc do |scope, filter|
21
+ scope.where(moduler_organization_id: filter.organization_id) if filter.organization_id.present?
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module Moduler
2
+ class EmailDeliveryJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ discard_on ActiveJob::DeserializationError
6
+
7
+ def perform(user_email)
8
+ return if user_email.sent?
9
+
10
+ EmailMailer.with(email: user_email.email, user: user_email.user).custom.deliver_now
11
+ user_email.touch(:sent_at)
12
+ end
13
+ end
14
+ end
@@ -4,7 +4,7 @@ module Moduler
4
4
  return unless error?(field)
5
5
 
6
6
  @template.content_tag :div, **options do
7
- @object.errors[field].first
7
+ error_attributes(field).flat_map { |attribute| @object.errors[attribute] }.first
8
8
  end
9
9
  end
10
10
 
@@ -12,12 +12,26 @@ module Moduler
12
12
  return unless error?(field)
13
13
 
14
14
  @template.content_tag :div, **options do
15
- @object.errors.full_messages_for(field).first
15
+ error_attributes(field).flat_map { |attribute| @object.errors.full_messages_for(attribute) }.first
16
16
  end
17
17
  end
18
18
 
19
19
  def error?(field)
20
- @object && @object.errors[field].try(:any?)
20
+ @object.present? && error_attributes(field).any? { |attribute| @object.errors[attribute].try(:any?) }
21
+ end
22
+
23
+ private
24
+
25
+ # The field itself and, for a foreign key field (e.g. moduler_user_id), the
26
+ # belongs_to association it belongs to (e.g. user), which Rails validates.
27
+ def error_attributes(field)
28
+ [ field, belongs_to_association(field) ].compact
29
+ end
30
+
31
+ def belongs_to_association(field)
32
+ return unless @object.class.respond_to?(:reflect_on_all_associations)
33
+
34
+ @object.class.reflect_on_all_associations(:belongs_to).find { |association| association.foreign_key.to_s == field.to_s }&.name
21
35
  end
22
36
  end
23
37
  end
@@ -1,6 +1,32 @@
1
1
  module Moduler
2
2
  class ApplicationMailer < ActionMailer::Base
3
3
  default from: "from@example.com"
4
- layout "mailer"
4
+ layout "moduler/mailer"
5
+
6
+ after_deliver :record_sent_email
7
+
8
+ private
9
+
10
+ # Keeps a copy of the delivered email, shown in the recipient's profile.
11
+ # A failure to record it does not fail the delivery.
12
+ def record_sent_email
13
+ to_address = message.to&.first
14
+ return if to_address.blank?
15
+
16
+ sent_email = SentEmail.new(
17
+ user: User.find_by(email_address: to_address.downcase),
18
+ to_address: to_address,
19
+ subject: message.subject,
20
+ body: (message.html_part || message).body.decoded
21
+ )
22
+
23
+ message.attachments.each do |attachment|
24
+ sent_email.attachments.attach(io: StringIO.new(attachment.body.decoded), filename: attachment.filename, content_type: attachment.mime_type)
25
+ end
26
+
27
+ sent_email.save!
28
+ rescue StandardError => error
29
+ Rails.error.report(error, handled: true)
30
+ end
5
31
  end
6
32
  end
@@ -0,0 +1,14 @@
1
+ module Moduler
2
+ class EmailMailer < ApplicationMailer
3
+ def custom
4
+ @email = params[:email]
5
+ @user = params[:user]
6
+
7
+ @email.attachments.each do |attachment|
8
+ attachments[attachment.filename.to_s] = attachment.download
9
+ end
10
+
11
+ mail(to: @user.email_address, subject: @email.subject, from: @email.sender.presence || self.class.default[:from])
12
+ end
13
+ end
14
+ end
@@ -7,6 +7,8 @@ module Moduler
7
7
 
8
8
  has_many_attached :images
9
9
 
10
+ enum :status, { unpublished: 0, published: 1 }, default: :unpublished, validate: true
11
+
10
12
  validates :path, presence: true, uniqueness: true, format: { with: PATH_FORMAT }
11
13
  validates :title, presence: true
12
14
  validates :description, presence: true
@@ -0,0 +1,59 @@
1
+ module Moduler
2
+ # An email composed in the administration area and sent to chosen users,
3
+ # or from code by its event name:
4
+ #
5
+ # Moduler::Email.deliver_event("order.paid", to: user)
6
+ #
7
+ # The engine itself sends "user.signup" after a user signs up.
8
+ class Email < ApplicationRecord
9
+ include PgSearch::Model
10
+
11
+ KINDS = %w[ custom system newsletter ].freeze
12
+ EVENT_FORMAT = /\A[a-z0-9_]+(\.[a-z0-9_]+)*\z/
13
+
14
+ has_many :user_emails, class_name: "Moduler::UserEmail", foreign_key: "moduler_email_id", dependent: :destroy
15
+
16
+ has_many_attached :attachments
17
+ has_many_attached :images
18
+
19
+ normalizes :event, with: ->(event) { event.strip.presence }
20
+ normalizes :sender, with: ->(sender) { sender.strip.presence }
21
+
22
+ validates :subject, presence: true
23
+ validates :kind, presence: true, inclusion: { in: KINDS }
24
+ validates :event, uniqueness: true, format: { with: EVENT_FORMAT }, allow_nil: true
25
+ validates :body, presence: true
26
+ validates :images, content_type: %w[ image/png image/jpeg image/gif image/webp ], size: { less_than: 10.megabytes }
27
+
28
+ pg_search_scope :search_by_query, against: %i[ subject kind ], using: { trigram: { word_similarity: true } }
29
+
30
+ # Sends the email of the given event to the user, once. Does nothing
31
+ # when no email has the event.
32
+ def self.deliver_event(event, to:)
33
+ find_by(event: event.to_s)&.deliver_to([ to ])
34
+ end
35
+
36
+ # Queues the email for the users it has not been queued for yet and
37
+ # returns how many were queued.
38
+ def deliver_to(users)
39
+ users.count do |user|
40
+ user_email = user_emails.find_or_create_by!(user: user)
41
+ next false unless user_email.previously_new_record?
42
+
43
+ EmailDeliveryJob.perform_later(user_email)
44
+ true
45
+ end
46
+ end
47
+
48
+ # The number of users the email has been delivered to.
49
+ def sent_count
50
+ user_emails.sent.count
51
+ end
52
+
53
+ def to_html
54
+ html = Redcarpet::Markdown.new(Redcarpet::Render::HTML, tables: true, fenced_code_blocks: true).render(body.to_s)
55
+
56
+ ActionController::Base.helpers.sanitize(html, tags: Moduler::Article::ALLOWED_TAGS)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ module Moduler
2
+ class Membership < ApplicationRecord
3
+ include PgSearch::Model
4
+
5
+ belongs_to :user, class_name: "Moduler::User", foreign_key: "moduler_user_id"
6
+ belongs_to :organization, class_name: "Moduler::Organization", foreign_key: "moduler_organization_id"
7
+ belongs_to :role, class_name: "Moduler::Role", foreign_key: "moduler_role_id"
8
+
9
+ validates :user, uniqueness: { scope: :moduler_organization_id }
10
+ validate :user_must_be_member
11
+ validate :role_must_belong_to_organization
12
+
13
+ pg_search_scope :search_by_query, associated_against: { user: :email_address }, using: { trigram: { word_similarity: true } }
14
+
15
+ private
16
+
17
+ def user_must_be_member
18
+ errors.add(:user, :not_member) if user && !user.member?
19
+ end
20
+
21
+ def role_must_belong_to_organization
22
+ errors.add(:role, :inclusion) if role && role.moduler_organization_id != moduler_organization_id
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Moduler
2
+ class Organization < ApplicationRecord
3
+ include PgSearch::Model
4
+
5
+ has_many :memberships, class_name: "Moduler::Membership", foreign_key: "moduler_organization_id", dependent: :delete_all
6
+ has_many :users, class_name: "Moduler::User", through: :memberships
7
+ has_many :roles, class_name: "Moduler::Role", foreign_key: "moduler_organization_id", dependent: :delete_all
8
+
9
+ validates :name, presence: true, uniqueness: true
10
+
11
+ pg_search_scope :search_by_query, against: %i[ name ], using: { trigram: { word_similarity: true } }
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ module Moduler
2
+ # Runtime settings and feature toggles managed in the administration area
3
+ # and read in code as Moduler::Preference::NAME, cast to the preference's
4
+ # kind. A preference that does not exist reads as nil.
5
+ class Preference < ApplicationRecord
6
+ include PgSearch::Model
7
+
8
+ KINDS = %w[ string boolean integer ].freeze
9
+ NAME_FORMAT = /\A[A-Z][A-Z0-9_]*\z/
10
+
11
+ before_validation :normalize_boolean_value
12
+
13
+ validates :kind, presence: true, inclusion: { in: KINDS }
14
+ validates :name, presence: true, uniqueness: true, format: { with: NAME_FORMAT }
15
+ validates :value, presence: true
16
+ validates :value, inclusion: { in: %w[ true false ] }, if: -> { kind == "boolean" }
17
+ validates :value, format: { with: /\A-?\d+\z/ }, if: -> { kind == "integer" }
18
+
19
+ pg_search_scope :search_by_query, against: %i[ kind name ], using: { trigram: { word_similarity: true } }
20
+
21
+ class << self
22
+ def const_missing(name)
23
+ return super unless name.to_s.match?(NAME_FORMAT)
24
+
25
+ find_by(name: name.to_s)&.cast_value
26
+ end
27
+ end
28
+
29
+ # The value cast to the preference's kind.
30
+ def cast_value
31
+ case kind
32
+ when "string" then value
33
+ when "integer" then Integer(value)
34
+ when "boolean" then value.to_s.strip.downcase == "true"
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def normalize_boolean_value
41
+ self.value = value.strip.downcase if kind == "boolean" && value.present?
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ module Moduler
2
+ class Role < ApplicationRecord
3
+ include PgSearch::Model
4
+
5
+ belongs_to :organization, class_name: "Moduler::Organization", foreign_key: "moduler_organization_id"
6
+ has_many :memberships, class_name: "Moduler::Membership", foreign_key: "moduler_role_id", dependent: :restrict_with_error
7
+
8
+ normalizes :permissions, with: ->(permissions) { Array(permissions).map(&:to_s).compact_blank.uniq }
9
+
10
+ validates :name, presence: true, uniqueness: { scope: :moduler_organization_id }
11
+ validate :permissions_must_be_configured
12
+
13
+ pg_search_scope :search_by_query, against: %i[ name ], using: { trigram: { word_similarity: true } }
14
+
15
+ def permission?(permission)
16
+ permissions.include?(permission.to_s)
17
+ end
18
+
19
+ private
20
+
21
+ def permissions_must_be_configured
22
+ unknown = permissions - Moduler.configuration.permissions
23
+ errors.add(:permissions, :inclusion, value: unknown.join(", ")) if unknown.any?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module Moduler
2
+ # A copy of every email delivered by the engine's mailers, recorded by
3
+ # Moduler::ApplicationMailer and shown in the recipient's profile.
4
+ class SentEmail < ApplicationRecord
5
+ belongs_to :user, class_name: "Moduler::User", foreign_key: "moduler_user_id", optional: true
6
+
7
+ has_many_attached :attachments
8
+
9
+ validates :to_address, presence: true
10
+ validates :subject, presence: true
11
+ validates :body, presence: true
12
+ end
13
+ end