fakturan_nu 1.0.2 → 1.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: 1ab202040f38b9432b6b261c32a0aa67f89d38d2
4
- data.tar.gz: 4fea1eda6ee8ecd18f6e34a1d2fd8c44f83896b0
3
+ metadata.gz: bb8aaece005780b6c5dcb3c76e27099268892c1e
4
+ data.tar.gz: 2991573e89a82de2fb2d211aff1a2e9693ebdf1c
5
5
  SHA512:
6
- metadata.gz: 65be8dbc83508e8944cf56db81c757e96e241c65097834949858f9a612b54d3920bb719823043d7ca9f2f120366aee3662ed594a5e5a981e2f3e1de62d3fe079
7
- data.tar.gz: c6863d2a83780936fa5886ddde654cfa31de7a6866c7865d1a043de9dcc2f2fb4f7b2f06aaa3ff087618395876665fefba9c07553804a7f8343a0539442a71d2
6
+ metadata.gz: eff15b4eb0b0c6644ddb5027530b6e2cc3c34282e45a5d6a1d639ba44fecd2a3fe52c4c8b9beeada2f5f9893033f30d32913dab4bc3fdd96ff822c5da124ee4f
7
+ data.tar.gz: f574a12863ac0225d212d4efef0b98678d7d0fb591ce988aba66e6f8bdd3a3725574512437b0217d2e01fb100d6f839c8f1398430f5d75041f515346e6287b35
data/fakturan_nu.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'fakturan_nu'
4
- s.version = '1.0.2'
5
- s.date = '2015-04-13'
4
+ s.version = '1.1.0'
5
+ s.date = '2015-04-14'
6
6
  s.summary = 'A ruby client for the Fakturan.nu - API'
7
7
  s.description = 'A ruby client for the Fakturan.nu - API. Fakturan.nu is a webbapp for billing.'
8
8
  s.authors = ['Jonathan Bourque Olivegren']
@@ -37,9 +37,12 @@ module Fakturan
37
37
  ass_name, field_name = field.split(".").map(&:to_sym)
38
38
  field_name = ass_name unless field_name
39
39
 
40
- if association = self.class.reflect_on_association(ass_name) # The errors are for an associatied object
40
+ # The errors are for an associated object, and there is an associated object to put them on
41
+ if (association = self.class.reflect_on_association(ass_name)) && !self.send(ass_name).blank?
41
42
  if association.type == Spyke::Associations::HasMany
42
- field_errors.each do |new_error_hash_with_index|
43
+ # We need to add one error to our "base" object so that it's not valid
44
+ self.add_to_errors(field_name.to_sym, [{error: :invalid}])
45
+ field_errors.each do |new_error_hash_with_index| # new_error_hash_OR_error_type ("blank") on presence of has_many
43
46
  new_error_hash_with_index.each do |index, inner_errors_hash|
44
47
  error_attribute = inner_errors_hash.keys.first.split('.').last.to_sym
45
48
  self.send(ass_name)[index.to_i].add_to_errors(error_attribute, inner_errors_hash.values.last)
@@ -1,4 +1,6 @@
1
1
  require 'test_helper'
2
+ require 'fixtures'
3
+
2
4
  VCR.turn_off!
3
5
 
4
6
  module Fakturan
@@ -68,6 +70,24 @@ module Fakturan
68
70
  assert_equal 400, error.status
69
71
  end
70
72
 
