From 14230ccab85b325359b9b21b604cb4a41a56bf3c Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 17:46:39 -0500 Subject: [PATCH 01/54] first test --- 01_inventory/spec/inventory_spec.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index cf987cb..af5d1b6 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -1,9 +1,24 @@ require "rspec" RSpec.describe "Inventory" do + class FakeStore + def initialize(records) + end + end + describe "shows the articles" do - it "with name" + it "with name" do + store = store_with([{name: "Camisa 1"}, {name: "Gorra 1"}]) + inventory = Inventory.new(store) + articles = inventory.articles_list + expect(articles.map(&:name)).to eq ["Camisa 1", "Gorra 1"] + end + it "with code" it "with quantity" + + def store_with(records) + FakeStore.new(records) + end end end From d0356d1ca713eb4941397e3bf7f92d4c1aa171b2 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 17:57:58 -0500 Subject: [PATCH 02/54] enough code to make the first test pass --- 01_inventory/spec/inventory_spec.rb | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index af5d1b6..78234c1 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -1,9 +1,41 @@ require "rspec" +class Inventory + def initialize(store) + @store = store + end + + def articles_list + store.all_articles.map { |raw| Article.new(raw) } + end + + private + + attr_reader :store +end + +class Article + attr_reader :name + + def initialize(data) + @name = data[:name] + end +end + + RSpec.describe "Inventory" do class FakeStore def initialize(records) + @records = records end + + def all_articles + records + end + + private + + attr_reader :records end describe "shows the articles" do From 663cb24dbc3667369f01a2e9ebe1a310f3d93c21 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 17:59:04 -0500 Subject: [PATCH 03/54] second test --- 01_inventory/spec/inventory_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 78234c1..9bb111a 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -46,7 +46,13 @@ def all_articles expect(articles.map(&:name)).to eq ["Camisa 1", "Gorra 1"] end - it "with code" + it "with code" do + store = store_with([{code: "c1"}, {code: "g1"}]) + inventory = Inventory.new(store) + articles = inventory.articles_list + expect(articles.map(&:code)).to eq ["c1", "g1"] + end + it "with quantity" def store_with(records) From 5d509d800b1a4c19ee4ca3e2a6d2029d6a13f3de Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:01:02 -0500 Subject: [PATCH 04/54] second test is green --- 01_inventory/spec/inventory_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 9bb111a..fd7027f 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -15,10 +15,11 @@ def articles_list end class Article - attr_reader :name + attr_reader :name, :code def initialize(data) @name = data[:name] + @code = data[:code] end end From b4e2beeb9d6f49979b865d01cef873fc7efe35b8 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:02:08 -0500 Subject: [PATCH 05/54] third test --- 01_inventory/spec/inventory_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index fd7027f..82e63a1 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -54,7 +54,12 @@ def all_articles expect(articles.map(&:code)).to eq ["c1", "g1"] end - it "with quantity" + it "with quantity" do + store = store_with([{quantity: 10}, {quantity: 35}]) + inventory = Inventory.new(store) + articles = inventory.articles_list + expect(articles.map(&:quantity)).to eq [10, 35] + end def store_with(records) FakeStore.new(records) From 21c48d5c85940a532a4b3da287b2f7f548580631 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:02:46 -0500 Subject: [PATCH 06/54] third test is green --- 01_inventory/spec/inventory_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 82e63a1..fdd7422 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -15,11 +15,12 @@ def articles_list end class Article - attr_reader :name, :code + attr_reader :name, :code, :quantity def initialize(data) @name = data[:name] @code = data[:code] + @quantity = data[:quantity] end end From 52b09b257a1439c0af7b5234cf4fbd44ca503437 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:04:46 -0500 Subject: [PATCH 07/54] small refactor --- 01_inventory/spec/inventory_spec.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index fdd7422..4ab4463 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -41,24 +41,26 @@ def all_articles end describe "shows the articles" do + attr_reader :articles + + before do + inventory = Inventory.new(store_with([ + {name: "Camisa 1", code: "c1", quantity: 10}, + {name: "Gorra 1", code: "g1", quantity: 35} + ])) + + @articles = inventory.articles_list + end + it "with name" do - store = store_with([{name: "Camisa 1"}, {name: "Gorra 1"}]) - inventory = Inventory.new(store) - articles = inventory.articles_list expect(articles.map(&:name)).to eq ["Camisa 1", "Gorra 1"] end it "with code" do - store = store_with([{code: "c1"}, {code: "g1"}]) - inventory = Inventory.new(store) - articles = inventory.articles_list expect(articles.map(&:code)).to eq ["c1", "g1"] end it "with quantity" do - store = store_with([{quantity: 10}, {quantity: 35}]) - inventory = Inventory.new(store) - articles = inventory.articles_list expect(articles.map(&:quantity)).to eq [10, 35] end From 4fa4321ae1668362fcd3e2319e88468ef552c06b Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:06:27 -0500 Subject: [PATCH 08/54] moves inventory to its own file --- 01_inventory/inventory.rb | 23 +++++++++++++++++++++++ 01_inventory/spec/inventory_spec.rb | 26 +------------------------- 2 files changed, 24 insertions(+), 25 deletions(-) create mode 100644 01_inventory/inventory.rb diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb new file mode 100644 index 0000000..87cd4a3 --- /dev/null +++ b/01_inventory/inventory.rb @@ -0,0 +1,23 @@ +class Inventory + def initialize(store) + @store = store + end + + def articles_list + store.all_articles.map { |raw| Article.new(raw) } + end + + private + + attr_reader :store +end + +class Article + attr_reader :name, :code, :quantity + + def initialize(data) + @name = data[:name] + @code = data[:code] + @quantity = data[:quantity] + end +end diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 4ab4463..19997b6 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -1,29 +1,5 @@ require "rspec" - -class Inventory - def initialize(store) - @store = store - end - - def articles_list - store.all_articles.map { |raw| Article.new(raw) } - end - - private - - attr_reader :store -end - -class Article - attr_reader :name, :code, :quantity - - def initialize(data) - @name = data[:name] - @code = data[:code] - @quantity = data[:quantity] - end -end - +require_relative "../inventory" RSpec.describe "Inventory" do class FakeStore From 05eb10295d4a1cd608818399e47682126cf7c54b Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:25:57 -0500 Subject: [PATCH 09/54] rendering with sinatra --- 01_inventory/app.rb | 16 +++++++++++++ 01_inventory/views/index.erb | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 01_inventory/app.rb create mode 100644 01_inventory/views/index.erb diff --git a/01_inventory/app.rb b/01_inventory/app.rb new file mode 100644 index 0000000..9fe6aa7 --- /dev/null +++ b/01_inventory/app.rb @@ -0,0 +1,16 @@ +require "sinatra" +require_relative "inventory" + +class Store + def all_articles + [{name: "Camisa 1", code: "c1", quantity: 10}, + {name: "Gorra 1", code: "g1", quantity: 35}] + end +end + +get '/' do + store = Store.new + inventory = Inventory.new(store) + @articles = inventory.articles_list + erb :index +end diff --git a/01_inventory/views/index.erb b/01_inventory/views/index.erb new file mode 100644 index 0000000..8b480ff --- /dev/null +++ b/01_inventory/views/index.erb @@ -0,0 +1,46 @@ + + + + + + + + Inventario + + + + + + +
+ + + + + + + + + + + <% @articles.each do |article| %> + + + + + + <% end %> + +
NombreCodigoCantidad
<%= article.name %><%= article.code %><%= article.quantity %>
+
+ + From 1dbd4965ae5711d2b782ade63aa188f0b01189ec Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:46:17 -0500 Subject: [PATCH 10/54] view for new article --- 01_inventory/app.rb | 4 ++ 01_inventory/views/index.erb | 68 ++++++++++-------------------- 01_inventory/views/layout.erb | 26 ++++++++++++ 01_inventory/views/new_article.erb | 23 ++++++++++ 4 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 01_inventory/views/layout.erb create mode 100644 01_inventory/views/new_article.erb diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 9fe6aa7..1f5fe44 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -14,3 +14,7 @@ def all_articles @articles = inventory.articles_list erb :index end + +get '/articles/new' do + erb :new_article +end diff --git a/01_inventory/views/index.erb b/01_inventory/views/index.erb index 8b480ff..6daff02 100644 --- a/01_inventory/views/index.erb +++ b/01_inventory/views/index.erb @@ -1,46 +1,22 @@ - - - - - - - - Inventario - - - - - - -
- - - - - - - - - - - <% @articles.each do |article| %> - - - - - - <% end %> - -
NombreCodigoCantidad
<%= article.name %><%= article.code %><%= article.quantity %>
-
- - + +Agregar artículo + + + + + + + + + + <% @articles.each do |article| %> + + + + + + <% end %> + +
NombreCodigoCantidad
<%= article.name %><%= article.code %><%= article.quantity %>
diff --git a/01_inventory/views/layout.erb b/01_inventory/views/layout.erb new file mode 100644 index 0000000..7bdfa17 --- /dev/null +++ b/01_inventory/views/layout.erb @@ -0,0 +1,26 @@ + + + + + + + + Inventario + + + + + + +
+ <%= yield %> +
+ + diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb new file mode 100644 index 0000000..0ac77db --- /dev/null +++ b/01_inventory/views/new_article.erb @@ -0,0 +1,23 @@ + + +
+
+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
From 4dec278683eeaf9246dcedcb5ccbd0e00f5d990e Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:51:06 -0500 Subject: [PATCH 11/54] inspect params --- 01_inventory/app.rb | 4 ++++ 01_inventory/views/new_article.erb | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 1f5fe44..5219677 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -18,3 +18,7 @@ def all_articles get '/articles/new' do erb :new_article end + +post '/articles' do + raise params.inspect +end diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index 0ac77db..ed2a385 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -7,15 +7,15 @@
- +
- +
- +
From f3dc99ae7f693787ef43b403fabb37821f2bf5d0 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 18:59:15 -0500 Subject: [PATCH 12/54] expect and :add_inventory method --- 01_inventory/app.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 5219677..80c440d 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -8,9 +8,10 @@ def all_articles end end +store = Store.new +inventory = Inventory.new(store) + get '/' do - store = Store.new - inventory = Inventory.new(store) @articles = inventory.articles_list erb :index end @@ -20,5 +21,6 @@ def all_articles end post '/articles' do - raise params.inspect + inventory.add_article(params) + redirect "/" end From 4bd664571450462566551bab28e7730c63490b02 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:04:08 -0500 Subject: [PATCH 13/54] problem with the hash keys --- 01_inventory/inventory.rb | 4 ++++ 01_inventory/spec/inventory_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 87cd4a3..80867eb 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -7,6 +7,10 @@ def articles_list store.all_articles.map { |raw| Article.new(raw) } end + def add_article(params) + store.create(params) + end + private attr_reader :store diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 19997b6..1a5385e 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -39,9 +39,29 @@ def all_articles it "with quantity" do expect(articles.map(&:quantity)).to eq [10, 35] end + end + + describe "adds article" do + it "with name, code and quantity" do + store = store_with([]) + inventory = Inventory.new(store) + expect(inventory.articles_list).to be_empty + + expect(store).to receive(:create).with({ + name: "Camisa 1", + code: "c1", + quantity: "10" + }) - def store_with(records) - FakeStore.new(records) + inventory.add_article({ + "name" => "Camisa 1", + "code" => "c1", + "quantity" => "10" + }) end end + + def store_with(records) + FakeStore.new(records) + end end From 9907550fb861a8074f0eff0d28326396dd59f96b Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:06:24 -0500 Subject: [PATCH 14/54] expect string params --- 01_inventory/inventory.rb | 6 +++--- 01_inventory/spec/inventory_spec.rb | 20 ++++++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 80867eb..cd69be5 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -20,8 +20,8 @@ class Article attr_reader :name, :code, :quantity def initialize(data) - @name = data[:name] - @code = data[:code] - @quantity = data[:quantity] + @name = data["name"] + @code = data["code"] + @quantity = data["quantity"] end end diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 1a5385e..40d02bd 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -21,8 +21,8 @@ def all_articles before do inventory = Inventory.new(store_with([ - {name: "Camisa 1", code: "c1", quantity: 10}, - {name: "Gorra 1", code: "g1", quantity: 35} + {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, + {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} ])) @articles = inventory.articles_list @@ -45,19 +45,15 @@ def all_articles it "with name, code and quantity" do store = store_with([]) inventory = Inventory.new(store) - expect(inventory.articles_list).to be_empty - - expect(store).to receive(:create).with({ - name: "Camisa 1", - code: "c1", - quantity: "10" - }) - - inventory.add_article({ + params = { "name" => "Camisa 1", "code" => "c1", "quantity" => "10" - }) + } + + expect(inventory.articles_list).to be_empty + expect(store).to receive(:create).with(params) + inventory.add_article(params) end end From 19f7cfcad107a7bbfc043ca09695d796061c6a82 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:13:10 -0500 Subject: [PATCH 15/54] adds create method to Store --- 01_inventory/app.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 80c440d..ae7a188 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -1,14 +1,30 @@ require "sinatra" require_relative "inventory" +INITIAL_RECORDS = [ + {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, + {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} +] + class Store + def initialize(records) + @records = records + end + + def create(record) + records << record + end + def all_articles - [{name: "Camisa 1", code: "c1", quantity: 10}, - {name: "Gorra 1", code: "g1", quantity: 35}] + records end + + private + + attr_reader :records end -store = Store.new +store = Store.new(INITIAL_RECORDS) inventory = Inventory.new(store) get '/' do From 7a6d0ee0afea3896f4a7c1c1f789b270a59d0b18 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:17:50 -0500 Subject: [PATCH 16/54] refactor to introduce an InMemoryStore that will be used in the app and in tests --- 01_inventory/app.rb | 21 ++------------------- 01_inventory/in_memory_store.rb | 17 +++++++++++++++++ 01_inventory/spec/inventory_spec.rb | 17 ++--------------- 3 files changed, 21 insertions(+), 34 deletions(-) create mode 100644 01_inventory/in_memory_store.rb diff --git a/01_inventory/app.rb b/01_inventory/app.rb index ae7a188..b162d10 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -1,30 +1,13 @@ require "sinatra" require_relative "inventory" +require_relative "in_memory_store" INITIAL_RECORDS = [ {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} ] -class Store - def initialize(records) - @records = records - end - - def create(record) - records << record - end - - def all_articles - records - end - - private - - attr_reader :records -end - -store = Store.new(INITIAL_RECORDS) +store = InMemoryStore.new(INITIAL_RECORDS) inventory = Inventory.new(store) get '/' do diff --git a/01_inventory/in_memory_store.rb b/01_inventory/in_memory_store.rb new file mode 100644 index 0000000..367e73d --- /dev/null +++ b/01_inventory/in_memory_store.rb @@ -0,0 +1,17 @@ +class InMemoryStore + def initialize(records) + @records = records + end + + def create(record) + records << record + end + + def all_articles + records + end + + private + + attr_reader :records +end diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 40d02bd..a6d6dc2 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -1,21 +1,8 @@ require "rspec" require_relative "../inventory" +require_relative "../in_memory_store" RSpec.describe "Inventory" do - class FakeStore - def initialize(records) - @records = records - end - - def all_articles - records - end - - private - - attr_reader :records - end - describe "shows the articles" do attr_reader :articles @@ -58,6 +45,6 @@ def all_articles end def store_with(records) - FakeStore.new(records) + InMemoryStore.new(records) end end From 00b6294f7da6916eb1b2bec26e7bd7ed0cd1e96e Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:33:53 -0500 Subject: [PATCH 17/54] a little design to expect errors when an article is added --- 01_inventory/app.rb | 9 +++++++-- 01_inventory/inventory.rb | 7 +++++++ 01_inventory/spec/inventory_spec.rb | 14 +++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index b162d10..1403c11 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -20,6 +20,11 @@ end post '/articles' do - inventory.add_article(params) - redirect "/" + status = inventory.add_article(params) + + if status.success? + redirect "/" + else + erb :new_article + end end diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index cd69be5..c1d20ed 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -9,6 +9,7 @@ def articles_list def add_article(params) store.create(params) + AddArticleStatus.new end private @@ -16,6 +17,12 @@ def add_article(params) attr_reader :store end +class AddArticleStatus + def success? + true + end +end + class Article attr_reader :name, :code, :quantity diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index a6d6dc2..9277f2b 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -38,10 +38,22 @@ "quantity" => "10" } - expect(inventory.articles_list).to be_empty expect(store).to receive(:create).with(params) inventory.add_article(params) end + + it "returns success when params are good" do + store = store_with([]) + inventory = Inventory.new(store) + params = { + "name" => "Camisa 1", + "code" => "c1", + "quantity" => "10" + } + + status = inventory.add_article(params) + expect(status).to be_success + end end def store_with(records) From 94fdca0e096847540fddf45b7c20f222101a471f Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:36:12 -0500 Subject: [PATCH 18/54] sending the right status --- 01_inventory/inventory.rb | 17 +++++++++++++++-- 01_inventory/spec/inventory_spec.rb | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index c1d20ed..6343760 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -9,7 +9,12 @@ def articles_list def add_article(params) store.create(params) - AddArticleStatus.new + + if params["name"] + AddArticleStatus.new(:success) + else + AddArticleStatus.new(:error) + end end private @@ -18,9 +23,17 @@ def add_article(params) end class AddArticleStatus + def initialize(status) + @status = status + end + def success? - true + status == :success end + + private + + attr_reader :status end class Article diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 9277f2b..dfed618 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -54,6 +54,18 @@ status = inventory.add_article(params) expect(status).to be_success end + + it "validates presence of name" do + store = store_with([]) + inventory = Inventory.new(store) + params = { + "code" => "c1", + "quantity" => "10" + } + + status = inventory.add_article(params) + expect(status).not_to be_success + end end def store_with(records) From 72361a268ea3f97becd67287803fb72acb4e38c9 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:42:20 -0500 Subject: [PATCH 19/54] validates presence of name, but does not handle empty string --- 01_inventory/inventory.rb | 3 +- 01_inventory/spec/inventory_spec.rb | 46 ++++++++++++++--------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 6343760..26236e3 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -8,9 +8,8 @@ def articles_list end def add_article(params) - store.create(params) - if params["name"] + store.create(params) AddArticleStatus.new(:success) else AddArticleStatus.new(:error) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index dfed618..6e18553 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -29,42 +29,40 @@ end describe "adds article" do - it "with name, code and quantity" do - store = store_with([]) - inventory = Inventory.new(store) - params = { + attr_reader :store, :inventory, :good_params + + before do + @store = store_with([]) + @inventory = Inventory.new(store) + @good_params = { "name" => "Camisa 1", "code" => "c1", "quantity" => "10" } + end - expect(store).to receive(:create).with(params) - inventory.add_article(params) + it "with name, code and quantity" do + expect(store).to receive(:create).with(good_params) + inventory.add_article(good_params) end it "returns success when params are good" do - store = store_with([]) - inventory = Inventory.new(store) - params = { - "name" => "Camisa 1", - "code" => "c1", - "quantity" => "10" - } - - status = inventory.add_article(params) + status = inventory.add_article(good_params) expect(status).to be_success end - it "validates presence of name" do - store = store_with([]) - inventory = Inventory.new(store) - params = { - "code" => "c1", - "quantity" => "10" - } + describe "validates presence of name" do + it "on error does not return success" do + params = good_params.merge("name" => nil) + status = inventory.add_article(params) + expect(status).not_to be_success + end - status = inventory.add_article(params) - expect(status).not_to be_success + it "on error does not create the article" do + params = good_params.merge("name" => nil) + expect(store).not_to receive(:create).with(params) + inventory.add_article(params) + end end end From a6b849fccc60e3295c7b67fc49fad9d7314a8172 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:47:02 -0500 Subject: [PATCH 20/54] handle empty string --- 01_inventory/inventory.rb | 6 +++++- 01_inventory/spec/inventory_spec.rb | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 26236e3..78bb760 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -8,7 +8,7 @@ def articles_list end def add_article(params) - if params["name"] + if present? params["name"] store.create(params) AddArticleStatus.new(:success) else @@ -19,6 +19,10 @@ def add_article(params) private attr_reader :store + + def present?(attr) + !attr.nil? && attr != "" + end end class AddArticleStatus diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 6e18553..0366eee 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -52,16 +52,18 @@ end describe "validates presence of name" do - it "on error does not return success" do - params = good_params.merge("name" => nil) - status = inventory.add_article(params) - expect(status).not_to be_success - end + [nil, ""].each do |blank| + it "on error does not return success" do + params = good_params.merge("name" => blank) + status = inventory.add_article(params) + expect(status).not_to be_success + end - it "on error does not create the article" do - params = good_params.merge("name" => nil) - expect(store).not_to receive(:create).with(params) - inventory.add_article(params) + it "on error does not create the article" do + params = good_params.merge("name" => blank) + expect(store).not_to receive(:create).with(params) + inventory.add_article(params) + end end end end From cd6d827ff305f0748bc9113f9008eceebdee4027 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 19:56:59 -0500 Subject: [PATCH 21/54] structure design to show form values --- 01_inventory/app.rb | 2 ++ 01_inventory/views/new_article.erb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 1403c11..9da779f 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -16,6 +16,7 @@ end get '/articles/new' do + @form = inventory.new_article_form erb :new_article end @@ -25,6 +26,7 @@ if status.success? redirect "/" else + @form = status.form_with_errors erb :new_article end end diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index ed2a385..f60685c 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -7,15 +7,15 @@
- +
- +
- +
From ea44a315b179a59a70458e961a5e7a601c8f6f7f Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:20:47 -0500 Subject: [PATCH 22/54] new article form --- 01_inventory/inventory.rb | 8 ++++++++ 01_inventory/spec/inventory_spec.rb | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 78bb760..4abf657 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -7,6 +7,10 @@ def articles_list store.all_articles.map { |raw| Article.new(raw) } end + def new_article_form + ArticleForm.new + end + def add_article(params) if present? params["name"] store.create(params) @@ -25,6 +29,10 @@ def present?(attr) end end +class ArticleForm + attr_reader :name, :code, :quantity +end + class AddArticleStatus def initialize(status) @status = status diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 0366eee..d1fe29e 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -51,6 +51,13 @@ expect(status).to be_success end + it "has a new article form" do + form = inventory.new_article_form + expect(form.name).to be_nil + expect(form.code).to be_nil + expect(form.quantity).to be_nil + end + describe "validates presence of name" do [nil, ""].each do |blank| it "on error does not return success" do From 0cd7a7a4cff58dc16bdc781ab63139720be9b1ff Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:23:25 -0500 Subject: [PATCH 23/54] restore params --- 01_inventory/inventory.rb | 48 +++++++++++++++++++++-------- 01_inventory/spec/inventory_spec.rb | 9 ++++++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 4abf657..5ccd434 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -12,39 +12,61 @@ def new_article_form end def add_article(params) - if present? params["name"] + form = ArticleForm.new(params) + + if form.valid? store.create(params) - AddArticleStatus.new(:success) + SuccessArticleStatus.new else - AddArticleStatus.new(:error) + ErrorArticleStatus.new(form) end end private attr_reader :store +end + +class ArticleForm + attr_reader :name, :code, :quantity + + def initialize(data = {}) + @name = data["name"] + @code = data["code"] + self.quantity = data["quantity"] + end + + def valid? + present?(name) + end + + private def present?(attr) !attr.nil? && attr != "" end -end -class ArticleForm - attr_reader :name, :code, :quantity + def quantity=(value) + @quantity = value.to_i if value + end end -class AddArticleStatus - def initialize(status) - @status = status +class ErrorArticleStatus + attr_reader :form_with_errors + + def initialize(form) + @form_with_errors = form end def success? - status == :success + false end +end - private - - attr_reader :status +class SuccessArticleStatus + def success? + true + end end class Article diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index d1fe29e..5f9aa66 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -71,6 +71,15 @@ expect(store).not_to receive(:create).with(params) inventory.add_article(params) end + + it "on error returns a form with the current values" do + params = good_params.merge("name" => blank) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.name).to eq blank + expect(form.code).to eq "c1" + expect(form.quantity).to eq 10 + end end end end From 578e56e6199c6ef619f30a61e3111d54cd994daa Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:26:57 -0500 Subject: [PATCH 24/54] form show errors but always shows error --- 01_inventory/inventory.rb | 4 ++++ 01_inventory/spec/inventory_spec.rb | 7 +++++++ 01_inventory/views/new_article.erb | 5 ++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 5ccd434..0bd854f 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -40,6 +40,10 @@ def valid? present?(name) end + def name_errors + "can't be blank" + end + private def present?(attr) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 5f9aa66..41c3a1d 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -80,6 +80,13 @@ expect(form.code).to eq "c1" expect(form.quantity).to eq 10 end + + it "on error returns a form with errors" do + params = good_params.merge("name" => blank) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.name_errors).to eq "can't be blank" + end end end end diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index f60685c..2b17877 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -5,9 +5,12 @@
-
+
"> + <% if @form.name_errors %> + <%= @form.name_errors %> + <% end %>
From 9bf7761c9aff23c913bef1b466bd79e354ffbc78 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:30:35 -0500 Subject: [PATCH 25/54] how to show no errors on empty form? --- 01_inventory/inventory.rb | 2 +- 01_inventory/spec/inventory_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 0bd854f..246c350 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -41,7 +41,7 @@ def valid? end def name_errors - "can't be blank" + "can't be blank" unless present? name end private diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 41c3a1d..49a3db0 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -51,9 +51,10 @@ expect(status).to be_success end - it "has a new article form" do + it "has a new empty article form" do form = inventory.new_article_form expect(form.name).to be_nil + expect(form.name_errors).to be_nil expect(form.code).to be_nil expect(form.quantity).to be_nil end From b00801405524e7b0431438fc9bd48164d918de90 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:34:49 -0500 Subject: [PATCH 26/54] little refactor to make ArticleForm to receive an Article --- 01_inventory/inventory.rb | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 246c350..ec58372 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -8,11 +8,11 @@ def articles_list end def new_article_form - ArticleForm.new + ArticleForm.new(Article.new) end def add_article(params) - form = ArticleForm.new(params) + form = ArticleForm.new(Article.new(params)) if form.valid? store.create(params) @@ -28,12 +28,20 @@ def add_article(params) end class ArticleForm - attr_reader :name, :code, :quantity + def initialize(article) + @article = article + end - def initialize(data = {}) - @name = data["name"] - @code = data["code"] - self.quantity = data["quantity"] + def name + article.name + end + + def code + article.code + end + + def quantity + article.quantity end def valid? @@ -46,13 +54,11 @@ def name_errors private + attr_reader :article + def present?(attr) !attr.nil? && attr != "" end - - def quantity=(value) - @quantity = value.to_i if value - end end class ErrorArticleStatus @@ -76,9 +82,15 @@ def success? class Article attr_reader :name, :code, :quantity - def initialize(data) + def initialize(data = {}) @name = data["name"] @code = data["code"] - @quantity = data["quantity"] + self.quantity = data["quantity"] + end + + private + + def quantity=(value) + @quantity = value.to_i if value end end From 1e8f7e4e1179cdb1ad6c634b90cadb50ee9532b0 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 20:45:37 -0500 Subject: [PATCH 27/54] separate validation to be able to render the initial form without errors --- 01_inventory/inventory.rb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index ec58372..ae226f9 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -12,24 +12,38 @@ def new_article_form end def add_article(params) - form = ArticleForm.new(Article.new(params)) + article = Article.new(params) + errors = validate_article(article) - if form.valid? + if errors.empty? store.create(params) SuccessArticleStatus.new else - ErrorArticleStatus.new(form) + ErrorArticleStatus.new(ArticleForm.new(article, errors)) end end private attr_reader :store + + def validate_article(article) + if present?(article.name) + {} + else + {name: "can't be blank"} + end + end + + def present?(attr) + !attr.nil? && attr != "" + end end class ArticleForm - def initialize(article) + def initialize(article, errors = {}) @article = article + @errors = errors end def name @@ -49,16 +63,12 @@ def valid? end def name_errors - "can't be blank" unless present? name + errors[:name] end private - attr_reader :article - - def present?(attr) - !attr.nil? && attr != "" - end + attr_reader :article, :errors end class ErrorArticleStatus From 2c598fc07e04c9b26c322f1c005b711530041514 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 21:01:34 -0500 Subject: [PATCH 28/54] validates presence of quantity but discovering a bug on Article.quantity= (maybe its time to share presence?) --- 01_inventory/inventory.rb | 22 +++++++-- 01_inventory/spec/inventory_spec.rb | 74 ++++++++++++++++++----------- 01_inventory/views/new_article.erb | 5 +- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index ae226f9..04b6638 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -28,11 +28,21 @@ def add_article(params) attr_reader :store def validate_article(article) - if present?(article.name) - {} - else - {name: "can't be blank"} + errors = {} + + unless present?(article.name) + errors[:name] = "can't be blank" end + + unless present?(article.code) + errors[:code] = "can't be blank" + end + + unless present?(article.quantity) + errors[:quantity] = "can't be blank" + end + + errors end def present?(attr) @@ -66,6 +76,10 @@ def name_errors errors[:name] end + def code_errors + errors[:code] + end + private attr_reader :article, :errors diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 49a3db0..c9ce440 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -59,35 +59,51 @@ expect(form.quantity).to be_nil end - describe "validates presence of name" do - [nil, ""].each do |blank| - it "on error does not return success" do - params = good_params.merge("name" => blank) - status = inventory.add_article(params) - expect(status).not_to be_success - end - - it "on error does not create the article" do - params = good_params.merge("name" => blank) - expect(store).not_to receive(:create).with(params) - inventory.add_article(params) - end - - it "on error returns a form with the current values" do - params = good_params.merge("name" => blank) - status = inventory.add_article(params) - form = status.form_with_errors - expect(form.name).to eq blank - expect(form.code).to eq "c1" - expect(form.quantity).to eq 10 - end - - it "on error returns a form with errors" do - params = good_params.merge("name" => blank) - status = inventory.add_article(params) - form = status.form_with_errors - expect(form.name_errors).to eq "can't be blank" - end + it "on error does not return success" do + params = good_params.merge("name" => nil) + status = inventory.add_article(params) + expect(status).not_to be_success + end + + it "on error does not create the article" do + params = good_params.merge("name" => nil) + expect(store).not_to receive(:create).with(params) + inventory.add_article(params) + end + + it "on error returns a form with the current values" do + params = good_params.merge("name" => nil) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.name).to eq nil + expect(form.code).to eq "c1" + expect(form.quantity).to eq 10 + end + + [nil, ""].each do |blank| + it "validates presence of name" do + params = good_params.merge("name" => blank) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.name_errors).to eq "can't be blank" + end + end + + [nil, ""].each do |blank| + it "validates presence of code" do + params = good_params.merge("code" => blank) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.code_errors).to eq "can't be blank" + end + end + + [nil, ""].each do |blank| + it "validates presence of quantity" do + params = good_params.merge("quantity" => blank) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.quantity_errors).to eq "can't be blank" end end end diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index 2b17877..237bfbd 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -12,9 +12,12 @@ <%= @form.name_errors %> <% end %>
-
+
"> + <% if @form.code_errors %> + <%= @form.code_errors %> + <% end %>
From e4814aa15d348e79cf214baa63aa898076cda347 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 21:04:53 -0500 Subject: [PATCH 29/54] is working but... should Inventory include Presence? --- 01_inventory/inventory.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 04b6638..6eb5a30 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -1,4 +1,12 @@ +module Presence + def present?(attr) + !attr.nil? && attr != "" + end +end + class Inventory + include Presence + def initialize(store) @store = store end @@ -80,6 +88,10 @@ def code_errors errors[:code] end + def quantity_errors + errors[:quantity] + end + private attr_reader :article, :errors @@ -104,6 +116,8 @@ def success? end class Article + include Presence + attr_reader :name, :code, :quantity def initialize(data = {}) @@ -115,6 +129,6 @@ def initialize(data = {}) private def quantity=(value) - @quantity = value.to_i if value + @quantity = value.to_i if present?(value) end end From 708ac7dc359a9b320cccc693a83f6fd6183ef26b Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 21:07:29 -0500 Subject: [PATCH 30/54] refactor to introduce an ArticleValidator --- 01_inventory/inventory.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 6eb5a30..648aeca 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -1,12 +1,4 @@ -module Presence - def present?(attr) - !attr.nil? && attr != "" - end -end - class Inventory - include Presence - def initialize(store) @store = store end @@ -21,7 +13,7 @@ def new_article_form def add_article(params) article = Article.new(params) - errors = validate_article(article) + errors = ArticleValidator.validate(article) if errors.empty? store.create(params) @@ -34,8 +26,18 @@ def add_article(params) private attr_reader :store +end + +module Presence + def present?(attr) + !attr.nil? && attr != "" + end +end - def validate_article(article) +class ArticleValidator + extend Presence + + def self.validate(article) errors = {} unless present?(article.name) @@ -52,10 +54,6 @@ def validate_article(article) errors end - - def present?(attr) - !attr.nil? && attr != "" - end end class ArticleForm From 1538b3439e46d7e04795bf087b58ddfcba970afc Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 21:13:52 -0500 Subject: [PATCH 31/54] validates quantity is greater than or equals than 0 --- 01_inventory/inventory.rb | 6 +++++- 01_inventory/spec/inventory_spec.rb | 7 +++++++ 01_inventory/views/new_article.erb | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 648aeca..9cc7b03 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -48,7 +48,11 @@ def self.validate(article) errors[:code] = "can't be blank" end - unless present?(article.quantity) + if present?(article.quantity) + if article.quantity < 0 + errors[:quantity] = "should be greater or equals than 0" + end + else errors[:quantity] = "can't be blank" end diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index c9ce440..ed514fd 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -106,6 +106,13 @@ expect(form.quantity_errors).to eq "can't be blank" end end + + it "validates quantity is greater or equals than 0" do + params = good_params.merge("quantity" => -1) + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.quantity_errors).to eq "should be greater or equals than 0" + end end def store_with(records) diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index 237bfbd..2d192ff 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -21,7 +21,7 @@
- +
From a6f159ab91ad8fd07c595804211ac6368c0d564c Mon Sep 17 00:00:00 2001 From: bhserna Date: Sat, 11 Jun 2016 21:19:12 -0500 Subject: [PATCH 32/54] render quantity errors --- 01_inventory/views/new_article.erb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/01_inventory/views/new_article.erb b/01_inventory/views/new_article.erb index 2d192ff..d249c00 100644 --- a/01_inventory/views/new_article.erb +++ b/01_inventory/views/new_article.erb @@ -19,9 +19,12 @@ <%= @form.code_errors %> <% end %>
-
+
"> + <% if @form.quantity_errors %> + <%= @form.quantity_errors %> + <% end %>
From 8cecd6f2927efb8c08fa143aa299e76428e839ff Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 19:36:00 -0500 Subject: [PATCH 33/54] code uniq validation is ready but the validation code is a little funky --- 01_inventory/in_memory_store.rb | 6 ++++++ 01_inventory/inventory.rb | 10 +++++++--- 01_inventory/spec/inventory_spec.rb | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/01_inventory/in_memory_store.rb b/01_inventory/in_memory_store.rb index 367e73d..d43755d 100644 --- a/01_inventory/in_memory_store.rb +++ b/01_inventory/in_memory_store.rb @@ -11,6 +11,12 @@ def all_articles records end + def find_with_code(code) + records.detect do |record| + record["code"] == code + end + end + private attr_reader :records diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 9cc7b03..0e2dcbd 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -13,7 +13,7 @@ def new_article_form def add_article(params) article = Article.new(params) - errors = ArticleValidator.validate(article) + errors = ArticleValidator.validate(article, store) if errors.empty? store.create(params) @@ -37,14 +37,18 @@ def present?(attr) class ArticleValidator extend Presence - def self.validate(article) + def self.validate(article, store) errors = {} unless present?(article.name) errors[:name] = "can't be blank" end - unless present?(article.code) + if present?(article.code) + if store.find_with_code(article.code) + errors[:code] = "already taken" + end + else errors[:code] = "can't be blank" end diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index ed514fd..e524347 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -113,6 +113,16 @@ form = status.form_with_errors expect(form.quantity_errors).to eq "should be greater or equals than 0" end + + it "validates that the code is unique" do + params = good_params.merge("code" => "c1") + status = inventory.add_article(params) + expect(status).to be_success + + status = inventory.add_article(params) + form = status.form_with_errors + expect(form.code_errors).to eq "already taken" + end end def store_with(records) From 1cd67cce8191ed7abd79e841a3afad0f3c7e07cf Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 19:43:17 -0500 Subject: [PATCH 34/54] validation is a little more clean but still a little funky --- 01_inventory/inventory.rb | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 0e2dcbd..a18124c 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -13,7 +13,7 @@ def new_article_form def add_article(params) article = Article.new(params) - errors = ArticleValidator.validate(article, store) + errors = ArticleValidator.new(article, store).validate! if errors.empty? store.create(params) @@ -35,15 +35,33 @@ def present?(attr) end class ArticleValidator - extend Presence + include Presence + + def initialize(article, store) + @store = store + @article = article + end + + def validate! + self.errors = {} + validate_name! + validate_code! + validate_quantity! + errors + end + + private - def self.validate(article, store) - errors = {} + attr_reader :article, :store + attr_accessor :errors + def validate_name! unless present?(article.name) errors[:name] = "can't be blank" end + end + def validate_code! if present?(article.code) if store.find_with_code(article.code) errors[:code] = "already taken" @@ -51,7 +69,9 @@ def self.validate(article, store) else errors[:code] = "can't be blank" end + end + def validate_quantity! if present?(article.quantity) if article.quantity < 0 errors[:quantity] = "should be greater or equals than 0" @@ -59,8 +79,6 @@ def self.validate(article, store) else errors[:quantity] = "can't be blank" end - - errors end end From 56a9a7e01b029b317c5479f3878a068a480b34f4 Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 19:50:06 -0500 Subject: [PATCH 35/54] introduction of method :send, to clean the code a little, but still not as good as it can be --- 01_inventory/inventory.rb | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index a18124c..a74d44d 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -44,9 +44,11 @@ def initialize(article, store) def validate! self.errors = {} - validate_name! - validate_code! - validate_quantity! + validate_presence_of! :name + validate_presence_of! :code + validate_presence_of! :quantity + validate_uniqness_of_code! + validate_quantity_is_greater_than_or_equals_than_zero! errors end @@ -55,29 +57,21 @@ def validate! attr_reader :article, :store attr_accessor :errors - def validate_name! - unless present?(article.name) - errors[:name] = "can't be blank" + def validate_presence_of!(attr_key) + unless present?(article.send(attr_key)) + errors[attr_key] = "can't be blank" end end - def validate_code! - if present?(article.code) - if store.find_with_code(article.code) - errors[:code] = "already taken" - end - else - errors[:code] = "can't be blank" + def validate_uniqness_of_code! + if present?(article.code) && store.find_with_code(article.code) + errors[:code] = "already taken" end end - def validate_quantity! - if present?(article.quantity) - if article.quantity < 0 - errors[:quantity] = "should be greater or equals than 0" - end - else - errors[:quantity] = "can't be blank" + def validate_quantity_is_greater_than_or_equals_than_zero! + if present?(article.quantity) && article.quantity < 0 + errors[:quantity] = "should be greater or equals than 0" end end end From 50753eee104d956a22860c91a3038a642061055e Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 19:59:31 -0500 Subject: [PATCH 36/54] using splat operator to accept a collection of attributes --- 01_inventory/inventory.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index a74d44d..8de372c 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -44,9 +44,7 @@ def initialize(article, store) def validate! self.errors = {} - validate_presence_of! :name - validate_presence_of! :code - validate_presence_of! :quantity + validate_presence_of! :name, :code, :quantity validate_uniqness_of_code! validate_quantity_is_greater_than_or_equals_than_zero! errors @@ -57,9 +55,11 @@ def validate! attr_reader :article, :store attr_accessor :errors - def validate_presence_of!(attr_key) - unless present?(article.send(attr_key)) - errors[attr_key] = "can't be blank" + def validate_presence_of!(*attr_keys) + attr_keys.each do |attr_key| + unless present?(article.send(attr_key)) + errors[attr_key] = "can't be blank" + end end end From 076289fed6483e227e6e10542a77e3746286368b Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 20:17:26 -0500 Subject: [PATCH 37/54] intruction of define method to remove some duplication... funky code --- 01_inventory/inventory.rb | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 8de372c..fd10949 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -82,33 +82,24 @@ def initialize(article, errors = {}) @errors = errors end - def name - article.name - end - - def code - article.code - end - - def quantity - article.quantity - end - - def valid? - present?(name) - end - - def name_errors - errors[:name] + def self.delegate_from_article(*keys) + keys.each do |key| + define_method key do + article.send(key) + end + end end - def code_errors - errors[:code] + def self.delegate_from_errors(*keys) + keys.each do |key| + define_method "#{key}_errors" do + errors[key] + end + end end - def quantity_errors - errors[:quantity] - end + delegate_from_article :name, :code, :quantity + delegate_from_errors :name, :code, :quantity private From 06e2e78ad6a7c99542a9283a3c918845c8bf871f Mon Sep 17 00:00:00 2001 From: bhserna Date: Sun, 12 Jun 2016 20:19:45 -0500 Subject: [PATCH 38/54] attempt to build a more generic delegation method --- 01_inventory/inventory.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index fd10949..a50a985 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -82,6 +82,14 @@ def initialize(article, errors = {}) @errors = errors end + def self.delegate(*keys, to:) + keys.each do |key| + define_method key do + self.send(to).send(key) + end + end + end + def self.delegate_from_article(*keys) keys.each do |key| define_method key do @@ -98,7 +106,7 @@ def self.delegate_from_errors(*keys) end end - delegate_from_article :name, :code, :quantity + delegate :name, :code, :quantity, to: :article delegate_from_errors :name, :code, :quantity private From f6d9ce9e55cd1ee6a6d1a43a9725b7df4bdb4e00 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 10:47:05 -0500 Subject: [PATCH 39/54] know we have a generic delegate method, extracted in a Delegate module --- 01_inventory/inventory.rb | 47 ++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index a50a985..606b4fa 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -1,3 +1,5 @@ +require "ostruct" + class Inventory def initialize(store) @store = store @@ -34,6 +36,17 @@ def present?(attr) end end +module Delegate + def delegate(*keys, to:, sufix: false) + keys.each do |key| + method_name = sufix && "#{key}_#{to}" || key + define_method method_name do + self.send(to).send(key) + end + end + end +end + class ArticleValidator include Presence @@ -77,38 +90,16 @@ def validate_quantity_is_greater_than_or_equals_than_zero! end class ArticleForm - def initialize(article, errors = {}) - @article = article - @errors = errors - end + extend Delegate - def self.delegate(*keys, to:) - keys.each do |key| - define_method key do - self.send(to).send(key) - end - end - end - - def self.delegate_from_article(*keys) - keys.each do |key| - define_method key do - article.send(key) - end - end - end + delegate :name, :code, :quantity, to: :article + delegate :name, :code, :quantity, to: :errors, sufix: true - def self.delegate_from_errors(*keys) - keys.each do |key| - define_method "#{key}_errors" do - errors[key] - end - end + def initialize(article, errors = {}) + @article = article + @errors = OpenStruct.new(errors) end - delegate :name, :code, :quantity, to: :article - delegate_from_errors :name, :code, :quantity - private attr_reader :article, :errors From d0ca88fdc6e07457373a86f7980390e0af1cc053 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 10:55:51 -0500 Subject: [PATCH 40/54] html and api design to increment article quantity --- 01_inventory/app.rb | 5 +++++ 01_inventory/views/index.erb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 9da779f..3d26aaf 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -30,3 +30,8 @@ erb :new_article end end + +post '/articles/:code/increment' do + inventory.increment_article_quantity(params[:code]) + redirect "/" +end diff --git a/01_inventory/views/index.erb b/01_inventory/views/index.erb index 6daff02..e332a92 100644 --- a/01_inventory/views/index.erb +++ b/01_inventory/views/index.erb @@ -8,6 +8,7 @@ Nombre Codigo Cantidad + @@ -16,6 +17,11 @@ <%= article.name %> <%= article.code %> <%= article.quantity %> + +
+ +
+ <% end %> From a3dcf6405076f0f1e5a3dd817a761a9c5320f56b Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 11:05:20 -0500 Subject: [PATCH 41/54] first test to increment article quantity --- 01_inventory/spec/inventory_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index e524347..bd02d7b 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -125,6 +125,24 @@ end end + it "increments article quantity" do + store = store_with([ + {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, + {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} + ]) + + inventory = Inventory.new(store) + + expect(store). + to receive(:update). + with({ + "name" => "Camisa 1", + "code" => "c1", + "quantity" => 11}) + + inventory.increment_article_quantity("c1") + end + def store_with(records) InMemoryStore.new(records) end From de793e1be1bf5c6f4142ea1069c53778c54cc8e4 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 11:16:12 -0500 Subject: [PATCH 42/54] make the test green --- 01_inventory/inventory.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 606b4fa..c86ac87 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -25,6 +25,12 @@ def add_article(params) end end + def increment_article_quantity(code) + article = Article.new(store.find_with_code(code)) + article.increment_quantity! + store.update(article.to_h) + end + private attr_reader :store @@ -134,6 +140,14 @@ def initialize(data = {}) self.quantity = data["quantity"] end + def to_h + {"name" => name, "code" => code, "quantity" => quantity} + end + + def increment_quantity! + self.quantity = quantity + 1 + end + private def quantity=(value) From c2bc6071cdd932b111ac9e0a9288c8aee173827b Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 11:20:36 -0500 Subject: [PATCH 43/54] uses Article#to_h to create record and fixes an spec --- 01_inventory/inventory.rb | 6 ++++-- 01_inventory/spec/inventory_spec.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index c86ac87..45f15d9 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -18,7 +18,7 @@ def add_article(params) errors = ArticleValidator.new(article, store).validate! if errors.empty? - store.create(params) + store.create(article.to_h) SuccessArticleStatus.new else ErrorArticleStatus.new(ArticleForm.new(article, errors)) @@ -141,7 +141,9 @@ def initialize(data = {}) end def to_h - {"name" => name, "code" => code, "quantity" => quantity} + {"name" => name, + "code" => code, + "quantity" => quantity} end def increment_quantity! diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index bd02d7b..4575546 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -42,7 +42,7 @@ end it "with name, code and quantity" do - expect(store).to receive(:create).with(good_params) + expect(store).to receive(:create).with(good_params.merge("quantity" => 10)) inventory.add_article(good_params) end From 2f843dc67d19c56c0fa8f4e74ac53817d304063e Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:03:16 -0500 Subject: [PATCH 44/54] adds update method to store in order to run the app --- 01_inventory/in_memory_store.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/01_inventory/in_memory_store.rb b/01_inventory/in_memory_store.rb index d43755d..53044c9 100644 --- a/01_inventory/in_memory_store.rb +++ b/01_inventory/in_memory_store.rb @@ -7,6 +7,11 @@ def create(record) records << record end + def update(record) + index = find_index(record) + records[index] = record + end + def all_articles records end @@ -20,4 +25,10 @@ def find_with_code(code) private attr_reader :records + + def find_index(record) + records.find_index do |current| + current["code"] == record["code"] + end + end end From a64b8c668c210cbb1da2b04d640296aff6ab059d Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:15:06 -0500 Subject: [PATCH 45/54] html and api to decrement quantity --- 01_inventory/app.rb | 5 +++++ 01_inventory/views/index.erb | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 3d26aaf..98b6bc7 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -35,3 +35,8 @@ inventory.increment_article_quantity(params[:code]) redirect "/" end + +post '/articles/:code/decrement' do + inventory.decrement_article_quantity(params[:code]) + redirect "/" +end diff --git a/01_inventory/views/index.erb b/01_inventory/views/index.erb index e332a92..95407bb 100644 --- a/01_inventory/views/index.erb +++ b/01_inventory/views/index.erb @@ -1,3 +1,14 @@ + + @@ -17,10 +28,14 @@ <%= article.name %> <%= article.code %> <%= article.quantity %> - +
+ +
+ +
<% end %> From 23a9464e46ec5797c4ce948b8d27e2bce48505fe Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:22:01 -0500 Subject: [PATCH 46/54] design and refactor to add a test to decrement article quantity --- 01_inventory/spec/inventory_spec.rb | 39 +++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 4575546..b89904b 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -125,22 +125,35 @@ end end - it "increments article quantity" do - store = store_with([ - {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, - {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} - ]) + describe "modifies articles quantity" do + attr_reader :store, :inventory - inventory = Inventory.new(store) + before do + @store = store_with([ + {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, + {"name" => "Gorra 1", "code" => "g1", "quantity" => 35}]) + @inventory = Inventory.new(store) + end - expect(store). - to receive(:update). - with({ - "name" => "Camisa 1", - "code" => "c1", - "quantity" => 11}) + it "incrementing it" do + expect(store). + to receive(:update). + with({ + "name" => "Camisa 1", + "code" => "c1", + "quantity" => 11}) + inventory.increment_article_quantity("c1") + end - inventory.increment_article_quantity("c1") + it "decrementing it" do + expect(store). + to receive(:update). + with({ + "name" => "Camisa 1", + "code" => "c1", + "quantity" => 9}) + inventory.decrement_article_quantity("c1") + end end def store_with(records) From c4cf647644901497c3668756bb515f9fd4b9c757 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:23:49 -0500 Subject: [PATCH 47/54] the fisrt test to decrement quantity is green --- 01_inventory/inventory.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 45f15d9..968a316 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -31,6 +31,12 @@ def increment_article_quantity(code) store.update(article.to_h) end + def decrement_article_quantity(code) + article = Article.new(store.find_with_code(code)) + article.decrement_quantity! + store.update(article.to_h) + end + private attr_reader :store @@ -150,6 +156,10 @@ def increment_quantity! self.quantity = quantity + 1 end + def decrement_quantity! + self.quantity = quantity - 1 + end + private def quantity=(value) From 10cdb7b5225840161c7af91042cc8d7fff2a4c4e Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:26:01 -0500 Subject: [PATCH 48/54] adds an spec to test that the quantity can't go less than 0 --- 01_inventory/spec/inventory_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index b89904b..7db2601 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -131,7 +131,7 @@ before do @store = store_with([ {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, - {"name" => "Gorra 1", "code" => "g1", "quantity" => 35}]) + {"name" => "Gorra 1", "code" => "g1", "quantity" => 0}]) @inventory = Inventory.new(store) end @@ -154,6 +154,11 @@ "quantity" => 9}) inventory.decrement_article_quantity("c1") end + + it "decrementing it (unless is 0)" do + expect(store).not_to receive(:update) + inventory.decrement_article_quantity("g1") + end end def store_with(records) From 7424efa66badbf607932d1632cc862243ef6845b Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:30:50 -0500 Subject: [PATCH 49/54] do not permit quantity go less than 0 --- 01_inventory/inventory.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 968a316..83054a1 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -33,8 +33,11 @@ def increment_article_quantity(code) def decrement_article_quantity(code) article = Article.new(store.find_with_code(code)) - article.decrement_quantity! - store.update(article.to_h) + + if article.quantity > 0 + article.decrement_quantity! + store.update(article.to_h) + end end private From 41111acb2208eae4912f845e7b2489457fb9f640 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 12:32:15 -0500 Subject: [PATCH 50/54] a better way to handle decrementing quantity knowledge --- 01_inventory/inventory.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/01_inventory/inventory.rb b/01_inventory/inventory.rb index 83054a1..0b2beb9 100644 --- a/01_inventory/inventory.rb +++ b/01_inventory/inventory.rb @@ -33,9 +33,7 @@ def increment_article_quantity(code) def decrement_article_quantity(code) article = Article.new(store.find_with_code(code)) - - if article.quantity > 0 - article.decrement_quantity! + article.decrement_quantity! do |article| store.update(article.to_h) end end @@ -159,8 +157,11 @@ def increment_quantity! self.quantity = quantity + 1 end - def decrement_quantity! - self.quantity = quantity - 1 + def decrement_quantity!(&block) + if quantity > 0 + self.quantity = quantity - 1 + block.call(self) + end end private From f7e2aa1d762094b182f7ce657dddb101d2bfba97 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 13:01:06 -0500 Subject: [PATCH 51/54] adds an in file store --- 01_inventory/app.rb | 9 +---- 01_inventory/in_file_store.rb | 58 +++++++++++++++++++++++++++++ 01_inventory/in_file_store/data.yml | 13 +++++++ 3 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 01_inventory/in_file_store.rb create mode 100644 01_inventory/in_file_store/data.yml diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 98b6bc7..0f6aaa7 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -1,13 +1,8 @@ require "sinatra" require_relative "inventory" -require_relative "in_memory_store" +require_relative "in_file_store" -INITIAL_RECORDS = [ - {"name" => "Camisa 1", "code" => "c1", "quantity" => 10}, - {"name" => "Gorra 1", "code" => "g1", "quantity" => 35} -] - -store = InMemoryStore.new(INITIAL_RECORDS) +store = InFileStore.new inventory = Inventory.new(store) get '/' do diff --git a/01_inventory/in_file_store.rb b/01_inventory/in_file_store.rb new file mode 100644 index 0000000..7c8700e --- /dev/null +++ b/01_inventory/in_file_store.rb @@ -0,0 +1,58 @@ +require "yaml" + +class InFileStore + def create(record) + update_records do |records| + records << record + end + end + + def update(record) + update_records do |records| + index = find_index(record) + records[index] = record + end + end + + def all_articles + load_records + end + + def find_with_code(code) + load_records.detect do |record| + record["code"] == code + end + end + + private + + attr_reader :records + + def find_index(record) + load_records.find_index do |current| + current["code"] == record["code"] + end + end + + def update_records(&block) + records = load_records + block.call(records) + save records + end + + def save(records) + File.open(file_path, "w") do |file| + file << YAML.dump(records) + end + end + + def load_records + File.open(file_path) do |file| + YAML.load(file.read) || [] + end + end + + def file_path + "./in_file_store/data.yml" + end +end diff --git a/01_inventory/in_file_store/data.yml b/01_inventory/in_file_store/data.yml new file mode 100644 index 0000000..941b7ff --- /dev/null +++ b/01_inventory/in_file_store/data.yml @@ -0,0 +1,13 @@ +--- +- name: Asd + code: asdfa + quantity: 11 +- name: uno + code: dos + quantity: 4 +- name: dos + code: tres + quantity: 1233 +- name: asdf + code: ffff + quantity: 21 From 7f9449e9420057cba7d4412ed3fa6ad1c35a7706 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 13:18:06 -0500 Subject: [PATCH 52/54] moving files to a better file structure --- 01_inventory/app.rb | 4 ++-- 01_inventory/{ => lib}/inventory.rb | 0 01_inventory/spec/inventory_spec.rb | 4 ++-- 01_inventory/{ => store}/in_file_store.rb | 2 +- 01_inventory/{ => store}/in_file_store/data.yml | 0 01_inventory/{ => store}/in_memory_store.rb | 0 6 files changed, 5 insertions(+), 5 deletions(-) rename 01_inventory/{ => lib}/inventory.rb (100%) rename 01_inventory/{ => store}/in_file_store.rb (96%) rename 01_inventory/{ => store}/in_file_store/data.yml (100%) rename 01_inventory/{ => store}/in_memory_store.rb (100%) diff --git a/01_inventory/app.rb b/01_inventory/app.rb index 0f6aaa7..e9b22e1 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -1,6 +1,6 @@ require "sinatra" -require_relative "inventory" -require_relative "in_file_store" +require_relative "lib/inventory" +require_relative "store/in_file_store" store = InFileStore.new inventory = Inventory.new(store) diff --git a/01_inventory/inventory.rb b/01_inventory/lib/inventory.rb similarity index 100% rename from 01_inventory/inventory.rb rename to 01_inventory/lib/inventory.rb diff --git a/01_inventory/spec/inventory_spec.rb b/01_inventory/spec/inventory_spec.rb index 7db2601..6e02f75 100644 --- a/01_inventory/spec/inventory_spec.rb +++ b/01_inventory/spec/inventory_spec.rb @@ -1,6 +1,6 @@ require "rspec" -require_relative "../inventory" -require_relative "../in_memory_store" +require_relative "../lib/inventory" +require_relative "../store/in_memory_store" RSpec.describe "Inventory" do describe "shows the articles" do diff --git a/01_inventory/in_file_store.rb b/01_inventory/store/in_file_store.rb similarity index 96% rename from 01_inventory/in_file_store.rb rename to 01_inventory/store/in_file_store.rb index 7c8700e..6109a4b 100644 --- a/01_inventory/in_file_store.rb +++ b/01_inventory/store/in_file_store.rb @@ -53,6 +53,6 @@ def load_records end def file_path - "./in_file_store/data.yml" + "./store/in_file_store/data.yml" end end diff --git a/01_inventory/in_file_store/data.yml b/01_inventory/store/in_file_store/data.yml similarity index 100% rename from 01_inventory/in_file_store/data.yml rename to 01_inventory/store/in_file_store/data.yml diff --git a/01_inventory/in_memory_store.rb b/01_inventory/store/in_memory_store.rb similarity index 100% rename from 01_inventory/in_memory_store.rb rename to 01_inventory/store/in_memory_store.rb From f0b2ea4c075e0faa3e07ad00ee6744d493c19cd4 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 14:28:13 -0500 Subject: [PATCH 53/54] Adds Gemfile --- 01_inventory/Gemfile | 3 +++ 01_inventory/Gemfile.lock | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 01_inventory/Gemfile create mode 100644 01_inventory/Gemfile.lock diff --git a/01_inventory/Gemfile b/01_inventory/Gemfile new file mode 100644 index 0000000..388bbd4 --- /dev/null +++ b/01_inventory/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' +gem "sinatra" +gem "rspec" diff --git a/01_inventory/Gemfile.lock b/01_inventory/Gemfile.lock new file mode 100644 index 0000000..b933d24 --- /dev/null +++ b/01_inventory/Gemfile.lock @@ -0,0 +1,32 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.5) + rack (1.6.4) + rack-protection (1.5.3) + rack + rspec (3.2.0) + rspec-core (~> 3.2.0) + rspec-expectations (~> 3.2.0) + rspec-mocks (~> 3.2.0) + rspec-core (3.2.0) + rspec-support (~> 3.2.0) + rspec-expectations (3.2.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-mocks (3.2.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-support (3.2.1) + sinatra (1.4.7) + rack (~> 1.5) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) + tilt (1.3.3) + +PLATFORMS + ruby + +DEPENDENCIES + rspec + sinatra From 79747660414686bdd63f9da526e8c2a81b933a62 Mon Sep 17 00:00:00 2001 From: bhserna Date: Mon, 13 Jun 2016 16:31:12 -0500 Subject: [PATCH 54/54] Use sqlite db as storage --- 01_inventory/Gemfile | 4 +++ 01_inventory/Gemfile.lock | 25 ++++++++++++++++++ 01_inventory/Rakefile | 10 +++++++ 01_inventory/app.rb | 9 +++++-- 01_inventory/store/db_store.rb | 24 +++++++++++++++++ 01_inventory/store/db_store/config.rb | 6 +++++ 01_inventory/store/db_store/inventory | Bin 0 -> 20480 bytes .../db_store/migrate/0_create_articles.rb | 9 +++++++ 8 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 01_inventory/Rakefile create mode 100644 01_inventory/store/db_store.rb create mode 100644 01_inventory/store/db_store/config.rb create mode 100644 01_inventory/store/db_store/inventory create mode 100644 01_inventory/store/db_store/migrate/0_create_articles.rb diff --git a/01_inventory/Gemfile b/01_inventory/Gemfile index 388bbd4..98ff2c0 100644 --- a/01_inventory/Gemfile +++ b/01_inventory/Gemfile @@ -1,3 +1,7 @@ source 'https://rubygems.org' gem "sinatra" gem "rspec" + +gem 'activerecord' +gem 'activesupport' +gem 'sqlite3' diff --git a/01_inventory/Gemfile.lock b/01_inventory/Gemfile.lock index b933d24..ac25061 100644 --- a/01_inventory/Gemfile.lock +++ b/01_inventory/Gemfile.lock @@ -1,7 +1,25 @@ GEM remote: https://rubygems.org/ specs: + activemodel (4.2.6) + activesupport (= 4.2.6) + builder (~> 3.1) + activerecord (4.2.6) + activemodel (= 4.2.6) + activesupport (= 4.2.6) + arel (~> 6.0) + activesupport (4.2.6) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.3) + builder (3.2.2) diff-lcs (1.2.5) + i18n (0.7.0) + json (1.8.3) + minitest (5.9.0) rack (1.6.4) rack-protection (1.5.3) rack @@ -22,11 +40,18 @@ GEM rack (~> 1.5) rack-protection (~> 1.4) tilt (>= 1.3, < 3) + sqlite3 (1.3.11) + thread_safe (0.3.5) tilt (1.3.3) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES + activerecord + activesupport rspec sinatra + sqlite3 diff --git a/01_inventory/Rakefile b/01_inventory/Rakefile new file mode 100644 index 0000000..fdd7d5d --- /dev/null +++ b/01_inventory/Rakefile @@ -0,0 +1,10 @@ +require "active_record" +require "active_support/all" + +namespace :db do + desc "migrate your database" + task :migrate do + require "./store/db_store/config" + ActiveRecord::Migrator.migrate('store/db_store/migrate') + end +end diff --git a/01_inventory/app.rb b/01_inventory/app.rb index e9b22e1..573103d 100644 --- a/01_inventory/app.rb +++ b/01_inventory/app.rb @@ -1,8 +1,9 @@ require "sinatra" require_relative "lib/inventory" -require_relative "store/in_file_store" +require_relative "store/db_store/config" +require_relative "store/db_store" -store = InFileStore.new +store = DbStore.new inventory = Inventory.new(store) get '/' do @@ -35,3 +36,7 @@ inventory.decrement_article_quantity(params[:code]) redirect "/" end + +after do + ActiveRecord::Base.clear_active_connections! +end diff --git a/01_inventory/store/db_store.rb b/01_inventory/store/db_store.rb new file mode 100644 index 0000000..1a66a6b --- /dev/null +++ b/01_inventory/store/db_store.rb @@ -0,0 +1,24 @@ +require "active_record" + +class DbArticle < ActiveRecord::Base + self.table_name = "articles" +end + +class DbStore + def create(record) + DbArticle.create(record) + end + + def update(record) + article = find_with_code(record["code"]) + article.update_attributes(record) + end + + def all_articles + DbArticle.all + end + + def find_with_code(code) + DbArticle.find_by code: code + end +end diff --git a/01_inventory/store/db_store/config.rb b/01_inventory/store/db_store/config.rb new file mode 100644 index 0000000..db95fff --- /dev/null +++ b/01_inventory/store/db_store/config.rb @@ -0,0 +1,6 @@ +require "active_record" + +ActiveRecord::Base.establish_connection({ + adapter: "sqlite3", + database: "store/db_store/inventory" +}) diff --git a/01_inventory/store/db_store/inventory b/01_inventory/store/db_store/inventory new file mode 100644 index 0000000000000000000000000000000000000000..42703c7e1d5e5c67134d23642a6aa1378303332a GIT binary patch literal 20480 zcmeI)Z)?*)90%~bCN(Oy5u`+g!f{Yo*`#yZ(}7Wg6uWe*DcDElY;RkzO=*%2eOOp`+2&X8Bdma|2HV?#qrRuH3s3sj1$gna$->!*c3% z{>A#Uri#;WL`&zTxweYTyQSEW;aG~cA9!!Po;d8a58U2By!8gc?FZeqPxB5uKM;PG za=+8r5;A+H6lW?PovN7HquF?^JzuCO4b#t3gi)$bblhB8CCk}w@ntKF#`@DD3MtfK zGCI{$HJpYgu}Z^PloUFQ)vo(~`>5~HE?;}^g}iRM6kT7)!XjCWXF5*k39>oimeZn* zTqqUpEp(LD-3sHE7tcxlS~at?Ik$f?x{x3M0SG_<0uX=z1Rwwb2tWV=5V(B;1~1v$ z_bD!%|8L*RMMMyQ00bZa0SG_<0uX=z1Rwx`m4Ip3_qMa&{|);avwzy(sX&4N1Rwwb z2tWV=5P$##AOHafK;Q-lRE;vL)woK=$64N~l*;T;jW43v^PSzjip^=*;qGc!J-+z= r|2ebIZ$NO^A_O1+0SG_<0uX=z1Rwwb2tWV=|0wW)8+D$4UBUPbv2VlD literal 0 HcmV?d00001 diff --git a/01_inventory/store/db_store/migrate/0_create_articles.rb b/01_inventory/store/db_store/migrate/0_create_articles.rb new file mode 100644 index 0000000..49e1e53 --- /dev/null +++ b/01_inventory/store/db_store/migrate/0_create_articles.rb @@ -0,0 +1,9 @@ +class CreateArticles < ActiveRecord::Migration + def change + create_table :articles do |t| + t.string :name + t.string :code + t.integer :quantity + end + end +end