Skip to content

Commit 6d955a4

Browse files
authored
Merge pull request #1791 from codidact/0valt/1099/license-pref
Fix default license preference choice's option label
2 parents 2edf7b6 + 83dfedc commit 6d955a4

File tree

7 files changed

+89
-29
lines changed

7 files changed

+89
-29
lines changed

app/assets/javascripts/preferences.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$(() => {
1+
document.addEventListener('DOMContentLoaded', () => {
22
$('.js-user-pref').on('change', async (ev) => {
33
const $tgt = $(ev.target);
44
let value;
@@ -10,6 +10,7 @@ $(() => {
1010
}
1111
const prefName = $tgt.attr('data-pref');
1212
const community = $tgt.attr('data-community') === 'true';
13+
1314
await QPixel.setPreference(prefName, value, community);
1415
});
1516

@@ -25,4 +26,4 @@ $(() => {
2526
$(e).addClass('is-yellow');
2627
}
2728
});
28-
});
29+
});

app/assets/javascripts/qpixel_api.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,9 @@ window.QPixel = {
238238

239239
const data = await resp.json();
240240

241-
if (data.status !== 'success') {
242-
console.error(`Preference persist failed (${name})`);
243-
console.error(resp);
244-
}
245-
else {
241+
QPixel.handleJSONResponse(data, (data) => {
246242
QPixel._updatePreferencesLocally(data.preferences);
247-
}
243+
});
248244
},
249245

