ActivePresenter 技术文档
1. 安装指南
通过 Gem 安装
$ sudo gem install active_presenter
作为 Rails Gem 依赖
在 config/environment.rb
文件中添加以下配置:
config.gem 'active_presenter'
通过 GitHub 获取源码
$ git clone git://github.com/giraffesoft/active_presenter.git
你也可以在 GitHub 上 fork 该项目:GitHub 仓库地址
2. 项目使用说明
创建 Presenter
创建一个 Presenter 非常简单,只需继承 ActivePresenter::Base
类,并使用 presents
方法指定需要呈现的模型。
class SignupPresenter < ActivePresenter::Base
presents :user, :account
end
在上面的例子中,:user
将自动成为 User
模型。如果你想覆盖这种行为,可以在 presents
方法中使用哈希指定模型类型:
class PresenterWithTwoAddresses < ActivePresenter::Base
presents :primary_address => Address, :secondary_address => Address
end
实例化 Presenter
你可以使用两种形式来实例化 Presenter:
- 通过参数哈希初始化:
SignupPresenter.new(:user_login => 'james', :user_password => 'swordfish', :user_password_confirmation => 'swordfish', :account_subdomain => 'giraffesoft')
- 通过模型实例初始化:
SignupPresenter.new(:user => User.find(1), :account => Account.find(2))
这两种形式可以混合使用:
SignupPresenter.new(:user => User.find(1), :user_login => 'james')
验证
#valid?
方法将根据呈现对象的有效性返回 true
或 false
。你可以通过以下两种方式获取错误信息:
- 调用
#errors
方法:
@presenter.errors.on(:user_login)
- 调用
@presenter.user_errors
或@presenter.user.errors
:
@presenter.user_errors
受保护和可访问的属性
ActivePresenter 支持 attr_protected
和 attr_accessible
,类似于 ActiveRecord 对象,以避免大规模赋值。
class AccountPresenter < ActivePresenter::Base
presents :user, :profile
attr_accessible :user_email, :profile_birthday
end
保存
你可以像保存 ActiveRecord 对象一样保存 Presenter。#save
和 #save!
的行为与 ActiveRecord 中的行为相同。
回调
回调的工作方式与 ActiveRecord 回调完全相同。支持 before_save
和 after_save
回调。
3. 项目 API 使用文档
presents
方法
presents
方法用于指定 Presenter 需要呈现的模型。可以接受符号或哈希作为参数。
presents :user, :account
new
方法
new
方法用于实例化 Presenter,可以接受参数哈希或模型实例。
SignupPresenter.new(:user_login => 'james', :user_password => 'swordfish')
valid?
方法
valid?
方法用于验证 Presenter 的有效性。
@presenter.valid?
errors
方法
errors
方法用于获取 Presenter 的错误信息。
@presenter.errors.on(:user_login)
save
和 save!
方法
save
和 save!
方法用于保存 Presenter。
@presenter.save
4. 项目安装方式
通过 Gem 安装
$ sudo gem install active_presenter
作为 Rails Gem 依赖
在 config/environment.rb
文件中添加以下配置:
config.gem 'active_presenter'
通过 GitHub 获取源码
$ git clone git://github.com/giraffesoft/active_presenter.git
你也可以在 GitHub 上 fork 该项目:GitHub 仓库地址
鸿蒙开发工具大赶集
本仓将收集和展示鸿蒙开发工具,欢迎大家踊跃投稿。通过pr附上您的工具介绍和使用指南,并加上工具对应的链接,通过的工具将会成功上架到我们社区。012hertz
Go 微服务 HTTP 框架,具有高易用性、高性能、高扩展性等特点。Go01每日精选项目
🔥🔥 每日精选已经升级为:【行业动态】,快去首页看看吧,后续都在【首页 - 行业动态】内更新,多条更新哦~🔥🔥 每日推荐行业内最新、增长最快的项目,快速了解行业最新热门项目动态~~029kitex
Go 微服务 RPC 框架,具有高性能、强可扩展的特点。Go00Cangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。Cangjie057毕方Talon工具
本工具是一个端到端的工具,用于项目的生成IR并自动进行缺陷检测。Python040PDFMathTranslate
PDF scientific paper translation with preserved formats - 基于 AI 完整保留排版的 PDF 文档全文双语翻译,支持 Google/DeepL/Ollama/OpenAI 等服务,提供 CLI/GUI/DockerPython06mybatis-plus
mybatis 增强工具包,简化 CRUD 操作。 文档 http://baomidou.com 低代码组件库 http://aizuda.comJava03国产编程语言蓝皮书
《国产编程语言蓝皮书》-编委会工作区018- DDeepSeek-R1探索新一代推理模型,DeepSeek-R1系列以大规模强化学习为基础,实现自主推理,表现卓越,推理行为强大且独特。开源共享,助力研究社区深入探索LLM推理能力,推动行业发展。【此简介由AI生成】。Python00
热门内容推荐
最新内容推荐
项目优选









