Skip to content

Commit

Permalink
FIX: first fixes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lis2 committed May 13, 2024
1 parent cfdd870 commit d4f1a1f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 36 deletions.
16 changes: 8 additions & 8 deletions app/models/post_flag.rb → app/models/flag.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# frozen_string_literal: true

class PostFlag < ActiveRecord::Base
class Flag < ActiveRecord::Base
scope :enabled, -> { where(enabled: true) }

before_create :set_custom_id, unless: :system?
before_save :set_position
after_save :reset_post_action_types!
after_destroy :reset_post_action_types!
after_save :reset_flag_settings!
after_destroy :reset_flag_settings!

def used?
PostAction.exists?(post_action_type_id: self.id)
end

def self.reset_post_action_types!
def self.reset_flag_settings!
PostActionType.initialize_flag_settings
end

private

def reset_post_action_types!
self.class.reset_post_action_types!
def reset_flag_settings!
self.class.reset_flag_settings!
end

def set_custom_id
return if self.id.to_i > 1000
self.id = PostFlag.maximum(:id).next
self.id = Flag.maximum(:id).next
self.id += 1000
end

def set_position
self.position = PostFlag.maximum(:position).to_i + 1 if !self.position
self.position = Flag.maximum(:position).to_i + 1 if !self.position
end
end

Expand Down
28 changes: 13 additions & 15 deletions app/models/post_action_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,19 @@ def is_flag?(sym)
def initialize_flag_settings
@types = nil
@flag_settings = FlagSettings.new
if ActiveRecord::Base.connection.table_exists?(:post_flags)
PostFlag
.enabled
.order(:position)
.find_each do |post_flag|
@flag_settings.add(
post_flag.id,
post_flag.name.to_sym,
topic_type: post_flag.topic_type,
notify_type: post_flag.notify_type,
auto_action_type: post_flag.auto_action_type,
custom_type: post_flag.custom_type,
)
end
end
Flag
.enabled
.order(:position)
.each do |post_flag|
@flag_settings.add(
post_flag.id,
post_flag.name.to_sym,
topic_type: post_flag.topic_type,
notify_type: post_flag.notify_type,
auto_action_type: post_flag.auto_action_type,
custom_type: post_flag.custom_type,
)
end
end
end

Expand Down
12 changes: 6 additions & 6 deletions db/fixtures/003_post_flags.rb → db/fixtures/003_flags.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

PostFlag.seed do |s|
Flag.seed do |s|
s.id = 3
s.name = "off_topic"
s.position = 2
Expand All @@ -10,7 +10,7 @@
s.auto_action_type = true
s.custom_type = false
end
PostFlag.seed do |s|
Flag.seed do |s|
s.id = 4
s.name = "inappropriate"
s.position = 3
Expand All @@ -20,7 +20,7 @@
s.auto_action_type = true
s.custom_type = false
end
PostFlag.seed do |s|
Flag.seed do |s|
s.id = 8
s.name = "spam"
s.position = 4
Expand All @@ -30,7 +30,7 @@
s.auto_action_type = true
s.custom_type = false
end
PostFlag.seed do |s|
Flag.seed do |s|
s.id = 6
s.name = "notify_user"
s.position = 0
Expand All @@ -40,7 +40,7 @@
s.auto_action_type = false
s.custom_type = true
end
PostFlag.seed do |s|
Flag.seed do |s|
s.id = 7
s.name = "notify_moderators"
s.position = 1
Expand All @@ -50,7 +50,7 @@
s.auto_action_type = true
s.custom_type = true
end
PostFlag.seed do |s|
Flag.seed do |s|
s.id = 10
s.name = "illegal"
s.position = 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CreatePostFlags < ActiveRecord::Migration[7.0]
class CreateFlags < ActiveRecord::Migration[7.0]
def change
create_table :post_flags do |t|
t.string :name, unique: true
Expand Down
4 changes: 2 additions & 2 deletions lib/guardian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "guardian/category_guardian"
require "guardian/ensure_magic"
require "guardian/group_guardian"
require "guardian/post_flag_guardian"
require "guardian/flag_guardian"
require "guardian/post_guardian"
require "guardian/post_revision_guardian"
require "guardian/sidebar_guardian"
Expand All @@ -17,9 +17,9 @@ class Guardian
include BookmarkGuardian
include CategoryGuardian
include EnsureMagic
include FlagGuardian
include GroupGuardian
include PostGuardian
include PostFlagGuardian
include PostRevisionGuardian
include SidebarGuardian
include TagGuardian
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module PostFlagGuardian
module FlagGuardian
def can_edit_post_flag?(post_flag)
@user.admin? && !post_flag.system? && !post_flag.used?
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/guardian/post_flag_guardian_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe PostFlagGuardian do
RSpec.describe FlagGuardian do
fab!(:user)
fab!(:admin)
fab!(:post_flag)
Expand Down
4 changes: 2 additions & 2 deletions spec/models/post_flag_spec.rb → spec/models/flag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe PostFlag, type: :model do
RSpec.describe Flag, type: :model do
it "has id lower than 1000 for system flags" do
post_flag = Fabricate(:post_flag, system: true)
expect(post_flag.system?).to be true
Expand All @@ -13,7 +13,7 @@
expect(post_flag.id).to be > 1000
end

before { PostFlag.reset_post_action_types! }
before { Flag.reset_flag_settings! }

it "updates post action types when created, modified or destroyed" do
expect(PostActionType.flag_types.keys).to eq(
Expand Down

0 comments on commit d4f1a1f

Please sign in to comment.