From 15fc9b0c129debb537f7674d7207a7c7dff4b6b5 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Tue, 9 Apr 2019 23:15:00 +0200 Subject: [PATCH 1/8] Zad2 partially solved --- lecture_2/homework/Gemfile | 2 +- lecture_2/homework/Gemfile.lock | 2 +- lecture_2/homework/app/models/barricade.rb | 2 + lecture_2/homework/app/models/building.rb | 4 + lecture_2/homework/app/models/character.rb | 13 +++ lecture_2/homework/app/models/clan.rb | 4 +- lecture_2/homework/app/models/gate.rb | 2 + lecture_2/homework/app/models/hussar.rb | 5 + lecture_2/homework/app/models/mage.rb | 5 + .../homework/app/models/magical_weapon.rb | 6 ++ lecture_2/homework/app/models/melee_weapon.rb | 6 ++ .../homework/app/models/ranged_weapon.rb | 6 ++ lecture_2/homework/app/models/ranger.rb | 5 + lecture_2/homework/app/models/samurai.rb | 14 +-- lecture_2/homework/app/models/stronghold.rb | 2 + lecture_2/homework/app/models/tower.rb | 2 + lecture_2/homework/app/models/wall.rb | 2 + lecture_2/homework/app/models/warrior.rb | 3 + lecture_2/homework/app/models/weapon.rb | 6 ++ ...0314150921_create_clans_and_characters.rb} | 4 +- .../migrate/20190409114936_create_hussars.rb | 8 ++ .../migrate/20190409115850_create_warriors.rb | 8 ++ .../migrate/20190409121807_create_samurais.rb | 8 ++ .../db/migrate/20190409121814_create_mages.rb | 8 ++ .../migrate/20190409121820_create_rangers.rb | 8 ++ .../20190409132527_create_buildings.rb | 8 ++ .../20190409132556_create_barricades.rb | 8 ++ .../20190409132607_create_strongholds.rb | 8 ++ .../db/migrate/20190409132618_create_walls.rb | 8 ++ .../migrate/20190409132624_create_towers.rb | 8 ++ .../db/migrate/20190409132636_create_gates.rb | 8 ++ .../migrate/20190409133633_create_weapons.rb | 8 ++ .../20190409133651_create_magical_weapons.rb | 8 ++ .../20190409133700_create_melee_weapons.rb | 8 ++ .../20190409133707_create_ranged_weapons.rb | 8 ++ .../20190409204516_index_name_on_clans.rb | 5 + ...0409210018_add_defensible_to_characters.rb | 5 + lecture_2/homework/db/schema.rb | 91 +++++++++++++++++-- 38 files changed, 294 insertions(+), 22 deletions(-) create mode 100644 lecture_2/homework/app/models/barricade.rb create mode 100644 lecture_2/homework/app/models/building.rb create mode 100644 lecture_2/homework/app/models/character.rb create mode 100644 lecture_2/homework/app/models/gate.rb create mode 100644 lecture_2/homework/app/models/hussar.rb create mode 100644 lecture_2/homework/app/models/mage.rb create mode 100644 lecture_2/homework/app/models/magical_weapon.rb create mode 100644 lecture_2/homework/app/models/melee_weapon.rb create mode 100644 lecture_2/homework/app/models/ranged_weapon.rb create mode 100644 lecture_2/homework/app/models/ranger.rb create mode 100644 lecture_2/homework/app/models/stronghold.rb create mode 100644 lecture_2/homework/app/models/tower.rb create mode 100644 lecture_2/homework/app/models/wall.rb create mode 100644 lecture_2/homework/app/models/warrior.rb create mode 100644 lecture_2/homework/app/models/weapon.rb rename lecture_2/homework/db/migrate/{20190314150921_create_clans_and_samurais.rb => 20190314150921_create_clans_and_characters.rb} (79%) create mode 100644 lecture_2/homework/db/migrate/20190409114936_create_hussars.rb create mode 100644 lecture_2/homework/db/migrate/20190409115850_create_warriors.rb create mode 100644 lecture_2/homework/db/migrate/20190409121807_create_samurais.rb create mode 100644 lecture_2/homework/db/migrate/20190409121814_create_mages.rb create mode 100644 lecture_2/homework/db/migrate/20190409121820_create_rangers.rb create mode 100644 lecture_2/homework/db/migrate/20190409132527_create_buildings.rb create mode 100644 lecture_2/homework/db/migrate/20190409132556_create_barricades.rb create mode 100644 lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb create mode 100644 lecture_2/homework/db/migrate/20190409132618_create_walls.rb create mode 100644 lecture_2/homework/db/migrate/20190409132624_create_towers.rb create mode 100644 lecture_2/homework/db/migrate/20190409132636_create_gates.rb create mode 100644 lecture_2/homework/db/migrate/20190409133633_create_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb create mode 100644 lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index f3629b6f..309def7b 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.6.2' +ruby '2.5.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.2' diff --git a/lecture_2/homework/Gemfile.lock b/lecture_2/homework/Gemfile.lock index ae757417..acea2d1c 100644 --- a/lecture_2/homework/Gemfile.lock +++ b/lecture_2/homework/Gemfile.lock @@ -144,7 +144,7 @@ DEPENDENCIES tzinfo-data RUBY VERSION - ruby 2.6.2p47 + ruby 2.5.1p57 BUNDLED WITH 2.0.1 diff --git a/lecture_2/homework/app/models/barricade.rb b/lecture_2/homework/app/models/barricade.rb new file mode 100644 index 00000000..1c3fc47a --- /dev/null +++ b/lecture_2/homework/app/models/barricade.rb @@ -0,0 +1,2 @@ +class Barricade < Building +end diff --git a/lecture_2/homework/app/models/building.rb b/lecture_2/homework/app/models/building.rb new file mode 100644 index 00000000..48182381 --- /dev/null +++ b/lecture_2/homework/app/models/building.rb @@ -0,0 +1,4 @@ +class Building < ApplicationRecord + has_many :characters, as: :defensible + validates :name, presence: true +end diff --git a/lecture_2/homework/app/models/character.rb b/lecture_2/homework/app/models/character.rb new file mode 100644 index 00000000..d2931c81 --- /dev/null +++ b/lecture_2/homework/app/models/character.rb @@ -0,0 +1,13 @@ +class Character < ApplicationRecord + belongs_to :clan + has_one :weapon, dependent: :destroy + belongs_to: defensible, polymorphic: true + + validates :name, presence: true, uniqueness: { scope: ['death_date.present?', :clan_id] } #, uniqueness: { scope: ['death_date.present?'] }# + 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/clan.rb b/lecture_2/homework/app/models/clan.rb index 71f4e9de..f84c2b96 100644 --- a/lecture_2/homework/app/models/clan.rb +++ b/lecture_2/homework/app/models/clan.rb @@ -1,4 +1,4 @@ class Clan < ApplicationRecord - has_many :samurais, dependent: :destroy - validates :name, presence: true + has_many :characters, dependent: :destroy + validates :name, presence: true, uniqueness: true end diff --git a/lecture_2/homework/app/models/gate.rb b/lecture_2/homework/app/models/gate.rb new file mode 100644 index 00000000..cb5a8734 --- /dev/null +++ b/lecture_2/homework/app/models/gate.rb @@ -0,0 +1,2 @@ +class Gate < Building +end diff --git a/lecture_2/homework/app/models/hussar.rb b/lecture_2/homework/app/models/hussar.rb new file mode 100644 index 00000000..8b09cfa2 --- /dev/null +++ b/lecture_2/homework/app/models/hussar.rb @@ -0,0 +1,5 @@ +class Hussar < Warrior + def attack + "#{name}: CHARGE!!" + end +end diff --git a/lecture_2/homework/app/models/mage.rb b/lecture_2/homework/app/models/mage.rb new file mode 100644 index 00000000..f9788b76 --- /dev/null +++ b/lecture_2/homework/app/models/mage.rb @@ -0,0 +1,5 @@ +class Mage < Character + def attack + "#{name}: Pew pew pew! Whizzzz! " + end +end diff --git a/lecture_2/homework/app/models/magical_weapon.rb b/lecture_2/homework/app/models/magical_weapon.rb new file mode 100644 index 00000000..83444f04 --- /dev/null +++ b/lecture_2/homework/app/models/magical_weapon.rb @@ -0,0 +1,6 @@ +class MagicalWeapon < Weapon + def calculate_damage(distance) + return 0 if distance > range + damage * (1 - distance / range) + end +end diff --git a/lecture_2/homework/app/models/melee_weapon.rb b/lecture_2/homework/app/models/melee_weapon.rb new file mode 100644 index 00000000..ad68cb21 --- /dev/null +++ b/lecture_2/homework/app/models/melee_weapon.rb @@ -0,0 +1,6 @@ +class MeleeWeapon < Weapon + def calculate_damage(distance) + return 0 if distance > range + damage * (1 - distance / range) + end +end diff --git a/lecture_2/homework/app/models/ranged_weapon.rb b/lecture_2/homework/app/models/ranged_weapon.rb new file mode 100644 index 00000000..738fcbb7 --- /dev/null +++ b/lecture_2/homework/app/models/ranged_weapon.rb @@ -0,0 +1,6 @@ +class RangedWeapon < Weapon + def calculate_damage(distance) + return 0 if distance > range + damage * (1 - distance / range) + end +end diff --git a/lecture_2/homework/app/models/ranger.rb b/lecture_2/homework/app/models/ranger.rb new file mode 100644 index 00000000..1e8ab15b --- /dev/null +++ b/lecture_2/homework/app/models/ranger.rb @@ -0,0 +1,5 @@ +class Ranger < Character + def attack + "#{name}: Ftong! Swoosh! *arrow hit noise*" + end +end diff --git a/lecture_2/homework/app/models/samurai.rb b/lecture_2/homework/app/models/samurai.rb index b2fafebb..fa4c59d0 100644 --- a/lecture_2/homework/app/models/samurai.rb +++ b/lecture_2/homework/app/models/samurai.rb @@ -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 + "#{name}: *katana flinging noises*" + end end diff --git a/lecture_2/homework/app/models/stronghold.rb b/lecture_2/homework/app/models/stronghold.rb new file mode 100644 index 00000000..3d291eec --- /dev/null +++ b/lecture_2/homework/app/models/stronghold.rb @@ -0,0 +1,2 @@ +class Stronghold < Building +end diff --git a/lecture_2/homework/app/models/tower.rb b/lecture_2/homework/app/models/tower.rb new file mode 100644 index 00000000..192af17b --- /dev/null +++ b/lecture_2/homework/app/models/tower.rb @@ -0,0 +1,2 @@ +class Tower < Building +end diff --git a/lecture_2/homework/app/models/wall.rb b/lecture_2/homework/app/models/wall.rb new file mode 100644 index 00000000..d921c673 --- /dev/null +++ b/lecture_2/homework/app/models/wall.rb @@ -0,0 +1,2 @@ +class Wall < Building +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..2bd64132 --- /dev/null +++ b/lecture_2/homework/app/models/warrior.rb @@ -0,0 +1,3 @@ +class Warrior < Character + +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..36a6d92c --- /dev/null +++ b/lecture_2/homework/app/models/weapon.rb @@ -0,0 +1,6 @@ +class Weapon < ApplicationRecord + belongs_to :character, optional: true + def calculate_damage(distance) + return 0 + end +end diff --git a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb similarity index 79% rename from lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb rename to lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb index ebfc3158..45b1e8cd 100644 --- a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_samurais.rb +++ b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb @@ -1,11 +1,11 @@ -class CreateClansAndSamurais < ActiveRecord::Migration[5.2] +class CreateClansAndCharacters < ActiveRecord::Migration[5.2] def change create_table :clans do |t| t.string :name, null: false t.timestamps end - create_table :samurais do |t| + create_table :characters do |t| t.string :name, null: false t.integer :armor_quality, default: 0 t.integer :number_of_battles, default: 0 diff --git a/lecture_2/homework/db/migrate/20190409114936_create_hussars.rb b/lecture_2/homework/db/migrate/20190409114936_create_hussars.rb new file mode 100644 index 00000000..6d5376f3 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409114936_create_hussars.rb @@ -0,0 +1,8 @@ +class CreateHussars < ActiveRecord::Migration[5.2] + def change + create_table :hussars do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb b/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb new file mode 100644 index 00000000..49551ac4 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb @@ -0,0 +1,8 @@ +class CreateWarriors < ActiveRecord::Migration[5.2] + def change + create_table :warriors do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409121807_create_samurais.rb b/lecture_2/homework/db/migrate/20190409121807_create_samurais.rb new file mode 100644 index 00000000..522f79d8 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409121807_create_samurais.rb @@ -0,0 +1,8 @@ +class CreateSamurais < ActiveRecord::Migration[5.2] + def change + create_table :samurais do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409121814_create_mages.rb b/lecture_2/homework/db/migrate/20190409121814_create_mages.rb new file mode 100644 index 00000000..44114014 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409121814_create_mages.rb @@ -0,0 +1,8 @@ +class CreateMages < ActiveRecord::Migration[5.2] + def change + create_table :mages do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb b/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb new file mode 100644 index 00000000..b61e9de8 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb @@ -0,0 +1,8 @@ +class CreateRangers < ActiveRecord::Migration[5.2] + def change + create_table :rangers do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb new file mode 100644 index 00000000..9de8c710 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb @@ -0,0 +1,8 @@ +class CreateBuildings < ActiveRecord::Migration[5.2] + def change + create_table :buildings do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb b/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb new file mode 100644 index 00000000..784e57b7 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb @@ -0,0 +1,8 @@ +class CreateBarricades < ActiveRecord::Migration[5.2] + def change + create_table :barricades do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb b/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb new file mode 100644 index 00000000..5ce70f63 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb @@ -0,0 +1,8 @@ +class CreateStrongholds < ActiveRecord::Migration[5.2] + def change + create_table :strongholds do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132618_create_walls.rb b/lecture_2/homework/db/migrate/20190409132618_create_walls.rb new file mode 100644 index 00000000..9b3ad308 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132618_create_walls.rb @@ -0,0 +1,8 @@ +class CreateWalls < ActiveRecord::Migration[5.2] + def change + create_table :walls do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132624_create_towers.rb b/lecture_2/homework/db/migrate/20190409132624_create_towers.rb new file mode 100644 index 00000000..82d7e26f --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132624_create_towers.rb @@ -0,0 +1,8 @@ +class CreateTowers < ActiveRecord::Migration[5.2] + def change + create_table :towers do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409132636_create_gates.rb b/lecture_2/homework/db/migrate/20190409132636_create_gates.rb new file mode 100644 index 00000000..eb3a55fa --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409132636_create_gates.rb @@ -0,0 +1,8 @@ +class CreateGates < ActiveRecord::Migration[5.2] + def change + create_table :gates do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb new file mode 100644 index 00000000..9bff4d33 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb @@ -0,0 +1,8 @@ +class CreateWeapons < ActiveRecord::Migration[5.2] + def change + create_table :weapons do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb b/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb new file mode 100644 index 00000000..c93e82a4 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb @@ -0,0 +1,8 @@ +class CreateMagicalWeapons < ActiveRecord::Migration[5.2] + def change + create_table :magical_weapons do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb b/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb new file mode 100644 index 00000000..e5eed85c --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb @@ -0,0 +1,8 @@ +class CreateMeleeWeapons < ActiveRecord::Migration[5.2] + def change + create_table :melee_weapons do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb b/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb new file mode 100644 index 00000000..2fd5c0c3 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb @@ -0,0 +1,8 @@ +class CreateRangedWeapons < ActiveRecord::Migration[5.2] + def change + create_table :ranged_weapons do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb b/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb new file mode 100644 index 00000000..99de03d4 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb @@ -0,0 +1,5 @@ +class IndexNameOnClans < ActiveRecord::Migration[5.2] + def change + add_index :clans, :name, unique: true + end +end diff --git a/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb b/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb new file mode 100644 index 00000000..7bb13cf2 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb @@ -0,0 +1,5 @@ +class AddDefensibleToCharacters < ActiveRecord::Migration[5.2] + def change + add_reference :characters, :defensible, polymorphic: true, index: true + end +end diff --git a/lecture_2/homework/db/schema.rb b/lecture_2/homework/db/schema.rb index 98a644ef..b610dde4 100644 --- a/lecture_2/homework/db/schema.rb +++ b/lecture_2/homework/db/schema.rb @@ -10,18 +10,22 @@ # # 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_09_210018) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "clans", force: :cascade do |t| - t.string "name", null: false + create_table "barricades", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "samurais", force: :cascade do |t| + create_table "buildings", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "characters", force: :cascade do |t| t.string "name", null: false t.integer "armor_quality", default: 0 t.integer "number_of_battles", default: 0 @@ -30,8 +34,83 @@ 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 "defensible_type" + t.bigint "defensible_id" + t.index ["clan_id"], name: "index_characters_on_clan_id" + t.index ["defensible_type", "defensible_id"], name: "index_characters_on_defensible_type_and_defensible_id" + 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 "gates", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "hussars", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "mages", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "magical_weapons", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "melee_weapons", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "ranged_weapons", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "rangers", 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 "strongholds", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "towers", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "walls", 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.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "weapons", 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 "characters", "clans" end From d24f06fe1892b9bf96b659c3231aec32a8baff67 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Sun, 14 Apr 2019 18:00:41 +0200 Subject: [PATCH 2/8] Add new characters and seeds --- lecture_2/homework/Gemfile | 6 ++ lecture_2/homework/Gemfile.lock | 21 +++++++ .../clans/characters_controller.rb | 59 +++++++++++++++++++ .../controllers/clans/samurais_controller.rb | 50 ---------------- .../app/controllers/clans_controller.rb | 29 ++++++++- lecture_2/homework/app/models/character.rb | 7 ++- lecture_2/homework/app/models/mage.rb | 1 + lecture_2/homework/app/models/ranger.rb | 1 + lecture_2/homework/app/models/warrior.rb | 5 +- lecture_2/homework/app/models/weapon.rb | 2 +- lecture_2/homework/config/routes.rb | 13 +++- lecture_2/homework/db/seeds.rb | 24 ++++++++ .../characters_controller_controller_test.rb | 9 +++ 13 files changed, 167 insertions(+), 60 deletions(-) create mode 100644 lecture_2/homework/app/controllers/clans/characters_controller.rb delete mode 100644 lecture_2/homework/app/controllers/clans/samurais_controller.rb create mode 100644 lecture_2/homework/test/controllers/characters_controller_controller_test.rb diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index 309def7b..265bb828 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -43,3 +43,9 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +#Rubocop +gem 'rubocop', require: false + +#Faker +gem 'faker' \ No newline at end of file diff --git a/lecture_2/homework/Gemfile.lock b/lecture_2/homework/Gemfile.lock index acea2d1c..e81662d4 100644 --- a/lecture_2/homework/Gemfile.lock +++ b/lecture_2/homework/Gemfile.lock @@ -43,6 +43,7 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) arel (9.0.0) + ast (2.4.0) bootsnap (1.4.1) msgpack (~> 1.0) builder (3.2.3) @@ -50,11 +51,14 @@ GEM concurrent-ruby (1.1.5) crass (1.0.4) erubi (1.8.0) + faker (1.9.3) + i18n (>= 0.7) ffi (1.10.0) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.6.0) concurrent-ruby (~> 1.0) + jaro_winkler (1.5.2) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -75,7 +79,11 @@ GEM nio4r (2.3.1) nokogiri (1.10.1) mini_portile2 (~> 2.4.0) + parallel (1.17.0) + parser (2.6.2.1) + ast (~> 2.4.0) pg (1.1.4) + psych (3.1.0) puma (3.12.0) rack (2.0.6) rack-test (1.1.0) @@ -104,10 +112,20 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) + rainbow (3.0.0) rake (12.3.2) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) + rubocop (0.67.2) + jaro_winkler (~> 1.5.1) + parallel (~> 1.10) + parser (>= 2.5, != 2.5.1.1) + psych (>= 3.1.0) + rainbow (>= 2.2.2, < 4.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 1.6) + ruby-progressbar (1.10.0) ruby_dep (1.5.0) spring (2.0.2) activesupport (>= 4.2) @@ -125,6 +143,7 @@ GEM thread_safe (0.3.6) tzinfo (1.2.5) thread_safe (~> 0.1) + unicode-display_width (1.5.0) websocket-driver (0.7.0) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) @@ -135,10 +154,12 @@ PLATFORMS DEPENDENCIES bootsnap (>= 1.1.0) byebug + faker listen (>= 3.0.5, < 3.2) pg puma (~> 3.11) rails (~> 5.2.2) + rubocop spring spring-watcher-listen (~> 2.0.0) tzinfo-data diff --git a/lecture_2/homework/app/controllers/clans/characters_controller.rb b/lecture_2/homework/app/controllers/clans/characters_controller.rb new file mode 100644 index 00000000..283b71cf --- /dev/null +++ b/lecture_2/homework/app/controllers/clans/characters_controller.rb @@ -0,0 +1,59 @@ +module Clans + class CharactersController < ApplicationController + def index + characters = clan.characters.all + if params.key?(:alive) + if params[:alive].to_i == 0 + render json: characters.dead.to_json + else + render json: characters.alive.to_json + end + else + render json: characters.to_json + end + end + + def create + character = clan.characters.create!(character_params) + render json: character.to_json, status: 201 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 422 + end + + def show + render json: character.to_json + end + + def update + character.update!(character_params) + render json: character.to_json, status: 201 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 422 + end + + def destroy + character.destroy! + head 204 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 404 + end + + private + + def clan + @clan ||= Clan.find(params[:clan_id]) + end + + def character + @character ||= Character.find_by!(id: params[:id], clan_id: params[:clan_id]) + end + +# def character +# clan.characters.find(params[:id]) +# end + + def character_params + params.permit(:name, :armour_rating, :battle_count, :join_date, :death_date) + end + end +end \ No newline at end of file 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_controller.rb b/lecture_2/homework/app/controllers/clans_controller.rb index 37db247b..dda264c5 100644 --- a/lecture_2/homework/app/controllers/clans_controller.rb +++ b/lecture_2/homework/app/controllers/clans_controller.rb @@ -5,13 +5,38 @@ def index def create clan = Clan.create!(clan_params) - render json: clan.to_json, status: 201 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 422 + end + + def show + render json: clan.to_json, status: 200 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 404 + end + + def update + clan.update(clan_params) + render json: clan.to_json, status: 201 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 422 + end + + def destroy + clan.destroy! + head 204 + rescue ActiveRecord::ActiveRecordError => e + render json: e.to_json, status: 404 end private + + def clan + Clan.find(params[:id]) + end def clan_params - params.permit(:name) + params.permit(:name, :id) end end diff --git a/lecture_2/homework/app/models/character.rb b/lecture_2/homework/app/models/character.rb index d2931c81..93a8ef38 100644 --- a/lecture_2/homework/app/models/character.rb +++ b/lecture_2/homework/app/models/character.rb @@ -1,12 +1,13 @@ class Character < ApplicationRecord belongs_to :clan has_one :weapon, dependent: :destroy - belongs_to: defensible, polymorphic: true + belongs_to :defensible, polymorphic: true, optional: true - validates :name, presence: true, uniqueness: { scope: ['death_date.present?', :clan_id] } #, uniqueness: { scope: ['death_date.present?'] }# + validates :name, presence: true, uniqueness: { scope: ['death_date.present?', :clan_id] } validates :armor_quality, numericality: { only_integer: true, greater_than_or_equal_to: 0, - less_than_or_equal_to: 100 } + less_than_or_equal_to: 1000 } + validates :weapon, inclusion: { in: %w(magical_weapon melee_weapon ranged_weapon)}, allow_nil: true scope :alive, -> { where('death_date IS NULL') } scope :dead, -> { where('death_date IS NOT NULL') } diff --git a/lecture_2/homework/app/models/mage.rb b/lecture_2/homework/app/models/mage.rb index f9788b76..59580570 100644 --- a/lecture_2/homework/app/models/mage.rb +++ b/lecture_2/homework/app/models/mage.rb @@ -1,4 +1,5 @@ class Mage < Character + validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true def attack "#{name}: Pew pew pew! Whizzzz! " end diff --git a/lecture_2/homework/app/models/ranger.rb b/lecture_2/homework/app/models/ranger.rb index 1e8ab15b..2e0d80a1 100644 --- a/lecture_2/homework/app/models/ranger.rb +++ b/lecture_2/homework/app/models/ranger.rb @@ -1,4 +1,5 @@ class Ranger < Character + validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true def attack "#{name}: Ftong! Swoosh! *arrow hit noise*" end diff --git a/lecture_2/homework/app/models/warrior.rb b/lecture_2/homework/app/models/warrior.rb index 2bd64132..501365e4 100644 --- a/lecture_2/homework/app/models/warrior.rb +++ b/lecture_2/homework/app/models/warrior.rb @@ -1,3 +1,6 @@ class Warrior < Character - + validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true + def attack + "#{name}: *melee noises*" + end end diff --git a/lecture_2/homework/app/models/weapon.rb b/lecture_2/homework/app/models/weapon.rb index 36a6d92c..03156869 100644 --- a/lecture_2/homework/app/models/weapon.rb +++ b/lecture_2/homework/app/models/weapon.rb @@ -1,5 +1,5 @@ class Weapon < ApplicationRecord - belongs_to :character, optional: true + belongs_to :character, optional: false def calculate_damage(distance) return 0 end diff --git a/lecture_2/homework/config/routes.rb b/lecture_2/homework/config/routes.rb index 9c03cd59..ebacb6c9 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -1,5 +1,12 @@ Rails.application.routes.draw do - resources :clans, only: %i[index create] do - resources :samurais, module: :clans + get 'clans/index' + get 'characters/index' +# get 'samurais/:samurai_id' +# get 'clans/:clan_id' + get '/clans/:clan_id/alive', to: 'characters#alive' + get '/clans/:clan_id/dead', to: 'characters#dead' +# resources :clans, only: %i[index create] do + resources :clans, only: %i[index create show update destroy] do + resources :characters, module: :clans end -end +end \ No newline at end of file diff --git a/lecture_2/homework/db/seeds.rb b/lecture_2/homework/db/seeds.rb index 1beea2ac..443595ee 100644 --- a/lecture_2/homework/db/seeds.rb +++ b/lecture_2/homework/db/seeds.rb @@ -5,3 +5,27 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + + + +3.times do + clan = Clan.create!(name: Faker::Creature::Cat.unique.registry) + Faker::Number.between(2, 10).times do + clan.characters.create!( + name: Faker::Creature::Cat.unique.name, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: nil + ) + end + Faker::Number.between(1, 5).times do + clan.characters.create!( + name: Faker::Creature::Cat.unique.name, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: Faker::Date.backward(20) + ) + end +end \ No newline at end of file diff --git a/lecture_2/homework/test/controllers/characters_controller_controller_test.rb b/lecture_2/homework/test/controllers/characters_controller_controller_test.rb new file mode 100644 index 00000000..39793e5f --- /dev/null +++ b/lecture_2/homework/test/controllers/characters_controller_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class CharactersControllerControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get characters_controller_index_url + assert_response :success + end + +end From 217a434ce51cd52ce0e44ee2ee7f53a14a07140b Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Mon, 15 Apr 2019 00:52:41 +0200 Subject: [PATCH 3/8] Add new weapons, correct characters --- .../controllers/samurai_warrior_controller.rb | 4 ++ lecture_2/homework/app/models/character.rb | 8 ++- lecture_2/homework/app/models/hussar.rb | 4 +- lecture_2/homework/app/models/hussar_mage.rb | 6 ++ .../homework/app/models/hussar_ranger.rb | 6 ++ .../homework/app/models/hussar_warrior.rb | 6 ++ lecture_2/homework/app/models/mage.rb | 13 ++-- .../homework/app/models/magical_weapon.rb | 10 ++- lecture_2/homework/app/models/melee_weapon.rb | 10 ++- .../homework/app/models/ranged_weapon.rb | 10 ++- lecture_2/homework/app/models/ranger.rb | 13 ++-- lecture_2/homework/app/models/samurai.rb | 4 +- lecture_2/homework/app/models/samurai_mage.rb | 6 ++ .../homework/app/models/samurai_ranger.rb | 6 ++ .../homework/app/models/samurai_warrior.rb | 6 ++ lecture_2/homework/app/models/warrior.rb | 13 ++-- lecture_2/homework/app/models/weapon.rb | 12 ++-- lecture_2/homework/config/application.rb | 1 + lecture_2/homework/config/routes.rb | 3 - ...90314150921_create_clans_and_characters.rb | 3 +- .../migrate/20190409115850_create_warriors.rb | 8 --- .../db/migrate/20190409121814_create_mages.rb | 8 --- .../migrate/20190409121820_create_rangers.rb | 8 --- .../migrate/20190409133633_create_weapons.rb | 5 +- ...s.rb => 20190414164925_create_samurais.rb} | 0 ...rs.rb => 20190414194133_create_hussars.rb} | 0 .../20190414203621_create_samurai_mages.rb | 8 +++ .../20190414203629_create_samurai_rangers.rb | 8 +++ .../20190414203645_create_samurai_warriors.rb | 8 +++ .../20190414210802_create_hussar_mages.rb | 8 +++ .../20190414210810_create_hussar_rangers.rb | 8 +++ .../20190414210818_create_hussar_warriors.rb | 8 +++ ...21757_add_ma_wpnable_to_magical_weapons.rb | 5 ++ ...221851_add_ra_wpnable_to_ranged_weapons.rb | 5 ++ ...4221902_add_me_wpnable_to_melee_weapons.rb | 5 ++ lecture_2/homework/db/schema.rb | 49 ++++++++++--- lecture_2/homework/db/seeds.rb | 68 ++++++++++++++++--- lecture_2/homework/lib/mage.rb | 11 +++ lecture_2/homework/lib/ranger.rb | 11 +++ lecture_2/homework/lib/warrior.rb | 12 ++++ .../characters_controller_controller_test.rb | 9 --- .../samurai_warrior_controller_test.rb | 9 +++ .../homework/test/fixtures/hussar_mages.yml | 11 +++ .../homework/test/fixtures/hussar_rangers.yml | 11 +++ .../test/fixtures/hussar_warriors.yml | 11 +++ lecture_2/homework/test/fixtures/hussars.yml | 11 +++ .../homework/test/fixtures/samurai_mages.yml | 11 +++ .../test/fixtures/samurai_rangers.yml | 11 +++ .../test/fixtures/samurai_warriors.yml | 11 +++ lecture_2/homework/test/fixtures/samurais.yml | 11 +++ .../homework/test/models/hussar_mage_test.rb | 7 ++ .../test/models/hussar_ranger_test.rb | 7 ++ lecture_2/homework/test/models/hussar_test.rb | 7 ++ .../test/models/hussar_warrior_test.rb | 7 ++ .../homework/test/models/samurai_mage_test.rb | 7 ++ .../test/models/samurai_ranger_test.rb | 7 ++ .../homework/test/models/samurai_test.rb | 7 ++ .../test/models/samurai_warrior_test.rb | 7 ++ 58 files changed, 458 insertions(+), 91 deletions(-) create mode 100644 lecture_2/homework/app/controllers/samurai_warrior_controller.rb create mode 100644 lecture_2/homework/app/models/hussar_mage.rb create mode 100644 lecture_2/homework/app/models/hussar_ranger.rb create mode 100644 lecture_2/homework/app/models/hussar_warrior.rb create mode 100644 lecture_2/homework/app/models/samurai_mage.rb create mode 100644 lecture_2/homework/app/models/samurai_ranger.rb create mode 100644 lecture_2/homework/app/models/samurai_warrior.rb delete mode 100644 lecture_2/homework/db/migrate/20190409115850_create_warriors.rb delete mode 100644 lecture_2/homework/db/migrate/20190409121814_create_mages.rb delete mode 100644 lecture_2/homework/db/migrate/20190409121820_create_rangers.rb rename lecture_2/homework/db/migrate/{20190409121807_create_samurais.rb => 20190414164925_create_samurais.rb} (100%) rename lecture_2/homework/db/migrate/{20190409114936_create_hussars.rb => 20190414194133_create_hussars.rb} (100%) create mode 100644 lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb create mode 100644 lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb create mode 100644 lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb create mode 100644 lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb create mode 100644 lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb create mode 100644 lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb create mode 100644 lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb create mode 100644 lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb create mode 100644 lecture_2/homework/lib/mage.rb create mode 100644 lecture_2/homework/lib/ranger.rb create mode 100644 lecture_2/homework/lib/warrior.rb delete mode 100644 lecture_2/homework/test/controllers/characters_controller_controller_test.rb create mode 100644 lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb create mode 100644 lecture_2/homework/test/fixtures/hussar_mages.yml create mode 100644 lecture_2/homework/test/fixtures/hussar_rangers.yml create mode 100644 lecture_2/homework/test/fixtures/hussar_warriors.yml create mode 100644 lecture_2/homework/test/fixtures/hussars.yml create mode 100644 lecture_2/homework/test/fixtures/samurai_mages.yml create mode 100644 lecture_2/homework/test/fixtures/samurai_rangers.yml create mode 100644 lecture_2/homework/test/fixtures/samurai_warriors.yml create mode 100644 lecture_2/homework/test/fixtures/samurais.yml create mode 100644 lecture_2/homework/test/models/hussar_mage_test.rb create mode 100644 lecture_2/homework/test/models/hussar_ranger_test.rb create mode 100644 lecture_2/homework/test/models/hussar_test.rb create mode 100644 lecture_2/homework/test/models/hussar_warrior_test.rb create mode 100644 lecture_2/homework/test/models/samurai_mage_test.rb create mode 100644 lecture_2/homework/test/models/samurai_ranger_test.rb create mode 100644 lecture_2/homework/test/models/samurai_test.rb create mode 100644 lecture_2/homework/test/models/samurai_warrior_test.rb diff --git a/lecture_2/homework/app/controllers/samurai_warrior_controller.rb b/lecture_2/homework/app/controllers/samurai_warrior_controller.rb new file mode 100644 index 00000000..a3dc4b7c --- /dev/null +++ b/lecture_2/homework/app/controllers/samurai_warrior_controller.rb @@ -0,0 +1,4 @@ +class SamuraiWarriorController < ApplicationController + def index + end +end diff --git a/lecture_2/homework/app/models/character.rb b/lecture_2/homework/app/models/character.rb index 93a8ef38..ed0e5c44 100644 --- a/lecture_2/homework/app/models/character.rb +++ b/lecture_2/homework/app/models/character.rb @@ -1,13 +1,15 @@ class Character < ApplicationRecord + #self.abstract_class = true belongs_to :clan - has_one :weapon, dependent: :destroy + #has_one :weapon, dependent: :destroy belongs_to :defensible, polymorphic: true, optional: true - + validates :name, presence: true, uniqueness: { scope: ['death_date.present?', :clan_id] } + #validates :profession, presence: true, inclusion: { in: %w(mage ranger warrior)} validates :armor_quality, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 } - validates :weapon, inclusion: { in: %w(magical_weapon melee_weapon ranged_weapon)}, allow_nil: true + #validates :weapon, inclusion: { in: %w(magical_weapon melee_weapon ranged_weapon)}, allow_nil: true scope :alive, -> { where('death_date IS NULL') } scope :dead, -> { where('death_date IS NOT NULL') } diff --git a/lecture_2/homework/app/models/hussar.rb b/lecture_2/homework/app/models/hussar.rb index 8b09cfa2..7e481d16 100644 --- a/lecture_2/homework/app/models/hussar.rb +++ b/lecture_2/homework/app/models/hussar.rb @@ -1,5 +1,5 @@ -class Hussar < Warrior +class Hussar < Character def attack - "#{name}: CHARGE!!" + "#{name}: *hussar attack*" end end diff --git a/lecture_2/homework/app/models/hussar_mage.rb b/lecture_2/homework/app/models/hussar_mage.rb new file mode 100644 index 00000000..cb3a0b26 --- /dev/null +++ b/lecture_2/homework/app/models/hussar_mage.rb @@ -0,0 +1,6 @@ +class HussarMage < Hussar + has_one :magical_weapon, dependent: :destroy, as: :ma_wpnable + def attack + "#{name}: *hussar magical attack*" + end +end diff --git a/lecture_2/homework/app/models/hussar_ranger.rb b/lecture_2/homework/app/models/hussar_ranger.rb new file mode 100644 index 00000000..53f0fb82 --- /dev/null +++ b/lecture_2/homework/app/models/hussar_ranger.rb @@ -0,0 +1,6 @@ +class HussarRanger < Hussar + has_one :ranged_weapon, dependent: :destroy, as: :ra_wpnable + def attack + "#{name}: *hussar ranged attack*" + end +end diff --git a/lecture_2/homework/app/models/hussar_warrior.rb b/lecture_2/homework/app/models/hussar_warrior.rb new file mode 100644 index 00000000..20d4ef76 --- /dev/null +++ b/lecture_2/homework/app/models/hussar_warrior.rb @@ -0,0 +1,6 @@ +class HussarWarrior < Hussar + has_one :melee_weapon, dependent: :destroy, as: :me_wpnable + def attack + "#{name}: *hussar melee attack*" + end +end diff --git a/lecture_2/homework/app/models/mage.rb b/lecture_2/homework/app/models/mage.rb index 59580570..14451f7f 100644 --- a/lecture_2/homework/app/models/mage.rb +++ b/lecture_2/homework/app/models/mage.rb @@ -1,6 +1,7 @@ -class Mage < Character - validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true - def attack - "#{name}: Pew pew pew! Whizzzz! " - end -end +#class Mage < Character +# self.abstract_class = true +# validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true +# def attack +# "#{name}: Pew pew pew! Whizzzz! " +# end +#end diff --git a/lecture_2/homework/app/models/magical_weapon.rb b/lecture_2/homework/app/models/magical_weapon.rb index 83444f04..bcf60dcc 100644 --- a/lecture_2/homework/app/models/magical_weapon.rb +++ b/lecture_2/homework/app/models/magical_weapon.rb @@ -1,6 +1,12 @@ class MagicalWeapon < Weapon + belongs_to :ma_wpnable, polymorphic: true def calculate_damage(distance) - return 0 if distance > range - damage * (1 - distance / range) + distance = distance.abs + range = range.abs + if distance > range + return 0 + else + damage * (0.5 * (1-distance/range) + 0.25) + end end end diff --git a/lecture_2/homework/app/models/melee_weapon.rb b/lecture_2/homework/app/models/melee_weapon.rb index ad68cb21..696d91f8 100644 --- a/lecture_2/homework/app/models/melee_weapon.rb +++ b/lecture_2/homework/app/models/melee_weapon.rb @@ -1,6 +1,12 @@ class MeleeWeapon < Weapon + belongs_to :me_wpnable, polymorphic: true def calculate_damage(distance) - return 0 if distance > range - damage * (1 - distance / range) + distance = distance.abs + range = range.abs + if distance > range + return 0 + else + damage * (1 - distance / range) + end end end diff --git a/lecture_2/homework/app/models/ranged_weapon.rb b/lecture_2/homework/app/models/ranged_weapon.rb index 738fcbb7..df6a4947 100644 --- a/lecture_2/homework/app/models/ranged_weapon.rb +++ b/lecture_2/homework/app/models/ranged_weapon.rb @@ -1,6 +1,12 @@ class RangedWeapon < Weapon + belongs_to :ra_wpnable, polymorphic: true def calculate_damage(distance) - return 0 if distance > range - damage * (1 - distance / range) + distance = distance.abs + range = range.abs + if distance > range + return 0 + else + damage * (0.5 * distance/range + 0.25) + end end end diff --git a/lecture_2/homework/app/models/ranger.rb b/lecture_2/homework/app/models/ranger.rb index 2e0d80a1..0c439d6b 100644 --- a/lecture_2/homework/app/models/ranger.rb +++ b/lecture_2/homework/app/models/ranger.rb @@ -1,6 +1,7 @@ -class Ranger < Character - validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true - def attack - "#{name}: Ftong! Swoosh! *arrow hit noise*" - end -end +#class Ranger < Character +# self.abstract_class = true +# validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true +# def attack +# "#{name}: Ftong! Swoosh! *arrow hit noise*" +# end +#end diff --git a/lecture_2/homework/app/models/samurai.rb b/lecture_2/homework/app/models/samurai.rb index fa4c59d0..b17f01a5 100644 --- a/lecture_2/homework/app/models/samurai.rb +++ b/lecture_2/homework/app/models/samurai.rb @@ -1,5 +1,5 @@ -class Samurai < Warrior +class Samurai < Character def attack - "#{name}: *katana flinging noises*" + "#{name}: *samurai attack*" end end diff --git a/lecture_2/homework/app/models/samurai_mage.rb b/lecture_2/homework/app/models/samurai_mage.rb new file mode 100644 index 00000000..79e363bd --- /dev/null +++ b/lecture_2/homework/app/models/samurai_mage.rb @@ -0,0 +1,6 @@ +class SamuraiMage < Samurai + has_one :magical_weapon, dependent: :destroy, as: :ma_wpnable + def attack + "#{name}: *samurai magical attack*" + end +end diff --git a/lecture_2/homework/app/models/samurai_ranger.rb b/lecture_2/homework/app/models/samurai_ranger.rb new file mode 100644 index 00000000..737ed801 --- /dev/null +++ b/lecture_2/homework/app/models/samurai_ranger.rb @@ -0,0 +1,6 @@ +class SamuraiRanger < Samurai + has_one :ranged_weapon, dependent: :destroy, as: :ra_wpnable + def attack + "#{name}: *samurai ranged attack*" + end +end diff --git a/lecture_2/homework/app/models/samurai_warrior.rb b/lecture_2/homework/app/models/samurai_warrior.rb new file mode 100644 index 00000000..10eef726 --- /dev/null +++ b/lecture_2/homework/app/models/samurai_warrior.rb @@ -0,0 +1,6 @@ +class SamuraiWarrior < Samurai + has_one :melee_weapon, dependent: :destroy, as: :me_wpnable + def attack + "#{name}: *samurai melee attack*" + end +end diff --git a/lecture_2/homework/app/models/warrior.rb b/lecture_2/homework/app/models/warrior.rb index 501365e4..9929040e 100644 --- a/lecture_2/homework/app/models/warrior.rb +++ b/lecture_2/homework/app/models/warrior.rb @@ -1,6 +1,7 @@ -class Warrior < Character - validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true - def attack - "#{name}: *melee noises*" - end -end +#class Warrior < Character +# self.abstract_class = true +# validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true +# def attack +# "#{name}: *melee noises*" +# end +#end diff --git a/lecture_2/homework/app/models/weapon.rb b/lecture_2/homework/app/models/weapon.rb index 03156869..c5ab4fe5 100644 --- a/lecture_2/homework/app/models/weapon.rb +++ b/lecture_2/homework/app/models/weapon.rb @@ -1,6 +1,10 @@ class Weapon < ApplicationRecord - belongs_to :character, optional: false - def calculate_damage(distance) - return 0 - end + #belongs_to :weaponable, polymorphic: true + validates :name, presence: true + validates :damage, numericality: { only_integer: true, + greater_than_or_equal_to: 0, + less_than_or_equal_to: 100 } + validates :range, numericality: { only_integer: true, + greater_than_or_equal_to: 0, + less_than_or_equal_to: 100 } end diff --git a/lecture_2/homework/config/application.rb b/lecture_2/homework/config/application.rb index f5390e33..214541a3 100644 --- a/lecture_2/homework/config/application.rb +++ b/lecture_2/homework/config/application.rb @@ -31,5 +31,6 @@ class Application < Rails::Application # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true + config.autoload_paths += %W(#{config.root}/lib) end end diff --git a/lecture_2/homework/config/routes.rb b/lecture_2/homework/config/routes.rb index ebacb6c9..1c2173e4 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -1,11 +1,8 @@ Rails.application.routes.draw do get 'clans/index' get 'characters/index' -# get 'samurais/:samurai_id' -# get 'clans/:clan_id' get '/clans/:clan_id/alive', to: 'characters#alive' get '/clans/:clan_id/dead', to: 'characters#dead' -# resources :clans, only: %i[index create] do resources :clans, only: %i[index create show update destroy] do resources :characters, module: :clans end diff --git a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb index 45b1e8cd..3dc6604e 100644 --- a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb +++ b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb @@ -9,8 +9,9 @@ def change t.string :name, null: false t.integer :armor_quality, default: 0 t.integer :number_of_battles, default: 0 - t.date :join_date + t.date :join_date, null: false t.date :death_date + t.string :type, default: 'Samurai' t.references :clan, foreign_key: true, index: true t.timestamps end diff --git a/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb b/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb deleted file mode 100644 index 49551ac4..00000000 --- a/lecture_2/homework/db/migrate/20190409115850_create_warriors.rb +++ /dev/null @@ -1,8 +0,0 @@ -class CreateWarriors < ActiveRecord::Migration[5.2] - def change - create_table :warriors do |t| - - t.timestamps - end - end -end diff --git a/lecture_2/homework/db/migrate/20190409121814_create_mages.rb b/lecture_2/homework/db/migrate/20190409121814_create_mages.rb deleted file mode 100644 index 44114014..00000000 --- a/lecture_2/homework/db/migrate/20190409121814_create_mages.rb +++ /dev/null @@ -1,8 +0,0 @@ -class CreateMages < ActiveRecord::Migration[5.2] - def change - create_table :mages do |t| - - t.timestamps - end - end -end diff --git a/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb b/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb deleted file mode 100644 index b61e9de8..00000000 --- a/lecture_2/homework/db/migrate/20190409121820_create_rangers.rb +++ /dev/null @@ -1,8 +0,0 @@ -class CreateRangers < ActiveRecord::Migration[5.2] - def change - create_table :rangers do |t| - - t.timestamps - end - end -end diff --git a/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb index 9bff4d33..9dcd9a79 100644 --- a/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb +++ b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb @@ -1,7 +1,10 @@ class CreateWeapons < ActiveRecord::Migration[5.2] def change create_table :weapons do |t| - + t.string :name, null: false + t.integer :damage, default: 10 + t.integer :range, default: 1 + t.string :type, default: 'MeleeWeapon' t.timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409121807_create_samurais.rb b/lecture_2/homework/db/migrate/20190414164925_create_samurais.rb similarity index 100% rename from lecture_2/homework/db/migrate/20190409121807_create_samurais.rb rename to lecture_2/homework/db/migrate/20190414164925_create_samurais.rb diff --git a/lecture_2/homework/db/migrate/20190409114936_create_hussars.rb b/lecture_2/homework/db/migrate/20190414194133_create_hussars.rb similarity index 100% rename from lecture_2/homework/db/migrate/20190409114936_create_hussars.rb rename to lecture_2/homework/db/migrate/20190414194133_create_hussars.rb diff --git a/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb b/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb new file mode 100644 index 00000000..2366a401 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb @@ -0,0 +1,8 @@ +class CreateSamuraiMages < ActiveRecord::Migration[5.2] + def change + create_table :samurai_mages do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb b/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb new file mode 100644 index 00000000..0f0515b8 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb @@ -0,0 +1,8 @@ +class CreateSamuraiRangers < ActiveRecord::Migration[5.2] + def change + create_table :samurai_rangers do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb b/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb new file mode 100644 index 00000000..df720448 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb @@ -0,0 +1,8 @@ +class CreateSamuraiWarriors < ActiveRecord::Migration[5.2] + def change + create_table :samurai_warriors do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb b/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb new file mode 100644 index 00000000..081515fa --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb @@ -0,0 +1,8 @@ +class CreateHussarMages < ActiveRecord::Migration[5.2] + def change + create_table :hussar_mages do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb b/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb new file mode 100644 index 00000000..e7793c90 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb @@ -0,0 +1,8 @@ +class CreateHussarRangers < ActiveRecord::Migration[5.2] + def change + create_table :hussar_rangers do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb b/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb new file mode 100644 index 00000000..8321d89b --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb @@ -0,0 +1,8 @@ +class CreateHussarWarriors < ActiveRecord::Migration[5.2] + def change + create_table :hussar_warriors do |t| + + t.timestamps + end + end +end diff --git a/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb b/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb new file mode 100644 index 00000000..f471ca0c --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb @@ -0,0 +1,5 @@ +class AddMaWpnableToMagicalWeapons < ActiveRecord::Migration[5.2] + def change + add_reference :magical_weapons, :ma_wpnable, polymorphic: true, index: true + end +end diff --git a/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb b/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb new file mode 100644 index 00000000..1504c1d7 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb @@ -0,0 +1,5 @@ +class AddRaWpnableToRangedWeapons < ActiveRecord::Migration[5.2] + def change + add_reference :ranged_weapons, :ra_wpnable, polymorphic: true, index: true + end +end diff --git a/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb b/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb new file mode 100644 index 00000000..b0719723 --- /dev/null +++ b/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb @@ -0,0 +1,5 @@ +class AddMeWpnableToMeleeWeapons < ActiveRecord::Migration[5.2] + def change + add_reference :melee_weapons, :me_wpnable, polymorphic: true, index: true + end +end diff --git a/lecture_2/homework/db/schema.rb b/lecture_2/homework/db/schema.rb index b610dde4..a42d1bbf 100644 --- a/lecture_2/homework/db/schema.rb +++ b/lecture_2/homework/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_04_09_210018) do +ActiveRecord::Schema.define(version: 2019_04_14_221902) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,8 +29,9 @@ t.string "name", null: false t.integer "armor_quality", default: 0 t.integer "number_of_battles", default: 0 - t.date "join_date" + t.date "join_date", null: false t.date "death_date" + t.string "type", default: "Samurai" t.bigint "clan_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -52,12 +53,22 @@ t.datetime "updated_at", null: false end - create_table "hussars", force: :cascade do |t| + create_table "hussar_mages", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "hussar_rangers", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "hussar_warriors", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "mages", force: :cascade do |t| + create_table "hussars", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end @@ -65,49 +76,67 @@ create_table "magical_weapons", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "ma_wpnable_type" + t.bigint "ma_wpnable_id" + t.index ["ma_wpnable_type", "ma_wpnable_id"], name: "index_magical_weapons_on_ma_wpnable_type_and_ma_wpnable_id" end create_table "melee_weapons", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "me_wpnable_type" + t.bigint "me_wpnable_id" + t.index ["me_wpnable_type", "me_wpnable_id"], name: "index_melee_weapons_on_me_wpnable_type_and_me_wpnable_id" end create_table "ranged_weapons", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "ra_wpnable_type" + t.bigint "ra_wpnable_id" + t.index ["ra_wpnable_type", "ra_wpnable_id"], name: "index_ranged_weapons_on_ra_wpnable_type_and_ra_wpnable_id" end - create_table "rangers", force: :cascade do |t| + create_table "samurai_mages", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "samurais", force: :cascade do |t| + create_table "samurai_rangers", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "strongholds", force: :cascade do |t| + create_table "samurai_warriors", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "towers", force: :cascade do |t| + create_table "samurais", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "walls", force: :cascade do |t| + create_table "strongholds", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "warriors", force: :cascade do |t| + create_table "towers", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "walls", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "weapons", force: :cascade do |t| + t.string "name", null: false + t.integer "damage", default: 10 + t.integer "range", default: 1 + t.string "type", default: "MeleeWeapon" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/lecture_2/homework/db/seeds.rb b/lecture_2/homework/db/seeds.rb index 443595ee..0c6433ae 100644 --- a/lecture_2/homework/db/seeds.rb +++ b/lecture_2/homework/db/seeds.rb @@ -6,26 +6,78 @@ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) +x = 0 - -3.times do +2.times do clan = Clan.create!(name: Faker::Creature::Cat.unique.registry) Faker::Number.between(2, 10).times do - clan.characters.create!( - name: Faker::Creature::Cat.unique.name, + sam = SamuraiWarrior.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: nil, + clan: clan + ) + MeleeWeapon.create!( + name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + damage: Faker::Number.between(0, 100), + range: Faker::Number.between(0, 3), + me_wpnable: sam + ) + x=x+1 + end + Faker::Number.between(2, 10).times do + hus = HussarMage.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: nil, + clan: clan + ) + MagicalWeapon.create!( + name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + damage: Faker::Number.between(0, 100), + range: Faker::Number.between(0, 100), + ma_wpnable: hus + ) + x=x+1 + end + Faker::UniqueGenerator.clear + Faker::Number.between(1, 5).times do + sam = SamuraiWarrior.create!( + name: Faker::Creature::Cat.name + x.to_s, number_of_battles: Faker::Number.between(0, 200), armor_quality: Faker::Number.between(0, 1000), join_date: Faker::Date.birthday(20, 70), - death_date: nil + death_date: Faker::Date.backward(20), + clan: clan ) + MeleeWeapon.create!( + name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + damage: Faker::Number.between(0, 100), + range: Faker::Number.between(0, 3), + me_wpnable: sam + ) + x=x+1 end Faker::Number.between(1, 5).times do - clan.characters.create!( - name: Faker::Creature::Cat.unique.name, + hus = HussarMage.create!( + name: Faker::Creature::Cat.name + x.to_s, number_of_battles: Faker::Number.between(0, 200), armor_quality: Faker::Number.between(0, 1000), join_date: Faker::Date.birthday(20, 70), - death_date: Faker::Date.backward(20) + death_date: Faker::Date.backward(20), + clan: clan ) + MagicalWeapon.create!( + name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + damage: Faker::Number.between(0, 100), + range: Faker::Number.between(0, 100), + ma_wpnable: hus + ) + x=x+1 end + end \ No newline at end of file diff --git a/lecture_2/homework/lib/mage.rb b/lecture_2/homework/lib/mage.rb new file mode 100644 index 00000000..104eec3e --- /dev/null +++ b/lecture_2/homework/lib/mage.rb @@ -0,0 +1,11 @@ +module Mage + extend ActiveSupport::Concern + included do + validates :profession, presence: true, inclusion: { in: %w(mage)} + end + #validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true + #validates :profession, presence: true, inclusion: { in: %w(mage)} +# def attack +# "#{name}: Pew pew pew! Whizzzz! " +# end +end diff --git a/lecture_2/homework/lib/ranger.rb b/lecture_2/homework/lib/ranger.rb new file mode 100644 index 00000000..712fe9ab --- /dev/null +++ b/lecture_2/homework/lib/ranger.rb @@ -0,0 +1,11 @@ +module Ranger + extend ActiveSupport::Concern + included do + validates :profession, presence: true, inclusion: { in: %w(ranger)} + end + #validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true + #validates :profession, presence: true, inclusion: { in: %w(ranger)} +# def attack +# "#{name}: Ftong! Swoosh! *arrow hit noise*" +# end +end diff --git a/lecture_2/homework/lib/warrior.rb b/lecture_2/homework/lib/warrior.rb new file mode 100644 index 00000000..834f42aa --- /dev/null +++ b/lecture_2/homework/lib/warrior.rb @@ -0,0 +1,12 @@ +module Warrior + extend ActiveSupport::Concern + included do + validates :profession, presence: true, inclusion: { in: %w(warrior)} + end + + #validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true + #validates :profession, presence: true, inclusion: { in: %w(warrior)} +# def attack +# "#{name}: *melee noises*" +# end +end diff --git a/lecture_2/homework/test/controllers/characters_controller_controller_test.rb b/lecture_2/homework/test/controllers/characters_controller_controller_test.rb deleted file mode 100644 index 39793e5f..00000000 --- a/lecture_2/homework/test/controllers/characters_controller_controller_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'test_helper' - -class CharactersControllerControllerTest < ActionDispatch::IntegrationTest - test "should get index" do - get characters_controller_index_url - assert_response :success - end - -end diff --git a/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb b/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb new file mode 100644 index 00000000..6b77693c --- /dev/null +++ b/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class SamuraiWarriorControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get samurai_warrior_index_url + assert_response :success + end + +end diff --git a/lecture_2/homework/test/fixtures/hussar_mages.yml b/lecture_2/homework/test/fixtures/hussar_mages.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/hussar_mages.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/hussar_rangers.yml b/lecture_2/homework/test/fixtures/hussar_rangers.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/hussar_rangers.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/hussar_warriors.yml b/lecture_2/homework/test/fixtures/hussar_warriors.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/hussar_warriors.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/samurai_mages.yml b/lecture_2/homework/test/fixtures/samurai_mages.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/samurai_mages.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/samurai_rangers.yml b/lecture_2/homework/test/fixtures/samurai_rangers.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/samurai_rangers.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/samurai_warriors.yml b/lecture_2/homework/test/fixtures/samurai_warriors.yml new file mode 100644 index 00000000..80aed36e --- /dev/null +++ b/lecture_2/homework/test/fixtures/samurai_warriors.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/models/hussar_mage_test.rb b/lecture_2/homework/test/models/hussar_mage_test.rb new file mode 100644 index 00000000..9ed671f1 --- /dev/null +++ b/lecture_2/homework/test/models/hussar_mage_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HussarMageTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/hussar_ranger_test.rb b/lecture_2/homework/test/models/hussar_ranger_test.rb new file mode 100644 index 00000000..506fc41c --- /dev/null +++ b/lecture_2/homework/test/models/hussar_ranger_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HussarRangerTest < 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..e4ad976e --- /dev/null +++ b/lecture_2/homework/test/models/hussar_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HussarTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/hussar_warrior_test.rb b/lecture_2/homework/test/models/hussar_warrior_test.rb new file mode 100644 index 00000000..1f53105c --- /dev/null +++ b/lecture_2/homework/test/models/hussar_warrior_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HussarWarriorTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/samurai_mage_test.rb b/lecture_2/homework/test/models/samurai_mage_test.rb new file mode 100644 index 00000000..ce53b4cf --- /dev/null +++ b/lecture_2/homework/test/models/samurai_mage_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SamuraiMageTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/samurai_ranger_test.rb b/lecture_2/homework/test/models/samurai_ranger_test.rb new file mode 100644 index 00000000..6ff616c8 --- /dev/null +++ b/lecture_2/homework/test/models/samurai_ranger_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SamuraiRangerTest < 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..a82ae29d --- /dev/null +++ b/lecture_2/homework/test/models/samurai_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SamuraiTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/lecture_2/homework/test/models/samurai_warrior_test.rb b/lecture_2/homework/test/models/samurai_warrior_test.rb new file mode 100644 index 00000000..dacdd2d4 --- /dev/null +++ b/lecture_2/homework/test/models/samurai_warrior_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SamuraiWarriorTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 12ad5ef34124d323995bc59b26b3eba917222cdb Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Mon, 15 Apr 2019 02:01:06 +0200 Subject: [PATCH 4/8] Add errorable and serializers --- lecture_2/homework/Gemfile | 9 ++++++--- lecture_2/homework/Gemfile.lock | 9 +++++++++ .../app/controllers/application_controller.rb | 13 +++++++------ .../controllers/clans/characters_controller.rb | 18 ++++++------------ .../app/controllers/clans_controller.rb | 18 +++++------------- .../app/controllers/concerns/errorable.rb | 13 +++++++++++++ .../controllers/samurai_warrior_controller.rb | 4 ---- .../app/serializers/building_serializer.rb | 3 +++ .../app/serializers/character_serializer.rb | 5 +++++ .../app/serializers/clan_serializer.rb | 3 +++ .../app/serializers/weapon_serializer.rb | 3 +++ lecture_2/homework/config/application.rb | 2 ++ .../migrate/20190409132527_create_buildings.rb | 2 +- 13 files changed, 63 insertions(+), 39 deletions(-) create mode 100644 lecture_2/homework/app/controllers/concerns/errorable.rb delete mode 100644 lecture_2/homework/app/controllers/samurai_warrior_controller.rb create mode 100644 lecture_2/homework/app/serializers/building_serializer.rb create mode 100644 lecture_2/homework/app/serializers/character_serializer.rb create mode 100644 lecture_2/homework/app/serializers/clan_serializer.rb create mode 100644 lecture_2/homework/app/serializers/weapon_serializer.rb diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index 265bb828..47f75cc8 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -44,8 +44,11 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] -#Rubocop +# Rubocop gem 'rubocop', require: false -#Faker -gem 'faker' \ No newline at end of file +# Faker +gem 'faker' + +# Serializers +gem 'active_model_serializers' \ No newline at end of file diff --git a/lecture_2/homework/Gemfile.lock b/lecture_2/homework/Gemfile.lock index e81662d4..27eecf10 100644 --- a/lecture_2/homework/Gemfile.lock +++ b/lecture_2/homework/Gemfile.lock @@ -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) @@ -48,6 +53,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) @@ -59,6 +66,7 @@ GEM i18n (1.6.0) concurrent-ruby (~> 1.0) jaro_winkler (1.5.2) + jsonapi-renderer (0.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -152,6 +160,7 @@ PLATFORMS ruby DEPENDENCIES + active_model_serializers bootsnap (>= 1.1.0) byebug faker diff --git a/lecture_2/homework/app/controllers/application_controller.rb b/lecture_2/homework/app/controllers/application_controller.rb index 891a2573..9da35fd2 100644 --- a/lecture_2/homework/app/controllers/application_controller.rb +++ b/lecture_2/homework/app/controllers/application_controller.rb @@ -1,9 +1,10 @@ class ApplicationController < ActionController::API - rescue_from ActiveRecord::RecordNotFound do |exception| - render json: { message: exception.message }, status: 404 - end +include Errorable +# 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 +# rescue_from ActiveRecord::RecordInvalid do |exception| +# render json: { message: exception.message }, status: 422 +# end end diff --git a/lecture_2/homework/app/controllers/clans/characters_controller.rb b/lecture_2/homework/app/controllers/clans/characters_controller.rb index 283b71cf..782263d9 100644 --- a/lecture_2/homework/app/controllers/clans/characters_controller.rb +++ b/lecture_2/homework/app/controllers/clans/characters_controller.rb @@ -4,38 +4,32 @@ def index characters = clan.characters.all if params.key?(:alive) if params[:alive].to_i == 0 - render json: characters.dead.to_json + render json: characters.dead else - render json: characters.alive.to_json + render json: characters.alive end else - render json: characters.to_json + render json: characters end end def create character = clan.characters.create!(character_params) - render json: character.to_json, status: 201 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 422 + render json: character, status: 201 end def show - render json: character.to_json + render json: character end def update character.update!(character_params) - render json: character.to_json, status: 201 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 422 + render json: character, status: 201 end def destroy character.destroy! head 204 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 404 end private diff --git a/lecture_2/homework/app/controllers/clans_controller.rb b/lecture_2/homework/app/controllers/clans_controller.rb index dda264c5..66e62540 100644 --- a/lecture_2/homework/app/controllers/clans_controller.rb +++ b/lecture_2/homework/app/controllers/clans_controller.rb @@ -1,33 +1,25 @@ class ClansController < ApplicationController def index - render json: Clan.all.to_json + render json: Clan.all end def create clan = Clan.create!(clan_params) - render json: clan.to_json, status: 201 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 422 + render json: clan, status: 201 end def show - render json: clan.to_json, status: 200 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 404 + render json: clan, status: 200 end def update clan.update(clan_params) - render json: clan.to_json, status: 201 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 422 + render json: clan, status: 201 end def destroy clan.destroy! head 204 - rescue ActiveRecord::ActiveRecordError => e - render json: e.to_json, status: 404 end private @@ -39,4 +31,4 @@ def clan def clan_params params.permit(:name, :id) end -end +end \ No newline at end of file diff --git a/lecture_2/homework/app/controllers/concerns/errorable.rb b/lecture_2/homework/app/controllers/concerns/errorable.rb new file mode 100644 index 00000000..9cc9f7f8 --- /dev/null +++ b/lecture_2/homework/app/controllers/concerns/errorable.rb @@ -0,0 +1,13 @@ +module Errorable + extend ActiveSupport::Concern + included do + rescue_from ActiveRecord::RecordInvalid, with: :render_validation_error + rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_error + end + def render_validation_error(exception) + render json: exception.record.errors, status: 422 + end + def render_not_found_error(exception) + render json: { errors: exception.message }, status: 404 + end +end \ No newline at end of file diff --git a/lecture_2/homework/app/controllers/samurai_warrior_controller.rb b/lecture_2/homework/app/controllers/samurai_warrior_controller.rb deleted file mode 100644 index a3dc4b7c..00000000 --- a/lecture_2/homework/app/controllers/samurai_warrior_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -class SamuraiWarriorController < ApplicationController - def index - end -end diff --git a/lecture_2/homework/app/serializers/building_serializer.rb b/lecture_2/homework/app/serializers/building_serializer.rb new file mode 100644 index 00000000..c5cb5b09 --- /dev/null +++ b/lecture_2/homework/app/serializers/building_serializer.rb @@ -0,0 +1,3 @@ +class BuildingSerializer < ActiveModel::Serializer + attributes :id, :name +end diff --git a/lecture_2/homework/app/serializers/character_serializer.rb b/lecture_2/homework/app/serializers/character_serializer.rb new file mode 100644 index 00000000..ebef0b20 --- /dev/null +++ b/lecture_2/homework/app/serializers/character_serializer.rb @@ -0,0 +1,5 @@ +class CharacterSerializer < ActiveModel::Serializer + attributes :id, :name, :armor_quality, :number_of_battles, :join_date, :death_date, :type + belongs_to :clan + belongs_to :defensible +end \ No newline at end of file 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..ff869360 --- /dev/null +++ b/lecture_2/homework/app/serializers/clan_serializer.rb @@ -0,0 +1,3 @@ +class ClanSerializer < ActiveModel::Serializer + attributes :id, :name +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..917d9529 --- /dev/null +++ b/lecture_2/homework/app/serializers/weapon_serializer.rb @@ -0,0 +1,3 @@ +class WeaponSerializer < ActiveModel::Serializer + attributes :id, :name, :damage, :range, :type #:wpnables? +end diff --git a/lecture_2/homework/config/application.rb b/lecture_2/homework/config/application.rb index 214541a3..f1703215 100644 --- a/lecture_2/homework/config/application.rb +++ b/lecture_2/homework/config/application.rb @@ -17,6 +17,8 @@ # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) +ActiveModelSerializers.config.adapter = :json_api + module Lecture1 class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. diff --git a/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb index 9de8c710..a16aab6e 100644 --- a/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb +++ b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb @@ -1,7 +1,7 @@ class CreateBuildings < ActiveRecord::Migration[5.2] def change create_table :buildings do |t| - + t.string :name, null: false t.timestamps end end From 3faeb8c06992fef257282fe54d52e810289eb0a1 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Mon, 15 Apr 2019 02:27:59 +0200 Subject: [PATCH 5/8] Add sentry.io support --- lecture_2/homework/Gemfile | 5 +- lecture_2/homework/Gemfile.lock | 6 +++ .../app/controllers/application_controller.rb | 10 +++- .../clans/characters_controller.rb | 10 ++-- .../app/controllers/clans_controller.rb | 2 +- lecture_2/homework/config/application.rb | 4 ++ lecture_2/homework/db/seeds.rb | 48 +++++++++---------- 7 files changed, 53 insertions(+), 32 deletions(-) diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index 47f75cc8..208696c9 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -51,4 +51,7 @@ gem 'rubocop', require: false gem 'faker' # Serializers -gem 'active_model_serializers' \ No newline at end of file +gem 'active_model_serializers' + +# Sentry +gem 'sentry-raven' \ No newline at end of file diff --git a/lecture_2/homework/Gemfile.lock b/lecture_2/homework/Gemfile.lock index 27eecf10..b5733688 100644 --- a/lecture_2/homework/Gemfile.lock +++ b/lecture_2/homework/Gemfile.lock @@ -60,6 +60,8 @@ GEM erubi (1.8.0) faker (1.9.3) i18n (>= 0.7) + faraday (0.15.4) + multipart-post (>= 1.2, < 3) ffi (1.10.0) globalid (0.4.2) activesupport (>= 4.2.0) @@ -84,6 +86,7 @@ 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) mini_portile2 (~> 2.4.0) @@ -135,6 +138,8 @@ GEM unicode-display_width (>= 1.4.0, < 1.6) ruby-progressbar (1.10.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) @@ -169,6 +174,7 @@ DEPENDENCIES puma (~> 3.11) rails (~> 5.2.2) rubocop + sentry-raven spring spring-watcher-listen (~> 2.0.0) tzinfo-data diff --git a/lecture_2/homework/app/controllers/application_controller.rb b/lecture_2/homework/app/controllers/application_controller.rb index 9da35fd2..ce066a69 100644 --- a/lecture_2/homework/app/controllers/application_controller.rb +++ b/lecture_2/homework/app/controllers/application_controller.rb @@ -1,5 +1,13 @@ class ApplicationController < ActionController::API -include Errorable + include Errorable + before_action :set_raven_context + + private + + 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 # rescue_from ActiveRecord::RecordNotFound do |exception| # render json: { message: exception.message }, status: 404 # end diff --git a/lecture_2/homework/app/controllers/clans/characters_controller.rb b/lecture_2/homework/app/controllers/clans/characters_controller.rb index 782263d9..d66bc73c 100644 --- a/lecture_2/homework/app/controllers/clans/characters_controller.rb +++ b/lecture_2/homework/app/controllers/clans/characters_controller.rb @@ -4,22 +4,22 @@ def index characters = clan.characters.all if params.key?(:alive) if params[:alive].to_i == 0 - render json: characters.dead + render json: characters.dead, include: ['clan', 'defensible'], status: 200 else - render json: characters.alive + render json: characters.alive, include: ['clan', 'defensible'], status: 200 end else - render json: characters + render json: characters, include: ['clan', 'defensible'], status: 200 end end def create character = clan.characters.create!(character_params) - render json: character, status: 201 + render json: character, include: ['clan', 'defensible'], status: 201 end def show - render json: character + render json: character, include: ['clan', 'defensible'], status: 200 end def update diff --git a/lecture_2/homework/app/controllers/clans_controller.rb b/lecture_2/homework/app/controllers/clans_controller.rb index 66e62540..611f84f6 100644 --- a/lecture_2/homework/app/controllers/clans_controller.rb +++ b/lecture_2/homework/app/controllers/clans_controller.rb @@ -1,6 +1,6 @@ class ClansController < ApplicationController def index - render json: Clan.all + render json: Clan.all, status: 200 end def create diff --git a/lecture_2/homework/config/application.rb b/lecture_2/homework/config/application.rb index f1703215..661fb211 100644 --- a/lecture_2/homework/config/application.rb +++ b/lecture_2/homework/config/application.rb @@ -19,6 +19,10 @@ ActiveModelSerializers.config.adapter = :json_api +Raven.configure do |config| + config.dsn = 'https://33b17af8a5c8411a83c52c1f399dbbea:65f4f8e22bc54eefa6b8a931db396ea7@sentry.io/1438780' +end + module Lecture1 class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. diff --git a/lecture_2/homework/db/seeds.rb b/lecture_2/homework/db/seeds.rb index 0c6433ae..4d8cb60c 100644 --- a/lecture_2/homework/db/seeds.rb +++ b/lecture_2/homework/db/seeds.rb @@ -19,12 +19,12 @@ death_date: nil, clan: clan ) - MeleeWeapon.create!( - name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - damage: Faker::Number.between(0, 100), - range: Faker::Number.between(0, 3), - me_wpnable: sam - ) + # MeleeWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 3), + # me_wpnable: sam + # ) x=x+1 end Faker::Number.between(2, 10).times do @@ -36,12 +36,12 @@ death_date: nil, clan: clan ) - MagicalWeapon.create!( - name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - damage: Faker::Number.between(0, 100), - range: Faker::Number.between(0, 100), - ma_wpnable: hus - ) + # MagicalWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 100), + # ma_wpnable: hus + # ) x=x+1 end Faker::UniqueGenerator.clear @@ -54,12 +54,12 @@ death_date: Faker::Date.backward(20), clan: clan ) - MeleeWeapon.create!( - name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - damage: Faker::Number.between(0, 100), - range: Faker::Number.between(0, 3), - me_wpnable: sam - ) + # MeleeWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 3), + # me_wpnable: sam + # ) x=x+1 end Faker::Number.between(1, 5).times do @@ -71,12 +71,12 @@ death_date: Faker::Date.backward(20), clan: clan ) - MagicalWeapon.create!( - name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - damage: Faker::Number.between(0, 100), - range: Faker::Number.between(0, 100), - ma_wpnable: hus - ) + # MagicalWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 100), + # ma_wpnable: hus + # ) x=x+1 end From a0df4e67f8d17a3adcc4f7ae583d0729706fdc49 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Mon, 15 Apr 2019 03:00:51 +0200 Subject: [PATCH 6/8] Correct most offences [RuboCop] --- lecture_2/homework/Gemfile | 18 +- lecture_2/homework/Rakefile | 5 +- .../app/channels/application_cable/channel.rb | 2 + .../channels/application_cable/connection.rb | 2 + .../app/controllers/application_controller.rb | 16 +- .../clans/characters_controller.rb | 42 ++-- .../app/controllers/clans_controller.rb | 14 +- .../app/controllers/concerns/errorable.rb | 5 +- .../homework/app/jobs/application_job.rb | 2 + .../app/mailers/application_mailer.rb | 2 + .../homework/app/models/application_record.rb | 2 + lecture_2/homework/app/models/barricade.rb | 2 + lecture_2/homework/app/models/building.rb | 2 + lecture_2/homework/app/models/character.rb | 17 +- lecture_2/homework/app/models/clan.rb | 2 + lecture_2/homework/app/models/gate.rb | 2 + lecture_2/homework/app/models/hussar.rb | 4 +- lecture_2/homework/app/models/hussar_mage.rb | 4 +- .../homework/app/models/hussar_ranger.rb | 4 +- .../homework/app/models/hussar_warrior.rb | 4 +- lecture_2/homework/app/models/mage.rb | 6 +- .../homework/app/models/magical_weapon.rb | 14 +- lecture_2/homework/app/models/melee_weapon.rb | 14 +- .../homework/app/models/ranged_weapon.rb | 14 +- lecture_2/homework/app/models/ranger.rb | 6 +- lecture_2/homework/app/models/samurai.rb | 4 +- lecture_2/homework/app/models/samurai_mage.rb | 4 +- .../homework/app/models/samurai_ranger.rb | 4 +- .../homework/app/models/samurai_warrior.rb | 4 +- lecture_2/homework/app/models/stronghold.rb | 2 + lecture_2/homework/app/models/tower.rb | 2 + lecture_2/homework/app/models/wall.rb | 2 + lecture_2/homework/app/models/warrior.rb | 6 +- lecture_2/homework/app/models/weapon.rb | 4 +- .../app/serializers/building_serializer.rb | 2 + .../app/serializers/character_serializer.rb | 9 +- .../app/serializers/clan_serializer.rb | 2 + .../app/serializers/weapon_serializer.rb | 2 + lecture_2/homework/bin/bundle | 2 + lecture_2/homework/bin/rails | 4 +- lecture_2/homework/bin/rake | 4 +- lecture_2/homework/bin/setup | 2 + lecture_2/homework/bin/spring | 3 +- lecture_2/homework/bin/update | 2 + lecture_2/homework/config.ru | 2 + lecture_2/homework/config/application.rb | 24 ++- lecture_2/homework/config/boot.rb | 2 + lecture_2/homework/config/environment.rb | 2 + .../config/environments/development.rb | 12 +- .../config/environments/production.rb | 30 ++- .../homework/config/environments/test.rb | 5 +- .../application_controller_renderer.rb | 2 + .../initializers/backtrace_silencers.rb | 2 + .../homework/config/initializers/cors.rb | 2 + .../initializers/filter_parameter_logging.rb | 2 + .../config/initializers/inflections.rb | 2 + .../config/initializers/mime_types.rb | 2 + .../config/initializers/wrap_parameters.rb | 2 + lecture_2/homework/config/puma.rb | 8 +- lecture_2/homework/config/routes.rb | 4 +- lecture_2/homework/config/spring.rb | 2 + ...90314150921_create_clans_and_characters.rb | 4 +- .../20190409132527_create_buildings.rb | 2 + .../20190409132556_create_barricades.rb | 7 +- .../20190409132607_create_strongholds.rb | 7 +- .../db/migrate/20190409132618_create_walls.rb | 7 +- .../migrate/20190409132624_create_towers.rb | 7 +- .../db/migrate/20190409132636_create_gates.rb | 7 +- .../migrate/20190409133633_create_weapons.rb | 6 +- .../20190409133651_create_magical_weapons.rb | 7 +- .../20190409133700_create_melee_weapons.rb | 7 +- .../20190409133707_create_ranged_weapons.rb | 7 +- .../20190409204516_index_name_on_clans.rb | 2 + ...0409210018_add_defensible_to_characters.rb | 2 + .../migrate/20190414164925_create_samurais.rb | 7 +- .../migrate/20190414194133_create_hussars.rb | 7 +- .../20190414203621_create_samurai_mages.rb | 7 +- .../20190414203629_create_samurai_rangers.rb | 7 +- .../20190414203645_create_samurai_warriors.rb | 7 +- .../20190414210802_create_hussar_mages.rb | 7 +- .../20190414210810_create_hussar_rangers.rb | 7 +- .../20190414210818_create_hussar_warriors.rb | 7 +- ...21757_add_ma_wpnable_to_magical_weapons.rb | 2 + ...221851_add_ra_wpnable_to_ranged_weapons.rb | 2 + ...4221902_add_me_wpnable_to_melee_weapons.rb | 2 + lecture_2/homework/db/schema.rb | 191 +++++++++--------- lecture_2/homework/db/seeds.rb | 143 ++++++------- lecture_2/homework/lib/mage.rb | 16 +- lecture_2/homework/lib/ranger.rb | 14 +- lecture_2/homework/lib/warrior.rb | 14 +- .../samurai_warrior_controller_test.rb | 5 +- .../homework/test/models/hussar_mage_test.rb | 2 + .../test/models/hussar_ranger_test.rb | 2 + lecture_2/homework/test/models/hussar_test.rb | 2 + .../test/models/hussar_warrior_test.rb | 2 + .../homework/test/models/samurai_mage_test.rb | 2 + .../test/models/samurai_ranger_test.rb | 2 + .../homework/test/models/samurai_test.rb | 2 + .../test/models/samurai_warrior_test.rb | 2 + 99 files changed, 540 insertions(+), 367 deletions(-) diff --git a/lecture_2/homework/Gemfile b/lecture_2/homework/Gemfile index 208696c9..135b7295 100644 --- a/lecture_2/homework/Gemfile +++ b/lecture_2/homework/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } @@ -25,24 +27,26 @@ gem 'puma', '~> 3.11' # 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 +# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), +# making cross-origin AJAX possible # gem 'rack-cors' 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] + # Call 'byebug' anywhere in the code to stop execution and get a debugger + # console + 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 + # 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] # Rubocop gem 'rubocop', require: false @@ -54,4 +58,4 @@ gem 'faker' gem 'active_model_serializers' # Sentry -gem 'sentry-raven' \ No newline at end of file +gem 'sentry-raven' diff --git a/lecture_2/homework/Rakefile b/lecture_2/homework/Rakefile index e85f9139..2d79e514 100644 --- a/lecture_2/homework/Rakefile +++ b/lecture_2/homework/Rakefile @@ -1,5 +1,8 @@ +# 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. +# for example lib/tasks/capistrano.rake, and they will automatically be +# available to Rake. require_relative 'config/application' 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 ce066a69..8b6b0038 100644 --- a/lecture_2/homework/app/controllers/application_controller.rb +++ b/lecture_2/homework/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationController < ActionController::API include Errorable before_action :set_raven_context @@ -5,14 +7,14 @@ class ApplicationController < ActionController::API private def set_raven_context - Raven.user_context(id: session[:current_user_id]) # or anything else in session + Raven.user_context(id: session[:current_user_id]) Raven.extra_context(params: params.to_unsafe_h, url: request.url) end -# rescue_from ActiveRecord::RecordNotFound do |exception| -# render json: { message: exception.message }, status: 404 -# end + # 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 + # rescue_from ActiveRecord::RecordInvalid do |exception| + # render json: { message: exception.message }, status: 422 + # end end diff --git a/lecture_2/homework/app/controllers/clans/characters_controller.rb b/lecture_2/homework/app/controllers/clans/characters_controller.rb index d66bc73c..42c254bf 100644 --- a/lecture_2/homework/app/controllers/clans/characters_controller.rb +++ b/lecture_2/homework/app/controllers/clans/characters_controller.rb @@ -1,32 +1,36 @@ +# frozen_string_literal: true + module Clans class CharactersController < ApplicationController def index characters = clan.characters.all if params.key?(:alive) - if params[:alive].to_i == 0 - render json: characters.dead, include: ['clan', 'defensible'], status: 200 + if params[:alive].to_i.zero? + render json: characters.dead, include: %w[clan defensible], + status: 200 else - render json: characters.alive, include: ['clan', 'defensible'], status: 200 + render json: characters.alive, include: %w[clan defensible], + status: 200 end else - render json: characters, include: ['clan', 'defensible'], status: 200 + render json: characters, include: %w[clan defensible], status: 200 end end def create character = clan.characters.create!(character_params) - render json: character, include: ['clan', 'defensible'], status: 201 + render json: character, include: %w[clan defensible], status: 201 end - - def show - render json: character, include: ['clan', 'defensible'], status: 200 + + def show + render json: character, include: %w[clan defensible], status: 200 end def update - character.update!(character_params) - render json: character, status: 201 + character.update!(character_params) + render json: character, status: 201 end - + def destroy character.destroy! head 204 @@ -39,15 +43,17 @@ def clan end def character - @character ||= Character.find_by!(id: params[:id], clan_id: params[:clan_id]) + @character ||= Character.find_by!(id: params[:id], + clan_id: params[:clan_id]) end - -# def character -# clan.characters.find(params[:id]) -# end + + # def character + # clan.characters.find(params[:id]) + # end def character_params - params.permit(:name, :armour_rating, :battle_count, :join_date, :death_date) + params.permit(:name, :armour_rating, :battle_count, :join_date, + :death_date) end end -end \ No newline at end of file +end diff --git a/lecture_2/homework/app/controllers/clans_controller.rb b/lecture_2/homework/app/controllers/clans_controller.rb index 611f84f6..ce7605b6 100644 --- a/lecture_2/homework/app/controllers/clans_controller.rb +++ b/lecture_2/homework/app/controllers/clans_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ClansController < ApplicationController def index render json: Clan.all, status: 200 @@ -7,23 +9,23 @@ def create clan = Clan.create!(clan_params) render json: clan, status: 201 end - + def show render json: clan, status: 200 end - + def update clan.update(clan_params) - render json: clan, status: 201 + render json: clan, status: 201 end - + def destroy clan.destroy! head 204 end private - + def clan Clan.find(params[:id]) end @@ -31,4 +33,4 @@ def clan def clan_params params.permit(:name, :id) end -end \ No newline at end of file +end diff --git a/lecture_2/homework/app/controllers/concerns/errorable.rb b/lecture_2/homework/app/controllers/concerns/errorable.rb index 9cc9f7f8..23f56186 100644 --- a/lecture_2/homework/app/controllers/concerns/errorable.rb +++ b/lecture_2/homework/app/controllers/concerns/errorable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Errorable extend ActiveSupport::Concern included do @@ -7,7 +9,8 @@ module Errorable def render_validation_error(exception) render json: exception.record.errors, status: 422 end + def render_not_found_error(exception) render json: { errors: exception.message }, status: 404 end -end \ No newline at end of file +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/barricade.rb b/lecture_2/homework/app/models/barricade.rb index 1c3fc47a..bd16aee0 100644 --- a/lecture_2/homework/app/models/barricade.rb +++ b/lecture_2/homework/app/models/barricade.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Barricade < Building end diff --git a/lecture_2/homework/app/models/building.rb b/lecture_2/homework/app/models/building.rb index 48182381..c1f2404e 100644 --- a/lecture_2/homework/app/models/building.rb +++ b/lecture_2/homework/app/models/building.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Building < ApplicationRecord has_many :characters, as: :defensible validates :name, presence: true diff --git a/lecture_2/homework/app/models/character.rb b/lecture_2/homework/app/models/character.rb index ed0e5c44..d6fcd1ae 100644 --- a/lecture_2/homework/app/models/character.rb +++ b/lecture_2/homework/app/models/character.rb @@ -1,15 +1,20 @@ +# frozen_string_literal: true + class Character < ApplicationRecord - #self.abstract_class = true + # self.abstract_class = true belongs_to :clan - #has_one :weapon, dependent: :destroy + # has_one :weapon, dependent: :destroy belongs_to :defensible, polymorphic: true, optional: true - - validates :name, presence: true, uniqueness: { scope: ['death_date.present?', :clan_id] } - #validates :profession, presence: true, inclusion: { in: %w(mage ranger warrior)} + + validates :name, presence: true, uniqueness: { scope: ['death_date.present?', + :clan_id] } + # validates :profession, presence: true, inclusion: { in: %w(mage ranger + # warrior)} validates :armor_quality, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 } - #validates :weapon, inclusion: { in: %w(magical_weapon melee_weapon ranged_weapon)}, allow_nil: true + # validates :weapon, inclusion: { in: %w(magical_weapon melee_weapon + # ranged_weapon)}, allow_nil: true scope :alive, -> { where('death_date IS NULL') } scope :dead, -> { where('death_date IS NOT NULL') } diff --git a/lecture_2/homework/app/models/clan.rb b/lecture_2/homework/app/models/clan.rb index f84c2b96..8399710f 100644 --- a/lecture_2/homework/app/models/clan.rb +++ b/lecture_2/homework/app/models/clan.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Clan < ApplicationRecord has_many :characters, dependent: :destroy validates :name, presence: true, uniqueness: true diff --git a/lecture_2/homework/app/models/gate.rb b/lecture_2/homework/app/models/gate.rb index cb5a8734..b3540216 100644 --- a/lecture_2/homework/app/models/gate.rb +++ b/lecture_2/homework/app/models/gate.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Gate < Building end diff --git a/lecture_2/homework/app/models/hussar.rb b/lecture_2/homework/app/models/hussar.rb index 7e481d16..3d60d4ca 100644 --- a/lecture_2/homework/app/models/hussar.rb +++ b/lecture_2/homework/app/models/hussar.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class Hussar < Character def attack - "#{name}: *hussar attack*" + "#{name}: *hussar attack*" end end diff --git a/lecture_2/homework/app/models/hussar_mage.rb b/lecture_2/homework/app/models/hussar_mage.rb index cb3a0b26..da894d22 100644 --- a/lecture_2/homework/app/models/hussar_mage.rb +++ b/lecture_2/homework/app/models/hussar_mage.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class HussarMage < Hussar has_one :magical_weapon, dependent: :destroy, as: :ma_wpnable def attack - "#{name}: *hussar magical attack*" + "#{name}: *hussar magical attack*" end end diff --git a/lecture_2/homework/app/models/hussar_ranger.rb b/lecture_2/homework/app/models/hussar_ranger.rb index 53f0fb82..5920b191 100644 --- a/lecture_2/homework/app/models/hussar_ranger.rb +++ b/lecture_2/homework/app/models/hussar_ranger.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class HussarRanger < Hussar has_one :ranged_weapon, dependent: :destroy, as: :ra_wpnable def attack - "#{name}: *hussar ranged attack*" + "#{name}: *hussar ranged attack*" end end diff --git a/lecture_2/homework/app/models/hussar_warrior.rb b/lecture_2/homework/app/models/hussar_warrior.rb index 20d4ef76..14bc04bf 100644 --- a/lecture_2/homework/app/models/hussar_warrior.rb +++ b/lecture_2/homework/app/models/hussar_warrior.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class HussarWarrior < Hussar has_one :melee_weapon, dependent: :destroy, as: :me_wpnable def attack - "#{name}: *hussar melee attack*" + "#{name}: *hussar melee attack*" end end diff --git a/lecture_2/homework/app/models/mage.rb b/lecture_2/homework/app/models/mage.rb index 14451f7f..a2c5a807 100644 --- a/lecture_2/homework/app/models/mage.rb +++ b/lecture_2/homework/app/models/mage.rb @@ -1,7 +1,9 @@ -#class Mage < Character +# frozen_string_literal: true + +# class Mage < Character # self.abstract_class = true # validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true # def attack # "#{name}: Pew pew pew! Whizzzz! " # end -#end +# end diff --git a/lecture_2/homework/app/models/magical_weapon.rb b/lecture_2/homework/app/models/magical_weapon.rb index bcf60dcc..491deab8 100644 --- a/lecture_2/homework/app/models/magical_weapon.rb +++ b/lecture_2/homework/app/models/magical_weapon.rb @@ -1,12 +1,12 @@ +# frozen_string_literal: true + class MagicalWeapon < Weapon belongs_to :ma_wpnable, polymorphic: true def calculate_damage(distance) - distance = distance.abs - range = range.abs - if distance > range - return 0 - else - damage * (0.5 * (1-distance/range) + 0.25) - end + distance = distance.abs + range = range.abs + return 0 if distance > range + + damage * (0.5 * (1 - distance / range) + 0.25) end end diff --git a/lecture_2/homework/app/models/melee_weapon.rb b/lecture_2/homework/app/models/melee_weapon.rb index 696d91f8..8cba0f9f 100644 --- a/lecture_2/homework/app/models/melee_weapon.rb +++ b/lecture_2/homework/app/models/melee_weapon.rb @@ -1,12 +1,12 @@ +# frozen_string_literal: true + class MeleeWeapon < Weapon belongs_to :me_wpnable, polymorphic: true def calculate_damage(distance) - distance = distance.abs - range = range.abs - if distance > range - return 0 - else - damage * (1 - distance / range) - end + distance = distance.abs + range = range.abs + return 0 if distance > range + + damage * (1 - distance / range) end end diff --git a/lecture_2/homework/app/models/ranged_weapon.rb b/lecture_2/homework/app/models/ranged_weapon.rb index df6a4947..60e4e3e3 100644 --- a/lecture_2/homework/app/models/ranged_weapon.rb +++ b/lecture_2/homework/app/models/ranged_weapon.rb @@ -1,12 +1,12 @@ +# frozen_string_literal: true + class RangedWeapon < Weapon belongs_to :ra_wpnable, polymorphic: true def calculate_damage(distance) - distance = distance.abs - range = range.abs - if distance > range - return 0 - else - damage * (0.5 * distance/range + 0.25) - end + distance = distance.abs + range = range.abs + return 0 if distance > range + + damage * (0.5 * distance / range + 0.25) end end diff --git a/lecture_2/homework/app/models/ranger.rb b/lecture_2/homework/app/models/ranger.rb index 0c439d6b..d34394c7 100644 --- a/lecture_2/homework/app/models/ranger.rb +++ b/lecture_2/homework/app/models/ranger.rb @@ -1,7 +1,9 @@ -#class Ranger < Character +# frozen_string_literal: true + +# class Ranger < Character # self.abstract_class = true # validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true # def attack # "#{name}: Ftong! Swoosh! *arrow hit noise*" # end -#end +# end diff --git a/lecture_2/homework/app/models/samurai.rb b/lecture_2/homework/app/models/samurai.rb index b17f01a5..c52514f5 100644 --- a/lecture_2/homework/app/models/samurai.rb +++ b/lecture_2/homework/app/models/samurai.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class Samurai < Character def attack - "#{name}: *samurai attack*" + "#{name}: *samurai attack*" end end diff --git a/lecture_2/homework/app/models/samurai_mage.rb b/lecture_2/homework/app/models/samurai_mage.rb index 79e363bd..cd203555 100644 --- a/lecture_2/homework/app/models/samurai_mage.rb +++ b/lecture_2/homework/app/models/samurai_mage.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class SamuraiMage < Samurai has_one :magical_weapon, dependent: :destroy, as: :ma_wpnable def attack - "#{name}: *samurai magical attack*" + "#{name}: *samurai magical attack*" end end diff --git a/lecture_2/homework/app/models/samurai_ranger.rb b/lecture_2/homework/app/models/samurai_ranger.rb index 737ed801..8f9999fe 100644 --- a/lecture_2/homework/app/models/samurai_ranger.rb +++ b/lecture_2/homework/app/models/samurai_ranger.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class SamuraiRanger < Samurai has_one :ranged_weapon, dependent: :destroy, as: :ra_wpnable def attack - "#{name}: *samurai ranged attack*" + "#{name}: *samurai ranged attack*" end end diff --git a/lecture_2/homework/app/models/samurai_warrior.rb b/lecture_2/homework/app/models/samurai_warrior.rb index 10eef726..917b457a 100644 --- a/lecture_2/homework/app/models/samurai_warrior.rb +++ b/lecture_2/homework/app/models/samurai_warrior.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class SamuraiWarrior < Samurai has_one :melee_weapon, dependent: :destroy, as: :me_wpnable def attack - "#{name}: *samurai melee attack*" + "#{name}: *samurai melee attack*" end end diff --git a/lecture_2/homework/app/models/stronghold.rb b/lecture_2/homework/app/models/stronghold.rb index 3d291eec..1218af3e 100644 --- a/lecture_2/homework/app/models/stronghold.rb +++ b/lecture_2/homework/app/models/stronghold.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Stronghold < Building end diff --git a/lecture_2/homework/app/models/tower.rb b/lecture_2/homework/app/models/tower.rb index 192af17b..bd2c75a0 100644 --- a/lecture_2/homework/app/models/tower.rb +++ b/lecture_2/homework/app/models/tower.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Tower < Building end diff --git a/lecture_2/homework/app/models/wall.rb b/lecture_2/homework/app/models/wall.rb index d921c673..c1363e99 100644 --- a/lecture_2/homework/app/models/wall.rb +++ b/lecture_2/homework/app/models/wall.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Wall < Building end diff --git a/lecture_2/homework/app/models/warrior.rb b/lecture_2/homework/app/models/warrior.rb index 9929040e..1049d3b9 100644 --- a/lecture_2/homework/app/models/warrior.rb +++ b/lecture_2/homework/app/models/warrior.rb @@ -1,7 +1,9 @@ -#class Warrior < Character +# frozen_string_literal: true + +# class Warrior < Character # self.abstract_class = true # validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true # def attack # "#{name}: *melee noises*" # end -#end +# end diff --git a/lecture_2/homework/app/models/weapon.rb b/lecture_2/homework/app/models/weapon.rb index c5ab4fe5..c1b6beff 100644 --- a/lecture_2/homework/app/models/weapon.rb +++ b/lecture_2/homework/app/models/weapon.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class Weapon < ApplicationRecord - #belongs_to :weaponable, polymorphic: true + # belongs_to :weaponable, polymorphic: true validates :name, presence: true validates :damage, numericality: { only_integer: true, greater_than_or_equal_to: 0, diff --git a/lecture_2/homework/app/serializers/building_serializer.rb b/lecture_2/homework/app/serializers/building_serializer.rb index c5cb5b09..e0e2e45d 100644 --- a/lecture_2/homework/app/serializers/building_serializer.rb +++ b/lecture_2/homework/app/serializers/building_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class BuildingSerializer < ActiveModel::Serializer attributes :id, :name end diff --git a/lecture_2/homework/app/serializers/character_serializer.rb b/lecture_2/homework/app/serializers/character_serializer.rb index ebef0b20..5cbc839a 100644 --- a/lecture_2/homework/app/serializers/character_serializer.rb +++ b/lecture_2/homework/app/serializers/character_serializer.rb @@ -1,5 +1,8 @@ +# frozen_string_literal: true + class CharacterSerializer < ActiveModel::Serializer - attributes :id, :name, :armor_quality, :number_of_battles, :join_date, :death_date, :type - belongs_to :clan + attributes :id, :name, :armor_quality, :number_of_battles, :join_date, + :death_date, :type + belongs_to :clan belongs_to :defensible -end \ No newline at end of file +end diff --git a/lecture_2/homework/app/serializers/clan_serializer.rb b/lecture_2/homework/app/serializers/clan_serializer.rb index ff869360..27748f34 100644 --- a/lecture_2/homework/app/serializers/clan_serializer.rb +++ b/lecture_2/homework/app/serializers/clan_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ClanSerializer < ActiveModel::Serializer attributes :id, :name end diff --git a/lecture_2/homework/app/serializers/weapon_serializer.rb b/lecture_2/homework/app/serializers/weapon_serializer.rb index 917d9529..cee22bea 100644 --- a/lecture_2/homework/app/serializers/weapon_serializer.rb +++ b/lecture_2/homework/app/serializers/weapon_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class WeaponSerializer < ActiveModel::Serializer attributes :id, :name, :damage, :range, :type #:wpnables? 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 661fb211..cc14b055 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. @@ -37,6 +39,6 @@ class Application < Rails::Application # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true - config.autoload_paths += %W(#{config.root}/lib) + config.autoload_paths += %W[#{config.root}/lib] 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/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..7ed6e965 100644 --- a/lecture_2/homework/config/environments/development.rb +++ b/lecture_2/homework/config/environments/development.rb @@ -1,5 +1,8 @@ +# frozen_string_literal: true + Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. + # Settings specified here will take precedence over those in + # config/application.rb. # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development @@ -27,7 +30,8 @@ config.cache_store = :null_store end - # Store uploaded files on the local file system (see config/storage.yml for options) + # Store uploaded files on the local file system (see config/storage.yml + # for options) config.active_storage.service = :local # Don't care if the mailer can't send. @@ -44,11 +48,11 @@ # 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 - # Use an evented file watcher to asynchronously detect changes in source code, + # Use an evented file watcher to asynchronously detect changes + # in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker end diff --git a/lecture_2/homework/config/environments/production.rb b/lecture_2/homework/config/environments/production.rb index 77930568..57066190 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. @@ -14,11 +16,14 @@ config.consider_all_requests_local = false config.action_controller.perform_caching = true - # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] - # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # Ensures that a master key has been made available in either + # ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials + # (and other encrypted files). # config.require_master_key = true - # Disable serving static files from the `/public` folder by default since + # Disable serving static files from the `/public` folder by default + # since # Apache or NGINX already handles this. config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? @@ -29,15 +34,18 @@ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX - # Store uploaded files on the local file system (see config/storage.yml for options) + # Store uploaded files on the local file system (see config/storage.yml + # for options) config.active_storage.service = :local # Mount Action Cable outside main process or domain # config.action_cable.mount_path = nil # config.action_cable.url = 'wss://example.com/cable' - # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + # config.action_cable.allowed_request_origins = [ 'http://example.com', + # /http:\/\/example.*/ ] - # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # Force all access to the app over SSL, use Strict-Transport-Security, + # and use secure cookies. # config.force_ssl = true # Use the lowest log level to ensure availability of diagnostic information @@ -45,7 +53,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 @@ -57,7 +65,8 @@ config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. - # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # Set this to true and configure the email server for immediate delivery + # to raise delivery errors. # config.action_mailer.raise_delivery_errors = false # Enable locale fallbacks for I18n (makes lookups for any locale fall back to @@ -72,9 +81,10 @@ # Use a different logger for distributed setups. # require 'syslog/logger' - # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + # 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..aa326123 100644 --- a/lecture_2/homework/config/environments/test.rb +++ b/lecture_2/homework/config/environments/test.rb @@ -1,5 +1,8 @@ +# frozen_string_literal: true + Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. + # Settings specified here will take precedence over those in + # config/application.rb. # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that 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/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 1c2173e4..d1d97bd9 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.routes.draw do get 'clans/index' get 'characters/index' @@ -6,4 +8,4 @@ resources :clans, only: %i[index create show update destroy] do resources :characters, module: :clans end -end \ No newline at end of file +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_characters.rb b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb index 3dc6604e..faea7380 100644 --- a/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb +++ b/lecture_2/homework/db/migrate/20190314150921_create_clans_and_characters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateClansAndCharacters < ActiveRecord::Migration[5.2] def change create_table :clans do |t| @@ -11,7 +13,7 @@ def change t.integer :number_of_battles, default: 0 t.date :join_date, null: false t.date :death_date - t.string :type, default: 'Samurai' + t.string :type, default: 'Samurai' t.references :clan, foreign_key: true, index: true t.timestamps end diff --git a/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb index a16aab6e..bdff8420 100644 --- a/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb +++ b/lecture_2/homework/db/migrate/20190409132527_create_buildings.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateBuildings < ActiveRecord::Migration[5.2] def change create_table :buildings do |t| diff --git a/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb b/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb index 784e57b7..a8d4ea91 100644 --- a/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb +++ b/lecture_2/homework/db/migrate/20190409132556_create_barricades.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateBarricades < ActiveRecord::Migration[5.2] def change - create_table :barricades do |t| - - t.timestamps - end + create_table :barricades, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb b/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb index 5ce70f63..e7e1fbf3 100644 --- a/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb +++ b/lecture_2/homework/db/migrate/20190409132607_create_strongholds.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateStrongholds < ActiveRecord::Migration[5.2] def change - create_table :strongholds do |t| - - t.timestamps - end + create_table :strongholds, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409132618_create_walls.rb b/lecture_2/homework/db/migrate/20190409132618_create_walls.rb index 9b3ad308..8df64ae6 100644 --- a/lecture_2/homework/db/migrate/20190409132618_create_walls.rb +++ b/lecture_2/homework/db/migrate/20190409132618_create_walls.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateWalls < ActiveRecord::Migration[5.2] def change - create_table :walls do |t| - - t.timestamps - end + create_table :walls, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409132624_create_towers.rb b/lecture_2/homework/db/migrate/20190409132624_create_towers.rb index 82d7e26f..642968a3 100644 --- a/lecture_2/homework/db/migrate/20190409132624_create_towers.rb +++ b/lecture_2/homework/db/migrate/20190409132624_create_towers.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateTowers < ActiveRecord::Migration[5.2] def change - create_table :towers do |t| - - t.timestamps - end + create_table :towers, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409132636_create_gates.rb b/lecture_2/homework/db/migrate/20190409132636_create_gates.rb index eb3a55fa..89635b7f 100644 --- a/lecture_2/homework/db/migrate/20190409132636_create_gates.rb +++ b/lecture_2/homework/db/migrate/20190409132636_create_gates.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateGates < ActiveRecord::Migration[5.2] def change - create_table :gates do |t| - - t.timestamps - end + create_table :gates, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb index 9dcd9a79..42a07052 100644 --- a/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb +++ b/lecture_2/homework/db/migrate/20190409133633_create_weapons.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + class CreateWeapons < ActiveRecord::Migration[5.2] def change create_table :weapons do |t| t.string :name, null: false - t.integer :damage, default: 10 + t.integer :damage, default: 10 t.integer :range, default: 1 - t.string :type, default: 'MeleeWeapon' + t.string :type, default: 'MeleeWeapon' t.timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb b/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb index c93e82a4..86c16900 100644 --- a/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb +++ b/lecture_2/homework/db/migrate/20190409133651_create_magical_weapons.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateMagicalWeapons < ActiveRecord::Migration[5.2] def change - create_table :magical_weapons do |t| - - t.timestamps - end + create_table :magical_weapons, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb b/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb index e5eed85c..7236ce4b 100644 --- a/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb +++ b/lecture_2/homework/db/migrate/20190409133700_create_melee_weapons.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateMeleeWeapons < ActiveRecord::Migration[5.2] def change - create_table :melee_weapons do |t| - - t.timestamps - end + create_table :melee_weapons, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb b/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb index 2fd5c0c3..b2e2dd15 100644 --- a/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb +++ b/lecture_2/homework/db/migrate/20190409133707_create_ranged_weapons.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateRangedWeapons < ActiveRecord::Migration[5.2] def change - create_table :ranged_weapons do |t| - - t.timestamps - end + create_table :ranged_weapons, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb b/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb index 99de03d4..7bab1664 100644 --- a/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb +++ b/lecture_2/homework/db/migrate/20190409204516_index_name_on_clans.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class IndexNameOnClans < ActiveRecord::Migration[5.2] def change add_index :clans, :name, unique: true diff --git a/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb b/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb index 7bb13cf2..38469797 100644 --- a/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb +++ b/lecture_2/homework/db/migrate/20190409210018_add_defensible_to_characters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddDefensibleToCharacters < ActiveRecord::Migration[5.2] def change add_reference :characters, :defensible, polymorphic: true, index: true diff --git a/lecture_2/homework/db/migrate/20190414164925_create_samurais.rb b/lecture_2/homework/db/migrate/20190414164925_create_samurais.rb index 522f79d8..c63c7f4d 100644 --- a/lecture_2/homework/db/migrate/20190414164925_create_samurais.rb +++ b/lecture_2/homework/db/migrate/20190414164925_create_samurais.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateSamurais < ActiveRecord::Migration[5.2] def change - create_table :samurais do |t| - - t.timestamps - end + create_table :samurais, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414194133_create_hussars.rb b/lecture_2/homework/db/migrate/20190414194133_create_hussars.rb index 6d5376f3..6e13d647 100644 --- a/lecture_2/homework/db/migrate/20190414194133_create_hussars.rb +++ b/lecture_2/homework/db/migrate/20190414194133_create_hussars.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateHussars < ActiveRecord::Migration[5.2] def change - create_table :hussars do |t| - - t.timestamps - end + create_table :hussars, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb b/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb index 2366a401..46b4248d 100644 --- a/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb +++ b/lecture_2/homework/db/migrate/20190414203621_create_samurai_mages.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateSamuraiMages < ActiveRecord::Migration[5.2] def change - create_table :samurai_mages do |t| - - t.timestamps - end + create_table :samurai_mages, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb b/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb index 0f0515b8..269260c3 100644 --- a/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb +++ b/lecture_2/homework/db/migrate/20190414203629_create_samurai_rangers.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateSamuraiRangers < ActiveRecord::Migration[5.2] def change - create_table :samurai_rangers do |t| - - t.timestamps - end + create_table :samurai_rangers, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb b/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb index df720448..1a8646d9 100644 --- a/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb +++ b/lecture_2/homework/db/migrate/20190414203645_create_samurai_warriors.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateSamuraiWarriors < ActiveRecord::Migration[5.2] def change - create_table :samurai_warriors do |t| - - t.timestamps - end + create_table :samurai_warriors, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb b/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb index 081515fa..d47a28a6 100644 --- a/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb +++ b/lecture_2/homework/db/migrate/20190414210802_create_hussar_mages.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateHussarMages < ActiveRecord::Migration[5.2] def change - create_table :hussar_mages do |t| - - t.timestamps - end + create_table :hussar_mages, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb b/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb index e7793c90..015dc69b 100644 --- a/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb +++ b/lecture_2/homework/db/migrate/20190414210810_create_hussar_rangers.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateHussarRangers < ActiveRecord::Migration[5.2] def change - create_table :hussar_rangers do |t| - - t.timestamps - end + create_table :hussar_rangers, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb b/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb index 8321d89b..b9810cdb 100644 --- a/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb +++ b/lecture_2/homework/db/migrate/20190414210818_create_hussar_warriors.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateHussarWarriors < ActiveRecord::Migration[5.2] def change - create_table :hussar_warriors do |t| - - t.timestamps - end + create_table :hussar_warriors, &:timestamps end end diff --git a/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb b/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb index f471ca0c..5ae22ab3 100644 --- a/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb +++ b/lecture_2/homework/db/migrate/20190414221757_add_ma_wpnable_to_magical_weapons.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddMaWpnableToMagicalWeapons < ActiveRecord::Migration[5.2] def change add_reference :magical_weapons, :ma_wpnable, polymorphic: true, index: true diff --git a/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb b/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb index 1504c1d7..b43cdebe 100644 --- a/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb +++ b/lecture_2/homework/db/migrate/20190414221851_add_ra_wpnable_to_ranged_weapons.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddRaWpnableToRangedWeapons < ActiveRecord::Migration[5.2] def change add_reference :ranged_weapons, :ra_wpnable, polymorphic: true, index: true diff --git a/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb b/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb index b0719723..a79e60f6 100644 --- a/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb +++ b/lecture_2/homework/db/migrate/20190414221902_add_me_wpnable_to_melee_weapons.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddMeWpnableToMeleeWeapons < ActiveRecord::Migration[5.2] def change add_reference :melee_weapons, :me_wpnable, polymorphic: true, index: true diff --git a/lecture_2/homework/db/schema.rb b/lecture_2/homework/db/schema.rb index a42d1bbf..95165a59 100644 --- a/lecture_2/homework/db/schema.rb +++ b/lecture_2/homework/db/schema.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -5,141 +7,146 @@ # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# from scratch. The latter is a flawed and unsustainable approach (the more +# migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # -# It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 2019_04_14_221902) do +# It's strongly recommended that you check this file into your version control +# system. +ActiveRecord::Schema.define(version: 20_190_414_221_902) do # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" + enable_extension 'plpgsql' - create_table "barricades", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'barricades', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "buildings", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'buildings', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "characters", force: :cascade do |t| - t.string "name", null: false - t.integer "armor_quality", default: 0 - t.integer "number_of_battles", default: 0 - t.date "join_date", null: false - t.date "death_date" - t.string "type", default: "Samurai" - t.bigint "clan_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "defensible_type" - t.bigint "defensible_id" - t.index ["clan_id"], name: "index_characters_on_clan_id" - t.index ["defensible_type", "defensible_id"], name: "index_characters_on_defensible_type_and_defensible_id" + create_table 'characters', force: :cascade do |t| + t.string 'name', null: false + t.integer 'armor_quality', default: 0 + t.integer 'number_of_battles', default: 0 + t.date 'join_date', null: false + t.date 'death_date' + t.string 'type', default: 'Samurai' + t.bigint 'clan_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'defensible_type' + t.bigint 'defensible_id' + t.index ['clan_id'], name: 'index_characters_on_clan_id' + t.index %w[defensible_type defensible_id], + name: 'index_characters_on_defensible_type_and_defensible_id' 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 + 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 "gates", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'gates', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "hussar_mages", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'hussar_mages', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "hussar_rangers", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'hussar_rangers', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "hussar_warriors", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'hussar_warriors', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "hussars", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'hussars', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "magical_weapons", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "ma_wpnable_type" - t.bigint "ma_wpnable_id" - t.index ["ma_wpnable_type", "ma_wpnable_id"], name: "index_magical_weapons_on_ma_wpnable_type_and_ma_wpnable_id" + create_table 'magical_weapons', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'ma_wpnable_type' + t.bigint 'ma_wpnable_id' + t.index %w[ma_wpnable_type ma_wpnable_id], + name: 'index_magical_weapons_on_ma_wpnable_type_and_ma_wpnable_id' end - create_table "melee_weapons", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "me_wpnable_type" - t.bigint "me_wpnable_id" - t.index ["me_wpnable_type", "me_wpnable_id"], name: "index_melee_weapons_on_me_wpnable_type_and_me_wpnable_id" + create_table 'melee_weapons', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'me_wpnable_type' + t.bigint 'me_wpnable_id' + t.index %w[me_wpnable_type me_wpnable_id], + name: 'index_melee_weapons_on_me_wpnable_type_and_me_wpnable_id' end - create_table "ranged_weapons", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "ra_wpnable_type" - t.bigint "ra_wpnable_id" - t.index ["ra_wpnable_type", "ra_wpnable_id"], name: "index_ranged_weapons_on_ra_wpnable_type_and_ra_wpnable_id" + create_table 'ranged_weapons', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'ra_wpnable_type' + t.bigint 'ra_wpnable_id' + t.index %w[ra_wpnable_type ra_wpnable_id], + name: 'index_ranged_weapons_on_ra_wpnable_type_and_ra_wpnable_id' end - create_table "samurai_mages", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'samurai_mages', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "samurai_rangers", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'samurai_rangers', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "samurai_warriors", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'samurai_warriors', 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 + create_table 'samurais', 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 + create_table 'strongholds', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "towers", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'towers', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "walls", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'walls', force: :cascade do |t| + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "weapons", force: :cascade do |t| - t.string "name", null: false - t.integer "damage", default: 10 - t.integer "range", default: 1 - t.string "type", default: "MeleeWeapon" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'weapons', force: :cascade do |t| + t.string 'name', null: false + t.integer 'damage', default: 10 + t.integer 'range', default: 1 + t.string 'type', default: 'MeleeWeapon' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - add_foreign_key "characters", "clans" + add_foreign_key 'characters', 'clans' end diff --git a/lecture_2/homework/db/seeds.rb b/lecture_2/homework/db/seeds.rb index 4d8cb60c..f20541d0 100644 --- a/lecture_2/homework/db/seeds.rb +++ b/lecture_2/homework/db/seeds.rb @@ -1,83 +1,88 @@ -# 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) +# frozen_string_literal: true x = 0 2.times do clan = Clan.create!(name: Faker::Creature::Cat.unique.registry) Faker::Number.between(2, 10).times do - sam = SamuraiWarrior.create!( - name: Faker::Creature::Cat.name + x.to_s, - number_of_battles: Faker::Number.between(0, 200), - armor_quality: Faker::Number.between(0, 1000), - join_date: Faker::Date.birthday(20, 70), - death_date: nil, - clan: clan - ) - # MeleeWeapon.create!( - # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - # damage: Faker::Number.between(0, 100), - # range: Faker::Number.between(0, 3), - # me_wpnable: sam - # ) - x=x+1 + # sam = + SamuraiWarrior.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: nil, + clan: clan + ) + # MeleeWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, + # Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero] + # .sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 3), + # me_wpnable: sam + # ) + x += 1 end Faker::Number.between(2, 10).times do - hus = HussarMage.create!( - name: Faker::Creature::Cat.name + x.to_s, - number_of_battles: Faker::Number.between(0, 200), - armor_quality: Faker::Number.between(0, 1000), - join_date: Faker::Date.birthday(20, 70), - death_date: nil, - clan: clan - ) - # MagicalWeapon.create!( - # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - # damage: Faker::Number.between(0, 100), - # range: Faker::Number.between(0, 100), - # ma_wpnable: hus - # ) - x=x+1 + # hus = + HussarMage.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: nil, + clan: clan + ) + # MagicalWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, + # Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero] + # .sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 100), + # ma_wpnable: hus + # ) + x += 1 end Faker::UniqueGenerator.clear Faker::Number.between(1, 5).times do - sam = SamuraiWarrior.create!( - name: Faker::Creature::Cat.name + x.to_s, - number_of_battles: Faker::Number.between(0, 200), - armor_quality: Faker::Number.between(0, 1000), - join_date: Faker::Date.birthday(20, 70), - death_date: Faker::Date.backward(20), - clan: clan - ) - # MeleeWeapon.create!( - # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - # damage: Faker::Number.between(0, 100), - # range: Faker::Number.between(0, 3), - # me_wpnable: sam - # ) - x=x+1 + # sam = + SamuraiWarrior.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: Faker::Date.backward(20), + clan: clan + ) + # MeleeWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, + # Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero] + # .sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 3), + # me_wpnable: sam + # ) + x += 1 end Faker::Number.between(1, 5).times do - hus = HussarMage.create!( - name: Faker::Creature::Cat.name + x.to_s, - number_of_battles: Faker::Number.between(0, 200), - armor_quality: Faker::Number.between(0, 1000), - join_date: Faker::Date.birthday(20, 70), - death_date: Faker::Date.backward(20), - clan: clan - ) - # MagicalWeapon.create!( - # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero].sample, - # damage: Faker::Number.between(0, 100), - # range: Faker::Number.between(0, 100), - # ma_wpnable: hus - # ) - x=x+1 + # hus = + HussarMage.create!( + name: Faker::Creature::Cat.name + x.to_s, + number_of_battles: Faker::Number.between(0, 200), + armor_quality: Faker::Number.between(0, 1000), + join_date: Faker::Date.birthday(20, 70), + death_date: Faker::Date.backward(20), + clan: clan + ) + # MagicalWeapon.create!( + # name: Faker::Appliance.equipment + " of " + [Faker::Ancient.god, + # Faker::Ancient.primordial, Faker::Ancient.titan, Faker::Ancient.hero] + # .sample, + # damage: Faker::Number.between(0, 100), + # range: Faker::Number.between(0, 100), + # ma_wpnable: hus + # ) + x += 1 end - -end \ No newline at end of file +end diff --git a/lecture_2/homework/lib/mage.rb b/lecture_2/homework/lib/mage.rb index 104eec3e..9b52490b 100644 --- a/lecture_2/homework/lib/mage.rb +++ b/lecture_2/homework/lib/mage.rb @@ -1,11 +1,13 @@ -module Mage +# frozen_string_literal: true + +module Mage extend ActiveSupport::Concern included do - validates :profession, presence: true, inclusion: { in: %w(mage)} + validates :profession, presence: true, inclusion: { in: %w[mage] } end - #validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true - #validates :profession, presence: true, inclusion: { in: %w(mage)} -# def attack -# "#{name}: Pew pew pew! Whizzzz! " -# end + # validates :weapon, inclusion: { in: "magical_weapon"}, allow_nil: true + # validates :profession, presence: true, inclusion: { in: %w(mage)} + # def attack + # "#{name}: Pew pew pew! Whizzzz! " + # end end diff --git a/lecture_2/homework/lib/ranger.rb b/lecture_2/homework/lib/ranger.rb index 712fe9ab..e4819fcb 100644 --- a/lecture_2/homework/lib/ranger.rb +++ b/lecture_2/homework/lib/ranger.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + module Ranger extend ActiveSupport::Concern included do - validates :profession, presence: true, inclusion: { in: %w(ranger)} + validates :profession, presence: true, inclusion: { in: %w[ranger] } end - #validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true - #validates :profession, presence: true, inclusion: { in: %w(ranger)} -# def attack -# "#{name}: Ftong! Swoosh! *arrow hit noise*" -# end + # validates :weapon, inclusion: { in: "ranged_weapon"}, allow_nil: true + # validates :profession, presence: true, inclusion: { in: %w(ranger)} + # def attack + # "#{name}: Ftong! Swoosh! *arrow hit noise*" + # end end diff --git a/lecture_2/homework/lib/warrior.rb b/lecture_2/homework/lib/warrior.rb index 834f42aa..eddfa1eb 100644 --- a/lecture_2/homework/lib/warrior.rb +++ b/lecture_2/homework/lib/warrior.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + module Warrior extend ActiveSupport::Concern included do - validates :profession, presence: true, inclusion: { in: %w(warrior)} + validates :profession, presence: true, inclusion: { in: %w[warrior] } end - #validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true - #validates :profession, presence: true, inclusion: { in: %w(warrior)} -# def attack -# "#{name}: *melee noises*" -# end + # validates :weapon, inclusion: { in: "melee_weapon"}, allow_nil: true + # validates :profession, presence: true, inclusion: { in: %w(warrior)} + # def attack + # "#{name}: *melee noises*" + # end end diff --git a/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb b/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb index 6b77693c..99f82045 100644 --- a/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb +++ b/lecture_2/homework/test/controllers/samurai_warrior_controller_test.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true + require 'test_helper' class SamuraiWarriorControllerTest < ActionDispatch::IntegrationTest - test "should get index" do + test 'should get index' do get samurai_warrior_index_url assert_response :success end - end diff --git a/lecture_2/homework/test/models/hussar_mage_test.rb b/lecture_2/homework/test/models/hussar_mage_test.rb index 9ed671f1..226f3938 100644 --- a/lecture_2/homework/test/models/hussar_mage_test.rb +++ b/lecture_2/homework/test/models/hussar_mage_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class HussarMageTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/hussar_ranger_test.rb b/lecture_2/homework/test/models/hussar_ranger_test.rb index 506fc41c..72f37d4a 100644 --- a/lecture_2/homework/test/models/hussar_ranger_test.rb +++ b/lecture_2/homework/test/models/hussar_ranger_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class HussarRangerTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/hussar_test.rb b/lecture_2/homework/test/models/hussar_test.rb index e4ad976e..7992b736 100644 --- a/lecture_2/homework/test/models/hussar_test.rb +++ b/lecture_2/homework/test/models/hussar_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class HussarTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/hussar_warrior_test.rb b/lecture_2/homework/test/models/hussar_warrior_test.rb index 1f53105c..f565ccc7 100644 --- a/lecture_2/homework/test/models/hussar_warrior_test.rb +++ b/lecture_2/homework/test/models/hussar_warrior_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class HussarWarriorTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/samurai_mage_test.rb b/lecture_2/homework/test/models/samurai_mage_test.rb index ce53b4cf..3bcbb2f2 100644 --- a/lecture_2/homework/test/models/samurai_mage_test.rb +++ b/lecture_2/homework/test/models/samurai_mage_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class SamuraiMageTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/samurai_ranger_test.rb b/lecture_2/homework/test/models/samurai_ranger_test.rb index 6ff616c8..3f00241a 100644 --- a/lecture_2/homework/test/models/samurai_ranger_test.rb +++ b/lecture_2/homework/test/models/samurai_ranger_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class SamuraiRangerTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/samurai_test.rb b/lecture_2/homework/test/models/samurai_test.rb index a82ae29d..ffbe9832 100644 --- a/lecture_2/homework/test/models/samurai_test.rb +++ b/lecture_2/homework/test/models/samurai_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class SamuraiTest < ActiveSupport::TestCase diff --git a/lecture_2/homework/test/models/samurai_warrior_test.rb b/lecture_2/homework/test/models/samurai_warrior_test.rb index dacdd2d4..11b5fa2e 100644 --- a/lecture_2/homework/test/models/samurai_warrior_test.rb +++ b/lecture_2/homework/test/models/samurai_warrior_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class SamuraiWarriorTest < ActiveSupport::TestCase From c2f4d55ed748b37110e513a050974e6f286f9f08 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Wed, 17 Apr 2019 13:40:22 +0200 Subject: [PATCH 7/8] Modify routes --- lecture_2/homework/config/routes.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lecture_2/homework/config/routes.rb b/lecture_2/homework/config/routes.rb index d1d97bd9..b94a8520 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -3,8 +3,6 @@ Rails.application.routes.draw do get 'clans/index' get 'characters/index' - get '/clans/:clan_id/alive', to: 'characters#alive' - get '/clans/:clan_id/dead', to: 'characters#dead' resources :clans, only: %i[index create show update destroy] do resources :characters, module: :clans end From a6ab2e3bfc751b8dd5261a95ba2a53eab171bd12 Mon Sep 17 00:00:00 2001 From: Maciej Janusz Krajsman Date: Wed, 17 Apr 2019 13:43:18 +0200 Subject: [PATCH 8/8] Modify routes --- lecture_2/homework/config/routes.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lecture_2/homework/config/routes.rb b/lecture_2/homework/config/routes.rb index b94a8520..90b88b2e 100644 --- a/lecture_2/homework/config/routes.rb +++ b/lecture_2/homework/config/routes.rb @@ -4,6 +4,9 @@ get 'clans/index' get 'characters/index' resources :clans, only: %i[index create show update destroy] do - resources :characters, module: :clans + resources :characters, module: :clans do + resources :alive, only: %i[index] + resources :dead, only: %i[index] + end end end