Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -151,6 +159,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
better_errors
binding_of_caller
byebug
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +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])
json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone], methods: [ :movies_checked_out_count])
)
end
end
1 change: 0 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class MoviesController < ApplicationController
# protect_from_forgery with: :null_session

def index
movies = Movie.all
Expand Down
7 changes: 6 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class Customer < ApplicationRecord
# include ActiveModel::Serialization

has_many :rentals

validates :name, presence: true


def movies_checked_out_count
self.rentals.where(checked_out: true).count
end # movies_checked_out

end
3 changes: 3 additions & 0 deletions app/serializers/customer_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# class CustomerSerializer < ActiveModel::Serializer
# attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count
# end
5 changes: 5 additions & 0 deletions db/migrate/20171107181648_add_checked_out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCheckedOut < ActiveRecord::Migration[5.1]
def change
add_column :rentals, :checked_out, :boolean
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions test/fixtures/rentals.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ one:
customer_id: 1
due_date: 2017-11-06


two:
movie_id: 1
customer_id: 1
Expand Down
11 changes: 11 additions & 0 deletions test/models/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@

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
end