Skip to content

Commit

Permalink
Bump v3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Feb 12, 2018
1 parent 23ad21e commit 4cea655
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.1.0] [2018-02-12]
- support `ActiveRecord::Relation` in models (nepalez)

```ruby
param :user, model: User.where(active: true)
```

## [3.0.0] [2018-02-01]

### Changed
Expand Down Expand Up @@ -38,3 +45,4 @@ First public release
[1.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v0.0.3...v1.0.0
[2.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v1.0.0...v2.0.0
[3.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v2.0.0...v3.0.0
[3.1.0]: https://github.com/nepalez/dry-initializer-rails/compare/v3.0.0...v3.1.0
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,43 @@ class CreateOrder
extend Dry::Initializer

# Params and options
param :customer, model: 'Customer' # use either a name
option :product, model: Product # or a class
param :customer, model: 'Customer' # use either a name
option :product, model: Product # or a class
option :coupon, model: Coupon.where(active: true) # or relation

def call
Order.create customer: customer, product: product
Order.create customer: customer, product: product, coupon: coupon
end
end
```

Now you can assign values as pre-initialized model instances:

```ruby
customer = Customer.find(1)
product = Product.find(2)

order = CreateOrder.new(customer, product: product).call
order.customer # => <Customer @id=1 ...>
order.product # => <Product @id=2 ...>
customer = Customer.create(id: 1)
product = Product.create(id: 2)
coupon = Coupon.create(id: 3, active: true)

order = CreateOrder.new(customer, product: product, coupon: coupon).call
order.customer # => #<Customer @id=1 ...>
order.product # => #<Product @id=2 ...>
order.coupon # => #<Coupon @id=3 ...>
```

...or their ids:

```ruby
order = CreateOrder.new(1, product: 2).call
order.customer # => <Customer @id=1 ...>
order.product # => <Product @id=2 ...>
order = CreateOrder.new(1, product: 2, coupon: 3).call
order.customer # => #<Customer @id=1 ...>
order.product # => #<Product @id=2 ...>
order.coupon # => #<Coupon @id=3 ...>
```

The instance is envoked using method `find_by(id: ...)`.
With wrong ids `nil` values are assigned to a corresponding params and options:
The instance is envoked using method `find_by!(id: ...)`.

```ruby
order = CreateOrder.new(0, product: 0).call
order.customer # => nil
order.product # => nil
order = CreateOrder.new(1, product: 2, coupon: 4).call
# raises #<ActiveRecord::RecordNotFound>
```

You can specify custom `key` for searching model instance:
Expand Down
2 changes: 1 addition & 1 deletion dry-initializer-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |gem|
gem.name = "dry-initializer-rails"
gem.version = "3.0.0"
gem.version = "3.1.0"
gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
gem.email = ["andrew.kozin@gmail.com"]
gem.homepage = "https://github.com/nepalez/dry-initializer-rails"
Expand Down

0 comments on commit 4cea655

Please sign in to comment.