Skip to content
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_2/homework/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'rails', '~> 5.2.2'
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.11'
gem 'active_model_serializers'
# 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
Expand Down
9 changes: 9 additions & 0 deletions 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 @@ -47,6 +52,8 @@ GEM
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)
Expand All @@ -55,6 +62,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jsonapi-renderer (0.2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -133,6 +141,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
bootsnap (>= 1.1.0)
byebug
listen (>= 3.0.5, < 3.2)
Expand Down
8 changes: 1 addition & 7 deletions lecture_2/homework/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
class ApplicationController < ActionController::API
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
include ErrorsHandler
end
50 changes: 0 additions & 50 deletions lecture_2/homework/app/controllers/clans/samurais_controller.rb

This file was deleted.

50 changes: 50 additions & 0 deletions lecture_2/homework/app/controllers/clans/warriors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Clans
class WarriorsController < ApplicationController
def show
render json: warrior, serializer: Clans::WarriorSerializer
end

def index
warriors = clan.warriors
if params.key?(:alive)
if params[:alive].to_i == 0
render json: warriors.dead, each_serializer: Clans::WarriorSerializer
else
render json: warriors, each_serializer: Clans::WarriorSerializer
end
else
render json: warriors
end
end

def create
warrior = clan.warriors.create!(warrior_params)

render json: warrior.to_json, status: 201
end

def update
warrior.update!(warrior_params)

render json: warrior.to_json
end

def destroy
warrior.destroy!
end

private

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

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

def warrior_params
params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date, :type)
end
end
end
2 changes: 1 addition & 1 deletion lecture_2/homework/app/controllers/clans_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ClansController < ApplicationController
def index
render json: Clan.all.to_json
render json: Clan.all, each_serializer: ClanSerializer
end

def create
Expand Down
13 changes: 13 additions & 0 deletions lecture_2/homework/app/controllers/concerns/errors_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ErrorsHandler
extend ActiveSupport::Concern

included do
rescue_from ActiveRecord::RecordInvalid do |exception|
render json: exception.record.errors, status: :unprocessable_entity
end

rescue_from ActiveRecord::RecordNotFound do |exception|
render json: { error: exception.message }, status: :not_found
end
end
end
5 changes: 5 additions & 0 deletions lecture_2/homework/app/models/archer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Archer < Warrior
def attack
"I aim for the knee. Always."
end
end
5 changes: 5 additions & 0 deletions lecture_2/homework/app/models/bpp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BPP < Warrior
def attack
"..."
end
end
3 changes: 3 additions & 0 deletions lecture_2/homework/app/models/castle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Castle < ApplicationRecord
has_many :warriors, as: :defensible
end
4 changes: 2 additions & 2 deletions lecture_2/homework/app/models/clan.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Clan < ApplicationRecord
has_many :samurais, dependent: :destroy
validates :name, presence: true
has_many :warriors, dependent: :destroy
validates :name, presence: true, uniqueness: true
end
5 changes: 5 additions & 0 deletions lecture_2/homework/app/models/husarz.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Husarz < Warrior
def attack
"Na pohybel!"
end
end
6 changes: 6 additions & 0 deletions lecture_2/homework/app/models/magical.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Magical < Weapon
def calculate_damage(distance)
return 0 if distance > range * Random.rand(100)
damage * Random.rand(100)
end
end
6 changes: 6 additions & 0 deletions lecture_2/homework/app/models/melee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Melee < Weapon
def calculate_damage(distance)
return 0 if distance > range
damage
end
end
7 changes: 7 additions & 0 deletions lecture_2/homework/app/models/ranged.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Ranged < Weapon
def calculate_damage(distance)
return 0 if distance > range
f =( (range.to_f/2) - distance.to_f)**2/((range.to_f/2)**2)
(damage * (1- f)).to_i
end
end
14 changes: 4 additions & 10 deletions lecture_2/homework/app/models/samurai.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
class Samurai < ApplicationRecord
belongs_to :clan

