1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/models/project.rb

95 lines
3.7 KiB
Ruby
Raw Normal View History

2019-06-06 16:34:53 +02:00
# frozen_string_literal: true
# Project is the documentation about an object built by a fab-user
# It can describe the steps taken by the fab-user to build his object, provide photos, description, attached CAO files, etc.
2020-03-25 10:16:47 +01:00
class Project < ApplicationRecord
2015-05-05 03:10:25 +02:00
include AASM
include NotifyWith::NotificationAttachedObject
2016-04-20 18:13:36 +02:00
include OpenlabSync
2020-06-22 11:25:35 +02:00
include PgSearch::Model
2016-03-23 18:39:41 +01:00
# kaminari
2019-06-06 16:34:53 +02:00
# -- dependency in app/assets/javascripts/controllers/projects.js.erb
paginates_per 16
2016-03-23 18:39:41 +01:00
# friendlyId
2015-05-05 03:10:25 +02:00
extend FriendlyId
friendly_id :name, use: :slugged
has_one :project_image, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :project_image, allow_destroy: true
has_many :project_caos, as: :viewable, dependent: :destroy
2015-05-18 10:16:21 +02:00
accepts_nested_attributes_for :project_caos, allow_destroy: true, reject_if: :all_blank
2015-05-05 03:10:25 +02:00
2019-06-06 16:34:53 +02:00
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'
2015-05-05 03:10:25 +02:00
has_many :project_users, dependent: :destroy
has_many :users, through: :project_users
2019-06-06 16:34:53 +02:00
belongs_to :author, foreign_key: :author_statistic_profile_id, class_name: 'StatisticProfile'
2015-05-05 03:10:25 +02:00
belongs_to :licence, foreign_key: :licence_id
has_many :project_steps, dependent: :destroy
accepts_nested_attributes_for :project_steps, allow_destroy: true
2016-04-21 17:10:50 +02:00
# validations
validates :author, :name, presence: true
2015-05-05 03:10:25 +02:00
after_save :after_save_and_publish
2019-06-06 16:34:53 +02:00
aasm column: 'state' do
2015-05-05 03:10:25 +02:00
state :draft, initial: true
state :published
2019-06-06 16:34:53 +02:00
event :publish, after: :notify_admin_when_project_published do
transitions from: :draft, to: :published
2015-05-05 03:10:25 +02:00
end
end
2019-06-06 16:34:53 +02:00
# scopes
2015-05-05 03:10:25 +02:00
scope :published, -> { where("state = 'published'") }
2020-06-23 11:19:20 +02:00
scope :published_or_drafts, lambda { |author_profile|
where("state = 'published' OR (state = 'draft' AND author_statistic_profile_id = ?)", author_profile)
}
scope :user_projects, ->(author_profile) { where('author_statistic_profile_id = ?', author_profile) }
scope :collaborations, ->(collaborators_ids) { joins(:projects_users).where(projects_users: { user_id: collaborators_ids }) }
scope :with_machine, ->(machines_ids) { joins(:projects_machines).where(projects_machines: { machine_id: machines_ids }) }
scope :with_theme, ->(themes_ids) { joins(:projects_themes).where(projects_themes: { theme_id: themes_ids }) }
scope :with_component, ->(component_ids) { joins(:projects_components).where(projects_components: { component_id: component_ids }) }
scope :with_space, ->(spaces_ids) { joins(:projects_spaces).where(projects_spaces: { space_id: spaces_ids }) }
2020-06-22 11:25:35 +02:00
pg_search_scope :search,
against: :search_vector,
2020-06-22 11:25:35 +02:00
using: {
tsearch: {
dictionary: Rails.application.secrets.postgresql_language_analyzer,
prefix: true,
tsvector_column: 'search_vector'
},
trigram: {
word_similarity: true,
threshold: 0.3
},
dmetaphone: {}
},
ignoring: :accents,
order_within_rank: 'created_at DESC'
2016-03-23 18:39:41 +01:00
2015-05-05 03:10:25 +02:00
private
2019-06-06 16:34:53 +02:00
2015-05-05 03:10:25 +02:00
def notify_admin_when_project_published
NotificationCenter.call type: 'notify_admin_when_project_published',
2020-04-29 15:34:30 +02:00
receiver: User.admins_and_managers,
2015-05-05 03:10:25 +02:00
attached_object: self
end
def after_save_and_publish
return unless saved_change_to_state? && published?
2019-06-06 16:34:53 +02:00
update_columns(published_at: DateTime.current)
2019-06-06 16:34:53 +02:00
notify_admin_when_project_published
2015-05-05 03:10:25 +02:00
end
end