diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index f3629b6f..3c13aa5c 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -1,45 +1,26 @@ +# frozen_string_literal: true + source 'https://rubygems.org' 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 'bootsnap', '>= 1.1.0', require: false +gem 'hirb' gem 'pg' -# Use Puma as the app server 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' +gem 'rails', '~> 5.2.2' +gem 'fast_jsonapi' +gem "sentry-raven" 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] + 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 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] diff --git a/lecture_2/homework/Gemfile.lock b/lecture_2/homework/Gemfile.lock index ae757417..116988da 100644 --- a/lecture_2/homework/Gemfile.lock +++ b/lecture_2/homework/Gemfile.lock @@ -1,58 +1,63 @@ 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) rails-html-sanitizer (~> 1.0, >= 1.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) - bootsnap (1.4.1) + bootsnap (1.4.3) msgpack (~> 1.0) builder (3.2.3) - byebug (11.0.0) + byebug (11.0.1) concurrent-ruby (1.1.5) crass (1.0.4) erubi (1.8.0) + faraday (0.15.4) + multipart-post (>= 1.2, < 3) + fast_jsonapi (1.5) + activesupport (>= 4.2) ffi (1.10.0) globalid (0.4.2) activesupport (>= 4.2.0) + hirb (0.7.3) i18n (1.6.0) concurrent-ruby (~> 1.0) listen (3.1.5) @@ -72,35 +77,36 @@ 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) + nokogiri (1.10.2) mini_portile2 (~> 2.4.0) pg (1.1.4) - 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) @@ -109,6 +115,8 @@ GEM rb-inotify (0.10.0) ffi (~> 1.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) @@ -135,10 +143,13 @@ PLATFORMS DEPENDENCIES bootsnap (>= 1.1.0) byebug + fast_jsonapi + hirb listen (>= 3.0.5, < 3.2) pg puma (~> 3.11) rails (~> 5.2.2) + sentry-raven spring spring-watcher-listen (~> 2.0.0) tzinfo-data diff --git a/lecture_2/homework/Rakefile b/lecture_2/homework/Rakefile index e85f9139..488c551f 100644 --- a/lecture_2/homework/Rakefile +++ b/lecture_2/homework/Rakefile @@ -1,3 +1,5 @@ +# 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. diff --git a/lecture_2/homework/app/channels/application_cable/channel.rb b/lecture_2/homework/app/channels/application_cable/channel.rb index d6726972..9aec2305 100644 --- a/lecture_2/homework/app/channels/application_cable/channel.rb +++ b/lecture_2/homework/app/channels/application_cable/channel.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ApplicationCable class Channel < ActionCable::Channel::Base end diff --git a/lecture_2/homework/app/channels/application_cable/connection.rb b/lecture_2/homework/app/channels/application_cable/connection.rb index 0ff5442f..8d6c2a1b 100644 --- a/lecture_2/homework/app/channels/application_cable/connection.rb +++ b/lecture_2/homework/app/channels/application_cable/connection.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ApplicationCable class Connection < ActionCable::Connection::Base end diff --git a/lecture_2/homework/app/controllers/application_controller.rb b/lecture_2/homework/app/controllers/application_controller.rb index 891a2573..80e2b704 100644 --- a/lecture_2/homework/app/controllers/application_controller.rb +++ b/lecture_2/homework/app/controllers/application_controller.rb @@ -1,9 +1,13 @@ +# frozen_string_literal: true + class ApplicationController < ActionController::API - rescue_from ActiveRecord::RecordNotFound do |exception| - render json: { message: exception.message }, status: 404 - end + include ErrorHandler + before_action :set_raven_context + + private - rescue_from ActiveRecord::RecordInvalid do |exception| - render json: { message: exception.message }, status: 422 + def set_raven_context + Raven.user_context(id: session[:current_user_id]) # or anything else in session + Raven.extra_context(params: params.to_unsafe_h, url: request.url) end end diff --git a/lecture_2/homework/app/controllers/clans/samurais_controller.rb b/lecture_2/homework/app/controllers/clans/samurais_controller.rb deleted file mode 100644 index 59fef45f..00000000 --- a/lecture_2/homework/app/controllers/clans/samurais_controller.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Clans - class SamuraisController < ApplicationController - def show - render json: samurai.to_json - end - - def index - samurais = clan.samurais - if params.key?(:alive) - if params[:alive].to_i == 0 - render json: samurais.dead.to_json - else - render json: samurais.alive.to_json - end - else - render json: samurais.to_json - end - end - - def create - samurai = clan.samurais.create!(samurai_params) - - render json: samurai.to_json, status: 201 - end - - def update - samurai.update!(samurai_params) - - render json: samurai.to_json - end - - def destroy - samurai.destroy! - end - - private - - def clan - @clan ||= Clan.find(params[:clan_id]) - end - - def samurai - @samurai ||= Samurai.find_by!(id: params[:id], clan_id: params[:clan_id]) - end - - def samurai_params - params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date) - end - end -end diff --git a/lecture_2/homework/app/controllers/clans/warriors_controller.rb b/lecture_2/homework/app/controllers/clans/warriors_controller.rb new file mode 100644 index 00000000..cc2706c6 --- /dev/null +++ b/lecture_2/homework/app/controllers/clans/warriors_controller.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Clans + class WarriorsController < ApplicationController + def show + options = {} + options[:include] = [:defensible, :weapon] + + render json: WarriorSerializer.new(warrior, options) + end + + def index + warriors = clan.warriors + options = {} + options[:include] = [:defensible, :weapon] + + render json: WarriorSerializer.new(alive?(warriors), options) + end + + def create + warrior = clan.warriors.create!(warrior_params) + + render json: serialize_it(warrior), status: 201 + end + + def update + warrior.update!(warrior_params) + + render json: serialize_it(warrior) + end + + def destroy + warrior.destroy! + end + + private + + def serialize_it(options) + WarriorSerializer.new(options) + end + 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 alive?(warriors) + if params.key?(:alive) + if params[:alive].to_i == 0 + warriors.dead + else + warriors.alive + end + else + warriors + end + end + + def warrior_params + params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date, :kind, :attack, :equipment, :defensible_id, :defensible_type) + end + end +end diff --git a/lecture_2/homework/app/controllers/clans_controller.rb b/lecture_2/homework/app/controllers/clans_controller.rb index 37db247b..3e4e42dc 100644 --- a/lecture_2/homework/app/controllers/clans_controller.rb +++ b/lecture_2/homework/app/controllers/clans_controller.rb @@ -1,16 +1,31 @@ +# frozen_string_literal: true + class ClansController < ApplicationController def index - render json: Clan.all.to_json + clans = Clan.all + render json: serialize_it(clans).serializable_hash, status: :ok + end + + def show + clan = Clan.find(params[:id]) + options = {} + options[:include] = [:warriors] + render json: ClanSerializer.new(clan, options).serializable_hash + end def create clan = Clan.create!(clan_params) - render json: clan.to_json, status: 201 + render json: serialize_it(clan), status: 201 end private + def serialize_it(options) + ClanSerializer.new(options) + end + def clan_params params.permit(:name) end diff --git a/lecture_2/homework/app/jobs/application_job.rb b/lecture_2/homework/app/jobs/application_job.rb index a009ace5..d92ffddc 100644 --- a/lecture_2/homework/app/jobs/application_job.rb +++ b/lecture_2/homework/app/jobs/application_job.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class ApplicationJob < ActiveJob::Base end diff --git a/lecture_2/homework/app/mailers/application_mailer.rb b/lecture_2/homework/app/mailers/application_mailer.rb index 286b2239..d84cb6e7 100644 --- a/lecture_2/homework/app/mailers/application_mailer.rb +++ b/lecture_2/homework/app/mailers/application_mailer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationMailer < ActionMailer::Base default from: 'from@example.com' layout 'mailer' diff --git a/lecture_2/homework/app/models/application_record.rb b/lecture_2/homework/app/models/application_record.rb index 10a4cba8..71fbba5b 100644 --- a/lecture_2/homework/app/models/application_record.rb +++ b/lecture_2/homework/app/models/application_record.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end diff --git a/lecture_2/homework/app/models/clan.rb b/lecture_2/homework/app/models/clan.rb index 71f4e9de..207af761 100644 --- a/lecture_2/homework/app/models/clan.rb +++ b/lecture_2/homework/app/models/clan.rb @@ -1,4 +1,6 @@ +# frozen_string_literal: true + class Clan < ApplicationRecord - has_many :samurais, dependent: :destroy - validates :name, presence: true + has_many :warriors, dependent: :destroy + validates :name, presence: true, uniqueness: true end diff --git a/lecture_2/homework/app/models/concerns/error_handler.rb b/lecture_2/homework/app/models/concerns/error_handler.rb new file mode 100644 index 00000000..d5ed61c9 --- /dev/null +++ b/lecture_2/homework/app/models/concerns/error_handler.rb @@ -0,0 +1,16 @@ +module ErrorHandler + extend ActiveSupport::Concern + + included do + rescue_from ActiveRecord::RecordNotFound, with: :render_record_not_found + rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid + end + + def render_record_not_found(exception) + render json: { message: exception.message }, status: 404 + end + + def render_record_invalid(exception) + render json: { message: exception.message }, status: 422 + end +end diff --git a/lecture_2/homework/app/models/constructions/barricade.rb b/lecture_2/homework/app/models/constructions/barricade.rb new file mode 100644 index 00000000..519ecbc4 --- /dev/null +++ b/lecture_2/homework/app/models/constructions/barricade.rb @@ -0,0 +1,5 @@ +module Constructions + class Barricade < ApplicationRecord + has_many :warriors, as: :defensible + end +end diff --git a/lecture_2/homework/app/models/constructions/battering_ram.rb b/lecture_2/homework/app/models/constructions/battering_ram.rb new file mode 100644 index 00000000..7b76e603 --- /dev/null +++ b/lecture_2/homework/app/models/constructions/battering_ram.rb @@ -0,0 +1,5 @@ +module Constructions + class BatteringRam < ApplicationRecord + has_many :warriors, as: :defensible + end +end diff --git a/lecture_2/homework/app/models/constructions/bunker.rb b/lecture_2/homework/app/models/constructions/bunker.rb new file mode 100644 index 00000000..84804406 --- /dev/null +++ b/lecture_2/homework/app/models/constructions/bunker.rb @@ -0,0 +1,5 @@ +module Constructions + class Bunker < ApplicationRecord + has_many :warriors, as: :defensible + end +end diff --git a/lecture_2/homework/app/models/constructions/stronghold.rb b/lecture_2/homework/app/models/constructions/stronghold.rb new file mode 100644 index 00000000..797d65cf --- /dev/null +++ b/lecture_2/homework/app/models/constructions/stronghold.rb @@ -0,0 +1,5 @@ +module Constructions + class Stronghold < ApplicationRecord + has_many :warriors, as: :defensible + end +end diff --git a/lecture_2/homework/app/models/constructions/trench.rb b/lecture_2/homework/app/models/constructions/trench.rb new file mode 100644 index 00000000..1ec95ed4 --- /dev/null +++ b/lecture_2/homework/app/models/constructions/trench.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Constructions + class Trench < ApplicationRecord + has_many :warriors, as: :defensible + end +end diff --git a/lecture_2/homework/app/models/samurai.rb b/lecture_2/homework/app/models/samurai.rb deleted file mode 100644 index b2fafebb..00000000 --- a/lecture_2/homework/app/models/samurai.rb +++ /dev/null @@ -1,11 +0,0 @@ -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') } -end diff --git a/lecture_2/homework/app/models/warrior.rb b/lecture_2/homework/app/models/warrior.rb new file mode 100644 index 00000000..2a40a7b2 --- /dev/null +++ b/lecture_2/homework/app/models/warrior.rb @@ -0,0 +1,26 @@ +class Warrior < ApplicationRecord + after_create :leads_of_warrior + belongs_to :defensible, polymorphic: true, optional: true + belongs_to :clan + has_one :weapon, dependent: :destroy + + validates :name, presence: true + validates :armor_quality, numericality: { only_integer: true, + greater_than_or_equal_to: 0, + less_than_or_equal_to: 100 } + validates :kind, inclusion: { in: %w[Samurai Hussar Archer Peasant Wizard] } + + validates_uniqueness_of :name, scope: %i[clan_id death_date] + validates :equipment, inclusion: { in: %w[Bow Katana Wand Lance Sword Stick_with_shit] }, allow_nil: true + + scope :alive, -> { where('death_date IS NULL') } + scope :dead, -> { where('death_date IS NOT NULL') } + + def attack + equipment ? "Use #{equipment}" : "Punch" + end + + def leads_of_warrior + puts "++++ You've created new #{kind} #{name}!++++\n---- Your clan #{Clan.find(clan_id).name} has now #{Clan.find(clan_id).warriors.count} warriors----\n" + end +end diff --git a/lecture_2/homework/app/models/warriors/archer.rb b/lecture_2/homework/app/models/warriors/archer.rb new file mode 100644 index 00000000..b268af91 --- /dev/null +++ b/lecture_2/homework/app/models/warriors/archer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Warriors + class Archer < Warrior + validates :equipment, inclusion: { in: 'Bow' }, allow_nil: true + + def weapons_of_choice + ["Bow"] + end + end +end diff --git a/lecture_2/homework/app/models/warriors/hussar.rb b/lecture_2/homework/app/models/warriors/hussar.rb new file mode 100644 index 00000000..0634c2f5 --- /dev/null +++ b/lecture_2/homework/app/models/warriors/hussar.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Warriors + class Hussar < Warrior + validates :equipment, inclusion: { in: 'Bow' }, allow_nil: true + def attack + "#{name} use #{equipment}" + end + def weapons_of_choice + %w[ lance sword ] + end + end +end diff --git a/lecture_2/homework/app/models/warriors/peasant.rb b/lecture_2/homework/app/models/warriors/peasant.rb new file mode 100644 index 00000000..95903bbd --- /dev/null +++ b/lecture_2/homework/app/models/warriors/peasant.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Warriors + class Peasant < Warrior + validates :equipment, inclusion: { in: 'Bow' }, allow_nil: true + def attack + "#{name} use #{equipment}" + end + def weapons_of_choice + %w[ stick_with_shit] + end + end +end diff --git a/lecture_2/homework/app/models/warriors/samurai.rb b/lecture_2/homework/app/models/warriors/samurai.rb new file mode 100644 index 00000000..3cbc800c --- /dev/null +++ b/lecture_2/homework/app/models/warriors/samurai.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Warriors + class Samurai < Warrior + validates :equipment, inclusion: { in: 'Bow' }, allow_nil: true + def attack + "#{name} use #{equipment}" + end + def weapons_of_choice + %w[ katana bow] + end + end +end diff --git a/lecture_2/homework/app/models/warriors/wizard.rb b/lecture_2/homework/app/models/warriors/wizard.rb new file mode 100644 index 00000000..5413f298 --- /dev/null +++ b/lecture_2/homework/app/models/warriors/wizard.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Warriors + class Wizard < Warrior + EFFECTS = %w[kills misses].freeze + def attack + "#{name} use #{equipment}, and #{EFFECTS.sample}" + end + def weapons_of_choice + ["wand"] + end + + validates :equipment, inclusion: { in: WEAPONS }, allow_nil: true + end +end diff --git a/lecture_2/homework/app/models/weapon.rb b/lecture_2/homework/app/models/weapon.rb new file mode 100644 index 00000000..36133ace --- /dev/null +++ b/lecture_2/homework/app/models/weapon.rb @@ -0,0 +1,14 @@ +class Weapon < ApplicationRecord + belongs_to :warrior + validate :is_this_apropriate_weapon + validates :range, :damage, :species, presence: true + validates :species, inclusion: { in: %w[Magical Melee Ranged] } + + def is_this_apropriate_weapon + if warrior + if warrior.equipment != species + errors.add(:species, 'This weapon cannot be used by this Warrior') + end + end + end +end diff --git a/lecture_2/homework/app/models/weapons/bow.rb b/lecture_2/homework/app/models/weapons/bow.rb new file mode 100644 index 00000000..c4043645 --- /dev/null +++ b/lecture_2/homework/app/models/weapons/bow.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Weapons + class Bow < Weapon + before_create :set_species + + def calculate_damage(distance) + if distance <= 2 || distance > range + @effect = 0 + else + calculate_luck(distance) + @effect = (damage - distance) * @luck + end + end + + private + + def calculate_luck(distance) + @luck = if rand(range) - distance >= 0 + 1 + else + 0 + end + end + + def set_species + self.species = 'Ranged' + end + end +end diff --git a/lecture_2/homework/app/models/weapons/katana.rb b/lecture_2/homework/app/models/weapons/katana.rb new file mode 100644 index 00000000..b40014f0 --- /dev/null +++ b/lecture_2/homework/app/models/weapons/katana.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Weapon + class Katana < Weapon + def calculate_damage(distance) + effect = damage + effect = 0 if distance > range + effect + end + end +end diff --git a/lecture_2/homework/app/models/weapons/lance.rb b/lecture_2/homework/app/models/weapons/lance.rb new file mode 100644 index 00000000..ed0d39a4 --- /dev/null +++ b/lecture_2/homework/app/models/weapons/lance.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Weapons + class Lance < Weapon + def calculate_damage(distance) + effect = damage + effect = 0 if distance > range + effect + end + end +end diff --git a/lecture_2/homework/app/models/weapons/stick_with_shit.rb b/lecture_2/homework/app/models/weapons/stick_with_shit.rb new file mode 100644 index 00000000..329afd2c --- /dev/null +++ b/lecture_2/homework/app/models/weapons/stick_with_shit.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Weapons + class StickWithShit < Weapon + def calculate_damage(distance) + effect = damage + effect = 0 if distance > range + effect + end + end +end diff --git a/lecture_2/homework/app/models/weapons/sword.rb b/lecture_2/homework/app/models/weapons/sword.rb new file mode 100644 index 00000000..17ce85c2 --- /dev/null +++ b/lecture_2/homework/app/models/weapons/sword.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Weapons + class Sword < Weapon + def calculate_damage(distance) + effect = damage + effect = 0 if distance > range + effect + end + end +end diff --git a/lecture_2/homework/app/models/weapons/wand.rb b/lecture_2/homework/app/models/weapons/wand.rb new file mode 100644 index 00000000..8a04dd52 --- /dev/null +++ b/lecture_2/homework/app/models/weapons/wand.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Weapons + class Wand < Weapon + before_create :set_species + + def calculate_damage(distance) + if distance <= 2 || distance > range + @effect = 0 + else + calculate_luck(distance) + @effect = (damage - distance) * @luck + end + end + + private + + def calculate_luck(distance) + @luck = if rand(range) - distance >= 0 + 1 + else + 0 + end + end + + def set_species + self.species = 'Magical' + end + end +end diff --git a/lecture_2/homework/app/serializers/clan_serializer.rb b/lecture_2/homework/app/serializers/clan_serializer.rb new file mode 100644 index 00000000..4aa62342 --- /dev/null +++ b/lecture_2/homework/app/serializers/clan_serializer.rb @@ -0,0 +1,7 @@ +class ClanSerializer + include FastJsonapi::ObjectSerializer + + has_many :warriors + set_id :id + attributes :name +end diff --git a/lecture_2/homework/app/serializers/defensible_serializer.rb b/lecture_2/homework/app/serializers/defensible_serializer.rb new file mode 100644 index 00000000..44a516db --- /dev/null +++ b/lecture_2/homework/app/serializers/defensible_serializer.rb @@ -0,0 +1,5 @@ +class DefensibleSerializer + include FastJsonapi::ObjectSerializer + attributes :id + has_many :warriors, polymorfic: true +end diff --git a/lecture_2/homework/app/serializers/warrior_serializer.rb b/lecture_2/homework/app/serializers/warrior_serializer.rb new file mode 100644 index 00000000..1b98a289 --- /dev/null +++ b/lecture_2/homework/app/serializers/warrior_serializer.rb @@ -0,0 +1,8 @@ +class WarriorSerializer + include FastJsonapi::ObjectSerializer + attributes :name, :clan_id, :death_date, :armor_quality, :number_of_battles, + :join_date, :kind, :attack, :equipment, :defensible_type + belongs_to :clan + belongs_to :defensible, polymorphic: true + has_one :weapon +end diff --git a/lecture_2/homework/app/serializers/weapon_serializer.rb b/lecture_2/homework/app/serializers/weapon_serializer.rb new file mode 100644 index 00000000..1956deff --- /dev/null +++ b/lecture_2/homework/app/serializers/weapon_serializer.rb @@ -0,0 +1,4 @@ +class WeaponSerializer + include FastJsonapi::ObjectSerializer + attributes :damage, :range, :species +end diff --git a/lecture_2/homework/bin/bundle b/lecture_2/homework/bin/bundle index f19acf5b..2dbb7176 100755 --- a/lecture_2/homework/bin/bundle +++ b/lecture_2/homework/bin/bundle @@ -1,3 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) load Gem.bin_path('bundler', 'bundle') diff --git a/lecture_2/homework/bin/rails b/lecture_2/homework/bin/rails index 5badb2fd..3504c3f9 100755 --- a/lecture_2/homework/bin/rails +++ b/lecture_2/homework/bin/rails @@ -1,6 +1,8 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + begin - load File.expand_path('../spring', __FILE__) + load File.expand_path('spring', __dir__) rescue LoadError => e raise unless e.message.include?('spring') end diff --git a/lecture_2/homework/bin/rake b/lecture_2/homework/bin/rake index d87d5f57..1fe6cf06 100755 --- a/lecture_2/homework/bin/rake +++ b/lecture_2/homework/bin/rake @@ -1,6 +1,8 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + begin - load File.expand_path('../spring', __FILE__) + load File.expand_path('spring', __dir__) rescue LoadError => e raise unless e.message.include?('spring') end diff --git a/lecture_2/homework/bin/setup b/lecture_2/homework/bin/setup index a334d86a..f17c5668 100755 --- a/lecture_2/homework/bin/setup +++ b/lecture_2/homework/bin/setup @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + require 'fileutils' include FileUtils diff --git a/lecture_2/homework/bin/spring b/lecture_2/homework/bin/spring index fb2ec2eb..9bd27ec6 100755 --- a/lecture_2/homework/bin/spring +++ b/lecture_2/homework/bin/spring @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true # This file loads spring without using Bundler, in order to be fast. # It gets overwritten when you run the `spring binstub` command. @@ -8,7 +9,7 @@ unless defined?(Spring) require 'bundler' lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) - spring = lockfile.specs.detect { |spec| spec.name == "spring" } + spring = lockfile.specs.detect { |spec| spec.name == 'spring' } if spring Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path gem 'spring', spring.version diff --git a/lecture_2/homework/bin/update b/lecture_2/homework/bin/update index 67d0d496..0b5306f4 100755 --- a/lecture_2/homework/bin/update +++ b/lecture_2/homework/bin/update @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + require 'fileutils' include FileUtils diff --git a/lecture_2/homework/config.ru b/lecture_2/homework/config.ru index f7ba0b52..842bccc3 100644 --- a/lecture_2/homework/config.ru +++ b/lecture_2/homework/config.ru @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file is used by Rack-based servers to start the application. require_relative 'config/environment' diff --git a/lecture_2/homework/config/application.rb b/lecture_2/homework/config/application.rb index f5390e33..74b8469e 100644 --- a/lecture_2/homework/config/application.rb +++ b/lecture_2/homework/config/application.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require_relative 'boot' -require "rails" +require 'rails' # Pick the frameworks you want: -require "active_model/railtie" -require "active_job/railtie" -require "active_record/railtie" -require "active_storage/engine" -require "action_controller/railtie" -require "action_mailer/railtie" -require "action_view/railtie" -require "action_cable/engine" +require 'active_model/railtie' +require 'active_job/railtie' +require 'active_record/railtie' +require 'active_storage/engine' +require 'action_controller/railtie' +require 'action_mailer/railtie' +require 'action_view/railtie' +require 'action_cable/engine' # require "sprockets/railtie" -require "rails/test_unit/railtie" +require 'rails/test_unit/railtie' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -32,4 +34,9 @@ class Application < Rails::Application # Skip views, helpers and assets when generating a new resource. config.api_only = true end +#SENTRY +Raven.configure do |config| + config.dsn = 'https://87babab81c5344c496a16c9b2515b9e3:7661070b86c04bd48fdd136cd6e81fe8@sentry.io/1438693' +end + end diff --git a/lecture_2/homework/config/boot.rb b/lecture_2/homework/config/boot.rb index b9e460ce..c04863fa 100644 --- a/lecture_2/homework/config/boot.rb +++ b/lecture_2/homework/config/boot.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/lecture_2/homework/config/database.yml b/lecture_2/homework/config/database.yml index 965a5d46..4af2b962 100644 --- a/lecture_2/homework/config/database.yml +++ b/lecture_2/homework/config/database.yml @@ -1,11 +1,23 @@ default: &default adapter: postgresql encoding: unicode + username: + password: database: rorlevelup2019 development: <<: *default + username: + password: + encoding: unicode + database: rorlevelup2019 test: &test <<: *default database: flode_test + +production: + encoding: unicode + pool: 5 + username: + password: diff --git a/lecture_2/homework/config/environment.rb b/lecture_2/homework/config/environment.rb index 426333bb..d5abe558 100644 --- a/lecture_2/homework/config/environment.rb +++ b/lecture_2/homework/config/environment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Load the Rails application. require_relative 'application' diff --git a/lecture_2/homework/config/environments/development.rb b/lecture_2/homework/config/environments/development.rb index d52ec9ef..b6c18169 100644 --- a/lecture_2/homework/config/environments/development.rb +++ b/lecture_2/homework/config/environments/development.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -44,7 +46,6 @@ # Highlight code that triggered database queries in logs. config.active_record.verbose_query_logs = true - # Raises error for missing translations # config.action_view.raise_on_missing_translations = true diff --git a/lecture_2/homework/config/environments/production.rb b/lecture_2/homework/config/environments/production.rb index 77930568..7be304fb 100644 --- a/lecture_2/homework/config/environments/production.rb +++ b/lecture_2/homework/config/environments/production.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -45,7 +47,7 @@ config.log_level = :debug # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + config.log_tags = [:request_id] # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -74,7 +76,7 @@ # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') - if ENV["RAILS_LOG_TO_STDOUT"].present? + if ENV['RAILS_LOG_TO_STDOUT'].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) diff --git a/lecture_2/homework/config/environments/test.rb b/lecture_2/homework/config/environments/test.rb index 0a38fd3c..3091ac4b 100644 --- a/lecture_2/homework/config/environments/test.rb +++ b/lecture_2/homework/config/environments/test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/lecture_2/homework/config/initializers/application_controller_renderer.rb b/lecture_2/homework/config/initializers/application_controller_renderer.rb index 89d2efab..6d56e439 100644 --- a/lecture_2/homework/config/initializers/application_controller_renderer.rb +++ b/lecture_2/homework/config/initializers/application_controller_renderer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # ActiveSupport::Reloader.to_prepare do diff --git a/lecture_2/homework/config/initializers/backtrace_silencers.rb b/lecture_2/homework/config/initializers/backtrace_silencers.rb index 59385cdf..4b63f289 100644 --- a/lecture_2/homework/config/initializers/backtrace_silencers.rb +++ b/lecture_2/homework/config/initializers/backtrace_silencers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. diff --git a/lecture_2/homework/config/initializers/cors.rb b/lecture_2/homework/config/initializers/cors.rb index 3b1c1b5e..82eafe5c 100644 --- a/lecture_2/homework/config/initializers/cors.rb +++ b/lecture_2/homework/config/initializers/cors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Avoid CORS issues when API is called from the frontend app. diff --git a/lecture_2/homework/config/initializers/filter_parameter_logging.rb b/lecture_2/homework/config/initializers/filter_parameter_logging.rb index 4a994e1e..7a4f47b4 100644 --- a/lecture_2/homework/config/initializers/filter_parameter_logging.rb +++ b/lecture_2/homework/config/initializers/filter_parameter_logging.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Configure sensitive parameters which will be filtered from the log file. diff --git a/lecture_2/homework/config/initializers/inflections.rb b/lecture_2/homework/config/initializers/inflections.rb index ac033bf9..dc847422 100644 --- a/lecture_2/homework/config/initializers/inflections.rb +++ b/lecture_2/homework/config/initializers/inflections.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Add new inflection rules using the following format. Inflections diff --git a/lecture_2/homework/config/initializers/mime_types.rb b/lecture_2/homework/config/initializers/mime_types.rb index dc189968..be6fedc5 100644 --- a/lecture_2/homework/config/initializers/mime_types.rb +++ b/lecture_2/homework/config/initializers/mime_types.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Add new mime types for use in respond_to blocks: diff --git a/lecture_2/homework/config/initializers/sentry.rb b/lecture_2/homework/config/initializers/sentry.rb new file mode 100644 index 00000000..dc1138b7 --- /dev/null +++ b/lecture_2/homework/config/initializers/sentry.rb @@ -0,0 +1,4 @@ +Raven.configure do |config| + config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s) +end + diff --git a/lecture_2/homework/config/initializers/wrap_parameters.rb b/lecture_2/homework/config/initializers/wrap_parameters.rb index bbfc3961..2f3c0db4 100644 --- a/lecture_2/homework/config/initializers/wrap_parameters.rb +++ b/lecture_2/homework/config/initializers/wrap_parameters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # This file contains settings for ActionController::ParamsWrapper which diff --git a/lecture_2/homework/config/puma.rb b/lecture_2/homework/config/puma.rb index a5eccf81..940c8ba4 100644 --- a/lecture_2/homework/config/puma.rb +++ b/lecture_2/homework/config/puma.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + # Puma can serve each request in a thread from an internal thread pool. # The `threads` method setting takes two numbers: a minimum and maximum. # Any libraries that use thread pools should be configured to match # the maximum value specified for Puma. Default is set to 5 threads for minimum # and maximum; this matches the default thread size of Active Record. # -threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 } threads threads_count, threads_count # Specifies the `port` that Puma will listen on to receive requests; default is 3000. # -port ENV.fetch("PORT") { 3000 } +port ENV.fetch('PORT') { 3000 } # Specifies the `environment` that Puma will run in. # -environment ENV.fetch("RAILS_ENV") { "development" } +environment ENV.fetch('RAILS_ENV') { 'development' } # Specifies the number of `workers` to boot in clustered mode. # Workers are forked webserver processes. If using threads and workers together diff --git a/lecture_2/homework/config/routes.rb b/lecture_2/homework/config/routes.rb index 9c03cd59..5ba5517e 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + Rails.application.routes.draw do - resources :clans, only: %i[index create] do - resources :samurais, module: :clans + resources :clans, only: %i[index create show] do + resources :warriors, module: :clans end end diff --git a/lecture_2/homework/config/spring.rb b/lecture_2/homework/config/spring.rb index 9fa7863f..c5933e49 100644 --- a/lecture_2/homework/config/spring.rb +++ b/lecture_2/homework/config/spring.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + %w[ .ruby-version .rbenv-vars diff --git a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb index ebfc3158..e13368e8 100644 --- a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb +++ b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateClansAndSamurais < ActiveRecord::Migration[5.2] def change create_table :clans do |t| diff --git a/lecture_2/homework/db/migrate/20190408232733_add_index_to_clan.rb b/lecture_2/homework/db/migrate/20190408232733_add_index_to_clan.rb new file mode 100644 index 00000000..05e80c93 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190408232733_add_index_to_clan.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddIndexToClan < ActiveRecord::Migration[5.2] + def change + add_index :clans, :name, unique: true + end +end diff --git a/lecture_2/homework/db/migrate/20190408234130_change_samurais_to_warriors.rb b/lecture_2/homework/db/migrate/20190408234130_change_samurais_to_warriors.rb new file mode 100644 index 00000000..e0a1af4e --- /dev/null +++ b/lecture_2/homework/db/migrate/20190408234130_change_samurais_to_warriors.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class ChangeSamuraisToWarriors < ActiveRecord::Migration[5.2] + def change + rename_table :samurais, :warriors + add_column :warriors, :type, :string, default: 'Samurai' + end +end diff --git a/lecture_2/homework/db/migrate/20190408235018_change_type_to_kind_in_warriors.rb b/lecture_2/homework/db/migrate/20190408235018_change_type_to_kind_in_warriors.rb new file mode 100644 index 00000000..fb3914dc --- /dev/null +++ b/lecture_2/homework/db/migrate/20190408235018_change_type_to_kind_in_warriors.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class ChangeTypeToKindInWarriors < ActiveRecord::Migration[5.2] + def change + remove_column :warriors, :type + add_column :warriors, :kind, :string, default: 'Samurai' + end +end diff --git a/lecture_2/homework/db/migrate/20190409000706_add_attack_to_warriors.rb b/lecture_2/homework/db/migrate/20190409000706_add_attack_to_warriors.rb new file mode 100644 index 00000000..c7a9ab00 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409000706_add_attack_to_warriors.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddAttackToWarriors < ActiveRecord::Migration[5.2] + def change + add_column :warriors, :attack, :string, default: 'Punch' + add_column :warriors, :weapon, :string + end +end diff --git a/lecture_2/homework/db/migrate/20190409001745_create_samurais.rb b/lecture_2/homework/db/migrate/20190409001745_create_samurais.rb new file mode 100644 index 00000000..c63c7f4d --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409001745_create_samurais.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateSamurais < ActiveRecord::Migration[5.2] + def change + create_table :samurais, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409001812_create_hussars.rb b/lecture_2/homework/db/migrate/20190409001812_create_hussars.rb new file mode 100644 index 00000000..6e13d647 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409001812_create_hussars.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateHussars < ActiveRecord::Migration[5.2] + def change + create_table :hussars, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409001827_create_archers.rb b/lecture_2/homework/db/migrate/20190409001827_create_archers.rb new file mode 100644 index 00000000..a8efbb52 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409001827_create_archers.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateArchers < ActiveRecord::Migration[5.2] + def change + create_table :archers, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409001837_create_peasants.rb b/lecture_2/homework/db/migrate/20190409001837_create_peasants.rb new file mode 100644 index 00000000..48723e45 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409001837_create_peasants.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreatePeasants < ActiveRecord::Migration[5.2] + def change + create_table :peasants, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409002823_create_wizards.rb b/lecture_2/homework/db/migrate/20190409002823_create_wizards.rb new file mode 100644 index 00000000..7254ed1d --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409002823_create_wizards.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateWizards < ActiveRecord::Migration[5.2] + def change + create_table :wizards, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409012120_rmove_weapon_from_warriors.rb b/lecture_2/homework/db/migrate/20190409012120_rmove_weapon_from_warriors.rb new file mode 100644 index 00000000..91cdcd30 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409012120_rmove_weapon_from_warriors.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class RmoveWeaponFromWarriors < ActiveRecord::Migration[5.2] + def change + remove_column :warriors, :weapon + end +end diff --git a/lecture_2/homework/db/migrate/20190409122650_create_barricades.rb b/lecture_2/homework/db/migrate/20190409122650_create_barricades.rb new file mode 100644 index 00000000..a8d4ea91 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409122650_create_barricades.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateBarricades < ActiveRecord::Migration[5.2] + def change + create_table :barricades, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409123034_create_strongholds.rb b/lecture_2/homework/db/migrate/20190409123034_create_strongholds.rb new file mode 100644 index 00000000..e7e1fbf3 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409123034_create_strongholds.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateStrongholds < ActiveRecord::Migration[5.2] + def change + create_table :strongholds, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409123246_create_battering_rams.rb b/lecture_2/homework/db/migrate/20190409123246_create_battering_rams.rb new file mode 100644 index 00000000..5e06a844 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409123246_create_battering_rams.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateBatteringRams < ActiveRecord::Migration[5.2] + def change + create_table :battering_rams, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409123359_create_bunkers.rb b/lecture_2/homework/db/migrate/20190409123359_create_bunkers.rb new file mode 100644 index 00000000..9d00da53 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409123359_create_bunkers.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateBunkers < ActiveRecord::Migration[5.2] + def change + create_table :bunkers, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409123412_create_trenches.rb b/lecture_2/homework/db/migrate/20190409123412_create_trenches.rb new file mode 100644 index 00000000..cd70d139 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409123412_create_trenches.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateTrenches < ActiveRecord::Migration[5.2] + def change + create_table :trenches, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190409123449_add_defensible_to_warriors.rb b/lecture_2/homework/db/migrate/20190409123449_add_defensible_to_warriors.rb new file mode 100644 index 00000000..c465908b --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409123449_add_defensible_to_warriors.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddDefensibleToWarriors < ActiveRecord::Migration[5.2] + def change + add_reference :warriors, :defensible, polymorphic: true, index: true + end +end diff --git a/lecture_2/homework/db/migrate/20190410034630_create_swords.rb b/lecture_2/homework/db/migrate/20190410034630_create_swords.rb new file mode 100644 index 00000000..767e040b --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410034630_create_swords.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateSwords < ActiveRecord::Migration[5.2] + def change + create_table :swords, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410034638_create_bows.rb b/lecture_2/homework/db/migrate/20190410034638_create_bows.rb new file mode 100644 index 00000000..96fd106d --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410034638_create_bows.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateBows < ActiveRecord::Migration[5.2] + def change + create_table :bows, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410034724_create_lances.rb b/lecture_2/homework/db/migrate/20190410034724_create_lances.rb new file mode 100644 index 00000000..ee7524ac --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410034724_create_lances.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateLances < ActiveRecord::Migration[5.2] + def change + create_table :lances, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410034743_create_stick_with_shits.rb b/lecture_2/homework/db/migrate/20190410034743_create_stick_with_shits.rb new file mode 100644 index 00000000..ee19e273 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410034743_create_stick_with_shits.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateStickWithShits < ActiveRecord::Migration[5.2] + def change + create_table :stick_with_shits, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410034821_create_wands.rb b/lecture_2/homework/db/migrate/20190410034821_create_wands.rb new file mode 100644 index 00000000..0274648f --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410034821_create_wands.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateWands < ActiveRecord::Migration[5.2] + def change + create_table :wands, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410035452_create_katanas.rb b/lecture_2/homework/db/migrate/20190410035452_create_katanas.rb new file mode 100644 index 00000000..c1aaf9fb --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410035452_create_katanas.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class CreateKatanas < ActiveRecord::Migration[5.2] + def change + create_table :katanas, &:timestamps + end +end diff --git a/lecture_2/homework/db/migrate/20190410043659_create_weapons.rb b/lecture_2/homework/db/migrate/20190410043659_create_weapons.rb new file mode 100644 index 00000000..3192352e --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410043659_create_weapons.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CreateWeapons < ActiveRecord::Migration[5.2] + def change + create_table :weapons do |t| + t.string :species + t.integer :damage + t.integer :range + t.references :warrior, foreign_key: true + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190410054437_default_value_in_weapons_species.rb b/lecture_2/homework/db/migrate/20190410054437_default_value_in_weapons_species.rb new file mode 100644 index 00000000..8f4d52ca --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410054437_default_value_in_weapons_species.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class DefaultValueInWeaponsSpecies < ActiveRecord::Migration[5.2] + def change + remove_column :weapons, :species + add_column :weapons, :species, :string, default: 'Meele' + end +end diff --git a/lecture_2/homework/db/migrate/20190410054724_change_defaul_value_because_of_syntax.rb b/lecture_2/homework/db/migrate/20190410054724_change_defaul_value_because_of_syntax.rb new file mode 100644 index 00000000..88add426 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410054724_change_defaul_value_because_of_syntax.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class ChangeDefaulValueBecauseOfSyntax < ActiveRecord::Migration[5.2] + def change + remove_column :weapons, :species + add_column :weapons, :species, :string, default: 'Melee' + end +end diff --git a/lecture_2/homework/db/migrate/20190410073723_add_weapon_to_warrior.rb b/lecture_2/homework/db/migrate/20190410073723_add_weapon_to_warrior.rb new file mode 100644 index 00000000..c509fbd5 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190410073723_add_weapon_to_warrior.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddWeaponToWarrior < ActiveRecord::Migration[5.2] + def change + add_column :warriors, :equipment, :string, null: true + end +end diff --git a/lecture_2/homework/db/migrate/20190414090925_remove_attack_from_warrior.rb b/lecture_2/homework/db/migrate/20190414090925_remove_attack_from_warrior.rb new file mode 100644 index 00000000..799b091e --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414090925_remove_attack_from_warrior.rb @@ -0,0 +1,5 @@ +class RemoveAttackFromWarrior < ActiveRecord::Migration[5.2] + def change + remove_column :warriors, :attack + end +end diff --git a/lecture_2/homework/db/schema.rb b/lecture_2/homework/db/schema.rb index 98a644ef..78186916 100644 --- a/lecture_2/homework/db/schema.rb +++ b/lecture_2/homework/db/schema.rb @@ -10,18 +10,94 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_14_150921) do +ActiveRecord::Schema.define(version: 2019_04_14_090925) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "archers", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "barricades", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "battering_rams", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "bows", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "bunkers", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "clans", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["name"], name: "index_clans_on_name", unique: true + end + + create_table "hussars", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "katanas", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "lances", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "peasants", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "samurais", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "stick_with_shits", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "strongholds", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "swords", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "trenches", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "wands", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "warriors", force: :cascade do |t| t.string "name", null: false t.integer "armor_quality", default: 0 t.integer "number_of_battles", default: 0 @@ -30,8 +106,29 @@ t.bigint "clan_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["clan_id"], name: "index_samurais_on_clan_id" + t.string "kind", default: "Samurai" + t.string "defensible_type" + t.bigint "defensible_id" + t.string "equipment" + t.index ["clan_id"], name: "index_warriors_on_clan_id" + t.index ["defensible_type", "defensible_id"], name: "index_warriors_on_defensible_type_and_defensible_id" + end + + create_table "weapons", force: :cascade do |t| + t.integer "damage" + t.integer "range" + t.bigint "warrior_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "species", default: "Melee" + t.index ["warrior_id"], name: "index_weapons_on_warrior_id" + end + + create_table "wizards", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - add_foreign_key "samurais", "clans" + add_foreign_key "warriors", "clans" + add_foreign_key "weapons", "warriors" end diff --git a/lecture_2/homework/db/seeds.rb b/lecture_2/homework/db/seeds.rb index 1beea2ac..04013e2a 100644 --- a/lecture_2/homework/db/seeds.rb +++ b/lecture_2/homework/db/seeds.rb @@ -1,7 +1,46 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +Clan.delete_all + +CLAN_NAMES = %w[ Yakuza Zaibatsu Ku_Kul_Ska Loonies Scientists Hare_Krishna Rednecs Russian Yutes ].freeze +WARRIORS_KINDS = %w[ Archer Hussar Peasant Samurai Wizard ] + + +WARRIORS_NAME = ['Akihiko', 'Akihiro', 'Akihisa', 'Akihito', 'Akimasa', 'Akimitsu', 'Akinari', 'Akinobu', 'Akinori', 'Akio', 'Akisada', 'Akitake', 'Akito', 'Akitoshi', 'Akitsugu', 'Akiya', 'Akiyoshi', 'Akiyuki', 'Arata', 'Arihiro', 'Arinaga', 'Arinobu', 'Aritomo', 'Asahiko', 'Asao', 'Atomu', 'Atsuhiko', 'Atsuhiro', 'Atsuji', 'Atsuki', 'Atsunobu', 'Atsuo', 'Atsushi', 'Atsuto', 'Atsuya', 'Azuma', 'Banri', 'Bunji', 'Bunta', 'Chikao', 'Chikashi', 'Chikayoshi', 'Dai', 'Daichi', 'Daigo', 'Daihachi', 'Daiji', 'Daiju', 'Daiki', 'Dairoku', 'Daisaku', 'Daishi', 'Daishin', 'Daisuke', 'Eiichi', 'Eiichiro', 'Eiji', 'Eikichi', 'Einosuke', 'Eisaku', 'Eisen', 'Eishun', 'Eisuke', 'Eita', 'Emon', 'Etsuji', 'Fujio', 'Fumiaki', 'Fumihiko', 'Fumihiro', 'Fuminori', 'Fumio', 'Fumitaka', 'Fumito', 'Fumiya', 'Fusanosuke', 'FusazaneFutoshi', 'Gaku', 'Gakuto', 'Gen', 'Hakaru', 'Haruaki', 'Haruchika', 'Haruhiko', 'Haruhiro', 'Haruhisa', 'Haruki', 'Harukichi', 'Harunobu', 'Harunori', 'Haruo', 'Harutaka', 'Haruto', 'Haruya', 'Haruyoshi', 'Hatsuo', 'Hayanari', 'Hayao', 'Hayata', 'Hayato', 'Hideaki', 'Hideharu', 'Hidehiko', 'Hidehisa', 'Hidehito', 'Hideji', 'Hidekazu', 'Hideki', 'Hidemaro', 'Hidemasa', 'Hidemi', 'Hidemitsu', 'Hidenobu', 'Hidenori', 'Hideo', 'Hideshi', 'Hirofumi', 'Hirohide', 'Hirohisa', 'Hiroji', 'Hirokatsu', 'Hirokazu', 'Hiroki', 'Hirokuni', 'Hiromasa', 'Hiromichi', 'Hiromitsu', 'Hiromori', 'Hiromoto', 'Hironari', 'Hironobu', 'Hironori', 'Hirotami', 'Hiroto', 'Hirotoki', 'Hirotomo', 'Hirotoshi', 'Hirotsugu', 'Hiroya', 'Hiroyasu', 'Hiroyoshi', 'Hiroyuki', 'Hisaharu', 'Hisahito', 'Hisaichi', 'Hisaki', 'HisamitsuHisamoto', 'Hisanobu', 'Hisanori', 'Hisao', 'Hisashi', 'Hisataka', 'Hisateru', 'Hisato', 'Hisatsugu', 'Hisayasu', 'Hisayoshi', 'Hisayuki', 'Hitoshi', 'Hokuto', 'Hozumi', 'Iehiro', 'Iehisa', 'Iemasa', 'Iemon', 'Iesada', 'Ikki', '(given', 'name)', 'Ikko', 'Ikuo', 'Ippei', 'Isami', 'Isamu', 'Isao', 'Issei', 'Junki', 'Junpei', 'Junto', 'Kagemori', 'Kagenori', 'Kagetaka', 'Kaichi', 'Kaii', 'Kaiji', 'Kaisei', 'Kaito', 'Kakichi', 'Kaku', '(name)', 'Kakuji', 'Kamon', '(name)', 'Kanehira', 'Kanehiro', 'Kanehisa', 'Kanemaru', 'Kanematsu', 'Kanemoto', 'Kanesuke', 'Kanetake', 'Kaneto', 'Kanetsugu', 'Kaneyoshi', 'Katsuhiro', 'Katsuhisa', 'Katsuhito', 'Katsuji', 'Katsuki', 'Katsukiyo', 'Katsumasa', 'Katsumoto', 'Katsunaga', 'Katsunari', 'Katsunori', 'Katsunosuke', 'Katsuo', 'Katsushi', 'Katsusuke', 'Kunihiko', 'Kunihiro', 'Kunihisa', 'Kunimasa', 'Kunimitsu', 'Kunio', 'Kunishige', 'Kunitake', 'Kuniyoshi', 'Kuniyuki', 'Kuranosuke', 'Kusuo', 'Mahiro', 'Makio', 'Makito', 'Mamoru', 'Manabu', 'Masabumi', 'Masachika', 'Masafumi', 'Masaharu', 'Masahide', 'Masahiko', 'Masahiro', 'Masahisa', 'Masahito', 'Masaichi', 'Masaie', 'Masaji', 'Masakage', 'Masakatsu', 'Masakazu', 'Masakuni', 'Masamichi', 'Masamitsu', 'Masamori', 'Masamune', '(disambiguation)', 'Masamura', 'Masanao', 'Masanari', 'Masanobu', 'Masanori', 'Masanosuke', 'Masao', 'Masaomi', 'Masaru', 'Masashi', 'Masashige', 'Masataka', 'Masatak', 'Mitsugi', 'Mitsugu', 'Mitsuharu', 'Mitsuhide', 'Mitsuhiko', 'Mitsuhira', 'Mitsuhiro', 'Mitsuhisa', 'Mitsumasa', 'Mitsumori', 'Mitsunobu', 'Mitsunori', 'Nagaharu', 'Nagahide', 'Nagamasa', 'Nagamichi', 'Naganao', 'Naganori', 'Nagatoki', 'Nagatomo', 'Namio', 'Nankichi', 'Naofumi', 'Naoharu', 'Naohiko', 'Naohiro', 'Naohisa', 'Naohito', 'Naoji', 'Naokatsu', 'Naoki', 'Naomasa', 'Naomichi', 'Naomori', 'Naonobu', 'Naoshi', 'Naotaka', 'Naotake', 'Naoto', 'Naoya', 'Naoyuki', 'Naozumi', 'Nariaki', 'Nariakira', 'Naritaka', 'Nobuatsu', 'Nobuharu', 'Nobuhiko', 'Nobuhiro', 'Nobuhisa', 'Nobuhito', 'Nobukatsu', 'Nobukazu', 'Nobumasa', 'Nobumitsu', 'Nobumoto', 'Nobunao', 'Nobunari', 'Nobuo', 'Nobusada', 'Nobusuke', 'Nobutada', 'Nobutaka', 'Nobuteru', 'Osamu', 'Otohiko', 'Raizo', 'Reiichi', 'Reiji', 'Sadaaki', 'Sadaharu', 'Sadahiko', 'Sadahiro', 'Sadakazu', 'Sadanaga', 'Sadao', '(given', 'name)', 'Sadatoshi', 'Sadayoshi', 'Sadazane', 'Saiichi', 'Sakichi', 'Sanji', '(given', 'name)', 'Sankichi', 'Shigeharu', 'Shigehiro', 'Shigehisa', 'Shigekazu', 'Shigeki', 'Shigemasa', 'Shigematsu', 'Shigemi', 'Shigemitsu', 'Shigemoto', 'Shigenaga', 'Shigenobu', 'Shigenori', 'Shigeo', 'Shigeru', 'Shigetada', 'Shigetaka', 'Shigeto', 'Shigetoshi', 'Shigeyasu', 'Shigeyoshi', 'Shigeyuki', 'Tadaaki', 'Tadachika', 'Tadafumi', 'Tadaharu', 'Tadahiko', 'Tadahiro', 'Tadahito', 'Tadakatsu', 'Tadamasa', 'Tadami', 'Tadamori', 'Tadanaga', 'Tadanao', 'Tadanari', 'Tadanobu', 'Tadanori', 'Tadao', 'Tadaoki', 'Tadashi', 'Tadataka', 'Tadateru', 'Tadatomo', 'Tadatoshi', 'Tadatsugu', 'Tadatsune', 'Tadayo', 'Tadayoshi', 'Tadayuki', 'Taichi', 'Teruhisa', 'Teruichi', 'Teruki', 'Terumasa', 'Terunobu', 'Teruo', 'Teruyoshi', 'Teruyuki', 'Tetsu', 'Tetsuharu', 'Tetsuhiro', 'Yahiko', 'Yahiro', 'Yanosuke', 'Yashiro', 'Yasuaki', 'Yasufumi', 'Yasuharu', 'Yasuhide', 'Yasuhiko', 'Yasuhiro', 'Yasuhisa', 'Yasuji', 'Yoshifumi', 'Yoshiharu', 'Yoshihide', 'Yoshihiko', 'Yoshihiro', 'Yoshihisa', 'Yoshihito', 'Yoshiie', 'Yoshikane', 'Yoshikatsu', 'Yoshikazu', 'Yoshiki', 'Yoshikiyo', 'Yoshikuni', 'Yoshimasa', 'Zenjiro', 'Zenkichi'].freeze +CLAN_NAMES.each do |clan| + Clan.create(name: clan) +end + +CLAN_NAMES.count.times do + Constructions::Barricade.create! + Constructions::Trench.create! + Constructions::BatteringRam.create! + Constructions::Bunker.create! + Constructions::Stronghold.create! +end + +CONSTRUCTION_NAMES = %w[ Barricade Trench BatteringRam Bunker Stronghold] + + +WARRIORS_NAME.each do |warrior| + war = Warrior.create( + name: warrior, + armor_quality: rand(100), + number_of_battles: rand(100), + join_date: Date.today - rand(1000).days, + clan_id: Clan.find_by_name(CLAN_NAMES.sample).id, + kind: WARRIORS_KINDS.sample, + defensible_type: CONSTRUCTION_NAMES.sample) + + #it wouldn't be good idea to keep warriors from enemy clans in the same construcitons + war.defensible_id = war.clan_id + war.save + + next unless rand(10) >= 7 + + joining_time = (Date.today - war.join_date).to_i + war.death_date = war.join_date + rand(joining_time).days + if war.save! + puts "#{warrior} created" + else + puts "#{warrior} not created" + end +end diff --git a/lecture_2/homework/test/fixtures/archers.yml b/lecture_2/homework/test/fixtures/archers.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/archers.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/barricades.yml b/lecture_2/homework/test/fixtures/barricades.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/barricades.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/battering_rams.yml b/lecture_2/homework/test/fixtures/battering_rams.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/battering_rams.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/bows.yml b/lecture_2/homework/test/fixtures/bows.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/bows.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/bunkers.yml b/lecture_2/homework/test/fixtures/bunkers.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/bunkers.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/hussars.yml b/lecture_2/homework/test/fixtures/hussars.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/hussars.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/katanas.yml b/lecture_2/homework/test/fixtures/katanas.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/katanas.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/lances.yml b/lecture_2/homework/test/fixtures/lances.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/lances.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/peasants.yml b/lecture_2/homework/test/fixtures/peasants.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/peasants.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/samurais.yml b/lecture_2/homework/test/fixtures/samurais.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/samurais.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/stick_with_shits.yml b/lecture_2/homework/test/fixtures/stick_with_shits.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/stick_with_shits.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/strongholds.yml b/lecture_2/homework/test/fixtures/strongholds.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/strongholds.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/swords.yml b/lecture_2/homework/test/fixtures/swords.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/swords.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/trenches.yml b/lecture_2/homework/test/fixtures/trenches.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/trenches.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/wands.yml b/lecture_2/homework/test/fixtures/wands.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/wands.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/fixtures/weapons.yml b/lecture_2/homework/test/fixtures/weapons.yml new file mode 100644 index 00000000..e446a40e --- /dev/null +++ b/lecture_2/homework/test/fixtures/weapons.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + species: MyString + damage: 1 + range: 1 + warrior: one + +two: + species: MyString + damage: 1 + range: 1 + warrior: two diff --git a/lecture_2/homework/test/fixtures/wizards.yml b/lecture_2/homework/test/fixtures/wizards.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/wizards.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/lecture_2/homework/test/models/archer_test.rb b/lecture_2/homework/test/models/archer_test.rb new file mode 100644 index 00000000..a7283bc7 --- /dev/null +++ b/lecture_2/homework/test/models/archer_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ArcherTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/barricade_test.rb b/lecture_2/homework/test/models/barricade_test.rb new file mode 100644 index 00000000..08b8908a --- /dev/null +++ b/lecture_2/homework/test/models/barricade_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BarricadeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/battering_ram_test.rb b/lecture_2/homework/test/models/battering_ram_test.rb new file mode 100644 index 00000000..cab42329 --- /dev/null +++ b/lecture_2/homework/test/models/battering_ram_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BatteringRamTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/bow_test.rb b/lecture_2/homework/test/models/bow_test.rb new file mode 100644 index 00000000..fbee290a --- /dev/null +++ b/lecture_2/homework/test/models/bow_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BowTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/bunker_test.rb b/lecture_2/homework/test/models/bunker_test.rb new file mode 100644 index 00000000..44da4d8a --- /dev/null +++ b/lecture_2/homework/test/models/bunker_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BunkerTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/hussar_test.rb b/lecture_2/homework/test/models/hussar_test.rb new file mode 100644 index 00000000..7992b736 --- /dev/null +++ b/lecture_2/homework/test/models/hussar_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class HussarTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/katana_test.rb b/lecture_2/homework/test/models/katana_test.rb new file mode 100644 index 00000000..5411d896 --- /dev/null +++ b/lecture_2/homework/test/models/katana_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class KatanaTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/lance_test.rb b/lecture_2/homework/test/models/lance_test.rb new file mode 100644 index 00000000..4d2cb9c5 --- /dev/null +++ b/lecture_2/homework/test/models/lance_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class LanceTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/peasant_test.rb b/lecture_2/homework/test/models/peasant_test.rb new file mode 100644 index 00000000..93b5333f --- /dev/null +++ b/lecture_2/homework/test/models/peasant_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class PeasantTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/samurai_test.rb b/lecture_2/homework/test/models/samurai_test.rb new file mode 100644 index 00000000..ffbe9832 --- /dev/null +++ b/lecture_2/homework/test/models/samurai_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class SamuraiTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/stick_with_shit_test.rb b/lecture_2/homework/test/models/stick_with_shit_test.rb new file mode 100644 index 00000000..bf646cdf --- /dev/null +++ b/lecture_2/homework/test/models/stick_with_shit_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class StickWithShitTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/stronghold_test.rb b/lecture_2/homework/test/models/stronghold_test.rb new file mode 100644 index 00000000..0540730a --- /dev/null +++ b/lecture_2/homework/test/models/stronghold_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class StrongholdTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/sword_test.rb b/lecture_2/homework/test/models/sword_test.rb new file mode 100644 index 00000000..db30b7dc --- /dev/null +++ b/lecture_2/homework/test/models/sword_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class SwordTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/trench_test.rb b/lecture_2/homework/test/models/trench_test.rb new file mode 100644 index 00000000..da129310 --- /dev/null +++ b/lecture_2/homework/test/models/trench_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class TrenchTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/wand_test.rb b/lecture_2/homework/test/models/wand_test.rb new file mode 100644 index 00000000..ea3cfcc2 --- /dev/null +++ b/lecture_2/homework/test/models/wand_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class WandTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/weapon_test.rb b/lecture_2/homework/test/models/weapon_test.rb new file mode 100644 index 00000000..06c71e8f --- /dev/null +++ b/lecture_2/homework/test/models/weapon_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class WeaponTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/wizard_test.rb b/lecture_2/homework/test/models/wizard_test.rb new file mode 100644 index 00000000..3a18d16d --- /dev/null +++ b/lecture_2/homework/test/models/wizard_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test_helper' + +class WizardTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/recruitment_task/converter.rb b/recruitment_task/converter.rb index 1a91dec6..94f59d53 100644 --- a/recruitment_task/converter.rb +++ b/recruitment_task/converter.rb @@ -1,5 +1,29 @@ require_relative 'price' class Converter - # TODO: Implement me + class InvalidCurrency < StandardError; end + SUPPORTED_CURRENCIES = %i[ eur pln usd] + + RATES = { "EUR/PLN" => 4.32, "EUR/USD" => 1.13, "USD/PLN" => 3.81, "USD/EUR" => 0.88, "PLN/EUR" => 0.23, "PLN/USD" => 0.26 } + + def initialize(price) + @price = price + end + + def convert_to(convert_currency) + @convert_currency = convert_currency + validate_convert_currency + + if @price.currency == convert_currency + return @price.amount + else + (@price.amount * RATES[[ @price.currency.upcase, convert_currency.upcase].join("/")]).round(2) + end + end + + private + + def validate_convert_currency + raise InvalidCurrency unless SUPPORTED_CURRENCIES.include?(@convert_currency) + end end