Skip to content

Commit 64c4db3

Browse files
committed
Add ability to CRUD Blocks independently of pages
This will allow platform managers to survey total list of blocks and their contents as well as create unattached blocks for use as dynamic content partials to be placed outside the contenxt of a BetterTogether::Page. Prevent deletion of blocks that are attached to pages. They are managed from the page editor form.
1 parent 542f9b3 commit 64c4db3

File tree

25 files changed

+253
-13
lines changed

25 files changed

+253
-13
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module BetterTogether
2+
module Content
3+
class BlocksController < ApplicationController
4+
before_action :authenticate_user!
5+
before_action :set_block, only: %i[show edit update destroy]
6+
before_action only: %i[index], if: -> { Rails.env.development? } do
7+
# Make sure that all BLock subclasses are loaded in dev to generate new block buttons
8+
BetterTogether::Content::Block.load_all_subclasses
9+
end
10+
11+
def index
12+
@blocks = BetterTogether::Content::Block.includes(:pages).all
13+
end
14+
15+
def create
16+
@block = BetterTogether::Content::Block.new(block_params)
17+
18+
if @block.save
19+
redirect_to content_block_path(@block), notice: 'Block was successfully created.'
20+
else
21+
render :new
22+
end
23+
end
24+
25+
def update
26+
respond_to do |format|
27+
if @block.update(block_params)
28+
redirect_to edit_content_block_path(@block), notice: 'Block was successfully updated.'
29+
else
30+
format.turbo_stream do
31+
render turbo_stream: turbo_stream.replace(helpers.dom_id(@block, 'form'), partial: 'form', locals: { block: @block } )
32+
end
33+
end
34+
end
35+
end
36+
37+
def new
38+
@block = BetterTogether::Content::Block.new(type: params[:block_type])
39+
40+
respond_to do |format|
41+
format.html
42+
end
43+
end
44+
45+
def destroy
46+
@block.destroy unless @block.pages.any?
47+
48+
redirect_to content_blocks_path, notice: 'Block was sucessfully deleted'
49+
end
50+
51+
private
52+
53+
def block_params
54+
params.require(:block).permit(
55+
:type, :media, :identifier,
56+
*BetterTogether::Content::Block.localized_block_attributes,
57+
*BetterTogether::Content::Block.storext_definitions.keys
58+
)
59+
end
60+
61+
def set_block
62+
@block = BetterTogether::Content::Block.find(params[:id])
63+
end
64+
end
65+
end
66+
end

app/controllers/better_together/content/page_blocks_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ class PageBlocksController < ApplicationController
44
before_action :authenticate_user!
55
before_action :set_page
66

7-
def index
8-
@blocks = @page.page_blocks.includes(:block)
9-
end
10-
117
def new
128
@page_block = @page.page_blocks.build
139

app/controllers/better_together/host_dashboard_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def index # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
1212
@resource_permissions = ResourcePermission.order(created_at: :desc).limit(3)
1313
@users = User.order(created_at: :desc).limit(3)
1414

15+
@content_blocks = Content::Block.order(created_at: :desc).limit(3)
16+
1517
@geography_continents = Geography::Continent.order(created_at: :desc).limit(3)
1618
@geography_countries = Geography::Country.order(created_at: :desc).limit(3)
1719
@geography_states = Geography::State.order(created_at: :desc).limit(3)
@@ -27,6 +29,8 @@ def index # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
2729
@resource_permission_count = ResourcePermission.count
2830
@user_count = User.count
2931

32+
@content_block_count = Content::Block.count
33+
3034
@geography_continent_count = Geography::Continent.count
3135
@geography_country_count = Geography::Country.count
3236
@geography_state_count = Geography::State.count

app/helpers/better_together/content/blocks_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Content
33
module BlocksHelper
44
# Returns an array of acceptable image file types
55
def acceptable_image_file_types
6-
%w[image/jpeg image/png image/gif image/webp image/svg+xml]
6+
BetterTogether::Content::Image::CONTENT_TYPES
77
end
88

99
# Helper to generate a unique temp_id for a model

app/models/better_together/content/block.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def self.localized_block_attributes
8282
def block_name
8383
self.class.block_name
8484
end
85+
86+
def to_s
87+
"#{block_name} - #{persisted? ? (identifier.present? ? identifier : id.split('-').first) : 'new'}"
88+
end
89+
8590
end
8691
end
8792
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% if block.pages.any? %>
2+
<div class="my-3">
3+
<h5>Associated Pages</h5>
4+
<%= block.pages.map { |page| link_to page }.join(', ').html_safe %>
5+
</div>
6+
<% else %>
7+
<p>No associated pages for this block.</p>
8+
<% end %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%# app/views/better_together/content/blocks/_block_row.html.erb %>
2+
3+
<tr>
4+
<td><%= block %></td> <!-- Adjust to your actual title attribute if different -->
5+
<td><%= block.class.name.demodulize %></td> <!-- Display block type without the namespace -->
6+
<td><%= block.pages.map { |page| link_to page }.join(', ').html_safe %></td> <!-- Display block type without the namespace -->
7+
<td>
8+
<%= link_to 'Show', content_block_path(block), class: 'btn btn-sm btn-info' %>
9+
<%= link_to 'Edit', edit_content_block_path(block), class: 'btn btn-sm btn-warning' %>
10+
<%= link_to 'Delete', content_block_path(block), method: :delete, data: { confirm: 'Are you sure?', turbo_confirm: 'Are you sure?', turbo_method: :delete }, class: 'btn btn-sm btn-danger' unless block.pages.any? %>
11+
</td>
12+
</tr>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%= form_with(model: block, url: block.persisted? ? content_block_path(block) : content_blocks_path, local: true, multipart: true, id: dom_id(block, 'form'), class: 'form', data: { controller: "form-validation" }) do |form| %>
2+
<% if block.errors.any? %>
3+
<div class="alert alert-danger">
4+
<h4>Please correct the following errors:</h4>
5+
<ul>
6+
<% block.errors.full_messages.each do |msg| %>
7+
<li><%= msg %></li>
8+
<% end %>
9+
</ul>
10+
</div>
11+
<% end %>
12+
13+
<!-- Add other block-specific fields here -->
14+
<div class="mb-3">
15+
<%= render partial: 'better_together/content/blocks/fields/block',
16+
locals: { block: } %>
17+
</div>
18+
19+
<div class="mb-3 text-end">
20+
<%= form.submit 'Save Changes', class: 'btn btn-primary' %>
21+
</div>
22+
<% end %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="container mt-3">
2+
<h1>Edit Block: <%= @block %></h1>
3+
4+
<%= render partial: 'associated_pages', locals: { block: @block } %>
5+
6+
<%= render partial: 'form', locals: { block: @block } %>
7+
8+
<div class="mt-3">
9+
<%= link_to 'Back to Block List', content_blocks_path, class: 'btn btn-secondary me-2' %>
10+
<%= link_to 'View Block', content_block_path(@block), class: 'btn btn-info' %>
11+
</div>
12+
</div>

app/views/better_together/content/blocks/fields/_block.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- scope = local_assigns[:scope] ? local_assigns[:scope] : block.block_name %>
1+
<%- scope = local_assigns[:scope] ? local_assigns[:scope] : BetterTogether::Content::Block.block_name %>
22
<%- temp_id = block.persisted? ? block.id : SecureRandom.uuid %>
33

44
<!-- Nested Block fields -->

0 commit comments

Comments
 (0)