Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMalayev committed Jan 22, 2021
1 parent 9cc2c7d commit d6f6f74
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 71 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.6'
gem "pry-rails"
gem "pry"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
Expand Down
23 changes: 13 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ GEM
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
bcrypt (3.1.16)
bootsnap (1.5.0)
bootsnap (1.5.1)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.1.7)
concurrent-ruby (1.1.8)
crass (1.0.6)
devise (4.7.3)
bcrypt (~> 3.0)
Expand All @@ -86,16 +86,16 @@ GEM
erubi (1.10.0)
fast_jsonapi (1.5)
activesupport (>= 4.2)
ffi (1.13.1)
ffi (1.14.2)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
jwt (2.2.2)
listen (3.2.1)
listen (3.4.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.8.0)
loofah (2.9.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand All @@ -105,21 +105,23 @@ GEM
method_source (1.0.0)
mimemagic (0.3.5)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
mini_portile2 (2.5.0)
minitest (5.14.3)
msgpack (1.3.3)
nio4r (2.5.4)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
nokogiri (1.11.1)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.2.3)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-rails (0.3.9)
pry (>= 0.10.4)
puma (4.3.6)
puma (4.3.7)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-cors (1.1.1)
rack (>= 2.0.0)
Expand Down Expand Up @@ -169,7 +171,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (1.0.1)
thor (1.1.0)
thread_safe (0.3.6)
tzinfo (1.2.9)
thread_safe (~> 0.1)
Expand All @@ -196,6 +198,7 @@ DEPENDENCIES
fast_jsonapi
listen (~> 3.2)
pg (>= 0.18, < 2.0)
pry
pry-rails
puma (~> 4.1)
rack-cors
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class ApplicationController < ActionController::API
def current_user
User.first
end
end
end

# We have one user and it has many calendars and has many events
# We have one user and it has many calendars and has many events
# first returns the first element of an element
29 changes: 24 additions & 5 deletions app/controllers/calendars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ class CalendarsController < ApplicationController
before_action :set_calendar, only: [:show, :update, :destroy]

# GET /calendars
def index
@calendars = current_user.calendars
def index #If status code isn't specified it will use the status code of 200 unless there is an error
@calendars = current_user.calendars
# binding.pry
render json: CalendarSerializer.new(@calendars).serializable_hash[:data].map{|info| info[:attributes]}

end


# GET /calendars/1
def show
# binding.pry
render json: @calendar
end


# POST /calendars
def create
@calendar = current_user.calendars.build(calendar_params)
Expand Down Expand Up @@ -50,4 +50,23 @@ def calendar_params
params.require(:calendar).permit(:title)
# The user id will be assigned by the current_user
end
end
end

# serializable_hash
# Before the serializer we had an array of objects and after we have a data property pointing to an array of objects. Each object has id, type and attribute. We need to things in attributes. serializable_hash returns a serialized hash of your object. We use the brackets method [] to access the key in a hash.


#full_messages => Returns all the full error messages in an array
# to_sentence(options = {})
# Converts the array to a comma-separated sentence where the last element is joined by the connector word.
# ['one', 'two'].to_sentence => "one and two"
# ['one', 'two', 'three'].to_sentence => "one, two, and three"


# full_messages() public
# Returns all the full error messages in an array.


# render :json
# JSON is a JS data format used by AJAX Libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser.
# We don't need to use to_json when we use :json render will automatically use to_json for us.
9 changes: 7 additions & 2 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ class EventsController < ApplicationController

# GET /events
def index
# binding.pry
@events = current_user.events

render json: EventSerializer.new(@events).serializable_hash[:data].map{|info| info[:attributes]}

end

# GET /events/1
Expand All @@ -15,14 +18,14 @@ def show
# POST /events
def create
@event = current_user.events.build(event_params)
if @event.save
if @event.save #saves to the database returns boolean
render json: EventSerializer.new(@event).serializable_hash[:data][:attributes], status: :created
else
render json: @event.errors.full_messages.to_sentence, status: :unprocessable_entity
end
end

# PATCH/PUT /events/1
# PATCH/PUT /events/1 CHECK ??
def update
if @event.update(event_params)
render json: EventSerializer.new(@event).serializable_hash[:data][:attributes], status: :ok
Expand All @@ -40,7 +43,9 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_event

@event = current_user.events.find(params[:id])

end

# Only allow a trusted parameter "white list" through.
Expand Down
51 changes: 1 addition & 50 deletions app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,4 @@ class EventSerializer

