fakturan_nu 1.1.4 → 1.1.5

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: 5501ee1e60ab587b727359c3bd163ea3fe3c8442
4
- data.tar.gz: 86a6c09815b128c03dc479b33e46655e67eb9fb6
3
+ metadata.gz: 6dafebdb78acba0e505fbe6832ca7ba96da38b30
4
+ data.tar.gz: 1ff884a250089a2fe0b7c59d96d9aac9819fcf71
5
5
  SHA512:
6
- metadata.gz: 6e4e9a54718fd57b108e7b8f7e6697e2b62aa70d9ec2e26e19fe1c0c87d25d0a292661a01aefde065cb6743225f8444a20711b12d916d88112be5a80407e3e41
7
- data.tar.gz: e838c643cba76d590bc423f6976011d556ea7cdf0de864c53d1c26d0011eeca31dc004c3177bcb059b118087b25f0574aa69081ac50f58429e5d46365d0c073c
6
+ metadata.gz: c69f27084c5484188782861526bd1c3959900de013e57a18366fa6b132226a7dec4ad3640b37127b608aab19bf5efe4af1e754cf16214cdf4ecb6f420f3306f4
7
+ data.tar.gz: 3511e035cc2b28edb65ccccec8e45d952503ae07afaebff9b3591407c6e104f73f8795cfe2cfb5370025d7bb2b30fa4ff5c16f42e84229e766825c3e4bff0ec1
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.1.4'
5
- s.date = '2015-04-30'
4
+ s.version = '1.1.5'
5
+ s.date = '2015-05-08'
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']
@@ -1,5 +1,7 @@
1
1
  module Fakturan
2
2
  class Address < Base
3
3
  uri nil
4
+
5
+ attributes :name, :care_of, :street_address, :zip_code, :city, :country
4
6
  end
5
7
  end
@@ -34,8 +34,10 @@ module Fakturan
34
34
  # to allow for nested errors on associated objects
35
35
  def add_errors_to_model(errors_hash)
36
36
  errors_hash.each do |field, field_errors|
37
- ass_name, field_name = field.split(".").map(&:to_sym)
38
- field_name = ass_name unless field_name
37
+
38
+ path_parts = field.split(".").map(&:to_sym) # 'client.address.country' for example, usually only 1-2 levels though (but sometimes more)
39
+ ass_name = path_parts.first
40
+ field_name = path_parts.last # Will be the same as ass_name if only 1 level
39
41
 
40
42
  # The errors are for an associated object, and there is an associated object to put them on
41
43
  if (association = self.class.reflect_on_association(ass_name)) && !self.send(ass_name).blank?
@@ -51,22 +53,30 @@ module Fakturan
51
53
  end
52
54
  end
53
55
  else # It's a belongs_to or has_one
54
- # We add the error to the associated object
55
- self.send(ass_name).add_to_errors(field_name, field_errors)
56
- # and then we get the errors (with generated messages) and add them to
57
- # the parent (but without details, like nested_attributes works)
58
- # This only makes sense on belongs_to and has_one, since it's impossible
59
- # to know which object is refered to on has_many
60
- self.send(ass_name).errors.each do |attribute, message|
61
- attribute = "#{ass_name}.#{attribute}"
62
- errors[attribute] << message
63
- errors[attribute].uniq!
56
+ path_progression = [] # Will become: [['client'], ['client', 'address'], ['client', 'address', 'country']]
57
+ path_progression = path_parts[0..-2].map {|ass_key| path_progression += [ass_key]}
58
+
59
+ path_progression.each do |path_sub_parts|
60
+ full_field_path = path_parts[path_sub_parts.length-1..path_parts.length].join('.')
61
+ field_name = path_parts[1..path_sub_parts.length].last.to_s
62
+ association_target = path_sub_parts.inject(self, :send)
63
+ association_target_parent = path_sub_parts[0..-2].inject(self, :send)
64
+
65
+ # We add the error to the associated object
66
+ association_target.add_to_errors(field_name, field_errors)
67
+ # and then we get the errors (with generated messages) and add them to
68
+ # the parent (but without details, like nested_attributes works)
69
+ # This only makes sense on belongs_to and has_one, since it's impossible
70
+ # to know which object is refered to on has_many
71
+ association_target.errors.each do |attribute, message|
72
+ association_target_parent.errors[full_field_path] << message
73
+ association_target_parent.errors[full_field_path].uniq!
74
+ end
64
75
  end
65
76
  end
66
77
  else
67
78
  self.add_to_errors(field_name.to_sym, field_errors)
68
79
  end
69
-
70
80
  end
71
81
  end
72
82
 
@@ -2,7 +2,7 @@ module Fakturan
2
2
  class Client < Base
3
3
  uri "clients/(:id)"
4
4
  has_many :invoices, uri: "clients/:id/invoices", foreign_key: 'id'
