Giter VIP home page Giter VIP logo

composed_of_validations's Introduction

composed_of_validations

ActiveModel::Validations support for composed_of

Installation

Add this line to your application's Gemfile:

gem 'composed_of_validations', git: '[email protected]:zwelch/composed_of_validations.git'

And then execute:

$ bundle

Usage

composed_of_validations patches ActiveRecord composed_of with support for ActiveModel::Validations. See issue 1513 for more background.

If we want to represent address attributes as a value object

class Person < ActiveRecord::Base
  composed_of :address, mapping: [%w(address_street street), 
                                  %w(address_city city), 
                                  %w(address_state state), 
                                  %w(address_zip zip)],
                        allow_nil: true
end

We can define our value object with validations

class Address
  include ActiveModel::Validations
  attr_reader :street, :city, :state, :zip
  
  def initialize(street, city, state, zip)
    @street = street
    @city = city
    @state = state
    @zip = zip
  end

  validates_presence_of :street, :city, :state, :zip
end

And receive support similar to ActiveRecord.

> person = Person.first
 => #<Person id: 1, name: "Tobias", address_street: "123 Sesame St", address_city: "Atlanta", address_state: "GA", address_zip: "30092"> 

> person.address
 => #<Address:0x007fc6b4e34ea0 @street="123 Sesame St", @city="Atlanta", @state="GA", @zip="30092"> 

> person.address = Address.new('New York Street', nil, 'NY', '10001')
> person.address.valid? 
 => false
> person.address.errors.full_messages
 => ["City can't be blank"]

composed_of_validations adds the autosave option to automatically save when a new value is assigned.

class Person < ActiveRecord::Base
  composed_of :address, mapping: [%w(address_street street), 
                                  %w(address_city city), 
                                  %w(address_state state), 
                                  %w(address_zip zip)],
                        allow_nil: true,
                        autosave: true
end

> person.persisted? 
 => false
> person.address = nil
 => nil
> person.address.persisted? 
 => true

Contributing

  1. Fork it ( https://cagit.careerbuilder.com/zwelch/composed_of_validations/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 a new Pull Request

composed_of_validations's People

Contributors

zacharywelch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.