73
+ def test_validation_errors_on_blank_associated_objects
74
+ stub_api_request(:post, '/trees').to_return(body: {errors: {apples: [{error: :blank}], crown: [{error: :blank}]}}.to_json, status: 422)
75
+ a = Fakturan::Tree.new()
76
+ assert_equal false, a.save
77
+ assert_equal (["Apples can't be blank", "Crown can't be blank"]), a.errors.to_a
78
+ assert_equal ({:apples=>[{:error=>:blank}], :crown=>[{:error=>:blank}]}), a.errors.details
79
+ end
80
+
81
+ def test_save_fails_when_has_many_validation_fails
82
+ stub_api_request(:post, '/invoices').to_return(body: {errors: {rows: [{"0" => {"rows.product_code" => [{error: :too_long, count:30}]}}] }}.to_json, status: 422)
83
+
84
+ invoice = Fakturan::Invoice.new(client: { company: "Acme inc" }, rows: [{product_code: '1234567890123456789012345678901'}])
85
+
86
+ assert_equal false, invoice.save
87
+ assert_equal ({:product_code=>[{:error=>:too_long, :count=>30}]}), invoice.rows[0].errors.details
88
+ assert_equal "Rows is invalid", invoice.errors.to_a.first
89
+ end
90
+
71
91
  def test_validation_errors_on_associations
72
92
  stub_api_request(:post, '/invoices').to_return(body: {errors: {date: [{error: :blank},{error: :invalid}], rows: [{"0" => {"rows.product_code" => [{error: :too_long, count:30}]}}, {"2" => {"rows.product_code" => [{error: :too_long, count:30}]}}], "client.company" => [{ error: :blank}]}}.to_json, status: 422)
73
93
 
@@ -75,7 +95,7 @@ module Fakturan
75
95
  invoice.save
76
96
 
77
97
  assert_equal "Client company can't be blank", invoice.errors.to_a.last
78
- assert_equal ({date: [{error: :blank}, {error: :invalid}]}), invoice.errors.details
98
+ assert_equal ({date: [{error: :blank}, {error: :invalid}], :rows=>[{:error=>:invalid}]}), invoice.errors.details
79
99
  assert_equal ({:product_code=>[{:error=>:too_long, :count=>30}]}), invoice.rows[0].errors.details
80
100
  # This one checks that our index / ordering has succeded and we have put the errors on the correct object
81
101
  assert_equal ({:product_code=>[{:error=>:too_long, :count=>30}]}), invoice.rows[2].errors.details
data/test/fixtures.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Fakturan
2
+ class Tree < Base
3
+ uri 'trees/(:id)'
4
+ has_many :apples, uri: nil
5
+ has_one :crown, uri: nil
6
+
7
+ accepts_nested_attributes_for :apples, :crown
8
+ end
9
+
10
+ class Apple < Base
11
+ attributes :colour
12
+ end
13
+
14
+ class Crown < Base
15
+ attributes :fluffyness
16
+ end
17
+ end
data/test/models_test.rb CHANGED
@@ -18,7 +18,7 @@ module Fakturan
18
18
  i_suck_and_my_tests_are_order_dependent!
19
19
 
20
20
  def good_invoice_params
21
- @good_invoice_params ||= { client: { company: 'Imagine it AB' }, date: Date.today, address: { name: "Imagine it AB", street_address: "Tage Erlandergatan 4" } }
21
+ @good_invoice_params ||= { client: { company: 'Imagine it AB' }, date: "2015-04-07".to_date, address: { name: "Imagine it AB", street_address: "Tage Erlandergatan 4" } }
22
22
  end
23
23
 
24
24
  def get_good_invoice
@@ -121,7 +121,7 @@ module Fakturan
121
121
 
122
122
  def test_should_be_able_to_set_params_on_association
123
123
  invoice = Fakturan::Invoice.new
124
- invoice.date = Date.today.to_s
124
+ invoice.date = "2015-04-07"
125
125
  invoice.build_client
126
126
  invoice.client.company = 'Imagine it AB'
127
127
  assert invoice.save
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakturan_nu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bourque Olivegren
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spyke
@@ -202,6 +202,7 @@ files:
202
202
  - lib/fakturan_nu/spyke_extensions.rb
203
203
  - test/basics_test.rb
204
204
  - test/exceptions_test.rb
205
+ - test/fixtures.rb
205
206
  - test/models_test.rb
206
207
  - test/test_helper.rb
207
208
  - test/vcr_cassettes/models.yml