Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zad2+3 #120

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 23 additions & 7 deletions lecture_2/homework/Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.2'
ruby '2.5.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.2'
Expand All @@ -25,21 +27,35 @@ gem 'puma', '~> 3.11'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS),
# making cross-origin AJAX possible
# gem 'rack-cors'

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]
# Call 'byebug' anywhere in the code to stop execution and get a debugger
# console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
# Spring speeds up development by keeping your application running in the
# background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Rubocop
gem 'rubocop', require: false

# Faker
gem 'faker'

# Serializers
gem 'active_model_serializers'

# Sentry
gem 'sentry-raven'
38 changes: 37 additions & 1 deletion lecture_2/homework/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.9)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (5.2.2.1)
activesupport (= 5.2.2.1)
globalid (>= 0.3.6)
Expand All @@ -43,18 +48,27 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
ast (2.4.0)
bootsnap (1.4.1)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.0)
case_transform (0.2)
activesupport
concurrent-ruby (1.1.5)
crass (1.0.4)
erubi (1.8.0)
faker (1.9.3)
i18n (>= 0.7)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
ffi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.2)
jsonapi-renderer (0.2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -72,10 +86,15 @@ GEM
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.9)
multipart-post (2.0.0)
nio4r (2.3.1)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
parallel (1.17.0)
parser (2.6.2.1)
ast (~> 2.4.0)
pg (1.1.4)
psych (3.1.0)
puma (3.12.0)
rack (2.0.6)
rack-test (1.1.0)
Expand Down Expand Up @@ -104,11 +123,23 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (12.3.2)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rubocop (0.67.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
psych (>= 3.1.0)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
sentry-raven (2.9.0)
faraday (>= 0.7.6, < 1.0)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -125,6 +156,7 @@ GEM
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.5.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand All @@ -133,18 +165,22 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
bootsnap (>= 1.1.0)
byebug
faker
listen (>= 3.0.5, < 3.2)
pg
puma (~> 3.11)
rails (~> 5.2.2)
rubocop
sentry-raven
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data

RUBY VERSION
ruby 2.6.2p47
ruby 2.5.1p57

BUNDLED WITH
2.0.1
5 changes: 4 additions & 1 deletion lecture_2/homework/Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
# for example lib/tasks/capistrano.rake, and they will automatically be
# available to Rake.

require_relative 'config/application'

Expand Down
2 changes: 2 additions & 0 deletions lecture_2/homework/app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Connection < ActionCable::Connection::Base
end
Expand Down
21 changes: 16 additions & 5 deletions lecture_2/homework/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
rescue_from ActiveRecord::RecordNotFound do |exception|
render json: { message: exception.message }, status: 404
end
include Errorable
before_action :set_raven_context

rescue_from ActiveRecord::RecordInvalid do |exception|
render json: { message: exception.message }, status: 422
private

def set_raven_context
Raven.user_context(id: session[:current_user_id])
Raven.extra_context(params: params.to_unsafe_h, url: request.url)
end
# rescue_from ActiveRecord::RecordNotFound do |exception|
# render json: { message: exception.message }, status: 404
# end

# rescue_from ActiveRecord::RecordInvalid do |exception|
# render json: { message: exception.message }, status: 422
# end
end
59 changes: 59 additions & 0 deletions lecture_2/homework/app/controllers/clans/characters_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module Clans
class CharactersController < ApplicationController
def index
characters = clan.characters.all
if params.key?(:alive)
if params[:alive].to_i.zero?
render json: characters.dead, include: %w[clan defensible],
status: 200
else
render json: characters.alive, include: %w[clan defensible],
status: 200
end
else
render json: characters, include: %w[clan defensible], status: 200
end
end

def create
character = clan.characters.create!(character_params)
render json: character, include: %w[clan defensible], status: 201
end

def show
render json: character, include: %w[clan defensible], status: 200
end

def update
character.update!(character_params)
render json: character, status: 201
end

def destroy
character.destroy!
head 204
end

private

def clan
@clan ||= Clan.find(params[:clan_id])
end

def character
@character ||= Character.find_by!(id: params[:id],
clan_id: params[:clan_id])
end

# def character
# clan.characters.find(params[:id])
# end

def character_params
params.permit(:name, :armour_rating, :battle_count, :join_date,
:death_date)
end
end
end
50 changes: 0 additions & 50 deletions lecture_2/homework/app/controllers/clans/samurais_controller.rb

This file was deleted.

25 changes: 22 additions & 3 deletions lecture_2/homework/app/controllers/clans_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
# frozen_string_literal: true

class ClansController < ApplicationController
def index
render json: Clan.all.to_json
render json: Clan.all, status: 200
end

def create
clan = Clan.create!(clan_params)
render json: clan, status: 201
end

def show
render json: clan, status: 200
end

render json: clan.to_json, status: 201
def update
clan.update(clan_params)
render json: clan, status: 201
end

def destroy
clan.destroy!
head 204
end

private

def clan
Clan.find(params[:id])
end

def clan_params
params.permit(:name)
params.permit(:name, :id)
end
end
16 changes: 16 additions & 0 deletions lecture_2/homework/app/controllers/concerns/errorable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Errorable
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordInvalid, with: :render_validation_error
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_error
end
def render_validation_error(exception)
render json: exception.record.errors, status: 422
end

def render_not_found_error(exception)
render json: { errors: exception.message }, status: 404
end
end
2 changes: 2 additions & 0 deletions lecture_2/homework/app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base
end
2 changes: 2 additions & 0 deletions lecture_2/homework/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
layout 'mailer'
Expand Down
2 changes: 2 additions & 0 deletions lecture_2/homework/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Loading