1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

space db model

This commit is contained in:
Sylvain 2017-02-13 14:38:28 +01:00
parent 5783f82418
commit 560bb46383
23 changed files with 417 additions and 90 deletions

View File

@ -1 +1 @@
2.4.10
2.5.0-dev

View File

@ -1,5 +1,12 @@
# Changelog Fab Manager
## next release
- TODO export availabilities (taiga#57)
- TODO bug: calendar (github#59)
- TODO bug: delete event (github#61)
- ONGOING spaces reservation
- [TODO DEPLOY] `rake db:migrate`
- [TODO DEPLOY] `rake db:seed`
## v2.4.10 2017 January 9
- Optimized notifications system

View File

@ -204,7 +204,9 @@ class Invoice < ActiveRecord::Base
private
def generate_and_send_invoice
puts "Creating an InvoiceWorker job to generate the following invoice: id(#{id}), invoiced_id(#{invoiced_id}), invoiced_type(#{invoiced_type}), user_id(#{user_id})"
unless Rails.env.test?
puts "Creating an InvoiceWorker job to generate the following invoice: id(#{id}), invoiced_id(#{invoiced_id}), invoiced_type(#{invoiced_type}), user_id(#{user_id})"
end
InvoiceWorker.perform_async(id)
end

View File

@ -4,7 +4,7 @@ class MachinesAvailability < ActiveRecord::Base
after_destroy :cleanup_availability
# when the MachinesAvailability is deleted (from Machine destroy cascade), we delete the corresponding
# availability if the deleted machine was the last is this availability slot and teh availability is not
# availability if the deleted machine was the last of this availability slot, and the availability is not
# currently being destroyed.
def cleanup_availability
unless availability.destroying

View File

@ -21,6 +21,7 @@ class Project < ActiveRecord::Base
accepts_nested_attributes_for :project_caos, allow_destroy: true, reject_if: :all_blank
has_and_belongs_to_many :machines, join_table: :projects_machines
has_and_belongs_to_many :spaces, join_table: :projects_spaces
has_and_belongs_to_many :components, join_table: :projects_components
has_and_belongs_to_many :themes, join_table: :projects_themes

54
app/models/space.rb Normal file
View File

@ -0,0 +1,54 @@
class Space < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_one :space_image, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :space_image, allow_destroy: true
has_and_belongs_to_many :projects, join_table: :projects_spaces
has_many :spaces_availabilities
has_many :availabilities, through: :spaces_availabilities, dependent: :destroy
has_many :reservations, as: :reservable, dependent: :destroy
has_many :prices, as: :priceable, dependent: :destroy
has_many :credits, as: :creditable, dependent: :destroy
after_create :create_statistic_subtype
after_create :create_space_prices
after_update :update_statistic_subtype, if: :name_changed?
after_destroy :remove_statistic_subtype
def create_statistic_subtype
index = StatisticIndex.find_by(es_type_key: 'space')
StatisticSubType.create!({statistic_types: index.statistic_types, key: self.slug, label: self.name})
end
def update_statistic_subtype
index = StatisticIndex.find_by(es_type_key: 'space')
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type).find_by(key: self.slug, statistic_types: { statistic_index_id: index.id })
subtype.label = self.name
subtype.save!
end
def remove_statistic_subtype
subtype = StatisticSubType.find_by(key: self.slug)
subtype.destroy!
end
def create_space_prices
Group.all.each do |group|
Price.create(priceable: self, group: group, amount: 0)
end
Plan.all.includes(:group).each do |plan|
Price.create(group: plan.group, plan: plan, priceable: self, amount: 0)
end
end
def destroyable?
reservations.empty?
end
end

View File

@ -0,0 +1,4 @@
class SpaceImage < Asset
mount_uploader :attachment, SpaceImageUploader
end

View File

@ -0,0 +1,14 @@
class SpacesAvailability < ActiveRecord::Base
belongs_to :space
belongs_to :availability
after_destroy :cleanup_availability
# when the SpacesAvailability is deleted (from Space destroy cascade), we delete the corresponding
# availability. We don't use 'dependent: destroy' as we need to prevent conflicts if the destroy came from
# the Availability destroy cascade.
def cleanup_availability
unless availability.destroying
availability.safe_destroy
end
end
end

View File

@ -1,4 +1,4 @@
class TrainingImage < Asset
mount_uploader :attachment, MachineImageUploader
mount_uploader :attachment, TrainingImageUploader
end

View File

@ -0,0 +1,58 @@
class SpaceImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
include UploadHelper
# Choose what kind of storage to use for this uploader:
storage :file
after :remove, :delete_empty_dirs
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{base_store_dir}/#{model.id}"
end
def base_store_dir
"uploads/#{model.class.to_s.underscore}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :large do
process :resize_to_fit => [1000, 700]
end
version :medium do
process :resize_to_fit => [700, 400]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
"space_image.#{file.extension}" if original_filename
end
end

View File

@ -0,0 +1,58 @@
class TrainingImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
include UploadHelper
# Choose what kind of storage to use for this uploader:
storage :file
after :remove, :delete_empty_dirs
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{base_store_dir}/#{model.id}"
end
def base_store_dir
"uploads/#{model.class.to_s.underscore}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :large do
process :resize_to_fit => [1000, 700]
end
version :medium do
process :resize_to_fit => [700, 400]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
"training_image.#{file.extension}" if original_filename
end
end

View File

@ -286,6 +286,7 @@ en:
# statistics tools for admins
subscriptions: "Subscriptions"
machines_hours: "Machines hours"
spaces: "Spaces"
trainings: "Trainings"
events: "Events"
registrations: "Registrations"

View File

@ -286,6 +286,7 @@ fr:
# outil de statistiques pour les administrateurs
subscriptions: "Abonnements"
machines_hours: "Heures machines"
spaces: "Espaces"
trainings: "Formations"
events: "Évènements"
registrations: "Inscriptions"

View File

@ -0,0 +1,12 @@
class CreateSpaces < ActiveRecord::Migration
def change
create_table :spaces do |t|
t.string :name
t.integer :default_places
t.text :description
t.string :slug
t.timestamps null: false
end
end
end

View File

@ -0,0 +1,8 @@
class CreateProjectsSpaces < ActiveRecord::Migration
def change
create_table :projects_spaces do |t|
t.belongs_to :project, index: true, foreign_key: true
t.belongs_to :space, index: true, foreign_key: true
end
end
end

View File

@ -0,0 +1,10 @@
class CreateSpacesAvailabilities < ActiveRecord::Migration
def change
create_table :spaces_availabilities do |t|
t.belongs_to :space, index: true, foreign_key: true
t.belongs_to :availability, index: true, foreign_key: true
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170109085345) do
ActiveRecord::Schema.define(version: 20170213103438) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -485,6 +485,14 @@ ActiveRecord::Schema.define(version: 20170109085345) do
add_index "projects_machines", ["machine_id"], name: "index_projects_machines_on_machine_id", using: :btree
add_index "projects_machines", ["project_id"], name: "index_projects_machines_on_project_id", using: :btree
create_table "projects_spaces", force: :cascade do |t|
t.integer "project_id"
t.integer "space_id"
end
add_index "projects_spaces", ["project_id"], name: "index_projects_spaces_on_project_id", using: :btree
add_index "projects_spaces", ["space_id"], name: "index_projects_spaces_on_space_id", using: :btree
create_table "projects_themes", force: :cascade do |t|
t.integer "project_id"
t.integer "theme_id"
@ -544,6 +552,25 @@ ActiveRecord::Schema.define(version: 20170109085345) do
add_index "slots", ["availability_id"], name: "index_slots_on_availability_id", using: :btree
add_index "slots", ["reservation_id"], name: "index_slots_on_reservation_id", using: :btree
create_table "spaces", force: :cascade do |t|
t.string "name"
t.integer "default_places"
t.text "description"
t.string "slug"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "spaces_availabilities", force: :cascade do |t|
t.integer "space_id"
t.integer "availability_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "spaces_availabilities", ["availability_id"], name: "index_spaces_availabilities_on_availability_id", using: :btree
add_index "spaces_availabilities", ["space_id"], name: "index_spaces_availabilities_on_space_id", using: :btree
create_table "statistic_custom_aggregations", force: :cascade do |t|
t.text "query"
t.integer "statistic_type_id"
@ -820,6 +847,10 @@ ActiveRecord::Schema.define(version: 20170109085345) do
add_foreign_key "organizations", "profiles"
add_foreign_key "prices", "groups"
add_foreign_key "prices", "plans"
add_foreign_key "projects_spaces", "projects"
add_foreign_key "projects_spaces", "spaces"
add_foreign_key "spaces_availabilities", "availabilities"
add_foreign_key "spaces_availabilities", "spaces"
add_foreign_key "statistic_custom_aggregations", "statistic_types"
add_foreign_key "tickets", "event_price_categories"
add_foreign_key "tickets", "reservations"

File diff suppressed because one or more lines are too long

1
test/fixtures/spaces.yml vendored Normal file
View File

@ -0,0 +1 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View File

@ -0,0 +1 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View File

@ -61,3 +61,12 @@ statistic_index_7:
updated_at: 2016-04-04 14:11:33.376810000 Z
table: false
ca: false
statistic_index_8:
id: 8
es_type_key: space
label: Espaces
created_at: 2017-02-13 14:08:36.211740000 Z
updated_at: 2017-02-13 14:08:36.211740000 Z
table: true
ca: true

View File

@ -108,3 +108,23 @@ statistic_type_11:
created_at: 2016-04-04 15:17:24.950033000 Z
updated_at: 2016-04-04 15:17:24.950033000 Z
simple: true
statistic_type_12:
id: 12
statistic_index_id: 8
key: booking
label: Réservations
graph: true
created_at: 2017-02-13 14:11:24.742280000 Z
updated_at: 2017-02-13 14:11:24.742280000 Z
simple: true
statistic_type_13:
id: 13
statistic_index_id: 8
key: hour
label: Nombre d'heures
graph: true
created_at: 2017-02-13 14:11:45.196140000 Z
updated_at: 2017-02-13 14:11:45.196140000 Z
simple: false

26
test/models/space_test.rb Normal file
View File

@ -0,0 +1,26 @@
require 'test_helper'
class SpaceTest < ActiveSupport::TestCase
test 'create a space' do
space = Space.create!({name: 'Bio-lab', description: 'An biological laboratory to experiment bio-technologies', default_places: 5})
assert_not_nil space
subtype = StatisticSubType.find_by(key: space.slug)
assert_not_nil subtype
end
test 'update a space' do
new_name = 'Bio-tech lab'
space = Space.create!({name: 'Bio-lab', description: 'An biological laboratory to experiment bio-technologies', default_places: 5})
space.update_attributes({name: new_name})
subtype = StatisticSubType.find_by(key: space.slug)
assert_equal new_name, subtype.label
end
test 'delete a space' do
space = Space.create!({name: 'Bio-lab', description: 'An biological laboratory to experiment bio-technologies', default_places: 5})
slug = space.slug
space.destroy!
assert_nil Space.find_by(slug: slug)
assert_nil StatisticSubType.find_by(key: slug)
end
end