项目作者: nsommer

项目描述 :
A toolkit for orchestrating actions on related models.
高级语言: Ruby
项目地址: git://github.com/nsommer/model_orchestration.git
创建时间: 2017-02-07T15:59:01Z
项目社区:https://github.com/nsommer/model_orchestration

开源协议:Other

下载


" class="reference-link">Model Orchestration - orchestrating actions on related models Build Status

Links: GitHub | Travis CI | RubyGems | RDocs

Have you ever collected multiple models (either ActiveModel or ActiveRecord) in a controller method and had to orchestrate persisting them in the correct order? This library offers an elegant solution to this.

Create a new class, also called OrchestrationModel, which collects as many models as you like and orchestrates through validation and persistence operations. Use an easy to read DSL-like API to register nested models to this OrchestrationModel and their dependencies between each other. OrchestrationModels act like a container and behave just like an ActiveModel or ActiveRecord objects themselves. Therefore you can ceep controller methods simple without writing lots of code.

Installing the Ruby Gem

If you use bundler (preferred), put model_orchestration into your applications Gemfile.

  1. gem "model_orchestration"

Otherwise you can of course install it globally with gem install model_orchestration.

Example

Consider you’re developing a B2B SaaS application with a signup form. When a new client signs up, you will probably need to create multiple models and save them to the database, for example a user object, representing the person signing up and an organization object which attaches the user to a legal entity which represents your customer, billing data etc.

ModelOrchestration allows you to nest the user model and the organization model into an OrchestrationModel, on which all the actions necessary can be performed (validation, persistence). Let’s call this OrchestrationModel simply Signup.

  1. class User < ActiveRecord::Base
  2. belongs_to :organization
  3. validates :name, presence: true
  4. validates :email, presence: true
  5. end
  6. class Organization < ActiveRecord::Base
  7. has_many :users
  8. validates :name, presence: true
  9. end
  10. class Signup
  11. include ModelOrchestration::Base
  12. include ModelOrchestration::Persistence
  13. nested_model :organization
  14. nested_model :user
  15. nested_model_dependency from: :user, to: :organization
  16. end
  17. signup = Signup.new(user: {name: "Nils", email: "nils@example.org"}, organization: {name: "Nils' Webdesign Agency"})
  18. signup.valid? # => true
  19. signup.user.name # => "Nils"
  20. signup.user # => #<User:0x007f81f498d078>
  21. signup[:user][:email] # => "nils@example.org"
  22. signup.save # => true

Development & Contributions

To start development on the model_orchestration itself, follow these steps.

  1. git clone https://github.com/nsommer/model_orchestration.git
  2. cd model_orchestration/
  3. bundle install

You can run the tests with rake.

  1. bundle exec rake

To build the rdoc files, run the corresponding rake task.

  1. bundle exec rake rdoc

This generates the docs directory and puts all the documentation into it.

Changelog

See CHANGELOG.md.

License

Model Orchestration is released under the MIT license.