-
Notifications
You must be signed in to change notification settings - Fork 37
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
Zad5 #125
base: master
Are you sure you want to change the base?
Zad5 #125
Changes from all commits
a05e3d6
b40a0a0
6eed0bf
82fbbd3
a168a89
006f4f6
92baed2
aecdfc7
24f5af7
8244a76
3d6be4b
f3a3ced
4ed5a03
72ba190
440e2ff
785dea2
2aa0f9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class BuildingsController < ApplicationController | ||
def index; end | ||
def index | ||
render json: Building.all.order(:id), include: [:warriors] | ||
end | ||
|
||
def show; end | ||
def show | ||
render json: building, include: [:warriors] | ||
end | ||
|
||
private | ||
|
||
def build_params | ||
params.permit(:name,:granary) | ||
end | ||
|
||
def building | ||
@building ||= Building.find(params[:id]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,41 @@ class Warrior < ApplicationRecord | |
|
||
scope :alive, -> { where('death_date IS NULL') } | ||
scope :dead, -> { where('death_date IS NOT NULL') } | ||
|
||
after_create :update_siege | ||
before_update :update_prev_building | ||
# cant put same if: :building_id_changed? since it will always return false | ||
after_update :update_siege_after | ||
# after_commit :update_siege_after | ||
after_destroy :update_siege | ||
|
||
def update_siege | ||
# call after warrior already was asigned to building list after create,update | ||
# works for delete as well | ||
Reports::SiegeReport.call(building: building) if building | ||
end | ||
|
||
# it seems like rspec didnt invoke those callbacks | ||
# when i was testing manually it works | ||
# probably messed up in factory and tests :( | ||
def update_siege_after | ||
if prev_building_id | ||
# if warrior changed his building run this on both | ||
# since in that moment he is no longer in old list , rather in new building | ||
# so this way i can keep both old and new buildings updated | ||
# puts "prev building: #{prev_building_id} current: #{building_id}" | ||
Reports::SiegeReport.call(building: Building.find(prev_building_id)) | ||
Reports::SiegeReport.call(building: Building.find(building_id)) | ||
# set this to nil to prevent callback invoking from random update | ||
update_column(:prev_building_id, nil) | ||
end | ||
end | ||
|
||
def update_prev_building | ||
# if warrior is changing his buildings | ||
if building_id_was != building_id | ||
# save this attribute wihtout triggering callbacks | ||
update_column(:prev_building_id, building_id_was) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Z tego co pamiętam update_column omija walidację więc nie jest to najlepszy sposób na edycje kolumny :D Najprosciej byłoby dodać relację typu belongs_to z opcją optional: true :) Optional pozwoli na ustawianie nila na tej kolumnie, nie trzeba nic zmieniać w bazie, bo bazy domyślnie pozwalają na nile, a jedynie rails sam waliduje to że nie mogą być nilem :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Poza tym w sumie gdy sie wbijasz callbackiem miedzy zmiane danych a sve, masz dostep do zmiennej metody changes, ktora zwraca to co sie zmienilo, dzieki czemu nie ma potrzeby w tym przypadku poprzednim budynek na uzytkowniku, bo w callbacku informacja z jakiego na jakiego zmienilo sie building id juz jest |
||
end | ||
end | ||
end |
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, :siege_ability, :granary | ||
|
||
has_many :warriors | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,21 @@ | |
|
||
module Reports | ||
class SiegeReport | ||
def initialize(building:) | ||
end | ||
def self.call(building:) | ||
# this should stop the method if getting null | ||
return unless building | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) |
||
|
||
def call | ||
raise NotImprementedYet | ||
# bulding cant be defended without warriors | ||
if building.warriors.any? | ||
warriors_count = building.warriors.where(type: 'Warriors::Samurai').count | ||
hussar_count = building.warriors.where(type: 'Warriors::Hussar').count | ||
total_rice_need = warriors_count + hussar_count * 2 + building.default_rice_need | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Te powyższe trzy linijki ładniej by wyglądałī, jeśli Warrior miałby zaimplementowaną metodę/atrybut np. supply_burn_rate, który zwraca 1 jeśli warrior nie ma konia i 2 jeśli go ma, wtedy nie trzeba by myśleć nad takimi ifami w tym serwisie :) |
||
siege_ability = building.granary / total_rice_need | ||
building.siege_ability = siege_ability | ||
else | ||
building.siege_ability = 0 | ||
end | ||
building.save! | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddSiegeAbility < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :buildings, :siege_ability, :integer, default: 0, null: false | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddDefaultRiceNeed < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :buildings, :default_rice_need, :integer, default: 0 | ||
|
||
Buildings::Stronghold.find_each { |building| update_attribute(:default_rice_need,10) } | ||
Building.find_each { |building| Reports::SiegeReport.check_siege_ability(building: building) } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddPreviousBuildingId < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :warriors, :prev_building_id, :integer | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe BuildingsController, type: :controller do | ||
subject(:building) { create(:building) } | ||
describe 'it get all buildings' do | ||
it 'responds with 200' do | ||
get 'index' | ||
expect(response).to have_http_status(200) | ||
end | ||
end | ||
|
||
describe 'it should find some building' do | ||
it 'responds with 200' do | ||
get :show, params: { id: building.id } | ||
expect(response).to have_http_status(200) | ||
end | ||
end | ||
|
||
describe 'it shows no building found' do | ||
it 'responds with 404' do | ||
get :show, params: { id: 999 } | ||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Id nie jest domyslnym orderem? :D