Skip to content

Commit

Permalink
Merge pull request #82 from fujjima/issue-78/auto_rspec
Browse files Browse the repository at this point in the history
rspecの自動化
  • Loading branch information
fujjima committed Jul 31, 2022
2 parents 17a54db + f9aee24 commit 6c84e58
Show file tree
Hide file tree
Showing 36 changed files with 217 additions and 196 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/github-action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Run rspec, rubocop

on: :push
on: [push]

jobs:
rspec:
run-rspec:
runs-on: ubuntu-latest
timeout-minutes: 10
services:
Expand Down Expand Up @@ -88,6 +88,7 @@ jobs:
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: 2.7.5

- name: Run rubocop
run: bundle exec rubocop
18 changes: 16 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
Style/FrozenStringLiteralComment:
Enabled: false
Style/MixinUsage:
Enabled: false
Style/Documentation:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Metrics/LineLength:
Max: 120
Metrics/MethodLength:
Max: 13
Exclude:
- 'db/**/*' # migrate系のファイルを除外する
Metrics/BlockLength:
Exclude:
- 'db/*'
- 'spec/**/*.rb'
AsciiComments:
Enabled: false
Style/Documentation:
Enabled: false
ClassAndModuleChildren:
EnforcedStyle: compact
Metrics/AbcSize:
Max: 20
# Rails/TopLevelInclude:
# Include:
# - app/**/*.rb
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'rubocop', '~> 0.64.0', require: false
gem 'rubocop', require: false
# gem 'spring', '>= 3.0.0'
end

Expand Down
24 changes: 13 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ GEM
concurrent-ruby (~> 1.0)
io-like (0.3.1)
io-wait (0.2.1)
jaro_winkler (1.5.4)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
Expand Down Expand Up @@ -195,12 +194,11 @@ GEM
nokogiri (1.13.7)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
parallel (1.21.0)
parser (3.1.1.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
pg (1.3.3)
popper_js (2.9.3)
powerpack (0.1.3)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -290,14 +288,18 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.11.0)
rubocop (0.64.0)
jaro_winkler (~> 1.5.1)
rubocop (1.32.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.19.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.19.1)
parser (>= 3.1.1.0)
ruby-graphviz (1.2.5)
rexml
ruby-progressbar (1.11.0)
Expand Down Expand Up @@ -353,7 +355,7 @@ GEM
concurrent-ruby (~> 1.0)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.4.1)
unicode-display_width (2.2.0)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -402,7 +404,7 @@ DEPENDENCIES
rails_best_practices
ransack
rspec-rails (~> 4.0.1)
rubocop (~> 0.64.0)
rubocop
sass-rails (~> 5.0)
selenium-webdriver
slim-rails
Expand Down
4 changes: 1 addition & 3 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
class ApplicationCable::Channel < ActionCable::Channel::Base
end
4 changes: 1 addition & 3 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
class ApplicationCable::Connection < ActionCable::Connection::Base
end
16 changes: 8 additions & 8 deletions app/controllers/api/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ def index
.group_by(&:recorded_at)

datas = period_range.map do |date|
hash = {}
# XXX: ここでキャメルケース使いたくない
hash['recordedAt'] = date
working_times = working_times_group_by_recorded_at[date]
next hash unless working_times

hash.merge(sum_hash(working_times))
end.compact
hash = {}
# XXX: ここでキャメルケース使いたくない
hash['recordedAt'] = date
working_times = working_times_group_by_recorded_at[date]
next hash unless working_times

hash.merge(sum_hash(working_times))
end.compact

@report_datas = datas.map do |d|
Hash[d.sort]
Expand Down
41 changes: 22 additions & 19 deletions app/controllers/api/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@ def create
end
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# FIXME: 複数のリソースに対するupdateを行なってしまっていて分かりにくいので、分解するかモデルに処理させる
def update
# update! + working_timeに関する更新はtransactionで囲む
@task.update!(task_params)
times = params[:task][:times]
if times
update_target = WorkingTime.find_by(task_id: @task.id, recorded_at: Date.parse(params[:task][:times][:start_at]))
if update_target
update_target.times << times
update_target.save!
else
working_time = WorkingTime.new(
task_id: @task.id,
times: [times],
# TODO: end_atが日を跨いだ場合の対応
recorded_at: Date.parse(params[:task][:times][:start_at])
)
working_time.save!
end

render :show and return unless times

update_target = WorkingTime.find_by(task_id: @task.id, recorded_at: Date.parse(params[:task][:times][:start_at]))
if update_target
update_target.times << times
update_target.save!
else
WorkingTime.new(
task_id: @task.id,
times: [times],
# TODO: end_atが日を跨いだ場合の対応
recorded_at: Date.parse(params[:task][:times][:start_at])
).save!
end
render :show
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def destroy
Task.where(id: params[:id]).destroy_all
Expand All @@ -75,11 +78,11 @@ def set_datas
tasks = current_user.tasks.eager_load(:order, :tags)
@datas = lists.reduce([]) do |array, list|
array.push({
list_id: list.id,
list_name: list.name,
tasks: tasks.where(order: { list_id: list.id })
list_id: list.id,
list_name: list.name,
tasks: tasks.where(order: { list_id: list.id })
.sort_by(&:position)
})
})
end
end

