Skip to content

Commit 03638ac

Browse files
committed
Allow for community resources; close #185
1 parent 18b15f5 commit 03638ac

File tree

10 files changed

+83
-11
lines changed

10 files changed

+83
-11
lines changed

app/controllers/application_controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,11 @@ def check_if_locked(post)
103103
end
104104

105105
def top_level_post_types
106-
Rails.cache.fetch 'top_level_post_types' do
107-
PostType.where(is_top_level: true).select(:id).map(&:id)
108-
end
106+
helpers.post_type_ids(is_top_level: true)
109107
end
110108

111109
def second_level_post_types
112-
Rails.cache.fetch 'second_level_post_types' do
113-
PostType.where(is_top_level: false, has_parent: true).select(:id).map(&:id)
114-
end
110+
helpers.post_type_ids(is_top_level: false, has_parent: true)
115111
end
116112

117113
def check_edits_limit!(post)

app/controllers/posts_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ def update
129129
return redirect_to post_path(@post)
130130
end
131131

132-
if current_user.privilege?('edit_posts') || current_user.is_moderator || current_user == @post.user
132+
if current_user.privilege?('edit_posts') || current_user.is_moderator || current_user == @post.user || \
133+
(@post_type.is_freely_editable && current_user.privilege?('unrestricted'))
133134
if @post.update(edit_post_params.merge(body: body_rendered,
134135
last_edited_at: DateTime.now, last_edited_by: current_user,
135136
last_activity: DateTime.now, last_activity_by: current_user))

app/helpers/post_types_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,15 @@ def post_type_badge(type)
88
tag.i(class: icon_class) + ' ' + tag.span(type) # rubocop:disable Style/StringConcatenation
99
end
1010
end
11+
12+
def post_type_criteria
13+
PostType.new.attributes.keys.select { |k| k.start_with?('has_') || k.start_with?('is_') }.map(&:to_sym)
14+
end
15+
16+
def post_type_ids(**opts)
17+
key = post_type_criteria.map { |a| opts[a] ? '1' : '0' }.join
18+
Rails.cache.fetch "post_type_ids/#{key}" do
19+
PostType.where(**opts).select(:id).map(&:id)
20+
end
21+
end
1122
end

app/models/community_user.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def suspended?
2525
# These are quite expensive, so we'll cache them for a while
2626
def post_score
2727
Rails.cache.fetch("privileges/#{id}/post_score", expires_in: 3.hours) do
28-
good_posts = Post.where(user: user).where('score > 0.5').count
29-
bad_posts = Post.where(user: user).where('score < 0.5').count
28+
exclude_types = ApplicationController.helpers.post_type_ids(is_freely_editable: true)
29+
good_posts = Post.where(user: user).where('score > 0.5').where.not(post_type_id: exclude_types).count
30+
bad_posts = Post.where(user: user).where('score < 0.5').where.not(post_type_id: exclude_types).count
3031

3132
(good_posts + 2.0) / (good_posts + bad_posts + 4.0)
3233
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddIsFreelyEditableToPostTypes < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :post_types, :is_freely_editable, :boolean, null: false, default: false
4+
end
5+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_12_12_235514) do
13+
ActiveRecord::Schema.define(version: 2020_12_16_225353) do
1414

1515
create_table "abilities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
1616
t.bigint "community_id"
@@ -309,6 +309,7 @@
309309
t.boolean "is_public_editable", default: false, null: false
310310
t.boolean "is_closeable", default: false, null: false
311311
t.boolean "is_top_level", default: false, null: false
312+
t.boolean "is_freely_editable", default: false, null: false
312313
t.index ["name"], name: "index_post_types_on_name"
313314
end
314315

@@ -545,6 +546,7 @@
545546
t.integer "failed_attempts", default: 0, null: false
546547
t.string "unlock_token"
547548
t.datetime "locked_at"
549+
t.integer "trust_level"
548550
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
549551
t.index ["email"], name: "index_users_on_email", unique: true
550552
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

test/controllers/posts_controller_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,20 @@ class PostsControllerTest < ActionController::TestCase
393393
assert_equal before_history, after_history, 'PostHistory event incorrectly created on update'
394394
end
395395

396+
test 'anyone with unrestricted can update free-edit post' do
397+
sign_in users(:standard_user)
398+
before_history = PostHistory.where(post: posts(:free_edit)).count
399+
patch :update, params: { id: posts(:free_edit).id,
400+
post: { title: sample.edit.title, body_markdown: sample.edit.body_markdown,
401+
tags_cache: sample.edit.tags_cache } }
402+
after_history = PostHistory.where(post: posts(:free_edit)).count
403+
assert_response 302
404+
assert_redirected_to post_path(posts(:free_edit))
405+
assert_not_nil assigns(:post)
406+
assert_equal sample.edit.body_markdown, assigns(:post).body_markdown
407+
assert_equal before_history + 1, after_history, 'No PostHistory event created on free-edit update'
408+
end
409+
396410
# Close
397411

398412
test 'can close question' do

test/fixtures/categories.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ main:
1111
- question
1212
- answer
1313
- article
14+
- free_edit
1415
tag_set: main
1516
license: cc_by_sa
1617

test/fixtures/post_types.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ question:
1010
is_public_editable: true
1111
is_closeable: true
1212
is_top_level: true
13+
is_freely_editable: false
1314

1415
answer:
1516
name: Answer
@@ -23,6 +24,7 @@ answer:
2324
is_public_editable: true
2425
is_closeable: false
2526
is_top_level: false
27+
is_freely_editable: false
2628

2729
article:
2830
name: Article
@@ -36,6 +38,7 @@ article:
3638
is_public_editable: true
3739
is_closeable: false
3840
is_top_level: true
41+
is_freely_editable: false
3942

4043
policy_doc:
4144
name: PolicyDoc
@@ -49,6 +52,7 @@ policy_doc:
4952
is_public_editable: false
5053
is_closeable: false
5154
is_top_level: false
55+
is_freely_editable: false
5256

5357
help_doc:
5458
name: HelpDoc
@@ -62,6 +66,7 @@ help_doc:
6266
is_public_editable: false
6367
is_closeable: false
6468
is_top_level: false
69+
is_freely_editable: false
6570

6671
blog_post:
6772
name: BlogPost
@@ -74,4 +79,19 @@ blog_post:
7479
has_license: true
7580
is_public_editable: false
7681
is_closeable: false
77-
is_top_level: true
82+
is_top_level: true
83+
is_freely_editable: false
84+
85+
free_edit:
86+
name: FreeEdit
87+
description: ~
88+
has_answers: true
89+
has_votes: true
90+
has_tags: true
91+
has_parent: false
92+
has_category: true
93+
has_license: true
94+
is_public_editable: true
95+
is_closeable: true
96+
is_top_level: true
97+
is_freely_editable: true

test/fixtures/posts.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ locked_mod:
183183
upvote_count: 0
184184
downvote_count: 0
185185

186+
free_edit:
187+
post_type: free_edit
188+
title: FE ABCDEF GHIJKL MNOPQR STUVWX YZ
189+
body: ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ
190+
body_markdown: ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ
191+
tags_cache:
192+
- discussion
193+
- support
194+
- bug
195+
tags:
196+
- discussion
197+
- support
198+
- bug
199+
score: 0.5
200+
user: moderator
201+
community: sample
202+
category: main
203+
license: cc_by_sa
204+
upvote_count: 0
205+
downvote_count: 0
206+
186207
answer_one:
187208
post_type: answer
188209
body: A1 ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ

0 commit comments

Comments
 (0)