百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yaaf

> 后端框架
开源

缓解 Rails 应用程序中的表单对象模式

373 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

缓解 Rails 应用程序中的表单对象模式

YAAF (Yet Another Active Form) is a gem that let you create form objects in an easy and Rails friendly way. It makes use of ActiveRecord and ActiveModel features in order to provide you with a form object that behaves pretty much like a Rails model, and still be completely configurable.

We were going to name this gem ActiveForm to follow Rails naming conventions but given there are a lot of form object gems named like that we preferred to go with YAAF.

Table of Contents

  • Motivation
    • Why YAAF?
  • Installation
  • Usage
    • Setting up a form object
    • #initialize
    • #valid?
    • #invalid?
    • #errors
    • #save
    • #save!
    • Validations
    • Callbacks
    • Normalizes
  • Sample app
  • Links
  • Development
  • Contributing
  • License
  • Code of Conduct
  • Credits

Motivation

Form Objects is a design pattern that allows us to:

  1. Keep views, models and controllers clean
  2. Create/update multiple models at the same time
  3. Keep business logic validations out of models

There are some other form objects gems but we felt none of them provided us all the features that we expected:

  1. Form objects that behave like Rails models
  2. Simple to use and to understand the implementation (no magic)
  3. Easy to customize
  4. Gem is well tested and maintained

For this reason we decided to build our own Form Object implementation. After several months in production without issues we decided to extract it into a gem to share it with the community.

If you want to learn more about Form Objects you can check out these great articles.

Why YAAF?

  • It is 71 lines long. As you can imagine, we did no magic in such a few lines of code, we just leveraged Rails modules in order to provide our form objects with a Rails-like behavior. You can review the code, it's easy to understand.

  • It provides a similar API to ActiveModel models so you can treat them interchangeably.

  • You can customize it 100%. We encourage you to have your own ApplicationForm which inherits from YAAF::Form and make the customizations you'd like for your app.

  • It helps decoupling the frontend from the database. This is particularly important when using Rails as a JSON API with a frontend in React/Ember/Vue/Angular/you name it. If you were to use accepts_nested_attributes_for your frontend would need to know your database structure in order to build the request. With YAAF you can provide a the interface you think it's best.

  • It easily supports nested models, collection of models and associated models. You have full control on their creation.

  • It helps you keep your models, views and controllers thin by providing a better place where to put business logic. In the end, this will improve the quality of your codebase and make it easier to maintain and extend.

  • It is an abstraction from production code. It has been working well for us, I'm confident it will work well for you too :)

Installation

Add this line to your application's Gemfile:

ruby
gem 'yaaf'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install yaaf

Usage

In the following sections we explain some basic usage and the API provided by the gem. You can also find some recipes here.

Setting up a form object

In order to use a YAAF form object, you need to inherit from YAAF::Form and define the @models of the form, for example:

ruby
# app/forms/registration_form.rb

class RegistrationForm (email) { email.strip.downcase }
  normalizes :name, with: ->(name) { name.strip.titleize }

  # ...
end

Sample app

You can find a sample app making use of the gem here. Its code is also open source, and you can find it here.

Links

  • How to improve maintainability in Rails applications using patterns. Part I
  • 7 Patterns to Refactor Fat ActiveRecord Models
  • ActiveModel Form Objects
  • Form Objects Design Pattern
  • Form Object from Railscasts
  • Validating Form Objects
  • Disciplined Rails: Form Object Techniques & Patterns — Part 1
  • Complex form objects with Rails
  • How to keep your controllers thin with form objects

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rootstrap/yaaf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the YAAF project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

YAAF is maintained by Rootstrap with the help of our contributors.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyformform-objectform-validationhacktoberfest

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架