Virtus.group

The idea of this gem is to define groups over virtus attributes. Which is especially useful when you are working with form objects.

Installation

Add this line to your application's Gemfile:

gem 'virtus-group'

And then execute:

$ bundle

Or install it yourself as:

$ gem install virtus-group

Usage

class SignUpForm
  include Virtus.model
  include Virtus.group

  group :user do
    attribute :email, String
    attribute :password, String
  end

  group :address do
    attribute :street, String
    attribute :zipcode, String
    attribute :city, String
  end
end

Don't worry, the SignUpForm class will still behave like a regular Virtus class.

# ---

SignUpForm.attribute_group
# => {user: [:email, :password], address: [:street, :zipcode, :city]}

SignUpForm.attribute_group[:user]
# => [:email, :password]

SignUpForm.attribute_group[:address]
# => [:street, :zipcode, :city]

# ---

 = SignUpForm.new({
  email: '[email protected]', password: '12344321',
  street: 'ABC-Street 1', zipcode: 'Z1234', city: 'ABC'
})

.attributes_for :user
# => {email: '[email protected]', password: '12344321'}

.attributes_for :address
# => {street: 'ABC-Street 1', zipcode: 'Z1234', city: 'ABC'}

Let's say you have a create method on your SignUpForm you can use the attribute groups to create your models:

class SignUpForm
  include Virtus.model
  include Virtus.group

  group :user do
    attribute :email, String
    attribute :password, String
  end

  group :address do
    attribute :street, String
    attribute :zipcode, String
    attribute :city, String
  end

  def create
    user = User.build(attributes_for(:user))
    user.build_address(attributes_for(:address))
    user.save
  end
end

Contributing

  1. Fork it ( http://github.com/spoptchev/virtus-group/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request