From 17a12a05d828c9e8d270abaa4584328653300540 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 10:17:50 -0800 Subject: [PATCH 1/7] add checked_out column to customers --- app/controllers/movies_controller.rb | 1 - db/migrate/20171107181648_add_checked_out.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20171107181648_add_checked_out.rb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 824101aa6..223fcdfbf 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,5 +1,4 @@ class MoviesController < ApplicationController - # protect_from_forgery with: :null_session def index movies = Movie.all diff --git a/db/migrate/20171107181648_add_checked_out.rb b/db/migrate/20171107181648_add_checked_out.rb new file mode 100644 index 000000000..f7ee98fad --- /dev/null +++ b/db/migrate/20171107181648_add_checked_out.rb @@ -0,0 +1,5 @@ +class AddCheckedOut < ActiveRecord::Migration[5.1] + def change + add_column :customers, :checked_out, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 73c02b67f..eade83cd2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171106215231) do +ActiveRecord::Schema.define(version: 20171107181648) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,6 +26,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.float "account_credit" + t.boolean "checked_out" end create_table "movies", force: :cascade do |t| From bf0e11aac53d8312d2fb623ea511b96b126192a4 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 10:35:49 -0800 Subject: [PATCH 2/7] committing to be able to pull --- app/controllers/customers_controller.rb | 2 +- app/models/customer.rb | 5 ++++- test/fixtures/rentals.yml | 2 ++ test/models/customer_test.rb | 12 ++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 456569149..153aa54f6 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all render( - json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone]) + json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out]) ) end end diff --git a/app/models/customer.rb b/app/models/customer.rb index d24ae68b0..1295a553f 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,5 +3,8 @@ class Customer < ApplicationRecord validates :name, presence: true - + def self.movies_checked_out + self.rentals.where(checked_out: true).count + end # movies_checked_out + end diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml index b9677884e..d140b5a04 100644 --- a/test/fixtures/rentals.yml +++ b/test/fixtures/rentals.yml @@ -4,8 +4,10 @@ one: movie_id: 1 customer_id: 1 due_date: 2017-11-06 + checked_out: true two: movie_id: 1 customer_id: 1 due_date: 2017-11-06 + checked_out: false diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index e3ce77543..e04edb21f 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -27,4 +27,16 @@ end end # validations + + describe "custom methods" do + describe "movies_checked_out" do + it "will return a list of the checkout out movies for that customer" do + get customers_path + + body = JSON.parse(response.body) + body[0][:movies_checked_out].must_equal 1 + + end + end # movies_checked_out + end # custom methods end From d0cdedde2e9d9313cad9ec2be16c388c7d4c517c Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 10:46:21 -0800 Subject: [PATCH 3/7] removed checked_out column from rentals table and deleted what was in this migration since we don't actually want this column --- app/controllers/customers_controller.rb | 2 +- db/migrate/20171107181648_add_checked_out.rb | 2 +- db/schema.rb | 3 +-- test/fixtures/rentals.yml | 3 +-- test/models/customer_test.rb | 3 +-- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 153aa54f6..456569149 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all render( - json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out]) + json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone]) ) end end diff --git a/db/migrate/20171107181648_add_checked_out.rb b/db/migrate/20171107181648_add_checked_out.rb index f7ee98fad..3dac652f6 100644 --- a/db/migrate/20171107181648_add_checked_out.rb +++ b/db/migrate/20171107181648_add_checked_out.rb @@ -1,5 +1,5 @@ class AddCheckedOut < ActiveRecord::Migration[5.1] def change - add_column :customers, :checked_out, :boolean + end end diff --git a/db/schema.rb b/db/schema.rb index eade83cd2..73c02b67f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171107181648) do +ActiveRecord::Schema.define(version: 20171106215231) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,7 +26,6 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.float "account_credit" - t.boolean "checked_out" end create_table "movies", force: :cascade do |t| diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml index d140b5a04..fb50b9fe7 100644 --- a/test/fixtures/rentals.yml +++ b/test/fixtures/rentals.yml @@ -4,10 +4,9 @@ one: movie_id: 1 customer_id: 1 due_date: 2017-11-06 - checked_out: true + two: movie_id: 1 customer_id: 1 due_date: 2017-11-06 - checked_out: false diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index e04edb21f..73f57bc3e 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -34,8 +34,7 @@ get customers_path body = JSON.parse(response.body) - body[0][:movies_checked_out].must_equal 1 - + body[0][:movies_checked_out].must_equal 0 end end # movies_checked_out end # custom methods From c18261ad72138545d291648bfce83c41ae4981ad Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 10:48:56 -0800 Subject: [PATCH 4/7] added back in the checked_out column cause do actually do want it --- db/migrate/20171107181648_add_checked_out.rb | 2 +- db/schema.rb | 3 ++- test/models/customer_test.rb | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/db/migrate/20171107181648_add_checked_out.rb b/db/migrate/20171107181648_add_checked_out.rb index 3dac652f6..902b8ef27 100644 --- a/db/migrate/20171107181648_add_checked_out.rb +++ b/db/migrate/20171107181648_add_checked_out.rb @@ -1,5 +1,5 @@ class AddCheckedOut < ActiveRecord::Migration[5.1] def change - + add_column :rentals, :checked_out, :boolean end end diff --git a/db/schema.rb b/db/schema.rb index 73c02b67f..6f7142676 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171106215231) do +ActiveRecord::Schema.define(version: 20171107181648) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -43,6 +43,7 @@ t.date "due_date" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "checked_out" end end diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 73f57bc3e..3f077bc1e 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -28,14 +28,14 @@ end end # validations - describe "custom methods" do - describe "movies_checked_out" do - it "will return a list of the checkout out movies for that customer" do - get customers_path - - body = JSON.parse(response.body) - body[0][:movies_checked_out].must_equal 0 - end - end # movies_checked_out - end # custom methods + # describe "custom methods" do + # describe "movies_checked_out" do + # it "will return a list of the checkout out movies for that customer" do + # get customers_path + # + # body = JSON.parse(response.body) + # body[0][:movies_checked_out].must_equal 0 + # end + # end # movies_checked_out + # end # custom methods end From 863b5d6616f95407cd55f2ef5ca9aa0640b5d044 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 11:00:06 -0800 Subject: [PATCH 5/7] got movies_checked_out_count to where it should be working, but need to add searializer to make sure it is working as expected --- app/controllers/customers_controller.rb | 2 +- app/models/customer.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 456569149..75a9f6909 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all render( - json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone]) + json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]) ) end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 1295a553f..fdf13a474 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,7 +3,7 @@ class Customer < ApplicationRecord validates :name, presence: true - def self.movies_checked_out + def movies_checked_out_count self.rentals.where(checked_out: true).count end # movies_checked_out From a714ed6e01a949e50e1bb660f3bf1e53d7568ad1 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 11:20:29 -0800 Subject: [PATCH 6/7] movies_checked_out_count is working and being passed as part of the api response --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ app/controllers/customers_controller.rb | 7 ++++--- app/models/customer.rb | 3 +++ app/serializers/customer_serializer.rb | 3 +++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 app/serializers/customer_serializer.rb diff --git a/Gemfile b/Gemfile index a6e0bdd54..54d1f540c 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ gem 'puma', '~> 3.7' # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible # gem 'rack-cors' +gem "active_model_serializers" group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 4a738acb5..7cc8096f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.6) + actionpack (>= 4.1, < 6) + activemodel (>= 4.1, < 6) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.2) activejob (5.1.4) activesupport (= 5.1.4) globalid (>= 0.3.6) @@ -48,6 +53,8 @@ GEM debug_inspector (>= 0.0.1) builder (3.2.3) byebug (9.1.0) + case_transform (0.2) + activesupport coderay (1.1.2) concurrent-ruby (1.0.5) crass (1.0.2) @@ -61,6 +68,7 @@ GEM jquery-turbolinks (2.1.0) railties (>= 3.1.0) turbolinks + jsonapi-renderer (0.1.3) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -151,6 +159,7 @@ PLATFORMS ruby DEPENDENCIES + active_model_serializers better_errors binding_of_caller byebug diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 75a9f6909..9287f3d42 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,8 +1,9 @@ class CustomersController < ApplicationController def index customers = Customer.all - render( - json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]) - ) + render json: customers, status: :ok + # render( + # json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]) + # ) end end diff --git a/app/models/customer.rb b/app/models/customer.rb index fdf13a474..859acc7a3 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,7 @@ class Customer < ApplicationRecord + + include ActiveModel::Serialization + has_many :rentals validates :name, presence: true diff --git a/app/serializers/customer_serializer.rb b/app/serializers/customer_serializer.rb new file mode 100644 index 000000000..5df7a944b --- /dev/null +++ b/app/serializers/customer_serializer.rb @@ -0,0 +1,3 @@ +class CustomerSerializer < ActiveModel::Serializer + attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count +end From 6607c40515ed94333b0af9737c895f28b59cfaa7 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 11:27:14 -0800 Subject: [PATCH 7/7] got movies_checked_out_count to be passed in api response not using serializer --- app/controllers/customers_controller.rb | 8 ++++---- app/models/customer.rb | 3 +-- app/serializers/customer_serializer.rb | 6 +++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 9287f3d42..c3919a8f6 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,9 +1,9 @@ class CustomersController < ApplicationController def index customers = Customer.all - render json: customers, status: :ok - # render( - # json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]) - # ) + # render json: customers, status: :ok + render( + json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone], methods: [ :movies_checked_out_count]) + ) end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 859acc7a3..8f4558692 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,7 +1,6 @@ class Customer < ApplicationRecord + # include ActiveModel::Serialization - include ActiveModel::Serialization - has_many :rentals validates :name, presence: true diff --git a/app/serializers/customer_serializer.rb b/app/serializers/customer_serializer.rb index 5df7a944b..336c84d80 100644 --- a/app/serializers/customer_serializer.rb +++ b/app/serializers/customer_serializer.rb @@ -1,3 +1,3 @@ -class CustomerSerializer < ActiveModel::Serializer - attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count -end +# class CustomerSerializer < ActiveModel::Serializer +# attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count +# end