ActiveAdmin
2018-06-12 15:00:38 1 举报
AI智能生成
ActiveAdmin概览模型
作者其他创作
大纲/内容
Rails
Template system
ActionView
FormBuilder
FormHelpers
RouteSet
ActiveAdmin.routes(self)<br>config/routes.rb
路由入口调度
ActiveAdmin::Router.apply
Controller
BaseController
ResourceController
动态生成子Controller
Admin::BlogsController
index action
Admin::UsersController
determine_active_admin_layout
default layout: false
active_admin/app/views/layouts/active_admin.html.arb
helper_method :renderer_for
active_admin_namespace.view_factory["#{action}_page"]
PageController
include ActiveAdmin::ViewHelpers
InheritedResources
Base
include Actions
index
show
Scaffolding style controllers
View
Arbre
write view layer in ruby OO way,从AA中抽出来的
.arb template
Element<br>DOMTree基本单元抽象
Arbre::Rails::Rendering#render
helpers.render
.new
arbre_context
delegate assigns, helpers
children
子标签 array like
.to_s
最终产生的渲染HTML内容
一般内容的构建通过content=来设置,用content方法来读取
content默认实现
children.to_s
include BuilderMethods
important! class_method: builder_method :div
Tag类加载时定义了标签方法, 如 div, para
成为Element实例方法<br>Arbre::Element::BuilderMethods.instance_methods.sort<br>
从而能达到在任何tag后的block内可以调到其它的tag方法<br>只要是HTML valid的
使用/调用div
insert_tag
build_tag
tag is sub Element instance
invoke tag.build(*args, &block)
特别注意: 调用时*args会被一路透传到调用tag.build(*args)处
特别注意:声明语句后的block语句是在对应的Tag/Component实例的上下文中执行的<br>block也会被透传,从声明处一直到关联的Tag实例上下文中执行
所以可用方法因具体tag而不同!
暴露了dsl的弊病?<br>要反复查代码,增加学习成本!
current_arbre_element
children
.build
约定的绘制方法,构建tag属性,内容等<br>为最终.to_s方法负责
一般会被子tag重写,参TextNode
append_return_block(block.call(self)) if block
Tag
para, div, span, html, input, li, form, footer, hr
table
html5 support
content创建方式
调用时作为参数传入
div "test div"
通过block方式以内部add_child方式创建<br>更灵活
div do<br> span 'a span'<br>end
.build
invoke super
TextNode
just text element
Special Tag
P
para as builder_method
Document
no builder_method
Div
Context<br>使用主入口
Arbre::Context.new(assigns, self) {<br> #{template.source}<br> }.to_s
arbre/rails/template_handler.rb
here self is helpers from caller, eg. rails
The contents of the block are instance eval'd within the Context <br>object. This means that you lose context to the outside world from<br> within the block. To pass local variables into the Context, use the<br> assigns param.
current_arbre_element
within, with_current_arbre_element(tag)
current arbre element buffer as array
top is context itself
Component
just div tag with css class
ActiveAdmin<br>Views
Component
with builder_method
Panel, TableFor, BlankSlate
TableFor
column
columns
without builder_method
Header, Footer, TitleBar, IndexAsTable, TabbedNavigation, ActionItems
Pages::Base
Index
config is pre-config page_presenter
.render_index
insert_tag(renderer_class, config, collection)
so config.block pass to insert_tag
结论:index后的block是在renderer_class实例中执行的,如IndexAsTable
table_for collection, table_options do |t|<br> table_config_block = page_presenter.block || default_table<br> instance_exec(t, &table_config_block)<br> end
进一步触发 insret_tag IndexTableFor
index_column
id_column
renderer_class refer to Views::IndexAsTable by :as key
Form
for new, edit page
Layout, Page, Show
delegate :active_admin_config, :controller, :params, to: :helpers
入口<br>app/views/
active_admin
page
resource/*.arb
index.html.arb
insert_tag renderer_for(:index)<br>
devise/**/*.erb
layouts
arb, erb
kaminari
!!! Rails是如何找到这个渲染路径的?
irb(main):018:0> puts Admin::TagsController.view_paths.map(&:to_s)<br>/Users/cao/dev/cao9/app/views<br>/rbox/rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.3.0/app/views<br>/rbox/rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/kaminari-core-1.1.1/app/views<br>/rbox/rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise-4.4.3/app/views<br>=> nil<br>irb(main):019:0> Admin::TagsController._prefixes<br>=> ["admin/tags", "active_admin/resource", "active_admin/base", "inherited_resources/base", "application"]
按照rails约定进行fallback动态查找!
覆盖方法
app/views
prepend_view_path
Formtastic
Input
great senmantic inputs
semantic_form_for
ActvieAdmin::Addons
顶层扩展<br>app/inputs
TagsInput
DateTimePickerInput
Ransack
filters
Devise
authentication
Core Config<br>Developer entry
ActiveAdmin
Application
顶级配置,打造Admin应用
view_factory as hash
.register Blog do; xxx
DSL
具体资源定制
run_registration_block(&block)
instance_exec &block if block_given?
so block xxx exec in dsl instance
结论:register的block是在dsl实例中执行的
目的:专门用来配置Resource的!
Namespace
.register Blog do; xxx
parse_registration_block
config.dsl = ResourceDSL.new(config)<br> config.dsl.run_registration_block(&block)
conf is the resource
Resource
ResourceDSL(as dsl attr)
block statement: index(options = {}, &block)
问题: index block的执行环境?
config.set_page_presenter :index, ActiveAdmin::PagePresenter.new(options, &block)
show page: show(options = {}, &block)
as active_admin_conf in controller
resource_class
User
Blog
Page
PageDSL
适合单页面逻辑,无具体model展示
ActiveAdmin.register page 'aPage' do; xxx
PagePresenter
new
block
options as hash
配置view各种属性的简单对象,在ActiveAdmin::Views::Pages各个类中使用
0 条评论
下一页
为你推荐
查看更多