# We use the Fast JSON API gem to create a close approximation to JSON DATA from ruby data ?? It provides a way for us to generate serializer classses for each resource object in our API that is involved in customized JSON rendering. We can use these serializer classes to define the specific attributes we want objects to share or not share, along with things like related object attributes. The result is that in our controller actions, rather than writing a custom render each time, we write out a serializer for each objectonce and use Fast JSON API to control the way our data is structured.

#ASIDE: Serializers generated by the Fast JSON API gem have two built-in methods called serializable_hash and serialized_json which return a serialized hash and a JSON string respectively. However, we don't actually need either of these in this example, as to_json will still be called on SightingSerializer.new(sighting) implicitly. As we will see, once our serializers are configured and initialized, we will not need to do any additional work


# [
# {
# id: 246,
# name: "First Event",
# start_time: "2021-01-20T20:07:00.000Z",
# end_time: "2021-01-21T20:07:00.000Z",
# notes: "Project Review Prep",
# calendar: {
# id: 17,
# title: "Personal",
# user_id: 1,
# created_at: "2021-01-20T02:23:02.558Z",
# updated_at: "2021-01-20T02:23:02.558Z",
# },
# calendar_title: "Personal",
# },
# {
# id: 247,
# name: "Doctor",
# start_time: "2021-01-21T20:07:00.000Z",
# end_time: "2021-01-21T20:07:00.000Z",
# notes: "Make an appt with ENT",
# calendar: {
# id: 19,
# title: "Private",
# user_id: 1,
# created_at: "2021-01-20T02:23:02.576Z",
# updated_at: "2021-01-20T02:23:02.576Z",
# },
# calendar_title: "Private",
# },
# {
# id: 248,
# name: "Resume",
# start_time: "2021-01-20T20:29:00.000Z",
# end_time: "2021-01-21T20:29:00.000Z",
# notes: "Make a new resume",
# calendar: {
# id: 18,
# title: "Work",
# user_id: 1,
# created_at: "2021-01-20T02:23:02.571Z",
# updated_at: "2021-01-20T02:23:02.571Z",
# },
# calendar_title: "Work",
# },
# ]
#ASIDE: Serializers generated by the Fast JSON API gem have two built-in methods called serializable_hash and serialized_json which return a serialized hash and a JSON string respectively. However, we don't actually need either of these in this example, as to_json will still be called on SightingSerializer.new(sighting) implicitly. As we will see, once our serializers are configured and initialized, we will not need to do any additional work.
58 changes: 56 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :events
resources :events
resources :calendars
devise_for :users, path: "", path_names: {
sign_in: "login",
Expand All @@ -13,4 +13,58 @@
end


# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

# # resources generates all 7 routes
# # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

# removing the path "" so we can remove routes starting with users and have it start with login logout and signup



# Prefix Verb URI Pattern Controller#Action
# events GET /events(.:format) events#index
# POST /events(.:format) events#create
# event GET /events/:id(.:format) events#show
# PATCH /events/:id(.:format) events#update
# PUT /events/:id(.:format) events#update
# DELETE /events/:id(.:format) events#destroy
# calendars GET /calendars(.:format) calendars#index
# POST /calendars(.:format) calendars#create
# calendar GET /calendars/:id(.:format) calendars#show
# PATCH /calendars/:id(.:format) calendars#update
# PUT /calendars/:id(.:format) calendars#update
# DELETE /calendars/:id(.:format) calendars#destroy
# new_user_session GET /login(.:format) users/sessions#new
# user_session POST /login(.:format) users/sessions#create
# destroy_user_session DELETE /logout(.:format) users/sessions#destroy
# new_user_password GET /password/new(.:format) devise/passwords#new
# edit_user_password GET /password/edit(.:format) devise/passwords#edit
# user_password PATCH /password(.:format) devise/passwords#update
# PUT /password(.:format) devise/passwords#update
# POST /password(.:format) devise/passwords#create
# cancel_user_registration GET /signup/cancel(.:format) users/registrations#cancel
# new_user_registration GET /signup/sign_up(.:format) users/registrations#new
# edit_user_registration GET /signup/edit(.:format) users/registrations#edit
# user_registration PATCH /signup(.:format) users/registrations#update
# PUT /signup(.:format) users/registrations#update
# DELETE /signup(.:format) users/registrations#destroy
# POST /signup(.:format) users/registrations#create

# rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
# rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
# rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
# rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
# rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
# rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
# rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
# POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
# rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
# PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
# PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
# DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
# rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
# rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
# rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
# rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
# update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
# rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
2 changes: 2 additions & 0 deletions config/spring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
"tmp/restart.txt",
"tmp/caching-dev.txt"
)


0 comments on commit d6f6f74

Please sign in to comment.