Skip to content
Open

Zad5 #145

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 lecture_5/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
41 changes: 9 additions & 32 deletions lecture_5/homework/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,27 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.2'
# Use pg as the database for Active Record
gem 'active_model_serializers'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'pg'
# Use Puma as the app server
gem 'pry', '~> 0.12.2'
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# 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
# gem 'rack-cors'

# ActiveModel::Serializer implementation and Rails hooks
gem 'active_model_serializers'
gem 'rails', '~> 5.2.2'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'database_cleaner'
gem 'factory_bot_rails'
gem 'rspec-rails'
gem 'rubocop-performance'
gem 'rubocop-rspec'
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
gem 'rubocop', require: false
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
gem 'rspec-rails'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
107 changes: 63 additions & 44 deletions lecture_5/homework/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.2.1)
actionpack (= 5.2.2.1)
actioncable (5.2.3)
actionpack (= 5.2.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.2.1)
actionpack (= 5.2.2.1)
actionview (= 5.2.2.1)
activejob (= 5.2.2.1)
actionmailer (5.2.3)
actionpack (= 5.2.3)
actionview (= 5.2.3)
activejob (= 5.2.3)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.2.1)
actionview (= 5.2.2.1)
activesupport (= 5.2.2.1)
actionpack (5.2.3)
actionview (= 5.2.3)
activesupport (= 5.2.3)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.2.1)
activesupport (= 5.2.2.1)
actionview (5.2.3)
activesupport (= 5.2.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
Expand All @@ -29,36 +29,43 @@ GEM
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)
activejob (5.2.3)
activesupport (= 5.2.3)
globalid (>= 0.3.6)
activemodel (5.2.2.1)
activesupport (= 5.2.2.1)
activerecord (5.2.2.1)
activemodel (= 5.2.2.1)
activesupport (= 5.2.2.1)
activemodel (5.2.3)
activesupport (= 5.2.3)
activerecord (5.2.3)
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
activestorage (5.2.2.1)
actionpack (= 5.2.2.1)
activerecord (= 5.2.2.1)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
marcel (~> 0.3.1)
activesupport (5.2.2.1)
activesupport (5.2.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
ast (2.4.0)
bootsnap (1.4.1)
bootsnap (1.4.3)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.0)
byebug (11.0.1)
case_transform (0.2)
activesupport
coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
database_cleaner (1.7.0)
diff-lcs (1.3)
erubi (1.8.0)
factory_bot (5.0.2)
activesupport (>= 4.2.0)
factory_bot_rails (5.0.2)
factory_bot (~> 5.0.2)
railties (>= 4.2.0)
ffi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
Expand All @@ -82,40 +89,43 @@ GEM
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.9)
msgpack (1.2.10)
nio4r (2.3.1)
nokogiri (1.10.1)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
parallel (1.17.0)
parser (2.6.2.0)
parser (2.6.2.1)
ast (~> 2.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
psych (3.1.0)
puma (3.12.0)
rack (2.0.6)
puma (3.12.1)
rack (2.0.7)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.2.1)
actioncable (= 5.2.2.1)
actionmailer (= 5.2.2.1)
actionpack (= 5.2.2.1)
actionview (= 5.2.2.1)
activejob (= 5.2.2.1)
activemodel (= 5.2.2.1)
activerecord (= 5.2.2.1)
activestorage (= 5.2.2.1)
activesupport (= 5.2.2.1)
rails (5.2.3)
actioncable (= 5.2.3)
actionmailer (= 5.2.3)
actionpack (= 5.2.3)
actionview (= 5.2.3)
activejob (= 5.2.3)
activemodel (= 5.2.3)
activerecord (= 5.2.3)
activestorage (= 5.2.3)
activesupport (= 5.2.3)
bundler (>= 1.3.0)
railties (= 5.2.2.1)
railties (= 5.2.3)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.2.1)
actionpack (= 5.2.2.1)
activesupport (= 5.2.2.1)
railties (5.2.3)
actionpack (= 5.2.3)
activesupport (= 5.2.3)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
Expand All @@ -126,7 +136,7 @@ GEM
ffi (~> 1.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
rspec-expectations (3.8.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
Expand All @@ -149,6 +159,10 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
rubocop-performance (1.1.0)
rubocop (>= 0.67.0)
rubocop-rspec (1.32.0)
rubocop (>= 0.60.0)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
spring (2.0.2)
Expand Down Expand Up @@ -179,12 +193,17 @@ DEPENDENCIES
active_model_serializers
bootsnap (>= 1.1.0)
byebug
database_cleaner
factory_bot_rails
listen (>= 3.0.5, < 3.2)
pg
pry (~> 0.12.2)
puma (~> 3.11)
rails (~> 5.2.2)
rspec-rails
rubocop
rubocop-performance
rubocop-rspec
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data
Expand Down
14 changes: 12 additions & 2 deletions lecture_5/homework/app/controllers/buildings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# frozen_string_literal: true

class BuildingsController < ApplicationController
def index; end
def index
render json: Building.all
end

def show; end
def show
render json: building, include: [:warriors]
end

private

def building
building = Building.find(params[:id])
end
end
14 changes: 4 additions & 10 deletions lecture_5/homework/app/controllers/clans/warriors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ def show

def index
warriors = clan.warriors
params_key = params[:alive]
render_alive = params_key ? DeadOrAliveService.new(warriors, params_key).call : warriors

if params.key?(:alive)
if params[:alive].to_i == 0
render json: warriors.dead
else
render json: warriors.alive
end
else
render json: warriors
end
render json: render_alive
end

def create
Expand Down Expand Up @@ -47,7 +41,7 @@ def warrior
end

def warrior_params
params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date)
params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date, :horse)
end
end
end
7 changes: 5 additions & 2 deletions lecture_5/homework/app/models/building.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

