# 第1回 Ruby勉強会
## みんぎょ
```ruby
# frozen_string_literal: true
class Api::ContactController < ApiController
def create
puts params #> { address: "uchigane.ryotaro@gmail.com", address_confirm: "uchigane.ryotaro@gmail.com" , title: "xxxができません", body: "ふじこ" }
contact = ContactForm.new(contact_params)
contact.create!
end
private
def contact_params
params.require(:contact).permit(:address ,:title, :body)
end
end
```
```ruby
# frozen_string_literal: true
# タイトルは必須
class Form::ContactForm
include ActiveModel
attr_accessor :address, :title, :body
validates :address, confirm: :address_confirm
validates :title, presense: true
def create!
valid! #> throw InvalidParameter
ContactMailer.send_later!(self)
end
end
```
## EntryController の改修
```ruby
class Api::EntriesController < ApiController
def index
search_form = EntrySearchForm.new(entry_params)
search_form.valid!
@entries = search_form.search
end
private
def entry_params
params.require(:entry).permit(:status_in, :updated_at_gt, :updated_at_lt, :created_at_gt, :created_at_lt, :room_id, :mansion_id, :system_code, :athome_id, :homes_id)
end
end
```
```ruby
class Form::EntrySearchForm
include ActiveModel
attr_accessor :operation_corporation, :status_in, :updated_at_gt, :updated_at_lt, :created_at_gt, :created_at_lt, :room_id, :mansion_id, :system_code, :athome_id, :homes_id
# :updated_at_gt, :updated_at_lt, :created_at_gt, :created_at_ltの初期値を定義
validates :operation_corporation, presense: true
# バリデーション
# status_inは"02,03"のようにカンマ区切り
# updated_at_ltはupdated_at_gtより未来(createdも同様)
# updated_at,created_atは、yyyy-MM-dd HH:mm:ss 形式
# system_code, athome_id, homes_idのいずれか
# 各パラメータのデータ型、桁数
def search
@entries = operation_corporation.entries
@entries = @entries.where(status: status_in&.split(",")) if status_in
@entries = @entries.where(updated_at: (updated_at_gt)..(updated_at_lt)) if updated_at_gt || updated_at_lt
@entries = @entries.where(created_at: (created_at_gt)..(created_at_lt)) if created_at_gt || created_at_lt
@entries = @entries.joins(room: :building).order(:id)
@entries = @entries.where(rooms: { id: room_id }) if room_id
@entries = @entries.where(buildings: { id: mansion_id }) if mansion_id
if service_id
building_system_code, room_system_code = service_id&.split("-")
@entries = @entries.where(buildings: { system_code: building_system_code }, rooms: { system_code: room_system_code })
end
@entries = @entries.where(rooms: { athome_id: athome_id }) if athome_id
@entries = @entries.where(rooms: { homes_id: homes_id }) if homes_id
@entries
end
end
```
## みんぎょ
```ruby
require "rails_helper"
RSpec.describe EntrySearchFormSpec, type: :model
let(entry_search_form) { build: :entry_search_form }
describe "valid!" do
subject { entry_search_form }
it "有効なパラメータの場合はtrueが返却される" do
is_expected.to be_valid
end
context "status_inが存在しない場合" do
let(entry_search_form) { build: :entry_search_form(status_in: nil) }
it "invalid" do
is_expected.to be_invalid
end
end
end
describe "#search" do
it "〜が送られてきた時" do
end
context "negative test" do
end
end
```
```
次回までの課題
FormObjectの中身でoperation_corporationという値を渡さないで綺麗にする
ヒント
ActiveRecordMultiTenantを使えるかも
罰則(センス無いと判断される可能性あるから気をつけて!!!!)
controllerはこれ以上いじっちゃだめ
```