250246
filters: async () => {

app/controllers/users_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,14 @@ def set_preference
193193
community_key = "prefs.#{current_user.id}.community.#{RequestContext.community_id}"
194194
key = params[:community].present? && params[:community] ? community_key : global_key
195195
current_user.validate_prefs!
196-
render json: { status: 'success', success: true,
196+
render json: { status: 'success',
197197
count: RequestContext.redis.hset(key, params[:name], params[:value].to_s),
198198
preferences: current_user.preferences }
199199
else
200-
render json: { status: 'failed', success: false, errors: ['Both name and value parameters are required'] },
201-
status: 400
200+
render json: { status: 'failed',
201+
message: 'Failed to save the preference',
202+
errors: ['Both name and value parameters are required'] },
203+
status: :bad_request
202204
end
203205
end
204206

app/views/posts/_form.html.erb

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<%#
2-
Post create/edit form covering all post types.
3-
Variables:
4-
post : a Post instance to work on (either via .new or .find - doesn't matter if it has content or not)
5-
submit_path : the URL to submit the form to (must accept POST requests)
6-
post_type : a PostType instance describing the requested post type
7-
category : a Category instance, required if the post type has a category, indicating which category to post in
8-
parent : a Post instance containing the parent for this post (required if the post type has a parent)
9-
inline_parent : optional, default true: display an inline copy of the parent? (if present)
10-
type_summary : optional, default true: display a summary of the requested post type?
11-
edit_comment : optional, default false: include a field for an edit comment?
12-
%>
2+
"Post create/edit form covering all post types.
3+
4+
Variables:
5+
post : a Post instance to work on (either via .new or .find - doesn't matter if it has content or not)
6+
submit_path : the URL to submit the form to (must accept POST requests)
7+
post_type : a PostType instance describing the requested post type
8+
category : a Category instance, required if the post type has a category, indicating which category to post in
9+
parent : a Post instance containing the parent for this post (required if the post type has a parent)
10+
inline_parent : optional, default true: display an inline copy of the parent? (if present)
11+
type_summary : optional, default true: display a summary of the requested post type?
12+
edit_comment : optional, default false: include a field for an edit comment?
13+
"%>
1314

1415
<%
1516
# defaults
@@ -54,8 +55,14 @@
5455
</p>
5556
<% end %>
5657

57-
<%= render 'shared/body_field', f: f, field_name: :body_markdown, field_label: t('posts.body_label'), post: post,
58-
cur_length: post.body_markdown&.length, min_length: min_body_length(category), max_length: max_body_length(category) %>
58+
<%= render 'shared/body_field',
59+
f: f,
60+
field_name: :body_markdown,
61+
field_label: t('posts.body_label'),
62+
post: post,
63+
cur_length: post.body_markdown&.length,
64+
min_length: min_body_length(category),
65+
max_length: max_body_length(category) %>
5966

6067
<div class="rejected-elements notice is-warning hide">
6168
<h3>Unsupported HTML detected</h3>
@@ -144,15 +151,19 @@
144151
<% category_default = category.license %>
145152
<% user_default = user_preference('default_license', community: true) %>
146153
<% if site_default.present? %>
147-
community default: <a href="javascript:void(0)" class="js-license-autofill" data-license-id="<%= site_default.id %>">
154+
community default: <a href="javascript:void(0)"
155+
class="js-license-autofill"
156+
data-license-id="<%= site_default.id %>">
148157
<%= site_default.name %>
149158
</a>
150159
<% end %>
151160
<% if site_default.present? && category_default.present? %>
152161
&middot;
153162
<% end %>
154163
<% if category_default.present? %>
155-
category default: <a href="javascript:void(0)" class="js-license-autofill" data-license-id="<%= category_default.id %>">
164+
category default: <a href="javascript:void(0)"
165+
class="js-license-autofill"
166+
data-license-id="<%= category_default.id %>">
156167
<%= category_default.name %>
157168
</a>
158169
<% end %>

app/views/users/_prefs.html.erb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
<input class="js-user-pref" type="checkbox" data-pref="<%= name %>" <%= 'checked' if value == 'true' %>
1212
data-community="<%= community %>" />
1313
<% when 'choice' %>
14-
<%= select_tag nil, options_for_select(preference_choice(pref_config), selected: value),
15-
include_blank: true, class: 'js-user-pref form-element', data: { pref: name, community: community } %>
14+
<%= select_tag nil,
15+
options_for_select(preference_choice(pref_config), selected: value),
16+
class: 'js-user-pref form-element',
17+
data: { pref: name, community: community } %>
1618
<% when 'integer' %>
1719
<input class="js-user-pref form-element" type="number" data-pref="<%= name %>" value="<%= value %>"
1820
data-community="<%= community %>" />
@@ -22,4 +24,4 @@
2224
<% end %>
2325
</div>
2426
</div>
25-
<% end %>
27+
<% end %>

config/config/preferences.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ default_license:
3838
Your choice of default license. This will override site and category default licensing.
3939
default: ~
4040
choice:
41+
- name: Site or category default
42+
value: ~
4143
- name: CC BY-SA 4.0
4244
value: CC BY-SA 4.0
4345
- name: CC BY 4.0

test/controllers/users_controller_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,43 @@ class UsersControllerTest < ActionController::TestCase
507507
assert(items.any? { |x| x.instance_of?(Flag) && x.id == declined_flag.id })
508508
end
509509

510+
test 'set_preference should correclty save valid preferences' do
511+
sign_in users(:standard_user)
512+
513+
pref_key = 'default_license'
514+
license = licenses(:cc_by_sa)
515+
516+
[nil, communities(:sample)].each do |community|
517+
try_save_preference(pref_key, license, community: community)
518+
519+
assert_response(:success)
520+
assert_valid_json_response
521+
522+
parsed_body = JSON.parse(response.body)
523+
assert_equal 'success', parsed_body['status']
524+
assert_not_nil parsed_body['preferences']
525+
526+
comm_key = community.present? ? 'community' : 'global'
527+
assert_not_nil parsed_body['preferences'][comm_key]
528+
assert_equal license.id.to_s, parsed_body['preferences'][comm_key][pref_key]
529+
end
530+
end
531+
532+
test 'set_preference should correctly handle invalid preferences' do
533+
sign_in users(:standard_user)
534+
535+
[nil, communities(:sample)].each do |community|
536+
post :set_preference, params: { community: community, format: :json }
537+
538+
assert_response(:bad_request)
539+
assert_valid_json_response
540+
541+
parsed_body = JSON.parse(response.body)
542+
assert_equal 'failed', parsed_body['status']
543+
assert_not_nil parsed_body['message']
544+
end
545+
end
546+
510547
private
511548

512549
def create_other_user
@@ -516,4 +553,13 @@ def create_other_user
516553
other_user.community_users.create!(community: other_community)
517554
other_user
518555
end
556+
557+
def try_save_preference(name, value, community: nil)
558+
post :set_preference, params: {
559+
community: community,
560+
name: name,
561+
value: value,
562+
format: :json
563+
}
564+
end
519565
end

0 commit comments

Comments
 (0)