Skip to content

Commit aecb7ed

Browse files
committed
feat: implement Active Model Serializers
- Implemented serialization for Contact, Phone, Address and Kind models - Enforced JSON:API request format
1 parent ba67587 commit aecb7ed

File tree

9 files changed

+47
-3
lines changed

9 files changed

+47
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
class ApplicationController < ActionController::API
2+
before_action :ensure_json_request
3+
4+
def ensure_json_request
5+
return if request.headers["Accept"] =~ /json/ # check in Header, if Accept contains smt with json, and continue if true.
6+
head 406 # otherwise respond with status "not acceptable"
7+
end
28
end

app/controllers/contacts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def index
1616

1717
# GET /contacts/1
1818
def show
19-
render json: @contact, include: [ :kind, :phones, :address ]
19+
render json: @contact, include: [ :kind, :address, :phones ]# , meta: { author: "fg" }
2020
# render json: @contact.attributes.merge({ author: "fg" })
2121
# render json: { name: @contact.name, email: @contact.email }
2222
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class AddressSerializer < ActiveModel::Serializer
2+
attributes :id, :street, :city
3+
end
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
class ContactSerializer < ActiveModel::Serializer
2-
attributes :id
2+
attributes :id, :name, :email, :birthdate, :author
3+
4+
belongs_to :kind do
5+
link(:related) { kind_url(object.kind.id) }
6+
end
7+
has_many :phones
8+
has_one :address
9+
10+
# add HATEOAS
11+
# link(:self) { contact_url(object.id) }
12+
# link(:kind) { kind_url(object.kind.id) } # - link added inside the relationships
13+
14+
meta do
15+
{ author: "fg" }
16+
end
17+
18+
def attributes(*arg) # splat operator
19+
h = super(*arg) # call parent ActiveModel::Serializer's attributes method - returns hash of the model's attributes
20+
21+
# Rails.logger.debug("Returned from super: #{h.inspect}")
22+
# Ex.: Returned from super: {:id=>77, :name=>"Shena Hilpert", :email=>"rosita@oconnell-runolfsson.test", :birthdate=>"1962-06-05"}
23+
24+
h[:birthdate] = object.birthdate.to_time.iso8601 unless object.birthdate.blank?
25+
h
26+
end
327
end

app/serializers/kind_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class KindSerializer < ActiveModel::Serializer
2-
attributes :id
2+
attributes :id, :description
33
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class PhoneSerializer < ActiveModel::Serializer
2+
attributes :id, :number
3+
end

config/environments/development.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Rails.application.configure do
44
# Settings specified here will take precedence over those in config/application.rb.
55

6+
Rails.application.routes.default_url_options = {
7+
host: "localhost",
8+
port: 3000
9+
}
610
# Make code changes take effect immediately without server restart.
711
config.enable_reloading = true
812

config/initializers/ams.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ActiveModel::Serializer.config.adapter = :json_api
2+
# to use use JsonApi Adapter

config/initializers/mime_types.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Mime::Type.register "application/vnd.api+json", :json
2+
# content formatted in JSON and follows a specific API format (JSON:API specification)

0 commit comments

Comments
 (0)