Expand All @@ -105,6 +108,6 @@ def set_useable_tags

def order_params
# TODO: リスト内の順番を受け取る方向性について(positionパラメータを必要とするか、task_idの配列内の順番で考えるか)
params.require(:order_params).map{ |param| param.permit(:position, :task_id, :list_id).to_h }
params.require(:order_params).map { |param| param.permit(:position, :task_id, :list_id).to_h }
end
end
1 change: 1 addition & 0 deletions app/models/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class List < ApplicationRecord

def tasks
return [] if orders.empty?

orders.map(&:task)
end
end
2 changes: 1 addition & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Order < ApplicationRecord
belongs_to :list

class << self
def update_order order_list
def update_order(order_list)
# TODO: 例外にしたいケース
# 配列内に異なるリスト or 異なるboardに属するタスクが検出される
# position被り
Expand Down
4 changes: 2 additions & 2 deletions app/models/task_tag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TaskTag < ApplicationRecord
belongs_to :task
belongs_to :tag
validates :task_id, presence:true
validates :tag_id, presence:true
validates :task_id, presence: true
validates :tag_id, presence: true
end
6 changes: 3 additions & 3 deletions app/models/working_time.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -------------------------------------------------
# WorkintTime
#
#
# 各タスクに紐づく作業時間を管理する
#
#
# recorded_at: 記録がされた日
# times: ある日における作業時間の開始時刻及び終了時刻の配列
# -------------------------------------------------
Expand All @@ -25,7 +25,7 @@ def total_working_time_per_task
end

# 特定タスクについての合計作業時間を返す
def total_working_time task_id
def total_working_time(task_id)
# 複数日におけるworking_timeが取得される予定
# 1working_time内でsumする、という計算を取得数分行う
search_with_task_id(task_id)
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/create_task.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ActiveRecord::Base.transaction do

# タスクに紐づくworking_timeを1個ずつ生成する
created_at = Date.new(2020, 11, 10)
start_date = Time.new(2020, 11, 10, 12, 00, 00)
start_date = Time.new(2020, 11, 10, 12, 0o0, 0o0)
end_date = Time.new(2020, 11, 10, 13, 30, 30)

created_tasks = user.tasks.last(5)
Expand Down
6 changes: 2 additions & 4 deletions app/views/api/tasks/index.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
if @tags.present?
json.usable_tags @tags, :id, :name
end
json.usable_tags @tags, :id, :name if @tags.present?

json.datas do
json.array! @datas do |data|
Expand All @@ -12,7 +10,7 @@ json.datas do
json.array! data.fetch(:tasks, []) do |task|
json.extract! task, :id, :name, :description, :finished_at, :status
json.partial! 'shared/tags', tags: task.tags
if working_time = @working_times&.dig(task.id)
if (working_time = @working_times&.dig(task.id))
json.working_time working_time
end
end
Expand Down
4 changes: 1 addition & 3 deletions app/views/api/tasks/show.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
if @tags.present?
json.usable_tags @tags, :id, :name
end
json.usable_tags @tags, :id, :name if @tags.present?

json.task do
json.extract! @task, :id, :name, :finished_at, :description, :status
Expand Down
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
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
1 change: 0 additions & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
include FileUtils
require 'fileutils'


# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)

Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

# rubocop:disable Style/ClassAndModuleChildren
module Taskleaf
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
Expand All @@ -19,3 +20,4 @@ class Application < Rails::Application
# the framework and any gems in your application.
end
end
# rubocop:enable Style/ClassAndModuleChildren
3 changes: 3 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: postgres
password: postgres
host: localhost

development:
<<: *default
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV['RAILS_LOG_TO_STDOUT'].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Rails.application.configure do
config.action_dispatch.show_exceptions = false
end
end
4 changes: 2 additions & 2 deletions config/initializers/json_param_key_transform.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# File: config/initializers/json_param_key_transform.rb
# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case:
ActionDispatch::Request.parameter_parsers[:json] = -> (raw_post) {
ActionDispatch::Request.parameter_parsers[:json] = lambda { |raw_post|
# Modified from action_dispatch/http/parameters.rb
data = ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(Hash)
data = { _json: data } unless data.is_a?(Hash)

# Transform camelCase param keys to snake_case:
data.deep_transform_keys!(&:underscore)
Expand Down
4 changes: 2 additions & 2 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# 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.
#
Expand Down

0 comments on commit 6c84e58

Please sign in to comment.