From 9811e3099a05587c3e549b75aaf88616bfaa58c7 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 7 Nov 2017 10:46:51 -0800 Subject: [PATCH] Add serializer to customer --- Gemfile | 2 ++ Gemfile.lock | 9 +++++++++ app/controllers/customers_controller.rb | 5 +++-- app/serializers/customer_serializer.rb | 4 ++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 app/serializers/customer_serializer.rb diff --git a/Gemfile b/Gemfile index a6e0bdd54..30570bf82 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,8 @@ 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 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 484707437..b49a96958 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 76f311958..afe09e3b9 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -5,7 +5,8 @@ def index customers = Customer.all render( - json: customers.as_json(except: [:created_at, :updated_at]), + # json: customers.as_json(except: [:created_at, :updated_at]), + json: customers, status: :ok ) end @@ -14,7 +15,7 @@ def show customer = Customer.find_by(id: params[:id]) if customer render( - json: customer.as_json(except: [:created_at, :updated_at]), + json: customer, status: :ok ) else diff --git a/app/serializers/customer_serializer.rb b/app/serializers/customer_serializer.rb new file mode 100644 index 000000000..8d940403d --- /dev/null +++ b/app/serializers/customer_serializer.rb @@ -0,0 +1,4 @@ +class CustomerSerializer < ActiveModel::Serializer + attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit, :movies_checked_out_count + +end