dry-initializer-rails 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c6808d3a56d819ed0d0f450cf8d866e69cdcf0c
4
- data.tar.gz: f7f9480365f3b68b1503eafbe8b57ecd6314bc1e
3
+ metadata.gz: be08cdd7d51330fca13c8f87074fb211b8461710
4
+ data.tar.gz: 357ac57f1fd962cb2140ac642da75f24c809e66b
5
5
  SHA512:
6
- metadata.gz: 605829836cc66781e67c0c2d2b4c299d5a4885ab9e9c0facb4f3e29d8c6e8a9c49e312ea661bf189f42d6e9374f4777ccb252f3d3ba1f86f5ef21443620428a9
7
- data.tar.gz: e3801f23bddecf3dbb6959ddca18f0b4b52f5de3bb7b11867da5976a700872695e1ae842d885ce79f6cd3c1f62c65cf260b5ca58f4d560db433c36d67b206cd7
6
+ metadata.gz: 480baa49814dc169dace0b975857b3faa8078c970dc22faf35f1489b1de1ce9165fe35ac73517118c23451e52520c566e55c40881284bef08cb13215289452e2
7
+ data.tar.gz: ff9e9fcac48cda861a02755d8e50ccc78575642b92065e50d6523b7b306b28c427f2547d928830ed1c2eac70ca0823f31de3ac9ce46ae4c3c9ac4c11667c4339
data/CHANGELOG.md CHANGED
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
+ ## [3.1.0] [2018-02-12]
9
+ - support `ActiveRecord::Relation` in models (nepalez)
10
+
11
+ ```ruby
12
+ param :user, model: User.where(active: true)
13
+ ```
14
+
8
15
  ## [3.0.0] [2018-02-01]
9
16
 
10
17
  ### Changed
