保持模型记录和相关目录同步(与测试)

 恋苦尘雪77 发布于 2023-02-13 17:50

在我的应用程序中,我在创建主题记录时创建了一个目录.这是为了存储与主题相关的文件资产.我一直在努力避免如何使目录的存在与记录的生命周期保持同步.这是我目前的看法:

after_create :create_theme_directory
after_rollback :destroy_theme_directory, :on => :create, :if => :id

def directory
    Rails.configuration.uploads_root.join('themes', id.to_s)
end

private

def create_theme_directory
    FileUtils.mkdir_p directory
end

def destroy_theme_directory
    FileUtils.remove_dir directory, :force => true
end

它运行良好,但Rspec在测试后回滚主题记录时似乎没有触发删除目录.

这种事情有最好的做法吗?我们的想法是永远不要留下没有相关记录的迷路目录.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有