glib-web 8.1.0 → 8.2.1

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 (179) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/glib/json/libs.rb +16 -0
  3. data/app/controllers/glib/glib_direct_uploads_controller.rb +2 -1
  4. data/app/helpers/glib/app_feature_support_helper.rb +33 -8
  5. data/app/helpers/glib/json_ui/view_builder/fields.rb +110 -9
  6. data/app/helpers/glib/json_ui/view_builder.rb +29 -0
  7. data/app/jobs/glib/scheduled_job.rb +581 -0
  8. data/app/jobs/glib/scheduled_job_tick.rb +21 -0
  9. data/app/models/glib/scheduled_job_run.rb +16 -0
  10. data/app/views/json_ui/garage/actions/sheet_content.json.jbuilder +1 -1
  11. data/app/views/json_ui/garage/forms/_action_text_conversion.json.jbuilder +12 -0
  12. data/app/views/json_ui/garage/forms/action_text.json.jbuilder +57 -0
  13. data/app/views/json_ui/garage/forms/action_text_attachment.json.jbuilder +7 -0
  14. data/app/views/json_ui/garage/forms/action_text_conversion.json.jbuilder +5 -0
  15. data/app/views/json_ui/garage/forms/action_text_library.json.jbuilder +26 -0
  16. data/app/views/json_ui/garage/forms/action_text_mentions.json.jbuilder +4 -0
  17. data/app/views/json_ui/garage/forms/action_text_preview.json.jbuilder +9 -0
  18. data/app/views/json_ui/garage/forms/dialogs_form.json.jbuilder +10 -10
  19. data/app/views/json_ui/garage/forms/dirty_prompt.json.jbuilder +1 -1
  20. data/app/views/json_ui/garage/forms/dynamic_group.json.jbuilder +1 -1
  21. data/app/views/json_ui/garage/forms/index.json.jbuilder +6 -0
  22. data/app/views/json_ui/garage/forms/pickers.json.jbuilder +7 -0
  23. data/app/views/json_ui/garage/forms/show_hide.json.jbuilder +1 -1
  24. data/app/views/json_ui/garage/test_page/_header.json.jbuilder +6 -0
  25. data/app/views/json_ui/garage/test_page/charts.json.jbuilder +4 -2
  26. data/app/views/json_ui/garage/test_page/fields_actionText.json.jbuilder +189 -0
  27. data/app/views/json_ui/garage/test_page/fields_richText.json.jbuilder +45 -1
  28. data/app/views/json_ui/garage/test_page/fields_sign.json.jbuilder +34 -3
  29. data/app/views/json_ui/garage/test_page/form_dirty_fields.json.jbuilder +1 -1
  30. data/app/views/json_ui/garage/test_page/form_dirty_hooks.json.jbuilder +22 -0
  31. data/app/views/json_ui/garage/test_page/form_hidden_panels.json.jbuilder +105 -0
  32. data/app/views/json_ui/garage/views/charts.json.jbuilder +4 -2
  33. data/app/views/layouts/json_ui/renderer.html.erb +11 -1
  34. data/engines/blog/README.md +172 -0
  35. data/engines/blog/app/assets/stylesheets/glib/blog/admin.css +855 -0
  36. data/engines/blog/app/assets/stylesheets/glib/blog/themes/clean.css +816 -0
  37. data/engines/blog/app/assets/stylesheets/glib/blog/themes/editorial.css +631 -0
  38. data/engines/blog/app/controllers/concerns/glib/blog/admin/media_listing.rb +27 -0
  39. data/engines/blog/app/controllers/concerns/glib/blog/pagination.rb +14 -0
  40. data/engines/blog/app/controllers/glib/blog/admin/application_controller.rb +28 -0
  41. data/engines/blog/app/controllers/glib/blog/admin/authors_controller.rb +55 -0
  42. data/engines/blog/app/controllers/glib/blog/admin/categories_controller.rb +53 -0
  43. data/engines/blog/app/controllers/glib/blog/admin/comment_replies_controller.rb +34 -0
  44. data/engines/blog/app/controllers/glib/blog/admin/comments_controller.rb +50 -0
  45. data/engines/blog/app/controllers/glib/blog/admin/dashboards_controller.rb +34 -0
  46. data/engines/blog/app/controllers/glib/blog/admin/direct_uploads_controller.rb +30 -0
  47. data/engines/blog/app/controllers/glib/blog/admin/editor_attachments_controller.rb +16 -0
  48. data/engines/blog/app/controllers/glib/blog/admin/media_item_trashes_controller.rb +43 -0
  49. data/engines/blog/app/controllers/glib/blog/admin/media_items_controller.rb +61 -0
  50. data/engines/blog/app/controllers/glib/blog/admin/media_picks_controller.rb +19 -0
  51. data/engines/blog/app/controllers/glib/blog/admin/post_bulk_actions_controller.rb +33 -0
  52. data/engines/blog/app/controllers/glib/blog/admin/posts_controller.rb +79 -0
  53. data/engines/blog/app/controllers/glib/blog/admin/settings_controller.rb +27 -0
  54. data/engines/blog/app/controllers/glib/blog/admin/tags_controller.rb +46 -0
  55. data/engines/blog/app/controllers/glib/blog/application_controller.rb +90 -0
  56. data/engines/blog/app/controllers/glib/blog/authors_controller.rb +15 -0
  57. data/engines/blog/app/controllers/glib/blog/categories_controller.rb +15 -0
  58. data/engines/blog/app/controllers/glib/blog/comments_controller.rb +41 -0
  59. data/engines/blog/app/controllers/glib/blog/posts_controller.rb +47 -0
  60. data/engines/blog/app/controllers/glib/blog/tags_controller.rb +15 -0
  61. data/engines/blog/app/helpers/glib/blog/admin_helper.rb +57 -0
  62. data/engines/blog/app/helpers/glib/blog/comments_helper.rb +16 -0
  63. data/engines/blog/app/helpers/glib/blog/post_list_helper.rb +26 -0
  64. data/engines/blog/app/helpers/glib/blog/theme_helper.rb +85 -0
  65. data/engines/blog/app/jobs/glib/blog/purge_trash_job.rb +13 -0
  66. data/engines/blog/app/models/concerns/glib/blog/sluggable.rb +72 -0
  67. data/engines/blog/app/models/concerns/glib/blog/uses_media.rb +35 -0
  68. data/engines/blog/app/models/glib/blog/application_record.rb +7 -0
  69. data/engines/blog/app/models/glib/blog/author.rb +47 -0
  70. data/engines/blog/app/models/glib/blog/category.rb +53 -0
  71. data/engines/blog/app/models/glib/blog/comment.rb +66 -0
  72. data/engines/blog/app/models/glib/blog/comment_token.rb +30 -0
  73. data/engines/blog/app/models/glib/blog/media_item.rb +121 -0
  74. data/engines/blog/app/models/glib/blog/media_upload.rb +97 -0
  75. data/engines/blog/app/models/glib/blog/media_usage.rb +10 -0
  76. data/engines/blog/app/models/glib/blog/post.rb +110 -0
  77. data/engines/blog/app/models/glib/blog/post_view.rb +24 -0
  78. data/engines/blog/app/models/glib/blog/setting.rb +55 -0
  79. data/engines/blog/app/models/glib/blog/slug_redirect.rb +7 -0
  80. data/engines/blog/app/models/glib/blog/tag.rb +15 -0
  81. data/engines/blog/app/models/glib/blog/tagging.rb +10 -0
  82. data/engines/blog/app/policies/glib/blog/admin/author_policy.rb +11 -0
  83. data/engines/blog/app/policies/glib/blog/admin/category_policy.rb +11 -0
  84. data/engines/blog/app/policies/glib/blog/admin/comment_policy.rb +16 -0
  85. data/engines/blog/app/policies/glib/blog/admin/comment_reply_policy.rb +12 -0
  86. data/engines/blog/app/policies/glib/blog/admin/dashboard_policy.rb +11 -0
  87. data/engines/blog/app/policies/glib/blog/admin/direct_upload_policy.rb +11 -0
  88. data/engines/blog/app/policies/glib/blog/admin/editor_attachment_policy.rb +11 -0
  89. data/engines/blog/app/policies/glib/blog/admin/media_item_policy.rb +20 -0
  90. data/engines/blog/app/policies/glib/blog/admin/media_item_trash_policy.rb +11 -0
  91. data/engines/blog/app/policies/glib/blog/admin/media_pick_policy.rb +11 -0
  92. data/engines/blog/app/policies/glib/blog/admin/post_bulk_action_policy.rb +12 -0
  93. data/engines/blog/app/policies/glib/blog/admin/post_policy.rb +16 -0
  94. data/engines/blog/app/policies/glib/blog/admin/setting_policy.rb +11 -0
  95. data/engines/blog/app/policies/glib/blog/admin/tag_policy.rb +11 -0
  96. data/engines/blog/app/policies/glib/blog/application_policy.rb +16 -0
  97. data/engines/blog/app/policies/glib/blog/author_policy.rb +9 -0
  98. data/engines/blog/app/policies/glib/blog/category_policy.rb +9 -0
  99. data/engines/blog/app/policies/glib/blog/comment_policy.rb +11 -0
  100. data/engines/blog/app/policies/glib/blog/post_policy.rb +9 -0
  101. data/engines/blog/app/policies/glib/blog/tag_policy.rb +9 -0
  102. data/engines/blog/app/views/glib/blog/admin/_delete_button.json.jbuilder +24 -0
  103. data/engines/blog/app/views/glib/blog/admin/_nav.json.jbuilder +45 -0
  104. data/engines/blog/app/views/glib/blog/admin/_status_chip.json.jbuilder +2 -0
  105. data/engines/blog/app/views/glib/blog/admin/authors/_form.json.jbuilder +17 -0
  106. data/engines/blog/app/views/glib/blog/admin/authors/edit.json.jbuilder +7 -0
  107. data/engines/blog/app/views/glib/blog/admin/authors/index.json.jbuilder +50 -0
  108. data/engines/blog/app/views/glib/blog/admin/authors/new.json.jbuilder +11 -0
  109. data/engines/blog/app/views/glib/blog/admin/categories/_form.json.jbuilder +13 -0
  110. data/engines/blog/app/views/glib/blog/admin/categories/edit.json.jbuilder +7 -0
  111. data/engines/blog/app/views/glib/blog/admin/categories/index.json.jbuilder +68 -0
  112. data/engines/blog/app/views/glib/blog/admin/categories/new.json.jbuilder +7 -0
  113. data/engines/blog/app/views/glib/blog/admin/comments/_moderation_buttons.json.jbuilder +35 -0
  114. data/engines/blog/app/views/glib/blog/admin/comments/index.json.jbuilder +59 -0
  115. data/engines/blog/app/views/glib/blog/admin/dashboards/show.json.jbuilder +109 -0
  116. data/engines/blog/app/views/glib/blog/admin/media_item_trashes/index.json.jbuilder +32 -0
  117. data/engines/blog/app/views/glib/blog/admin/media_items/_rows.json.jbuilder +32 -0
  118. data/engines/blog/app/views/glib/blog/admin/media_items/edit.json.jbuilder +68 -0
  119. data/engines/blog/app/views/glib/blog/admin/media_items/index.json.jbuilder +68 -0
  120. data/engines/blog/app/views/glib/blog/admin/media_items/new.json.jbuilder +18 -0
  121. data/engines/blog/app/views/glib/blog/admin/media_picks/index.json.jbuilder +29 -0
  122. data/engines/blog/app/views/glib/blog/admin/posts/_form.json.jbuilder +104 -0
  123. data/engines/blog/app/views/glib/blog/admin/posts/edit.json.jbuilder +1 -0
  124. data/engines/blog/app/views/glib/blog/admin/posts/index.json.jbuilder +109 -0
  125. data/engines/blog/app/views/glib/blog/admin/posts/new.json.jbuilder +1 -0
  126. data/engines/blog/app/views/glib/blog/admin/settings/edit.json.jbuilder +55 -0
  127. data/engines/blog/app/views/glib/blog/admin/tags/_form.json.jbuilder +6 -0
  128. data/engines/blog/app/views/glib/blog/admin/tags/edit.json.jbuilder +7 -0
  129. data/engines/blog/app/views/glib/blog/admin/tags/index.json.jbuilder +30 -0
  130. data/engines/blog/app/views/glib/blog/admin/tags/new.json.jbuilder +7 -0
  131. data/engines/blog/app/views/glib/blog/authors/show.json.jbuilder +4 -0
  132. data/engines/blog/app/views/glib/blog/categories/show.json.jbuilder +4 -0
  133. data/engines/blog/app/views/glib/blog/comments/pending.json.jbuilder +7 -0
  134. data/engines/blog/app/views/glib/blog/media_items/_media_item.html.erb +14 -0
  135. data/engines/blog/app/views/glib/blog/posts/_comment.json.jbuilder +23 -0
  136. data/engines/blog/app/views/glib/blog/posts/_comment_form.json.jbuilder +14 -0
  137. data/engines/blog/app/views/glib/blog/posts/_comments.json.jbuilder +21 -0
  138. data/engines/blog/app/views/glib/blog/posts/index.json.jbuilder +4 -0
  139. data/engines/blog/app/views/glib/blog/posts/show.json.jbuilder +5 -0
  140. data/engines/blog/app/views/glib/blog/shared/errors.json.jbuilder +5 -0
  141. data/engines/blog/app/views/glib/blog/tags/show.json.jbuilder +3 -0
  142. data/engines/blog/app/views/glib/blog/themes/_nav_links.json.jbuilder +12 -0
  143. data/engines/blog/app/views/glib/blog/themes/clean/_brand.json.jbuilder +11 -0
  144. data/engines/blog/app/views/glib/blog/themes/clean/_card.json.jbuilder +15 -0
  145. data/engines/blog/app/views/glib/blog/themes/clean/_featured.json.jbuilder +23 -0
  146. data/engines/blog/app/views/glib/blog/themes/clean/_footer.json.jbuilder +15 -0
  147. data/engines/blog/app/views/glib/blog/themes/clean/_header.json.jbuilder +9 -0
  148. data/engines/blog/app/views/glib/blog/themes/clean/_post.json.jbuilder +98 -0
  149. data/engines/blog/app/views/glib/blog/themes/clean/_post_list.json.jbuilder +40 -0
  150. data/engines/blog/app/views/glib/blog/themes/clean/_post_rows.json.jbuilder +10 -0
  151. data/engines/blog/app/views/glib/blog/themes/editorial/_footer.json.jbuilder +15 -0
  152. data/engines/blog/app/views/glib/blog/themes/editorial/_header.json.jbuilder +11 -0
  153. data/engines/blog/app/views/glib/blog/themes/editorial/_post.json.jbuilder +62 -0
  154. data/engines/blog/app/views/glib/blog/themes/editorial/_post_list.json.jbuilder +25 -0
  155. data/engines/blog/app/views/glib/blog/themes/editorial/_post_rows.json.jbuilder +18 -0
  156. data/engines/blog/config/locales/en.yml +500 -0
  157. data/engines/blog/config/routes.rb +33 -0
  158. data/engines/blog/db/migrate/20261103090000_create_glib_blog_tables.rb +70 -0
  159. data/engines/blog/db/migrate/20261104090000_create_glib_blog_media_tables.rb +44 -0
  160. data/engines/blog/db/migrate/20261104120000_create_glib_blog_comments.rb +23 -0
  161. data/engines/blog/db/migrate/20261105090000_add_glib_blog_comment_moderation.rb +16 -0
  162. data/engines/blog/db/migrate/20261105100000_add_deleted_at_to_glib_blog_posts.rb +6 -0
  163. data/engines/blog/db/migrate/20261105110000_add_parent_and_default_to_glib_blog_categories.rb +10 -0
  164. data/engines/blog/db/migrate/20261105120000_create_glib_blog_post_views.rb +13 -0
  165. data/engines/blog/db/migrate/20261105130000_create_glib_blog_settings.rb +26 -0
  166. data/engines/blog/db/migrate/20261106090000_add_theme_to_glib_blog_settings.rb +7 -0
  167. data/engines/blog/lib/tasks/glib_blog_tasks.rake +32 -0
  168. data/lib/generators/glib/blog/install_generator.rb +44 -0
  169. data/lib/generators/glib/blog/templates/initializer.rb +22 -0
  170. data/lib/generators/glib/scheduled_jobs_generator.rb +30 -0
  171. data/lib/generators/templates/scheduled_jobs/create_glib_scheduled_job_runs.rb +22 -0
  172. data/lib/glib/blog/engine.rb +20 -0
  173. data/lib/glib/blog/theme.rb +35 -0
  174. data/lib/glib/blog.rb +50 -0
  175. data/lib/glib/engine.rb +17 -0
  176. data/lib/glib/scheduled_jobs/config.rb +36 -0
  177. data/lib/glib/scheduled_jobs/loader.rb +98 -0
  178. data/lib/glib-web.rb +2 -0
  179. metadata +177 -3
