<file_sep>Rails.application.routes.draw do
  
  get 'product/new'

  get 'company/new'

  root   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/companies', to: 'company#index'
  get    '/create_company', to: 'company#new'
  post   '/companies', to: 'company#create'
  get    '/api/companies', to: 'company#indexApi'
  get    '/products', to: 'product#index'
  get    '/new_product', to: 'product#new'
  post   '/products', to: 'product#create'
  get    '/api/products', to: 'product#showApi'
  get    '/signup',  to: 'users#new'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users
  resources :company
  resources :product
 
end
<file_sep>require 'test_helper'

class CompanyRegisterTest < ActionDispatch::IntegrationTest
  test "invalid register information" do
    get companies_path
    #If the POST request is bad, we check that the user hasn't been added (ie we still have the same number of users)
    assert_no_difference 'Company.count' do
      post companies_path, params: { company: { name:  "",
                                         nb_employees: "",
                                         city: "",
                                         turnover:  "",
                                         phone: "" } }
    end
  end
  
   test "valid signup information" do
    get companies_path
    #If the POST request is ok, we expect a count difference of 1 in the number or users
    assert_difference 'Company.count', 1 do
      post companies_path, params: { company: { name:  "Company",
                                         nb_employees: "10",
                                         city: "London",
                                         turnover:  "10000000",
                                         phone: "7817817397" } }
    end
  end
end
<file_sep>class Company < ApplicationRecord
  validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
  validates :nb_employees, presence: true
  validates :city, presence: true
  validates :phone, presence: true

  
end
<file_sep>class ProductController < ApplicationController
  before_action :logged_in_user, only: [:new, :index]
  
  def new
    @product= Product.new
    @company= Company.find_by(id: current_user.company_id)
  end
  
  def show
    @product = Product.find(params[:id])
  end
  
  def index
    @products = Product.paginate(page: params[:page])
  end
  
  def create
    @product = Product.new(product_params)
    if @product.save
      flash[:success] = "Successfully created a product"
      redirect_to products_path
    else
      render 'new'
    end
  end
  
  
  def showApi
    @products = Product.where(company_id: params[:company_id])
    render json: @products
  end
  
  def destroy
    Product.find(params[:id]).destroy
    flash[:success] = "Product deleted"
    redirect_to products_path
  end
  
  private

    def product_params
      params.require(:product).permit(:name, :description, :price, :company_id)
    end
  
end
<file_sep>require 'test_helper'

class ProductControllerTest < ActionDispatch::IntegrationTest

  
  test "should redirect when not logged in" do
    get products_path
    assert_redirected_to login_url
  end

end
<file_sep>require 'test_helper'

class CompanyControllerTest < ActionDispatch::IntegrationTest
  test "should get redirected when not logged in" do
    get companies_path
    assert_redirected_to login_url
  end

end
<file_sep>class CompanyController < ApplicationController
  before_action :logged_in_user, only: [:index]
  def new
    @company= Company.new
  end
  
  def show
    @company = Company.find(params[:id])
    @users = User.where(company_id: params[:id])
  end
  
  def index
    @companies = Company.paginate(page: params[:page])
  end
  
  def indexApi
    @companies = Company.paginate(page: params[:page])
    render json: @companies
  end
  
  def create
    @company = Company.new(company_params)
    if @company.save
      flash[:success] = "Successfully created a company"
      redirect_to signup_path
    else
      render 'new'
    end
  end
  
  # When destroying a company, all users and products are destroyed
  def destroy
    User.where(company_id: params[:id]).destroy_all
    Product.where(company_id: params[:id]).destroy_all
    Company.find(params[:id]).destroy
    flash[:success] = "Company deleted"
    redirect_to companies_path
  end
  
  
  private

    def company_params
      params.require(:company).permit(:name,:nb_employees, :city, :turnover,:phone)
    end
  
end
<file_sep># 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)

User.create!(name:  "Admin",
             email: "<EMAIL>",
             password:              "<PASSWORD>",
             password_confirmation: "<PASSWORD>",
             company_id: 1,
             admin: true)


Company.create!(name:  "Pro-Duct",
             nb_employees:1,
             city: "Compiègne",
             phone: "0781781739",
             turnover: 100000000)
            
30.times do |n|
  name  = Faker::Company.name
  nb_employees = Faker::Company.duns_number
  city = Faker::Address.city
  phone = Faker::PhoneNumber.phone_number
  turnover = rand(1000...300000000)
  
  Company.create!(name:  name,
             nb_employees: nb_employees,
             city: city,
             phone: phone,
             turnover: turnover)
end

60.times do |n|
  name  = Faker::Name.name
  email = Faker::Internet.safe_email
  password = "<PASSWORD>"
  company_id = rand(1...30)
  User.create!(name:  name,
               email: email,
               password:              <PASSWORD>,
               password_confirmation: <PASSWORD>,
               company_id: company_id)
end

61.times do |n|
  name  = Faker::Commerce.product_name
  description = Faker::Hacker.say_something_smart
  price = Faker::Commerce.price
  company_id = rand(1...30)
  Product.create!(name:  name,
               description: description,
               price: price,
               company_id: company_id)
end<file_sep>require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest
  
  test "invalid signup information" do
    get signup_path
    #If the POST request is bad, we check that the user hasn't been added (ie we still have the same number of users)
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         company_id: 0,
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
  end
  
   test "valid signup information" do
    get signup_path
    #If the POST request is ok, we expect a count difference of 1 in the number or users
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { name:  "<NAME>",
                                         email: "<EMAIL>",
                                         company_id: 3,
                                         password:              "<PASSWORD>",
                                         password_confirmation: "<PASSWORD>" } }
    end
  end
end