@@ -38,3 +45,4 @@ First public release
38
45
  [1.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v0.0.3...v1.0.0
39
46
  [2.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v1.0.0...v2.0.0
40
47
  [3.0.0]: https://github.com/nepalez/dry-initializer-rails/compare/v2.0.0...v3.0.0
48
+ [3.1.0]: https://github.com/nepalez/dry-initializer-rails/compare/v3.0.0...v3.1.0
data/README.md CHANGED
@@ -52,11 +52,12 @@ class CreateOrder
52
52
  extend Dry::Initializer
53
53
 
54
54
  # Params and options
55
- param :customer, model: 'Customer' # use either a name
56
- option :product, model: Product # or a class
55
+ param :customer, model: 'Customer' # use either a name
56
+ option :product, model: Product # or a class
57
+ option :coupon, model: Coupon.where(active: true) # or relation
57
58
 
58
59
  def call
59
- Order.create customer: customer, product: product
60
+ Order.create customer: customer, product: product, coupon: coupon
60
61
  end
61
62
  end
62
63
  ```
@@ -64,29 +65,30 @@ end
64
65
  Now you can assign values as pre-initialized model instances:
65
66
 
66
67
  ```ruby
67
- customer = Customer.find(1)
68
- product = Product.find(2)
69
-
70
- order = CreateOrder.new(customer, product: product).call
71
- order.customer # => <Customer @id=1 ...>
72
- order.product # => <Product @id=2 ...>
68
+ customer = Customer.create(id: 1)
69
+ product = Product.create(id: 2)
70
+ coupon = Coupon.create(id: 3, active: true)
71
+
72
+ order = CreateOrder.new(customer, product: product, coupon: coupon).call
73
+ order.customer # => #<Customer @id=1 ...>
74
+ order.product # => #<Product @id=2 ...>
75
+ order.coupon # => #<Coupon @id=3 ...>
73
76
  ```
74
77
 
75
78
  ...or their ids:
76
79
 
77
80
  ```ruby
78
- order = CreateOrder.new(1, product: 2).call
79
- order.customer # => <Customer @id=1 ...>
80
- order.product # => <Product @id=2 ...>
81
+ order = CreateOrder.new(1, product: 2, coupon: 3).call
82
+ order.customer # => #<Customer @id=1 ...>
83
+ order.product # => #<Product @id=2 ...>
84
+ order.coupon # => #<Coupon @id=3 ...>
81
85
  ```
82
86
 
83
- The instance is envoked using method `find_by(id: ...)`.
84
- With wrong ids `nil` values are assigned to a corresponding params and options:
87
+ The instance is envoked using method `find_by!(id: ...)`.
85
88
 
86
89
  ```ruby
87
- order = CreateOrder.new(0, product: 0).call
88
- order.customer # => nil
89
- order.product # => nil
90
+ order = CreateOrder.new(1, product: 2, coupon: 4).call
91
+ # raises #<ActiveRecord::RecordNotFound>
90
92
  ```
91
93
 
92
94
  You can specify custom `key` for searching model instance:
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer-rails"
3
- gem.version = "3.0.0"
3
+ gem.version = "3.1.0"
4
4
  gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
5
5
  gem.email = ["andrew.kozin@gmail.com"]
6
6
  gem.homepage = "https://github.com/nepalez/dry-initializer-rails"
@@ -5,9 +5,11 @@ require "rails"
5
5
  rails_dispatcher = lambda do |model: nil, find_by: :id, **options|
6
6
  return options unless model
7
7
 
8
- model = model.constantize if model.is_a? String
8
+ model = eval(model) if model.is_a? String
9
+ klass = model.is_a?(ActiveRecord::Relation) ? model.klass : model
10
+
9
11
  coercer = lambda do |value|
10
- return value if value.nil? || value.instance_of?(model)
12
+ return value if value.nil? || value.is_a?(klass)
11
13
  model.find_by! find_by => value
12
14
  end
13
15
 
@@ -0,0 +1,26 @@
1
+ describe "modelling relation" do
2
+ before do
3
+ class Test::Order
4
+ extend Dry::Initializer
5
+
6
+ param :user, model: "User.where(name: 'Dude')"
7
+ option :product, model: Item.where(name: "The thing")
8
+ end
9
+ end
10
+
11
+ let!(:user) { User.create id: 9, name: "Dude" }
12
+ let!(:item) { Item.create id: 1, name: "The thing" }
13
+ let!(:other) { Item.create id: 2, name: "Another thing" }
14
+
15
+ it "uses relation" do
16
+ subject = Test::Order.new(9, product: 1)
17
+
18
+ expect(subject.user).to eq user
19
+ expect(subject.product).to eq item
20
+ end
21
+
22
+ it "raises when record not found for the relation" do
23
+ expect { Test::Order.new(9, product: 2) }
24
+ .to raise_error(ActiveRecord::RecordNotFound)
25
+ end
26
+ end
@@ -11,6 +11,7 @@
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
13
  ActiveRecord::Schema.define(version: 20160509134900) do
14
+
14
15
  create_table "items", force: :cascade do |t|
15
16
  t.string "name"
16
17
  t.index ["name"], name: "index_items_on_name"
@@ -20,4 +21,5 @@ ActiveRecord::Schema.define(version: 20160509134900) do
20
21
  t.string "name"
21
22
  t.index ["name"], name: "index_users_on_name"
22
23
  end
24
+
23
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-initializer-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Kochnev (marshall-lee)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-02-03 00:00:00.000000000 Z
12
+ date: 2018-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -137,6 +137,7 @@ files:
137
137
  - spec/dry/initializer/assignment_by_model_spec.rb
138
138
  - spec/dry/initializer/assignment_by_nil_spec.rb
139
139
  - spec/dry/initializer/base_syntax_spec.rb
140
+ - spec/dry/initializer/modelling_relation_spec.rb
140
141
  - spec/dummy/Rakefile
141
142
  - spec/dummy/app/models/item.rb
142
143
  - spec/dummy/app/models/user.rb
@@ -178,6 +179,7 @@ test_files:
178
179
  - spec/dry/initializer/assignment_by_model_spec.rb
179
180
  - spec/dry/initializer/assignment_by_nil_spec.rb
180
181
  - spec/dry/initializer/base_syntax_spec.rb
182
+ - spec/dry/initializer/modelling_relation_spec.rb
181
183
  - spec/dummy/Rakefile
182
184
  - spec/dummy/app/models/item.rb
183
185
  - spec/dummy/app/models/user.rb