Skip to content

Commit 4f4ab39

Browse files
committed
WIP: category list and editing translatable fields
1 parent 611d45e commit 4f4ab39

File tree

11 files changed

+85
-29
lines changed

11 files changed

+85
-29
lines changed

app/concerns/better_together/translatable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def self.localized_attribute_list
2929

3030
localized_attributes.flatten
3131
end
32+
33+
def self.extra_permitted_attributes
34+
super + localized_attribute_list
35+
end
3236
end
3337
end
3438
end

app/controllers/better_together/categories_controller.rb

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
module BetterTogether
2-
class CategoriesController < ApplicationController
3-
before_action :set_category, only: %i[ show edit update destroy ]
2+
class CategoriesController < FriendlyResourceController
3+
before_action :set_model_instance, only: %i[show edit update destroy]
4+
before_action :authorize_category, only: %i[show edit update destroy]
5+
after_action :verify_authorized, except: :index
46

57
# GET /categories
68
def index
7-
@categories = Category.all
9+
authorize resource_class
10+
@categories = resource_class.all
811
end
912

1013
# GET /categories/1
@@ -13,7 +16,8 @@ def show
1316

1417
# GET /categories/new
1518
def new
16-
@category = Category.new
19+
@category = resource_class.new
20+
authorize_category
1721
end
1822

1923
# GET /categories/1/edit
@@ -22,7 +26,8 @@ def edit
2226

2327
# POST /categories
2428
def create
25-
@category = Category.new(category_params)
29+
@category = resource_class.new(category_params)
30+
authorize_category
2631

2732
if @category.save
2833
redirect_to @category, notice: "Category was successfully created."
@@ -46,15 +51,32 @@ def destroy
4651
redirect_to categories_url, notice: "Category was successfully destroyed.", status: :see_other
4752
end
4853

49-
private
50-
# Use callbacks to share common setup or constraints between actions.
51-
def set_category
52-
@category = Category.find(params[:id])
53-
end
54+
protected
5455

55-
# Only allow a list of trusted parameters through.
56-
def category_params
57-
params.fetch(:category, {})
58-
end
56+
# Adds a policy check for the category
57+
def authorize_category
58+
authorize @category
59+
end
60+
61+
def set_model_instance
62+
@category = set_resource_instance
63+
end
64+
65+
# Only allow a list of trusted parameters through.
66+
def category_params
67+
permitted = [
68+
*resource_class.extra_permitted_attributes
69+
]
70+
71+
params.require(resource_class.name.demodulize.underscore.to_sym).permit(permitted)
72+
end
73+
74+
def resource_class
75+
::BetterTogether::Category
76+
end
77+
78+
def resource_collection
79+
resource_class.with_translations
80+
end
5981
end
6082
end

app/mailers/better_together/application_mailer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module BetterTogether
55
class ApplicationMailer < ActionMailer::Base
66
default from: ENV.fetch(
77
'DEFAULT_FROM_EMAIL',
8-
8+
'Better Together Community <[email protected]>'
99
)
1010
layout 'better_together/mailer'
1111

app/models/better_together/category.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Category < ApplicationRecord
99
has_many :pages, through: :categorizations, source: :categorizable, source_type: 'BetterTogether::Page'
1010

1111
translates :name, type: :string
12-
translates :description, type: :text
12+
translates :description, backend: :action_text
1313

1414
validates :name, presence: true
1515
validates :type, presence: true

app/policies/better_together/category_policy.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@
22
module BetterTogether
33
class CategoryPolicy < ApplicationPolicy
44

5+
def index?
6+
permitted_to?('manage_platform')
7+
end
8+
9+
def create?
10+
permitted_to?('manage_platform')
11+
end
12+
13+
def update?
14+
permitted_to?('manage_platform')
15+
end
16+
17+
def show?
18+
permitted_to?('manage_platform')
19+
end
20+
521
class Scope < ApplicationPolicy::Scope
6-
22+
723
end
824
end
925
end

app/policies/better_together/conversation_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module BetterTogether
33
class ConversationPolicy < ApplicationPolicy
44

55
class Scope < ApplicationPolicy::Scope
6-
6+
77
end
88
end
99
end
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
<div id="<%= dom_id category %>">
1+
<div id="<%= dom_id category %>" class="col mb-3">
2+
<div class="card h-100">
3+
<div class="card-body">
4+
<h3 class="card-title"><%= link_to category, category, class: "text-decoration-none" %></h3>
5+
</div>
6+
</div>
27
</div>

app/views/better_together/categories/_form.html.erb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= form_with(model: category) do |form| %>
1+
<%= form_with(model: category, class: 'form') do |form| %>
22
<% if category.errors.any? %>
33
<div style="color: red">
44
<h2><%= pluralize(category.errors.count, "error") %> prohibited this category from being saved:</h2>
@@ -11,6 +11,18 @@
1111
</div>
1212
<% end %>
1313

14+
<%= form.text_field :identifier, class: 'form-control' %>
15+
16+
<div class="mb-3">
17+
<%= render partial: 'better_together/shared/translated_string_field', locals: { model: category, form: form, attribute: 'name' } %>
18+
</div>
19+
20+
<div class="mb-3">
21+
<%= render partial: 'better_together/shared/translated_rich_text_field', locals: { model: category, form: form, attribute: 'description' } %>
22+
</div>
23+
<%= form.text_field :icon, class: 'form-control' %>
24+
<%= form.number_field :position, class: 'form-control' %>
25+
1426
<div>
1527
<%= form.submit %>
1628
</div>

app/views/better_together/categories/index.html.erb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22

33
<h1>Categories</h1>
44

5-
<div id="categories">
6-
<% @categories.each do |category| %>
7-
<%= render category %>
8-
<p>
9-
<%= link_to "Show this category", category %>
10-
</p>
11-
<% end %>
5+
<div id="categories" class="row row-cols-1 row-cols-sm-2 row-cols-lg-3 row-cols-xl-4">
6+
<%= render partial: 'better_together/categories/category', collection: @categories %>
127
</div>
138

14-
<%= link_to "New category", new_category_path %>
9+
<%= link_to "New topic", new_topic_path %>

app/views/better_together/categories/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p style="color: green"><%= notice %></p>
22

3-
<%= render @category %>
3+
<%= render partial: 'better_together/categories/category', object: @category %>
44

55
<div>
66
<%= link_to "Edit this category", edit_category_path(@category) %> |

0 commit comments

Comments
 (0)