5
- has_one :address
5
+ has_one :address, uri: nil
6
6
 
7
7
  accepts_nested_attributes_for :address
8
8
 
@@ -71,14 +71,14 @@ module Fakturan
71
71
  end
72
72
 
73
73
  def test_multiple_validation_errors_on_has_many
74
- stub_api_request(:post, '/trees').to_return(body: {errors: {apples: [{"0" => {"apples.colour" => [{error: :too_short, count:4}], "apples.shape" => [{error: :taken, value: "square"}]}}]}}.to_json, status: 422)
74
+ stub_api_request(:post, '/trees').to_return(body: {errors: {apples: [{"0" => {:"apples.colour" => [{error: :too_short, count:4}], :"apples.shape" => [{error: :taken, value: "square"}]}}]}}.to_json, status: 422)
75
75
  tree = Fakturan::Tree.new({"apples"=>[{"colour"=>"", "shape"=>"square"}]})
76
76
  assert_equal false, tree.save
77
77
  assert_equal ({:colour=>[{:error=>:too_short, :count=>4}], :shape=>[{:error=>:taken, :value=>"square"}]}), tree.apples.first.errors.details
78
78
  end
79
79
 
80
80
  def test_validation_errors_on_associations_when_created_through_params
81
- stub_api_request(:post, '/trees').to_return(body: {errors: {apples: [{"0" => {"apples.colour" => [{error: :blank}]}}], "crown.fluffyness" => [{error: :invalid}]}}.to_json, status: 422)
81
+ stub_api_request(:post, '/trees').to_return(body: {errors: {apples: [{"0" => {:"apples.colour" => [{error: :blank}]}}], :"crown.fluffyness" => [{error: :invalid}]}}.to_json, status: 422)
82
82
  tree = Fakturan::Tree.new({"apples"=>[{"something"=>""}], "crown"=>{"fluffyness"=>""}})
83
83
  assert_equal false, tree.save
84
84
  assert_equal ({:colour=>[{:error=>:blank}]}), tree.apples.first.errors.details
@@ -95,7 +95,7 @@ module Fakturan
95
95
  end
96
96
 
97
97
  def test_save_fails_when_has_many_validation_fails
98
- stub_api_request(:post, '/invoices').to_return(body: {errors: {rows: [{"0" => {"rows.product_code" => [{error: :too_long, count:30}]}}] }}.to_json, status: 422)
98
+ stub_api_request(:post, '/invoices').to_return(body: {errors: {rows: [{"0" => {:"rows.product_code" => [{error: :too_long, count:30}]}}] }}.to_json, status: 422)
99
99
  invoice = Fakturan::Invoice.new(client: { company: "Acme inc" }, rows: [{product_code: '1234567890123456789012345678901'}])
100
100
  assert_equal false, invoice.save
101
101
  assert_equal ({:product_code=>[{:error=>:too_long, :count=>30}]}), invoice.rows[0].errors.details
@@ -103,7 +103,7 @@ module Fakturan
103
103
  end
104
104
 
105
105
  def test_validation_errors_on_associations
106
- 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)
106
+ 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)
107
107
  invoice = Fakturan::Invoice.new(client: {}, rows: [{product_code: '1234567890123456789012345678901'}, {product_code: '1'}, {product_code: '1234567890123456789012345678901'}])
108
108
  invoice.save
109
109
  assert_equal "Client company can't be blank", invoice.errors.to_a.last
@@ -114,6 +114,15 @@ module Fakturan
114
114
  assert_equal ({:company=>[{:error=>:blank}]}), invoice.client.errors.details
115
115
  end
116
116
 
117
+ def test_validation_errors_on_nested_associated_objects
118
+ stub_api_request(:post, '/invoices').to_return(body: {errors: {date: [{error: :blank},{error: :invalid}], :"client.address.country" => [{ error: :blank}]}}.to_json, status: 422)
119
+ invoice = Fakturan::Invoice.new(client: { address: { street_address: 'Some street' } })
120
+ assert_equal false, invoice.save
121
+ assert_equal "Client address country can't be blank", invoice.errors.to_a.last
122
+ assert_equal ({date: [{error: :blank}, {error: :invalid}]}), invoice.errors.details
123
+ assert_equal ({:date => ["can't be blank", "is invalid"], :"client.address.country" => ["can't be blank"]}), invoice.errors.messages
124
+ end
125
+
117
126
  def test_raise_resource_invalid
118
127
  stub_api_request(:post, '/invoices').to_return(body: { errors: { client_id: [{ error: :blank}], date: [{ error: :blank}, { error: :invalid}] }}.to_json, status: 422)
119
128
  error = assert_raises Fakturan::Error::ResourceInvalid do
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.1.4
4
+ version: 1.1.5
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-30 00:00:00.000000000 Z
11
+ date: 2015-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spyke