class Building < ApplicationRecord
has_many :warriors, dependent: :nullify
KINDS = %w[Buildings::Walls Buildings::Stronghold].freeze

validates :granary, presence: true,
numericality: { greater_than_or_equal_to: 0, only_integer: true }
validates :name, presence: true
validates :granary, :horse_units, :infantry_units, presence: true,
numericality: { greater_than_or_equal_to: 0, only_integer: true }
validates :type, presence: true, inclusion: { in: KINDS }
end
3 changes: 3 additions & 0 deletions lecture_5/homework/app/models/warrior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class Warrior < ApplicationRecord
greater_than_or_equal_to: 0,
less_than_or_equal_to: 100 }
validates :preferred_weapon_kind, presence: true, inclusion: { in: Weapon::KINDS }
validates :horse, inclusion: { in: [true, false] }

scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }
scope :with_horse, -> { where(horse: true) }
scope :without_horse, -> { where(horse: false) }
end
5 changes: 5 additions & 0 deletions lecture_5/homework/app/models/warriors/hussar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
module Warriors
class Hussar < Warrior
attribute :preferred_weapon_kind, :string, default: :ranged
before_save :give_him_horse

def attack
"Hussar #{name} charged while yielding #{weapon.type.demodulize}"
end

def give_him_horse
self.horse = true
end
end
end
14 changes: 13 additions & 1 deletion lecture_5/homework/app/serializers/building_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# frozen_string_literal: true

class BuildingSerializer < ActiveModel::Serializer
attributes :name
attributes :name, :type, :granary, :horse_units, :infantry_units, :warriors_alive, :siege_ability

has_many :warriors

def type
object.type.demodulize
end

def warriors_alive
object.warriors.select { |w| !w.death_date }.length
end

def siege_ability
return Reports::SiegeReport.new(building: object).call if object
end
end
14 changes: 14 additions & 0 deletions lecture_5/homework/app/services/dead_or_alive_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class DeadOrAliveService
attr_reader :warriors, :params_key

def initialize(warriors, params_key)
@warriors = warriors
@params_key = params_key
end

def call
@params_key.to_i == 0 ? warriors.alive : warriors.dead
end
end
6 changes: 5 additions & 1 deletion lecture_5/homework/app/services/reports/siege_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

module Reports
class SiegeReport
attr_reader :building, :warriors

def initialize(building:)
@building = building
@warriors = building.warriors.alive
end

def call
raise NotImprementedYet
warriors.any? ? SiegeCalculatorService.new(building, warriors).call : 0
end
end
end
Loading