Skip to content

Commit 75df2a0

Browse files
committed
Make entry templates editable
Before this change, only one entry template for paged entry type was editable through the account form, and a scrolled entry template would be initialized on creation of the first scrolled entry, but not persisted to the database. We add an entry template admin for new/create and edit/update actions. It is accessible only from an entry template tab on account show page of accounts that have already been created. We need to customize breadcrumbs and redirects, since there isn't an index page or a show page for entry templates. To make it clear to the user what they are currently editing, we customize the form's page title, as well. The account now has a list of existing and potential entry templates that is used to populate the entry templates tab on its show page. This'll probably change at some point when we allow multiple entry templates per account/entry type. Entry template provides translated entry type name as a virtual attribute for use in several places in the admin. Even though account publishers cannot access the account show page, they would be able to edit entry templates using a link, if they have it. The account form hints where the form fields have gone. There isn't a link to the entry templates tab, because for account create, this link wouldn't work yet. For now, the entry templates form hard-codes the fact that currently available config and widget inputs are for the paged entry type. When we make the analytics widget available for scrolled entry type, this is likely to change. Entry templates tab contains entries for all existing entry templates and all those that could be created according to enabled entry types. If an entry type is disabled, its corresponding entry template is not displayed here anymore. `config_for` can now deal with an entry template argument, which is used in the entry templates form to retrieve an account config for the entry template's account. REDMINE-17306
1 parent f217566 commit 75df2a0

File tree

21 files changed

+770
-395
lines changed

21 files changed

+770
-395
lines changed

admins/pageflow/accounts.rb

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ module Pageflow
3030
membership_role_with_tooltip(own_role, scope: 'own_account_role')
3131
end
3232
end
33-
column :default_theming do |account|
34-
account.first_paged_entry_template.theme_name if authorized?(:read, account)
35-
end
3633
end
3734

3835
csv do
@@ -63,7 +60,6 @@ module Pageflow
6360
show :title => :name do |account|
6461
render 'account_details', :account => account
6562
render 'theming_details', :account => account
66-
render 'entry_template_details', account: account
6763