@@ -0,0 +1,55 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class AuthorsController < ApplicationController
5
+ def index
6
+ @authors = Author.ordered.includes(:user)
7
+ end
8
+
9
+ def new
10
+ @users = users_without_author
11
+ end
12
+
13
+ def create
14
+ @author.assign_attributes(author_params.merge(user_id: params.dig(:glib_blog_author, :user_id)))
15
+ save_and_redirect(@author, admin_authors_url)
16
+ end
17
+
18
+ def edit
19
+ end
20
+
21
+ def update
22
+ @author.assign_attributes(author_params)
23
+ save_and_redirect(@author, admin_authors_url)
24
+ end
25
+
26
+ def destroy
27
+ if @author.destroy
28
+ glib_redirect_to admin_authors_url
29
+ else
30
+ render_errors(@author.errors.full_messages)
31
+ end
32
+ end
33
+
34
+ private
35
+ def glib_load_resource
36
+ case action_name
37
+ when 'new', 'create'
38
+ @author = Author.new
39
+ when 'edit', 'update', 'destroy'
40
+ @author = Author.find(params[:id])
41
+ end
42
+ end
43
+
44
+ # The user is chosen once, when the author is created.
45
+ def author_params
46
+ params.require(:glib_blog_author).permit(:display_name, :slug, :role, :bio)
47
+ end
48
+
49
+ def users_without_author
50
+ Glib::Blog.user_class.constantize.where.not(id: Author.select(:user_id)).order(:id)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,53 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class CategoriesController < ApplicationController
5
+ # Tags share the page, as a compact list under the categories.
6
+ def index
7
+ @categories = Category.in_tree_order
8
+ @tags = Tag.ordered
9
+ end
10
+
11
+ def new
12
+ @parents = Category.top_level.ordered
13
+ end
14
+
15
+ def create
16
+ @category.assign_attributes(category_params)
17
+ save_and_redirect(@category, admin_categories_url)
18
+ end
19
+
20
+ def edit
21
+ @parents = Category.top_level.where.not(id: @category.id).ordered
22
+ end
23
+
24
+ def update
25
+ @category.assign_attributes(category_params)
26
+ save_and_redirect(@category, admin_categories_url)
27
+ end
28
+
29
+ def destroy
30
+ if @category.destroy
31
+ glib_redirect_to admin_categories_url
32
+ else
33
+ render_errors(@category.errors.full_messages)
34
+ end
35
+ end
36
+
37
+ private
38
+ def glib_load_resource
39
+ case action_name
40
+ when 'new', 'create'
41
+ @category = Category.new
42
+ when 'edit', 'update', 'destroy'
43
+ @category = Category.find(params[:id])
44
+ end
45
+ end
46
+
47
+ def category_params
48
+ params.require(:glib_blog_category).permit(:name, :slug, :description, :position, :parent_id, :default)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # Staff answering a reader's comment from the admin.
5
+ class CommentRepliesController < ApplicationController
6
+ def create
7
+ @comment_reply.body = params.dig(:comment, :body)
8
+ if @comment_reply.save
9
+ approve_thread
10
+ glib_redirect_to admin_comments_url
11
+ else
12
+ render_errors(@comment_reply.errors.full_messages)
13
+ end
14
+ end
15
+
16
+ private
17
+ # Replies go one level deep, so answering a reply joins its thread.
18
+ def glib_load_resource
19
+ @comment = Comment.includes(:post).find(params[:comment_id])
20
+ @comment_reply = @comment.post.comments.new(
21
+ parent: @comment.parent || @comment, author: current_blog_author, author_name: current_blog_author&.display_name,
22
+ status: :approved
23
+ )
24
+ end
25
+
26
+ # Answering a comment publishes it, and the thread it belongs to; otherwise the answer
27
+ # would hang under a comment readers can't see.
28
+ def approve_thread
29
+ [@comment_reply.parent, @comment].uniq.each { |comment| comment.update!(status: :approved) if comment.pending? }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,50 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class CommentsController < ApplicationController
5
+ PER_PAGE = 50
6
+
7
+ def index
8
+ unless params[:section_only]
9
+ @status_counts = current_blog_author.manageable_comments.group(:status).count
10
+ @status_counts['all'] = @status_counts.values.sum
11
+ end
12
+
13
+ scope = current_blog_author.manageable_comments.includes(:post).newest_first
14
+ scope = scope.where(status: params[:status]) if Comment.statuses.key?(params[:status].to_s)
15
+ if params[:q].present?
16
+ pattern = "%#{Comment.sanitize_sql_like(params[:q].downcase)}%"
17
+ scope = scope.where('LOWER(author_name) LIKE :pattern OR LOWER(body) LIKE :pattern', pattern: pattern)
18
+ end
19
+ @comments, @next_page = paginate(scope, per_page: PER_PAGE)
20
+ end
21
+
22
+ # Moderation moves a comment between statuses; its text stays the commenter's.
23
+ def update
24
+ status = params.require(:glib_blog_comment).require(:status)
25
+ return render_errors(t('.unknown_status')) unless Comment.statuses.key?(status)
26
+
27
+ @comment.update!(status: status)
28
+ glib_redirect_to return_url
29
+ end
30
+
31
+ def destroy
32
+ @comment.destroy!
33
+ glib_redirect_to return_url
34
+ end
35
+
36
+ private
37
+ # Back to the tab the moderator acted from, or the dashboard they acted from.
38
+ def return_url
39
+ return admin_dashboard_url if params[:return_to] == 'dashboard'
40
+
41
+ admin_comments_url(status: params[:return_status].presence)
42
+ end
43
+
44
+ def glib_load_resource
45
+ @comment = Comment.includes(:post).find(params[:id]) if %w[update destroy].include?(action_name)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,34 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # The admin's home: counts and recent activity, limited to what the author manages. The
5
+ # media library is shared, so its totals cover every file.
6
+ class DashboardsController < ApplicationController
7
+ RECENT = 5
8
+
9
+ def show
10
+ posts = current_blog_author.manageable_posts.kept
11
+ comments = current_blog_author.manageable_comments
12
+ month = (Time.current.beginning_of_month..)
13
+
14
+ @stats = {
15
+ live: posts.live.count,
16
+ live_this_month: posts.live.where(published_at: month).count,
17
+ drafts: posts.draft.count,
18
+ scheduled: posts.scheduled.count,
19
+ pending_comments: comments.pending.count,
20
+ approved_this_month: comments.approved.where(created_at: month).count
21
+ }
22
+ views = PostView.where(post_id: posts.select(:id))
23
+ today = Date.current
24
+ @views = views.where(date: (today - 29)..today).sum(:views)
25
+ @previous_views = views.where(date: (today - 59)..(today - 30)).sum(:views)
26
+
27
+ @recent_posts = posts.recently_updated.limit(RECENT)
28
+ @recent_post_views = PostView.where(post_id: @recent_posts.map(&:id)).group(:post_id).sum(:views)
29
+ @pending_comments = comments.pending.newest_first.includes(:post).limit(RECENT)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # Active Storage's direct upload endpoint (ActiveStorage::DirectUploadsController), limited
5
+ # to blog authors, so the blog doesn't depend on glib's Devise-only upload endpoint.
6
+ class DirectUploadsController < ApplicationController
7
+ include ::ActiveStorage::SetCurrent
8
+
9
+ def create
10
+ blob = ::ActiveStorage::Blob.create_before_direct_upload!(**blob_args)
11
+ render json: direct_upload_json(blob)
12
+ end
13
+
14
+ private
15
+ # `prefix` and `tagging` are sent by glib's uploader and only apply to glib's own endpoint.
16
+ def blob_args
17
+ params.require(:blob)
18
+ .permit(:filename, :byte_size, :checksum, :content_type, :prefix, :tagging, metadata: {})
19
+ .to_h.symbolize_keys.slice(:filename, :byte_size, :checksum, :content_type, :metadata)
20
+ end
21
+
22
+ def direct_upload_json(blob)
23
+ blob.as_json(root: false, methods: :signed_id).merge(
24
+ direct_upload: { url: blob.service_url_for_direct_upload, headers: blob.service_headers_for_direct_upload }
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # Turns a file uploaded in the post editor into a media library item and answers with the
5
+ # attributes `fields_actionText` gives the Trix attachment.
6
+ class EditorAttachmentsController < ApplicationController
7
+ def create
8
+ media_item = MediaUpload.create!(signed_id: params.require(:signed_id), uploaded_by: current_blog_author)
9
+ render json: media_item.trix_attachment_attributes
10
+ rescue MediaUpload::Rejected => e
11
+ render json: { message: e.message }, status: :unprocessable_entity
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # The media trash: `create` trashes files, `destroy` restores one. Authors act on their own
5
+ # uploads; editors and admins on any file.
6
+ class MediaItemTrashesController < ApplicationController
7
+ def index
8
+ @media_items = current_blog_author.manageable_media_items.trashed.order(deleted_at: :desc).with_attached_file
9
+ end
10
+
11
+ def create
12
+ in_use = []
13
+ current_blog_author.manageable_media_items.kept.where(id: selected_ids).find_each do |media_item|
14
+ media_item.trash!
15
+ rescue MediaItem::InUse
16
+ in_use << media_item.title_or_filename
17
+ end
18
+
19
+ if in_use.empty?
20
+ glib_redirect_to admin_media_items_url
21
+ else
22
+ render_errors(t('.in_use', names: in_use.to_sentence), reload_url: admin_media_items_url)
23
+ end
24
+ end
25
+
26
+ def destroy
27
+ current_blog_author.manageable_media_items.trashed.find(params[:id]).restore!
28
+ glib_redirect_to admin_media_item_trashes_url
29
+ end
30
+
31
+ private
32
+ # One file from its edit page (`id`), or the files checked in the library grid
33
+ # (`media_items[<id>][checked]`).
34
+ def selected_ids
35
+ return [params[:id]] if params[:id].present?
36
+
37
+ checked = params.fetch(:media_items, {}).permit!.to_h.select { |_id, row| row['checked'] == 'true' }
38
+ checked.keys
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class MediaItemsController < ApplicationController
5
+ include MediaListing
6
+
7
+ def index
8
+ @media_items, @next_page = paginate_media(filtered_media(MediaItem.kept))
9
+ return if params[:section_only]
10
+
11
+ @months = media_upload_months
12
+ @library_count, @library_bytes = MediaItem.kept.pick(Arel.sql('COUNT(*)'), Arel.sql('COALESCE(SUM(byte_size), 0)'))
13
+ end
14
+
15
+ def new
16
+ end
17
+
18
+ def create
19
+ errors = Array(params[:signed_ids]).compact_blank.filter_map do |signed_id|
20
+ MediaUpload.create!(signed_id: signed_id, uploaded_by: current_blog_author)
21
+ nil
22
+ rescue MediaUpload::Rejected => e
23
+ e.message
24
+ end
25
+
26
+ if errors.empty?
27
+ glib_redirect_to admin_media_items_url
28
+ else
29
+ render_errors(errors, reload_url: admin_media_items_url)
30
+ end
31
+ end
32
+
33
+ def edit
34
+ @usages = @media_item.media_usages.includes(:record)
35
+ end
36
+
37
+ def update
38
+ @media_item.assign_attributes(media_item_params)
39
+ save_and_redirect(@media_item, admin_media_items_url)
40
+ end
41
+
42
+ # Permanent deletion is only for files already in the trash.
43
+ def destroy
44
+ return render_errors(t('.not_trashed')) unless @media_item.trashed?
45
+
46
+ @media_item.destroy!
47
+ glib_redirect_to admin_media_item_trashes_url
48
+ end
49
+
50
+ private
51
+ def glib_load_resource
52
+ @media_item = MediaItem.find(params[:id]) if %w[edit update destroy].include?(action_name)
53
+ end
54
+
55
+ def media_item_params
56
+ params.require(:glib_blog_media_item).permit(:title, :alt_text, :caption, :credit, :focal_x, :focal_y)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # Dialog content for choosing a library file: `mode=body` inserts it into the editor named
5
+ # by `target`; `mode=cover` and `mode=logo` set the image field named by `target`.
6
+ class MediaPicksController < ApplicationController
7
+ include MediaListing
8
+
9
+ def index
10
+ @mode = %w[cover logo].include?(params[:mode]) ? params[:mode].to_sym : :body
11
+ @target = params.require(:target)
12
+ scope = MediaItem.kept
13
+ scope = scope.where(kind: :image) unless @mode == :body
14
+ @media_items, @next_page = paginate_media(filtered_media(scope))
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ # Acts on the posts checked in the post list, or on one post from its row or editor. Authors
5
+ # act on their own posts; editors and admins on any.
6
+ class PostBulkActionsController < ApplicationController
7
+ ACTIONS = {
8
+ 'publish' => ->(post) { post.update(status: :published) },
9
+ 'draft' => ->(post) { post.update(status: :draft) },
10
+ 'trash' => ->(post) { post.trash },
11
+ 'restore' => ->(post) { post.restore }
12
+ }.freeze
13
+
14
+ def create
15
+ perform = ACTIONS[params[:bulk_action]]
16
+ return render_errors(t('.unknown')) unless perform
17
+
18
+ failed = []
19
+ current_blog_author.manageable_posts.where(id: Array(params[:post_ids]).compact_blank).find_each do |post|
20
+ failed << post.title unless perform.call(post)
21
+ end
22
+
23
+ return_url = admin_posts_url(status: params[:return_status].presence)
24
+ if failed.empty?
25
+ glib_redirect_to return_url
26
+ else
27
+ render_errors(t('.failed', titles: failed.to_sentence), reload_url: return_url)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,79 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class PostsController < ApplicationController
5
+ PER_PAGE = 50
6
+
7
+ # The status tabs, each with the posts it lists. "Published" means live, so a scheduled post
8
+ # appears under one tab only, and a trashed post only under Trash.
9
+ STATUS_FILTERS = {
10
+ 'published' => ->(posts) { posts.live },
11
+ 'draft' => ->(posts) { posts.kept.draft },
12
+ 'scheduled' => ->(posts) { posts.kept.scheduled },
13
+ 'archived' => ->(posts) { posts.kept.archived },
14
+ 'trash' => ->(posts) { posts.trashed }
15
+ }.freeze
16
+
17
+ def index
18
+ posts = current_blog_author.manageable_posts
19
+ unless params[:section_only]
20
+ @status_counts = STATUS_FILTERS.transform_values { |filter| filter.call(posts).count }
21
+ @status_counts['all'] = posts.kept.count
22
+ end
23
+
24
+ @status = params[:status] if STATUS_FILTERS.key?(params[:status])
25
+ posts = @status ? STATUS_FILTERS[@status].call(posts) : posts.kept
26
+ if params[:q].present?
27
+ posts = posts.where('LOWER(title) LIKE ?', "%#{Post.sanitize_sql_like(params[:q].downcase)}%")
28
+ end
29
+ @posts, @next_page = paginate(posts.recently_updated.includes(:author, :category), per_page: PER_PAGE)
30
+ end
31
+
32
+ def new
33
+ @categories = Category.in_tree_order
34
+ end
35
+
36
+ def create
37
+ @post.assign_attributes(post_params)
38
+ save_and_redirect(@post, admin_posts_url)
39
+ end
40
+
41
+ def edit
42
+ @categories = Category.in_tree_order
43
+ end
44
+
45
+ def update
46
+ @post.assign_attributes(post_params)
47
+ save_and_redirect(@post, admin_posts_url)
48
+ rescue ActiveRecord::StaleObjectError
49
+ render_errors(t('.stale'))
50
+ end
51
+
52
+ # Permanent deletion is only for posts already in the trash.
53
+ def destroy
54
+ return render_errors(t('.not_trashed')) unless @post.trashed?
55
+
56
+ @post.destroy!
57
+ glib_redirect_to admin_posts_url(status: 'trash')
58
+ end
59
+
60
+ private
61
+ def glib_load_resource
62
+ case action_name
63
+ when 'new', 'create'
64
+ @post = Post.new(author: current_blog_author, comments_enabled: Setting.current.comments_enabled_by_default)
65
+ when 'edit', 'update', 'destroy'
66
+ @post = Post.includes(cover_media_item: { file_attachment: :blob }).find(params[:id])
67
+ end
68
+ end
69
+
70
+ def post_params
71
+ params.require(:glib_blog_post).permit(
72
+ :title, :slug, :excerpt, :body, :category_id, :tag_list, :status, :published_at, :featured,
73
+ :meta_title, :meta_description, :canonical_url, :noindex, :lock_version, :cover_media_item_id, :comments_enabled
74
+ )
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,27 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class SettingsController < ApplicationController
5
+ def edit
6
+ end
7
+
8
+ def update
9
+ @setting.assign_attributes(setting_params)
10
+ save_and_redirect(@setting, edit_admin_settings_url)
11
+ end
12
+
13
+ private
14
+ def glib_load_resource
15
+ @setting = Setting.current
16
+ end
17
+
18
+ def setting_params
19
+ params.require(:glib_blog_setting).permit(
20
+ :title, :tagline, :logo_media_item_id, :theme, :posts_per_page, :noindex,
21
+ :comments_enabled_by_default, :comments_require_approval, :comments_close_after_days
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ module Glib
2
+ module Blog
3
+ module Admin
4
+ class TagsController < ApplicationController
5
+ def index
6
+ @tags = Tag.ordered
7
+ end
8
+
9
+ def new
10
+ end
11
+
12
+ def create
13
+ @tag.assign_attributes(tag_params)
14
+ save_and_redirect(@tag, admin_tags_url)
15
+ end
16
+
17
+ def edit
18
+ end
19
+
20
+ def update
21
+ @tag.assign_attributes(tag_params)
22
+ save_and_redirect(@tag, admin_tags_url)
23
+ end
24
+
25
+ def destroy
26
+ @tag.destroy!
27
+ glib_redirect_to admin_tags_url
28
+ end
29
+
30
+ private
31
+ def glib_load_resource
32
+ case action_name
33
+ when 'new', 'create'
34
+ @tag = Tag.new
35
+ when 'edit', 'update', 'destroy'
36
+ @tag = Tag.find(params[:id])
37
+ end
38
+ end
39
+
40
+ def tag_params
41
+ params.require(:glib_blog_tag).permit(:name, :slug)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,90 @@
1
+ module Glib
2
+ module Blog
3
+ class ApplicationController < Glib::Blog.parent_controller.constantize
4
+ include Glib::Json::Libs unless self < Glib::Json::Libs
5
+ # `json_libs_init` registers block callbacks, which Rails never de-duplicates, so a
6
+ # parent that already initialized JSON UI must not be initialized twice.
7
+ json_libs_init(renderer_path: 'layouts/json_ui/no_custom') unless __json_libs_init_options
8
+
9
+ include Glib::Auth::Policy unless self < Glib::Auth::Policy
10
+ glib_auth_init
11
+
12
+ # Inheriting the host's controller means Rails never adds this engine's helpers on its own.
13
+ helper Glib::Blog::Engine.helpers
14
+ helper_method :current_blog_author, :blog_theme
15
+
16
+ include Pagination
17
+
18
+ # The blog's settings can hide every page from search engines; a post can also hide itself.
19
+ before_action { seo_head_tags if Setting.current.noindex? }
20
+
21
+ rescue_from Glib::Auth::Policy::UnauthorizedError do
22
+ sign_in_url = instance_exec(&Glib::Blog.sign_in_url) unless current_user
23
+
24
+ # A browser page load has already been switched to JSON for the Vue renderer, and the
25
+ # HTML wrapping runs after the action, which a refused request never reaches; the
26
+ # glib_handle_4xx dialogs would arrive as raw JSON. Redirect the browser instead.
27
+ if __json_ui_rendering?
28
+ json_ui_save_return_url if sign_in_url && request.get?
29
+ redirect_to glib_html_url(sign_in_url || root_url)
30
+ elsif sign_in_url
31
+ glib_handle_401(sign_in_url)
32
+ else
33
+ glib_handle_403(root_url)
34
+ end
35
+ end
36
+
37
+ def current_blog_author
38
+ return @current_blog_author if defined?(@current_blog_author)
39
+
40
+ @current_blog_author = Author.for(current_user)
41
+ end
42
+
43
+ # The public blog's look (a Glib::Blog::Theme), chosen in the settings.
44
+ def blog_theme
45
+ @blog_theme ||= Setting.current.public_theme
46
+ end
47
+
48
+ private
49
+ # Controllers whose policy needs a record load it here; the rest need nothing.
50
+ def glib_load_resource
51
+ end
52
+
53
+ # Policies live beside their controllers, e.g. Glib::Blog::Admin::PostsController is
54
+ # authorized by Glib::Blog::Admin::PostPolicy. The default lookup would resolve the
55
+ # host's own `::Post` instead.
56
+ def glib_authorize_resource
57
+ super(class: controller_path.singularize.to_sym)
58
+ end
59
+
60
+ # `reload_url` reloads the page once the dialog closes, for requests that changed some
61
+ # records before failing on others.
62
+ def render_errors(messages, reload_url: nil)
63
+ @errors = Array.wrap(messages)
64
+ @reload_url = reload_url
65
+ render 'glib/blog/shared/errors'
66
+ end
67
+
68
+ # Public post lists, a page at a time; the next page loads as the reader scrolls.
69
+ def paginate_posts(scope)
70
+ @posts, @next_page = paginate(scope, per_page: Setting.current.posts_per_page)
71
+ end
72
+
73
+ # Old links (a slug the record no longer uses) move permanently to the current URL.
74
+ def redirect_to_current_slug(record)
75
+ return if record.slug == params[:id]
76
+
77
+ redirect_to url_for(id: record.slug, format: params[:format]), status: :moved_permanently
78
+ end
79
+
80
+ # Search engines read these from the HTML page; the JSON UI client never sees them.
81
+ def seo_head_tags(description: nil, canonical_url: nil, noindex: false)
82
+ tags = []
83
+ tags << helpers.tag.meta(name: 'description', content: description) if description
84
+ tags << helpers.tag.link(rel: 'canonical', href: canonical_url) if canonical_url.present?
85
+ tags << helpers.tag.meta(name: 'robots', content: 'noindex') if noindex || Setting.current.noindex?
86
+ @__glib_head_code = helpers.safe_join(tags, "\n")
87
+ end
88
+ end
89
+ end
90
+ end