validates :name, presence: true
validates :armor_quality, numericality: { only_integer: true,
greater_than_or_equal_to: 0,
less_than_or_equal_to: 100 }

scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }
class Samurai < Warrior
def attack
"Nani?!"
end
end
3 changes: 3 additions & 0 deletions lecture_2/homework/app/models/tower.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Tower < ApplicationRecord
has_many :warriors, as: :defensible
end
3 changes: 3 additions & 0 deletions lecture_2/homework/app/models/wall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Wall < ApplicationRecord
has_many :warriors, as: :defensible
end
16 changes: 16 additions & 0 deletions lecture_2/homework/app/models/warrior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Warrior < ApplicationRecord
belongs_to :clan
belongs_to :defensible, polymorphic: true
has_one :weapon, dependent: :destroy
validates :name, presence: true, uniqueness: {conditions: -> {where('death_date IS NULL')}}
validates :armor_quality, numericality: { only_integer: true,
greater_than_or_equal_to: 0,
less_than_or_equal_to: 100 }

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

def attack
"Ugabuga!"
end
end
11 changes: 11 additions & 0 deletions lecture_2/homework/app/models/weapon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Weapon < ApplicationRecord
belongs_to :warrior

validates :damage, :range, presence: true, numericality: { only_integer: true,
greater_than_or_equal_to: 0,
less_than_or_equal_to: 100 }

def calculate_damage(distance)
0
end
end
11 changes: 11 additions & 0 deletions lecture_2/homework/app/serializers/clan_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ClanSerializer < ActiveModel::Serializer
attributes :name, :warriors
def warriors
object.warriors.map do |warrior|
{
name: warrior.name,
type: warrior.type}
end
end

end
23 changes: 23 additions & 0 deletions lecture_2/homework/app/serializers/warrior_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

class WarriorSerializer < ActiveModel::Serializer
attributes :id, :name, :defensible, :weapon, :attack

def weapon
d = object.weapon
if d.nil? then
return nil
end
{ type: d.type,
range: d.range,
damage: d.damage}

end

def defensible
d = object.defensible
{ type: d.class.table_name,
name: d.name}

end

end
2 changes: 1 addition & 1 deletion lecture_2/homework/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Lecture1
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2

ActiveModelSerializers.config.adapter = :json_api
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
Expand Down
2 changes: 2 additions & 0 deletions lecture_2/homework/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ development:
test: &test
<<: *default
database: flode_test
host: localhost

4 changes: 3 additions & 1 deletion lecture_2/homework/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
resources :clans, only: %i[index create] do
resources :samurais, module: :clans
resources :warriors, module: :clans
end


end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateClansAndSamurais < ActiveRecord::Migration[5.2]
def change
create_table :clans do |t|
t.string :name, null: false
t.string :name, null: false, uniquness: true
t.timestamps
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeSamuraisToWarriors < ActiveRecord::Migration[5.2]
def change
rename_table :samurais, :warriors
add_column :warriors, :type, :string, default: 'Samurai'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDefensibleToWarriors < ActiveRecord::Migration[5.2]
def change
add_reference :warriors, :defensible, polymorphic: true, index: true
end
end
17 changes: 17 additions & 0 deletions lecture_2/homework/db/migrate/20190408202703_create_walls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateWalls < ActiveRecord::Migration[5.2]
def change
create_table :walls do |t|
t.string :name, null: false
t.timestamps
end
create_table :towers do |t|
t.string :name, null: false
t.timestamps
end
create_table :castles do |t|
t.string :name, null: false
t.timestamps
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddWeaponToWarrior < ActiveRecord::Migration[5.2]
def change

create_table :weapons do |t|
t.integer :damage, null: false
t.integer :range, null: false
t.timestamps
t.string :type, null: false
end

add_reference :weapons, :warrior

end
end
Loading