<file_sep>require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def setup
    @company = companies(:test)
    @user = @company.users.build(last_name: "睦男", first_name: "下田",email: "<EMAIL>",
                                 affiliation: "営業部", position: "部長",
                                 password: "<PASSWORD>", password_confirmation: "<PASSWORD>")
  end
  
  test "should be valid" do
    assert @user.valid?
  end

  test "company id should be presend" do
    @user.company_id = nil
    assert_not @user.valid?
  end
  
  
end
<file_sep>require 'test_helper'

class CompanyTest < ActiveSupport::TestCase
  def setup
    @company = Company.new(office_name: "Example Office", phone_number: "00-0000-0000",
                           zip_code: "111-11111", address1: "Example address", address2: "Example address2",
                           staff_name: "<NAME>", staff_mail_address: "<EMAIL>")
  end
  
  test "should be valid" do
    assert @company.valid?
  end

  test "office_name should be present" do
    @company.office_name = "   "
    assert_not @company.valid?
  end

  test "phone_number should be present" do
    @company.phone_number = "   "
    assert_not @company.valid?
  end

  test "staff_mail_address validation should accept valid addresses" do
    valid_addresses = %w[<EMAIL> <EMAIL> <EMAIL>-<EMAIL>
                         <EMAIL> <EMAIL>]
    valid_addresses.each do |valid_address|
      @company.staff_mail_address = valid_address
      assert @company.valid?, "#{valid_address.inspect} should be valid"
    end
    
  end

  test "staff_mail_address validation should reject invalid addresses" do
    invalid_addresses = %w[<EMAIL>@<EMAIL>,com user_at_foo.org user.<EMAIL>.
                           <EMAIL> <EMAIL>]
    invalid_addresses.each do |invalid_address|
      @company.staff_mail_address = invalid_address
      assert_not @company.valid?, "#{invalid_address.inspect} should be invalid"
    end
  end
  
end
<file_sep>class CreateTimeCards < ActiveRecord::Migration[5.0]
  def change
    create_table :time_cards do |t|
      t.string :working_day
      t.string :start_time
      t.string :end_time
      t.string :outing_time
      t.string :return_time
      t.string :remark
      t.references :user, foreign_key: true

      t.timestamps
    end
    add_index :time_cards, [:user_id, :created_at]
  end
end
<file_sep>class Company < ApplicationRecord
    has_many :users
    validates :office_name, presence:true
    validates :phone_number, presence:true

    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :staff_mail_address, presence: true, length: { maximum: 255 },
                format: { with: VALID_EMAIL_REGEX }
end
<file_sep>class User < ApplicationRecord
  belongs_to :company
  has_many :time_card
  validates :company_id, presence: true
  
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }

    # 渡された文字列のハッシュ値を返す
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end
end
<file_sep>class CreateCompanies < ActiveRecord::Migration[5.0]
  def change
    create_table :companies do |t|
      t.string :office_name
      t.string :phone_number
      t.string :zip_code
      t.string :address1
      t.string :address2
      t.string :staff_name
      t.string :staff_mail_address

      t.timestamps
    end
  end
end
<file_sep>class UsersController < ApplicationController
  def new
      session[:company_id] = params[:company_id]
      @company = Company.find_by(id: params[:company_id])
      @user = @company.users.build
  end

 def show
   @user = User.find(params[:id]);
  end

  def create
    @company = Company.find_by(id: session[:company_id])
    @user = @company.users.build(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcom "
      redirect_to @user
    else
      render 'new'
    end
    
  end

  private

    def user_params
      params.require(:user).permit(:last_name, :first_name, :email,
                                   :affiliation, :position,:password,
                                   :password_confirmation)
    end
end
