目前找到代码如下 想仿照这个写,但是不知道怎么传入 topic.created_at
setting.rb
HOT_UPDATE_KEYS = %w[
...
+ topic_delete_second
]
...
scope :limits do
field :rack_attack, type: :hash, default: {
limit: 0,
period: 3.minutes
}
field :newbie_limit_time, type: :integer, default: 0
field :topic_create_limit_interval, type: :integer, default: 0
field :topic_create_hour_limit_count, type: :integer, default: 0
+ field :topic_delete_second, type: :integer, default: 0
end
application_helper.rb 新增一个函数
+ def time_edit(time)
+ return false if time.blank?
+ return true if Setting.topic_delete_second == 0
+ time.to_i + Setting.topic_delete_second.to_i > Time.now.to_i
+ end
def roles_for_topics
unless user.newbie?
can :create, Topic
end
can %i[favorite unfavorite follow unfollow], Topic
- can %i[update open close], Topic, user_id: user.id
+ can %i[open close], Topic, user_id: user.id
can :change_node, Topic, user_id: user.id, lock_node: false
+ can :update, Topic do |topic|
+ can_update = Setting.topic_delete_second == 0 ? true: ( topic.created_at.to_i + Setting.topic_delete_second.to_i > Time.now.to_i)
+ topic.user_id == user.id && can_update
+ end
can :destroy, Topic do |topic|
topic.user_id == user.id && topic.replies_count == 0
end
end
剩下的就是在按钮处调用 time_edit 这个函数判断是否有权限修改
最后在 admin 后台管理设置时间就可以了