Skip to content

Commit

Permalink
update post
Browse files Browse the repository at this point in the history
  • Loading branch information
mangege committed Aug 8, 2017
1 parent 0960999 commit 6f6a572
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion _posts/2017-03-19-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,56 @@ cat app/models/demo.rb # 最终生成的文件, 如果需要改 demo.rb 生成
3. 生成的 lib/generators/demo_generator.rb 文件有个 create_demo_file 方法,此方法创建了 app/models.demo.rb 文件.


未完待续...
### Rails Admin Generator 示例

示例项目: [https://github.com/adminonrails/aor](https://github.com/adminonrails/aor)

挖了这个坑,后面因为做的项目都是前后端分离,就没有再填了. 另外觉得这个项目也比较鸡肋,对新手不友好,上手和定制有点难度.对高手来说,有自己的一套方式,一般是自己挖个坑.

大部分功能都有写单元测试,项目有在生产环境使用过.

项目介绍:

* bootstrap: bootstrap 的静态资源,无 sass 依赖.
* authentication: 提供登录验证的一些辅助方法,源码就一个文件. 主要提供 logged_in?, current_user 等方法. 参考老版 publify .
* authorization: 基于 cancancan 提供后台权限验证的一些辅助方法. 主要基于 controller_name 和 action_name 来限制. 参考 spree 的后台验证逻辑.
* theme: 代码生成器

使用请参考 test 和 dummy 目录代码.

#### theme 详解:

[https://github.com/adminonrails/aor/blob/master/theme/Rakefile](https://github.com/adminonrails/aor/blob/master/theme/Rakefile)

上文件 DummyGenerator 部分,是一个新 rails 项目使用 aor 的主要流程, 这里是用来每次运行 dummy rails 测试项目,先生成最新的 aor 代码.

1. 生成 kaminari 的 bootstrap3 模板.
2. 添加 cancan 的 AdmAbility 文件.
3. 运行 aor:theme
4. 生成 admin user model.


[https://github.com/adminonrails/aor/blob/master/theme/lib/generators/aor/theme/theme_generator.rb](https://github.com/adminonrails/aor/blob/master/theme/lib/generators/aor/theme/theme_generator.rb)

上文件主要在安装了 aor-theme gem,运行 aor:theme 命令的代码.

1. 复制 admin js 和 css 文件.
2. 添加公共的头部,侧边,表单验证错误提示文件.
3. 添加 base admin controller 和 helper 文件.
4. 把 admin.js 和 admin.css 添加到 assets 里,这样编译 js 和 css 会单独生成 admin 文件.
5. 生成表单验证错误提示的 bootstrap 样式.
6 复制项目的 admin generator 到当前 rails 的 lib/generators 目录.


[https://github.com/adminonrails/aor/tree/master/theme/lib/generators/aor/theme/templates/generator](https://github.com/adminonrails/aor/tree/master/theme/lib/generators/aor/theme/templates/generator)

此目录的文件,主要增强 rails 自带的 scaffold, scaffold_controller . 我们不覆盖 rails scaffold,只是添加一个自己的 admin:scaffold . 使用时运行 rails g admin:scaffold .

里面的 rb 文件逻辑,主要是修改 scaffold 的 source_paths 路径,优先使用我们的 controller 和 views 模板.

子目录 erb 和 rails 即是模板.


##### 总结

此项目混合两种方式,一种是通过代码继承,子类通过重写父类方法来实现自定义.一种是生成代码,再修改生成的生成器代码,来实现自定义.

0 comments on commit 6f6a572

Please sign in to comment.