6864
tabs_view(Pageflow.config.admin_resource_tabs.find_by_resource(account.default_theming),
6965
i18n: 'pageflow.admin.resource_tabs',
@@ -76,76 +72,25 @@ module Pageflow
7672
helper Pageflow::Admin::FormHelper
7773
helper Pageflow::Admin::LocalesHelper
7874
helper Pageflow::Admin::MembershipsHelper
79-
helper Pageflow::Admin::WidgetsHelper
8075
helper ThemesHelper
8176

8277
def new
8378
@account = Account.new
8479
@account.build_default_theming
85-
@entry_template = @account.entry_templates.build(
86-
default_locale: current_user.locale,
87-
share_providers: Pageflow.config.default_share_providers,
88-
entry_type: 'paged'
89-
)
9080
end
9181

9282
def create
93-
account_params = (permitted_params[:account] || {})
94-
.except(:paged_entry_template_attributes)
83+
account_params = permitted_params[:account] || {}
9584
@account = Account.new(account_params)
9685
@account.build_default_theming(permitted_params.fetch(:account, {})[
9786
:default_theming_attributes])
98-
@entry_template = @account.entry_templates.build({entry_type: 'paged'}
99-
.merge(entry_template_params))
100-
101-
super
102-
update_widgets('paged')
103-
end
104-
105-
def edit
106-
@entry_template = resource.first_paged_entry_template
10787
super
10888
end
10989

11090
def update
111-
@entry_template = resource.entry_templates.find_or_initialize_by(
112-
entry_type: 'paged'
113-
)
114-
@entry_template.assign_attributes(entry_template_params)
115-
@entry_template.save
11691
update! do |success, failure|
11792
success.html { redirect_to(admin_account_path(resource, params.permit(:tab))) }
11893
end
119-
update_widgets('paged')
120-
end
121-
122-
def entry_template_params
123-
current_params = permitted_params_with_entry_template_attributes.fetch(:account, {})[
124-
:paged_entry_template_attributes]&.to_hash
125-
if current_params
126-
config = true_false_strings_to_booleans_or_numbers(
127-
current_params['configuration']
128-
)
129-
share_providers = true_false_strings_to_booleans_or_numbers(
130-
current_params['share_providers']
131-
)
132-
current_params.merge('configuration' => config, 'share_providers' => share_providers)
133-
else
134-
{}
135-
end
136-
end
137-
138-
def update_widgets(entry_type)
139-
if @account.valid?
140-
EntryTemplate.find_by(account_id: @account.id, entry_type: entry_type)
141-
&.widgets&.batch_update!(widgets_params)
142-
end
143-
end
144-
145-
def widgets_params
146-
(params[:widgets].try(:permit!).to_h || {}).map do |role, type_name|
147-
{role: role, type_name: type_name}
148-
end
14994
end
15095

15196
def permitted_params
@@ -154,12 +99,6 @@ def permitted_params
15499
with_permitted_feature_states(result)
155100
end
156101

157-
def permitted_params_with_entry_template_attributes
158-
result = params.permit(account: permitted_account_attributes_plus_entry_template)
159-
160-
with_permitted_feature_states(result)
161-
end
162-
163102
def scoped_collection
164103
super.includes(:default_theming)
165104
end
@@ -172,25 +111,6 @@ def with_permitted_feature_states(result)
172111
result
173112
end
174113

175-
def true_false_strings_to_booleans_or_numbers(hash)
176-
hash&.map { |k, v| [k, to_boolean_or_number(v)] }&.to_h
177-
end
178-
179-
def to_boolean_or_number(value)
180-
case value
181-
when 'false'
182-
false
183-
when 'true'
184-
true
185-
when '0'
186-
0
187-
when '1'
188-
1
189-
else
190-
value
191-
end
192-
end
193-
194114
def permitted_account_attributes
195115
[
196116
:name,
@@ -200,11 +120,6 @@ def permitted_account_attributes
200120
permitted_attributes_for(:account)
201121
end
202122

203-
def permitted_account_attributes_plus_entry_template
204-
permitted_account_attributes +
205-
[paged_entry_template_attributes: permitted_entry_template_attributes]
206-
end
207-
208123
def permitted_theming_attributes
209124
[
210125
:cname,
@@ -219,18 +134,6 @@ def permitted_theming_attributes
219134
permitted_attributes_for(:theming)
220135
end
221136

222-
def permitted_entry_template_attributes
223-
[
224-
:theme_name,
225-
:default_author,
226-
:default_publisher,
227-
:default_keywords,
228-
:default_locale,
229-
share_providers: {},
230-
configuration: {}
231-
]
232-
end
233-
234137
def permitted_attributes_for(resource_name)
235138
if params[:id]
236139
Pageflow.config_for(resource).admin_form_inputs.permitted_attributes_for(resource_name)

admins/pageflow/entry_templates.rb

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
module Pageflow
2+
ActiveAdmin.register EntryTemplate, as: 'EntryTemplate' do
3+
menu false
4+
config.batch_actions = false
5+
actions :new, :create, :edit, :update
6+
form partial: 'form'
7+
8+
belongs_to :account, parent_class: Pageflow::Account
9+
10+
breadcrumb do
11+
breadcrumb_links.first 3
12+
end
13+
14+
controller do
15+
helper Pageflow::Admin::FormHelper
16+
helper Pageflow::Admin::WidgetsHelper
17+
18+
def new
19+
@page_title = page_title('new', params[:entry_type])
20+
super
21+
end
22+
23+
def create
24+
@entry_template = EntryTemplate.new(entry_template_params.merge(
25+
account_id: permitted_params[:account_id]
26+
))
27+
authorize!(:create, @entry_template)
28+
create! { redirect_path }
29+
update_widgets if @entry_template.errors.empty?
30+
end
31+
32+
def edit
33+
@page_title = page_title('edit', resource.entry_type)
34+
super
35+
end
36+
37+
def update
38+
@entry_template = EntryTemplate.find(params[:id])
39+
@entry_template.assign_attributes(entry_template_params)
40+
params[:entry_template].delete('share_providers')
41+
params[:entry_template].delete('configuration')
42+
authorize!(:update, @entry_template)
43+
update! { redirect_path }
44+
update_widgets if @entry_template.errors.empty?
45+
end
46+
47+
private
48+
49+
def page_title(rest_verb, entry_type_name)
50+
"#{I18n.t('pageflow.admin.entry_templates.page_title.' + rest_verb)} "\
51+
"#{I18n.t('activerecord.values.pageflow/entry.type_names.' + entry_type_name)}"
52+
end
53+
54+
def permitted_params
55+
params.permit(
56+
:account_id,
57+
entry_template: permitted_entry_template_attributes
58+
)
59+
end
60+
61+
def permitted_entry_template_attributes
62+
[
63+
:id,
64+
:account_id,
65+
:entry_type,
66+
:theme_name,
67+
:default_author,
68+
:default_publisher,
69+
:default_keywords,
70+
:default_locale,
71+
share_providers: {},
72+
configuration: {}
73+
]
74+
end
75+
76+
def redirect_path
77+
admin_account_path(params[:account_id], tab: 'entry_templates')
78+
end
79+
80+
def entry_template_params
81+
current_params = permitted_params[:entry_template]&.to_hash
82+
83+
if current_params
84+
config = true_false_strings_to_booleans_or_numbers(
85+
current_params['configuration']
86+
)
87+
share_providers = true_false_strings_to_booleans_or_numbers(
88+
current_params['share_providers']
89+
)
90+
current_params.merge('configuration' => config,
91+
'share_providers' => share_providers)
92+
else
93+
{}
94+
end
95+
end
96+
97+
def true_false_strings_to_booleans_or_numbers(hash)
98+
hash&.map { |k, v| [k, to_boolean_or_number(v)] }&.to_h
99+
end
100+
101+
def to_boolean_or_number(value)
102+
case value
103+
when 'false'
104+
false
105+
when 'true'
106+
true
107+
when '0'
108+
0
109+
when '1'
110+
1
111+
else
112+
value
113+
end
114+
end
115+
116+
def update_widgets
117+
resource.widgets&.batch_update!(widgets_params)
118+
end
119+
120+
def widgets_params
121+
(params[:widgets].try(:permit!).to_h || {}).map do |role, type_name|
122+
{role: role, type_name: type_name}
123+
end
124+
end
125+
end
126+
end
127+
end

app/models/pageflow/account.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ def first_paged_entry_template
3030
EntryTemplate.find_or_initialize_by(account: self, entry_type: 'paged')
3131
end
3232

33+
def existing_and_potential_entry_templates
34+
entry_type_names = Pageflow.config_for(self).entry_types.map(&:name)
35+
existing_entry_templates = EntryTemplate.where(account_id: id).load
36+
allowed_existing_entry_templates =
37+
existing_entry_templates.select do |template|
38+
entry_type_names.include?(template.entry_type)
39+
end
40+
free_type_names =
41+
entry_type_names - allowed_existing_entry_templates.map(&:entry_type)
42+
43+
potential_entry_templates = free_type_names.map do |type_name|
44+
EntryTemplate.new(
45+
account_id: id,
46+
entry_type: type_name
47+
)
48+
end
49+
50+
allowed_existing_entry_templates + potential_entry_templates
51+
end
52+
3353
def blacklist_for_serialization
3454
[:features_configuration]
3555
end

app/models/pageflow/entry_template.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class EntryTemplate < ApplicationRecord
1616
)
1717
}
1818

19+
def translated_entry_type
20+
I18n.t("activerecord.values.pageflow/entry.type_names.#{entry_type}")
21+
end
22+
1923
def resolve_widgets(options = {})
2024
widgets.resolve(Pageflow.config_for(account), options)
2125
end

app/policies/pageflow/entry_template_policy.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ def initialize(user, entry_template)
55
@entry_template = entry_template
66
end
77

8-
def edit?
8+
def create?
9+
update?
10+
end
11+
12+
def update?
913
allows?(%w(publisher manager))
1014
end
1115

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
extensible_attributes_table_for(account.first_paged_entry_template,
2-
Pageflow.config_for(account)
1+
h5(entry_template.translated_entry_type)
2+
3+
extensible_attributes_table_for(entry_template,
4+
Pageflow.config_for(entry_template.account)
35
.admin_attributes_table_rows.for(:entry_template)) do
46
row :theme, class: 'theme' do
5-
account.first_paged_entry_template.theme_name
7+
entry_template.theme_name
68
end
79
row :default_locale, class: 'default_locale' do
8-
t('pageflow.public._language', locale: account.first_paged_entry_template.default_locale)
10+
t('pageflow.public._language', locale: entry_template.default_locale)
911
end
1012
row :default_author, class: 'default_author'
1113
row :default_publisher, class: 'default_publisher'
1214
row :default_keywords, class: 'default_keywords'
1315
row :default_share_providers, class: 'default_share_providers' do
14-
account.first_paged_entry_template.default_share_providers
16+
entry_template.default_share_providers
1517
.select { |_k, v| v == true }.keys.map(&:camelize).join(', ')
1618
end
1719
end

0 commit comments

Comments
 (0)