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

Add homework_5 #134

Open
wants to merge 1 commit 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
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
7 changes: 6 additions & 1 deletion lecture_5/homework/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.2'
ruby '2.5.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.2'
Expand Down Expand Up @@ -42,6 +42,7 @@ 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 'rubocop-rspec'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
Expand All @@ -50,5 +51,9 @@ group :test do
gem 'rspec-rails'
end

group :development, :test do
gem 'factory_bot_rails'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
20 changes: 19 additions & 1 deletion lecture_5/homework/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ GEM
crass (1.0.4)
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)
ffi (1.10.0-x64-mingw32)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
Expand All @@ -83,14 +89,19 @@ GEM
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.9)
msgpack (1.2.9-x64-mingw32)
nio4r (2.3.1)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.1-x64-mingw32)
mini_portile2 (~> 2.4.0)
parallel (1.17.0)
parser (2.6.2.0)
ast (~> 2.4.0)
pg (1.1.4)
pg (1.1.4-x64-mingw32)
psych (3.1.0)
psych (3.1.0-x64-mingw32)
puma (3.12.0)
rack (2.0.6)
rack-test (1.1.0)
Expand Down Expand Up @@ -149,6 +160,8 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
rubocop-rspec (1.32.0)
rubocop (>= 0.60.0)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
spring (2.0.2)
Expand All @@ -167,30 +180,35 @@ GEM
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2019.1)
tzinfo (>= 1.0.0)
unicode-display_width (1.5.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)

PLATFORMS
ruby
x64-mingw32

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

RUBY VERSION
ruby 2.6.2p47
ruby 2.5.3p105

BUNDLED WITH
2.0.1
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: 'warrior'
end

private

def building
@building ||= Building.find(params[:id])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
warriors = clan.warriors

if params.key?(:alive)
if params[:alive].to_i == 0
if params[:alive].to_i.zero?
render json: warriors.dead
else
render json: warriors.alive
Expand Down Expand Up @@ -47,7 +47,8 @@ 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, :building_id)
end
end
end
2 changes: 2 additions & 0 deletions lecture_5/homework/app/models/building.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Building < ApplicationRecord

validates :granary, presence: true,
numericality: { greater_than_or_equal_to: 0, only_integer: true }
validates :siege_ability, presence: true,
numericality: { greater_than_or_equal_to: 0, only_integer: true }
end
7 changes: 7 additions & 0 deletions lecture_5/homework/app/models/warrior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ class Warrior < ApplicationRecord

scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }

after_save :siege_report
after_destroy :siege_report

def siege_report
return Reports::SiegeReport.new(building: building).call if building_id
end
end
2 changes: 1 addition & 1 deletion lecture_5/homework/app/serializers/building_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class BuildingSerializer < ActiveModel::Serializer
attributes :name
attributes :name, :granary, :siege_ability

has_many :warriors
end
20 changes: 19 additions & 1 deletion lecture_5/homework/app/services/reports/siege_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
module Reports
class SiegeReport
def initialize(building:)
@building = building
@warriors = building.warriors
end

def call
raise NotImprementedYet
siege = @warriors.present? ? @building.siege_ability = granary_ability : @building.siege_ability = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To sprawdzenie spokojnie by się mogło znaleźć gdzieś "wyżej", żeby nie obciążać logiki serwisu :) Serwis najlepiej jeśli ma jedną odpowiedzialność, a Twój serwis poza liczeniem ile dni przetrwa dany budynek, stwierdza upadłość budynku jeśli nie ma w nim wojowników :)

@building.save
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To zapisywanie atrybutu niepotrzebnie dociąża nam ten serwis, powinno też się znaleźć "wyżej" :) Wiąże się to też między innymi ze wzorcem dobrego projektowania aplikacji, Single Responsibility Principle. Ogólnie możnaby zoptymalizować nasz callback przez obsługę wyliczenia tych dni i zapisania tej wartości na budynku w kolejce asynchronicznej np. Sidekiq, Resque, ActiveJob :D Albo przez w ogóle zastanowieniem się czy nie wystarczy liczyć tych dni w serializerze, który by potrafił scachować wywołanie tego serwisu na jakiś czas, żeby oszczędzić pracy apce, ale to są tematy których nie było na LevelUp, nie mniej są bardzo ważne i polecam je zbadać :) napewno pomyślimy o nich w kolejnych edycjach, a napewno będą na praktykach :D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hej,
dziękuję za komentarze, postaram się nanieść poprawki po majówce;-)
Pozdrawiam

siege
end

private

def granary_request
daily_request = 10
samurais = @warriors.where(type: 'Warriors::Samurai').count
hussars = @warriors.where(type: 'Warriors::Hussar').count * 2
daily_request + samurais + hussars
end

def granary_ability
granary = @building.granary
granary / granary_request
end
end
end
4 changes: 3 additions & 1 deletion lecture_5/homework/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
default: &default
adapter: postgresql
encoding: unicode
database: rorlevelup2019
database: homework_5
username: postgres
password: Potato

development:
<<: *default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddSiegeAbilityOnBuilding < ActiveRecord::Migration[5.2]
def change
add_column :buildings, :siege_ability, :integer, default: 0, null: false

Building.find_each { |building| Reports::SiegeReport.new(building: building).call }
end
end
4 changes: 3 additions & 1 deletion lecture_5/homework/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: 2019_04_05_090544) do
ActiveRecord::Schema.define(version: 2019_04_16_095847) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -20,6 +20,8 @@
t.string "type", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "granary", default: 0, null: false
t.integer "siege_ability", default: 0, null: false
end

create_table "clans", force: :cascade do |t|
Expand Down
64 changes: 64 additions & 0 deletions lecture_5/homework/spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
28 changes: 28 additions & 0 deletions lecture_5/homework/spec/requests/buildings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Buildings API', type: :request do
describe 'GET /buildings' do
it 'responds with 200' do
get '/buildings'
expect(response).to have_http_status(200)
end
end

describe 'GET /buildings/:id' do
it 'response with 404' do
get '/buildings/1'
expect(response).to have_http_status(404)
end
end

describe 'GET /buildings/:id' do
let(:building) { create(:building) }

it 'response with 200' do
get "/buildings/#{building.id}"
expect(response).to have_http_status(200)
end
end
end
55 changes: 55 additions & 0 deletions lecture_5/homework/spec/services/reports/siege_report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Reports::SiegeReport, type: :service do
subject(:siege_report) { Reports::SiegeReport.new(building: building).call }

let(:building) { create(:building) }

describe '#call' do
context 'when building without warriors' do
it 'returns 0' do
expect(siege_report).to eq(0)
end
end
end

describe '#call' do
context 'when building with one samurai' do
let(:warrior) { build(:warrior, building: building) }

it 'returns 18' do
warrior.save
expect(siege_report).to eq(18)
end
end
end

describe '#call' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jeden describe wrapujący całość dotyczących go testów by wystarczył :)

context 'when building with one hussar' do
let(:warrior) { build(:warrior, type: warrior_type, building: building) }
let(:warrior_type) { 'Warriors::Hussar' }

it 'returns 16' do
warrior.save
expect(siege_report).to eq(16)
end
end
end

describe '#call' do
context 'when building with two types of warriors' do
let(:warrior1) { build(:warrior, building: building) }
let(:warrior2) { build(:warrior, name: hussar_name, type: warrior_type, building: building) }
let(:hussar_name) { 'Frog Hussar' }
let(:warrior_type) { 'Warriors::Hussar' }

it 'returns 15' do
warrior1.save
warrior2.save
expect(siege_report).to eq(15)
end
end
end
end
Loading