Skip to content

Commit

Permalink
feat(batiments) : creation de l'entité batiment et son crud associé (#75
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Lelievre-david authored Nov 4, 2024
1 parent 143e0ad commit aac3c19
Show file tree
Hide file tree
Showing 22 changed files with 622 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.3.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 7.2.0'
gem 'rails', '~> 7.2.1.1'
# Use Puma as the app server
gem 'puma', '~> 6.4'
# A PostgreSQL client library for Ruby
Expand Down
112 changes: 56 additions & 56 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (7.2.1)
actionpack (= 7.2.1)
activesupport (= 7.2.1)
actioncable (7.2.1.2)
actionpack (= 7.2.1.2)
activesupport (= 7.2.1.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.2.1)
actionpack (= 7.2.1)
activejob (= 7.2.1)
activerecord (= 7.2.1)
activestorage (= 7.2.1)
activesupport (= 7.2.1)
actionmailbox (7.2.1.2)
actionpack (= 7.2.1.2)
activejob (= 7.2.1.2)
activerecord (= 7.2.1.2)
activestorage (= 7.2.1.2)
activesupport (= 7.2.1.2)
mail (>= 2.8.0)
actionmailer (7.2.1)
actionpack (= 7.2.1)
actionview (= 7.2.1)
activejob (= 7.2.1)
activesupport (= 7.2.1)
actionmailer (7.2.1.2)
actionpack (= 7.2.1.2)
actionview (= 7.2.1.2)
activejob (= 7.2.1.2)
activesupport (= 7.2.1.2)
mail (>= 2.8.0)
rails-dom-testing (~> 2.2)
actionpack (7.2.1)
actionview (= 7.2.1)
activesupport (= 7.2.1)
actionpack (7.2.1.2)
actionview (= 7.2.1.2)
activesupport (= 7.2.1.2)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4, < 3.2)
Expand All @@ -32,35 +32,35 @@ GEM
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
useragent (~> 0.16)
actiontext (7.2.1)
actionpack (= 7.2.1)
activerecord (= 7.2.1)
activestorage (= 7.2.1)
activesupport (= 7.2.1)
actiontext (7.2.1.2)
actionpack (= 7.2.1.2)
activerecord (= 7.2.1.2)
activestorage (= 7.2.1.2)
activesupport (= 7.2.1.2)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.2.1)
activesupport (= 7.2.1)
actionview (7.2.1.2)
activesupport (= 7.2.1.2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.2.1)
activesupport (= 7.2.1)
activejob (7.2.1.2)
activesupport (= 7.2.1.2)
globalid (>= 0.3.6)
activemodel (7.2.1)
activesupport (= 7.2.1)
activerecord (7.2.1)
activemodel (= 7.2.1)
activesupport (= 7.2.1)
activemodel (7.2.1.2)
activesupport (= 7.2.1.2)
activerecord (7.2.1.2)
activemodel (= 7.2.1.2)
activesupport (= 7.2.1.2)
timeout (>= 0.4.0)
activestorage (7.2.1)
actionpack (= 7.2.1)
activejob (= 7.2.1)
activerecord (= 7.2.1)
activesupport (= 7.2.1)
activestorage (7.2.1.2)
actionpack (= 7.2.1.2)
activejob (= 7.2.1.2)
activerecord (= 7.2.1.2)
activesupport (= 7.2.1.2)
marcel (~> 1.0)
activesupport (7.2.1)
activesupport (7.2.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
Expand Down Expand Up @@ -268,20 +268,20 @@ GEM
rackup (1.0.0)
rack (< 3)
webrick
rails (7.2.1)
actioncable (= 7.2.1)
actionmailbox (= 7.2.1)
actionmailer (= 7.2.1)
actionpack (= 7.2.1)
actiontext (= 7.2.1)
actionview (= 7.2.1)
activejob (= 7.2.1)
activemodel (= 7.2.1)
activerecord (= 7.2.1)
activestorage (= 7.2.1)
activesupport (= 7.2.1)
rails (7.2.1.2)
actioncable (= 7.2.1.2)
actionmailbox (= 7.2.1.2)
actionmailer (= 7.2.1.2)
actionpack (= 7.2.1.2)
actiontext (= 7.2.1.2)
actionview (= 7.2.1.2)
activejob (= 7.2.1.2)
activemodel (= 7.2.1.2)
activerecord (= 7.2.1.2)
activestorage (= 7.2.1.2)
activesupport (= 7.2.1.2)
bundler (>= 1.15.0)
railties (= 7.2.1)
railties (= 7.2.1.2)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand All @@ -294,9 +294,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.2.1)
actionpack (= 7.2.1)
activesupport (= 7.2.1)
railties (7.2.1.2)
actionpack (= 7.2.1.2)
activesupport (= 7.2.1.2)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
Expand All @@ -318,7 +318,7 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.3.7)
rexml (3.3.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
Expand Down Expand Up @@ -404,7 +404,7 @@ GEM
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
useragent (0.16.10)
webrick (1.8.1)
webrick (1.8.2)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand Down Expand Up @@ -440,7 +440,7 @@ DEPENDENCIES
puma (~> 6.4)
pundit (~> 2.1)
rack-cors (~> 1.1)
rails (~> 7.2.0)
rails (~> 7.2.1.1)
rails-erd (~> 1.6)
rspec-rails
rspec_api_documentation (~> 6.1)
Expand Down
6 changes: 6 additions & 0 deletions app/blueprinter/building_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class BuildingBlueprint < Base
# Fields
fields :name, :description
end
2 changes: 1 addition & 1 deletion app/blueprinter/greenhouse_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class GreenhouseBlueprint < Base
# Fields
fields :name, :width, :height, :occupancy
fields :name, :width, :height, :occupancy, :building_id
end
49 changes: 49 additions & 0 deletions app/controllers/api/v1/buildings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

class Api::V1::BuildingsController < ApiController
before_action :set_building, only: %i[show update destroy]

def index
buildings = policy_scope(Building)
authorize buildings
render json: apply_fetcheable(buildings).to_blueprint, status: :ok
end

def show
render json: @building.to_blueprint, status: :ok
end

def create
authorize Building
building = Building.new(building_params)
if building.save
render json: building.to_blueprint, status: :created
else
render_validation_error(building)
end
end

def update
if @building.update(building_params)
render json: @building.to_blueprint, status: :ok
else
render_validation_error(@building)
end
end

def destroy
@building.destroy
head :no_content
end

private

def set_building
@building = policy_scope(Building).find(params[:id])
authorize @building
end

def building_params
params.require(:building).permit(%i[name description])
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/greenhouses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ def set_greenhouse
end

def greenhouse_params
params.permit(%i[name width height])
params.permit(%i[name width height building_id])
end
end
20 changes: 20 additions & 0 deletions app/models/building.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class Building < ApplicationRecord
# Associations
has_many :greenhouses, dependent: :destroy

# Validations
validates :name, presence: true
end

# == Schema Information
#
# Table name: buildings
#
# id :bigint not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# description :text
#
40 changes: 21 additions & 19 deletions app/models/greenhouse.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
# frozen_string_literal: true

class Greenhouse < ApplicationRecord
# Associations
belongs_to :building
has_many :benches, class_name: 'Bench', inverse_of: :greenhouse, dependent: :destroy

# Validations
validates :name, presence: true

validates :width,
:height,
numericality: { greater_than: 0 }

validates :width, :height, numericality: { greater_than: 0 }
validates :occupancy, inclusion: { in: 0..100 }

# Associations
has_many :benches,
class_name: 'Bench',
inverse_of: :greenhouse,
dependent: :destroy

# Public instance methods
# TODO: complete this method when request distributions will be created
def compute_occupancy
100
end
Expand All @@ -27,11 +20,20 @@ def compute_occupancy
#
# Table name: greenhouses
#
# id :bigint not null, primary key
# name :string not null
# width :integer not null
# height :integer not null
# occupancy :decimal(, ) default(0.0), not null
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# name :string not null
# width :integer not null
# height :integer not null
# occupancy :decimal(, ) default(0.0), not null
# created_at :datetime not null
# updated_at :datetime not null
# building_id :bigint
#
# Indexes
#
# index_greenhouses_on_building_id (building_id)
#
# Foreign Keys
#
# fk_rails_... (building_id => buildings.id)
#
34 changes: 34 additions & 0 deletions app/policies/building_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

class BuildingPolicy < ApplicationPolicy
def index?
grower?
end

def show?
grower?
end

def create?
grower?
end

def update?
grower?
end

def destroy?
return false unless grower?

return true if record.greenhouses.flat_map(&:benches).flat_map(&:request_distributions).empty?

record.errors.add(:request_distributions, 'can\'t delete a building with ongoing requests')
false
end

class Scope < Scope
def resolve
grower? ? scope.all : scope.none
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
post :complete, on: :member
resources :request_distributions, only: %i[index show create update destroy]
end
resources :buildings, only: %i[index show create update destroy]
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20241015112327_create_buildings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateBuildings < ActiveRecord::Migration[7.2]
def change
create_table :buildings do |t|
t.string :name
t.string :description

t.timestamps
end
end
end
Loading

0 comments on commit aac3c19

Please sign in to comment.