Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lecture_1/homework/Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.4.0'
ruby '2.5.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'sqlite3', '~> 1.3.6'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
Expand All @@ -30,7 +32,7 @@ gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

group :development do
Expand All @@ -40,6 +42,11 @@ group :development do
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]

# faker
gem 'faker'

# rubocop
# gem 'rubocop', require: false
9 changes: 6 additions & 3 deletions lecture_1/homework/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ 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)
Expand Down Expand Up @@ -120,7 +122,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.4.0)
sqlite3 (1.3.13)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
Expand All @@ -135,16 +137,17 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
faker
listen (>= 3.0.5, < 3.2)
puma (~> 3.11)
rails (~> 5.2.2)
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
sqlite3 (~> 1.3.6)
tzinfo-data

RUBY VERSION
ruby 2.4.0p0
ruby 2.5.1p57

BUNDLED WITH
2.0.1
2 changes: 2 additions & 0 deletions lecture_1/homework/Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Connection < ActionCable::Connection::Base
end
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
end
44 changes: 44 additions & 0 deletions lecture_1/homework/app/controllers/clans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

class ClansController < ApplicationController
def index
clans = Clan.all
render json: clans.to_json, status: 200
end

def show
render json: clan.to_json, status: 200
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
end

def update
clan.update!(orc_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 ||= Clan.find(clan_params[:id])
end

def clan_params
params.permit(:name)
end
end

57 changes: 57 additions & 0 deletions lecture_1/homework/app/controllers/samurais_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

class SamuraisController < ApplicationController
def index
samurais = clan.samurais.all
if params.key?(:alive)
if params[:alive].to_i.zero?
render json: samurais.dead, include: %w[clan],
status: 200
else
render json: samurais.alive, include: %w[clan],
status: 200
end
else
render json: samurais, include: %w[clan], status: 200
end
end

def show
render json: samurai.to_json, status: 200
end

def destroy
samurai.destroy!
head 204
rescue ActiveRecord::ActiveRecordError => e
render json: e.to_json, status: 404
end

def create
samurai = clan.samurais.create!(samurai_params)
render json: samurai.to_json, status: 201
rescue ActiveRecord::ActiveRecordError => e
render json: e.to_json, status: 422
end

def update
samurai.update!(samurai_params)
render json: samurai.to_json, status: 201
rescue ActiveRecord::ActiveRecordError => e
render json: e.to_json, status: 422
end

private

def samurai
@samurai ||= clan.samurais.find(params[:id])
end

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

def samurai_params
params.permit(:name, :armour_rating, :battle_count, :join_date, :death_date)
end
end
2 changes: 2 additions & 0 deletions lecture_1/homework/app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base
end
2 changes: 2 additions & 0 deletions lecture_1/homework/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
7 changes: 7 additions & 0 deletions lecture_1/homework/app/models/clan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Clan < ApplicationRecord
has_many :samurais, dependent: :destroy

validates name, presence: true
end
12 changes: 12 additions & 0 deletions lecture_1/homework/app/models/samurai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class Samurai < ApplicationRecord
belongs_to :clan
validates name, presence: true
validates armour_rating, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 }
validates battle_count, presence: true
validates join_date, presence: true
# validates death_date
scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }
end
2 changes: 2 additions & 0 deletions lecture_1/homework/bin/bundle
Original file line number Diff line number Diff line change
@@ -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')
4 changes: 3 additions & 1 deletion lecture_1/homework/bin/rails
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion lecture_1/homework/bin/rake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/bin/setup
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'fileutils'
include FileUtils

Expand Down
3 changes: 2 additions & 1 deletion lecture_1/homework/bin/spring
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/bin/update
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'fileutils'
include FileUtils

Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is used by Rack-based servers to start the application.

require_relative 'config/environment'
Expand Down
22 changes: 12 additions & 10 deletions lecture_1/homework/config/application.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/config/boot.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Load the Rails application.
require_relative 'application'

Expand Down
3 changes: 2 additions & 1 deletion lecture_1/homework/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down Expand Up @@ -44,7 +46,6 @@
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true


# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

Expand Down
6 changes: 4 additions & 2 deletions lecture_1/homework/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down Expand Up @@ -45,7 +47,7 @@
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
config.log_tags = [:request_id]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down Expand Up @@ -74,7 +76,7 @@
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
if ENV['RAILS_LOG_TO_STDOUT'].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
Expand Down
2 changes: 2 additions & 0 deletions lecture_1/homework/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# ActiveSupport::Reloader.to_prepare do
Expand Down
Loading