diff --git a/app/assets/javascripts/controllers/projects.js.erb b/app/assets/javascripts/controllers/projects.js.erb index 3de0fa16d..c8e78be77 100644 --- a/app/assets/javascripts/controllers/projects.js.erb +++ b/app/assets/javascripts/controllers/projects.js.erb @@ -257,6 +257,7 @@ Application.Controllers.controller('ProjectsController', ['$scope', '$state', 'P /* PRIVATE STATIC CONSTANTS */ // Number of projects added to the page when the user clicks on 'load more projects' + // -- dependency in app/models/project.rb const PROJECTS_PER_PAGE = 16; /* PUBLIC SCOPE */ diff --git a/app/assets/templates/admin/members/_form.html.erb b/app/assets/templates/admin/members/_form.html.erb index 9d81a9a60..a6d9fc7d6 100644 --- a/app/assets/templates/admin/members/_form.html.erb +++ b/app/assets/templates/admin/members/_form.html.erb @@ -14,11 +14,11 @@
- + - + diff --git a/app/controllers/api/members_controller.rb b/app/controllers/api/members_controller.rb index 7b0a19a8c..47c32369a 100644 --- a/app/controllers/api/members_controller.rb +++ b/app/controllers/api/members_controller.rb @@ -203,7 +203,7 @@ class API::MembersController < API::ApiController elsif current_user.admin? params.require(:user).permit(:username, :email, :password, :password_confirmation, :is_allow_contact, :is_allow_newsletter, :group_id, - training_ids: [], tag_ids: [], + tag_ids: [], profile_attributes: [:id, :first_name, :last_name, :phone, :interest, :software_mastered, :website, :job, :facebook, :twitter, :google_plus, :viadeo, :linkedin, :instagram, :youtube, :vimeo, :dailymotion, :github, :echosciences, :pinterest, :lastfm, :flickr, @@ -213,7 +213,7 @@ class API::MembersController < API::ApiController address_attributes: %i[id address], organization_attributes: [:id, :name, address_attributes: %i[id address]] ], - statistic_profile_attributes: %i[id gender birthday]) + statistic_profile_attributes: [:id, :gender, :birthday, training_ids: []]) end end diff --git a/app/controllers/api/projects_controller.rb b/app/controllers/api/projects_controller.rb index 126bb382c..adf27a948 100644 --- a/app/controllers/api/projects_controller.rb +++ b/app/controllers/api/projects_controller.rb @@ -20,7 +20,7 @@ class API::ProjectsController < API::ApiController end def create - @project = Project.new(project_params.merge(author_id: current_user.id)) + @project = Project.new(project_params.merge(author_statistic_profile_id: current_user.statistic_profile.id)) if @project.save render :show, status: :created, location: @project else @@ -71,8 +71,7 @@ class API::ProjectsController < API::ApiController end def project_params - params.require(:project).permit(:name, :description, :tags, :machine_ids, :component_ids, :theme_ids, :licence_id, - :author_id, :licence_id, :state, + params.require(:project).permit(:name, :description, :tags, :machine_ids, :component_ids, :theme_ids, :licence_id, :licence_id, :state, user_ids: [], machine_ids: [], component_ids: [], theme_ids: [], project_image_attributes: [:attachment], project_caos_attributes: %i[id attachment _destroy], diff --git a/app/models/availability.rb b/app/models/availability.rb index fe3b28f46..54881fa8f 100644 --- a/app/models/availability.rb +++ b/app/models/availability.rb @@ -1,3 +1,8 @@ +# frozen_string_literal: true + +# Availability stores time slots that are available to reservation for an associated reservable +# Eg. a 3D printer will be reservable on thursday from 9 to 11 pm +# Availabilities may be subdivided into Slots (of 1h), for some types of reservables (eg. Machine) class Availability < ActiveRecord::Base # elastic initialisations @@ -87,9 +92,7 @@ class Availability < ActiveRecord::Base def title(filter = {}) case available_type when 'machines' - if filter[:machine_ids] - return machines.to_ary.delete_if { |m| !filter[:machine_ids].include?(m.id) }.map(&:name).join(' - ') - end + return machines.to_ary.delete_if { |m| !filter[:machine_ids].include?(m.id) }.map(&:name).join(' - ') if filter[:machine_ids] machines.map(&:name).join(' - ') when 'event' @@ -134,13 +137,13 @@ class Availability < ActiveRecord::Base json['hours_duration'] = (end_at - start_at) / (60 * 60) json['subType'] = case available_type when 'machines' - machines_availabilities.map{ |ma| ma.machine.friendly_id } + machines_availabilities.map { |ma| ma.machine.friendly_id } when 'training' - trainings_availabilities.map{ |ta| ta.training.friendly_id } + trainings_availabilities.map { |ta| ta.training.friendly_id } when 'event' [event.category.friendly_id] when 'space' - spaces_availabilities.map{ |sa| sa.space.friendly_id } + spaces_availabilities.map { |sa| sa.space.friendly_id } else [] end @@ -156,7 +159,9 @@ class Availability < ActiveRecord::Base end def should_be_associated - errors.add(:machine_ids, I18n.t('availabilities.must_be_associated_with_at_least_1_machine')) if available_type == 'machines' && machine_ids.count == 0 + return unless available_type == 'machines' && machine_ids.count.zero? + + errors.add(:machine_ids, I18n.t('availabilities.must_be_associated_with_at_least_1_machine')) end end diff --git a/app/models/project.rb b/app/models/project.rb index 351cdcca3..056a86775 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,3 +1,7 @@ +# 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. class Project < ActiveRecord::Base include AASM include NotifyWith::NotificationAttachedObject @@ -9,7 +13,8 @@ class Project < ActiveRecord::Base document_type 'projects' # kaminari - paginates_per 12 # dependency in projects.coffee + # -- dependency in app/assets/javascripts/controllers/projects.js.erb + paginates_per 16 # friendlyId extend FriendlyId @@ -20,15 +25,15 @@ class Project < ActiveRecord::Base has_many :project_caos, as: :viewable, dependent: :destroy 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 + 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' has_many :project_users, dependent: :destroy has_many :users, through: :project_users - belongs_to :author, foreign_key: :author_id, class_name: 'User' + belongs_to :author, foreign_key: :author_statistic_profile_id, class_name: 'StatisticProfile' belongs_to :licence, foreign_key: :licence_id has_many :project_steps, dependent: :destroy @@ -39,22 +44,22 @@ class Project < ActiveRecord::Base after_save :after_save_and_publish - aasm :column => 'state' do + aasm column: 'state' do state :draft, initial: true state :published - event :publish, :after => :notify_admin_when_project_published do - transitions from: :draft, :to => :published + event :publish, after: :notify_admin_when_project_published do + transitions from: :draft, to: :published end end - #scopes + # scopes scope :published, -> { where("state = 'published'") } ## elastic # callbacks - after_save { ProjectIndexerWorker.perform_async(:index, self.id) } - after_destroy { ProjectIndexerWorker.perform_async(:delete, self.id) } + after_save { ProjectIndexerWorker.perform_async(:index, id) } + after_destroy { ProjectIndexerWorker.perform_async(:delete, id) } # mapping settings index: { number_of_replicas: 0 } do @@ -64,31 +69,19 @@ class Project < ActiveRecord::Base indexes 'name', analyzer: Rails.application.secrets.elasticsearch_language_analyzer indexes 'description', analyzer: Rails.application.secrets.elasticsearch_language_analyzer indexes 'project_steps' do - indexes 'title', analyzer: Rails.application.secrets.elasticsearch_language_analyzer - indexes 'description', analyzer: Rails.application.secrets.elasticsearch_language_analyzer + indexes 'title', analyzer: Rails.application.secrets.elasticsearch_language_analyzer + indexes 'description', analyzer: Rails.application.secrets.elasticsearch_language_analyzer end end end def as_indexed_json - Jbuilder.new do |json| - json.id id - json.state state - json.author_id author_id - json.user_ids user_ids - json.machine_ids machine_ids - json.theme_ids theme_ids - json.component_ids component_ids - json.tags tags - json.name name - json.description description - json.project_steps project_steps do |project_step| - json.title project_step.title - json.description project_step.description - end - json.created_at created_at - json.updated_at updated_at - end.target! + ApplicationController.new.view_context.render( + partial: 'api/projects/indexed', + locals: { project: self }, + formats: [:json], + handlers: [:jbuilder] + ) end def self.search(params, current_user) @@ -101,43 +94,45 @@ class Project < ActiveRecord::Base bool: { must: [], should: [], - filter: [], + filter: [] } } } - if params['q'].blank? # we sort by created_at if there isn't a query + # we sort by created_at if there isn't a query + if params['q'].blank? search[:sort] = { created_at: { order: :desc } } - else # otherwise we search for the word (q) in various fields + else + # otherwise we search for the word (q) in various fields search[:query][:bool][:must] << { multi_match: { query: params['q'], type: 'most_fields', - fields: %w(tags^4 name^5 description^3 project_steps.title^2 project_steps.description) + fields: %w[tags^4 name^5 description^3 project_steps.title^2 project_steps.description] } } end - params.each do |name, value| # we filter by themes, components, machines + # we filter by themes, components, machines + params.each do |name, value| if name =~ /(.+_id$)/ search[:query][:bool][:filter] << { term: { "#{name}s" => value } } if value end end - if current_user and params.key?('from') # if use select filter 'my project' or 'my collaborations' - if params['from'] == 'mine' - search[:query][:bool][:filter] << { term: { author_id: current_user.id } } - end - if params['from'] == 'collaboration' - search[:query][:bool][:filter] << { term: { user_ids: current_user.id } } - end + # if use select filter 'my project' or 'my collaborations' + if current_user && params.key?('from') + search[:query][:bool][:filter] << { term: { author_id: current_user.id } } if params['from'] == 'mine' + search[:query][:bool][:filter] << { term: { user_ids: current_user.id } } if params['from'] == 'collaboration' end - if current_user # if user is connected, also display his draft projects + # if user is connected, also display his draft projects + if current_user search[:query][:bool][:should] << { term: { state: 'published' } } search[:query][:bool][:should] << { term: { author_id: current_user.id } } search[:query][:bool][:should] << { term: { user_ids: current_user.id } } - else # otherwise display only published projects + else + # otherwise display only published projects search[:query][:bool][:must] << { term: { state: 'published' } } end @@ -145,6 +140,7 @@ class Project < ActiveRecord::Base end private + def notify_admin_when_project_published NotificationCenter.call type: 'notify_admin_when_project_published', receiver: User.admins, @@ -152,9 +148,9 @@ class Project < ActiveRecord::Base end def after_save_and_publish - if state_changed? and published? - update_columns(published_at: Time.now) - notify_admin_when_project_published - end + return unless state_changed? && published? + + update_columns(published_at: Time.now) + notify_admin_when_project_published end end diff --git a/app/models/statistic_profile.rb b/app/models/statistic_profile.rb index 42bd99220..4d1c9b023 100644 --- a/app/models/statistic_profile.rb +++ b/app/models/statistic_profile.rb @@ -21,6 +21,9 @@ class StatisticProfile < ActiveRecord::Base has_many :statistic_profile_trainings, dependent: :destroy has_many :trainings, through: :statistic_profile_trainings + # Projects that the current user is the author + has_many :my_projects, foreign_key: :author_statistic_profile_id, class_name: 'Project', dependent: :destroy + def str_gender gender ? 'male' : 'female' end diff --git a/app/models/user.rb b/app/models/user.rb index 1da8adddf..64d75a29c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,7 +27,6 @@ class User < ActiveRecord::Base has_one :statistic_profile, dependent: :nullify accepts_nested_attributes_for :statistic_profile - has_many :my_projects, foreign_key: :author_id, class_name: 'Project', dependent: :destroy has_many :project_users, dependent: :destroy has_many :projects, through: :project_users @@ -66,6 +65,7 @@ class User < ActiveRecord::Base delegate :subscriptions, to: :statistic_profile delegate :reservations, to: :statistic_profile delegate :trainings, to: :statistic_profile + delegate :my_projects, to: :statistic_profile delegate :wallet, to: :invoicing_profile delegate :wallet_transactions, to: :invoicing_profile delegate :invoices, to: :invoicing_profile diff --git a/app/policies/project_policy.rb b/app/policies/project_policy.rb index 79b15c0ff..514295bf9 100644 --- a/app/policies/project_policy.rb +++ b/app/policies/project_policy.rb @@ -2,23 +2,24 @@ class ProjectPolicy < ApplicationPolicy class Scope < Scope def resolve if user + statistic_profile = StatisticProfile.find_by(user_id: user.id) scope.includes(:project_image, :machines, :users) - .where("state = 'published' OR (state = 'draft' AND (author_id = ? OR users.id = ?))", user.id, user.id) - .references(:users) - .order(created_at: :desc) + .where("state = 'published' OR (state = 'draft' AND (author_statistic_profile_id = ? OR users.id = ?))", statistic_profile.id, user.id) + .references(:users) + .order(created_at: :desc) else scope.includes(:project_image, :machines, :users) - .where("state = 'published'") - .order(created_at: :desc) + .where("state = 'published'") + .order(created_at: :desc) end end end def update? - user.admin? or record.author == user or record.users.include?(user) + user.admin? or record.author.user_id == user.id or record.users.include?(user) end def destroy? - user.admin? or record.author == user + user.admin? or record.author.user_id == user end end diff --git a/app/services/availabilities/status_service.rb b/app/services/availabilities/status_service.rb index 1effc2729..438be1d0b 100644 --- a/app/services/availabilities/status_service.rb +++ b/app/services/availabilities/status_service.rb @@ -9,7 +9,7 @@ class Availabilities::StatusService # check that the provided machine slot is reserved or not and modify it accordingly def machine_reserved_status(slot, reservations, user) - statistic_profile_id = user.statistic_profile.id + statistic_profile_id = user&.statistic_profile&.id reservations.each do |r| r.slots.each do |s| next unless slot.machine.id == r.reservable_id @@ -34,7 +34,7 @@ class Availabilities::StatusService # check that the provided space slot is reserved or not and modify it accordingly def space_reserved_status(slot, reservations, user) - statistic_profile_id = user.statistic_profile.id + statistic_profile_id = user&.statistic_profile&.id reservations.each do |r| r.slots.each do |s| next unless slot.space.id == r.reservable_id @@ -57,7 +57,7 @@ class Availabilities::StatusService # check that the provided availability (training or event) is reserved or not and modify it accordingly def training_event_reserved_status(availability, reservations, user) - statistic_profile_id = user.statistic_profile.id + statistic_profile_id = user&.statistic_profile&.id reservations.each do |r| r.slots.each do |s| next unless ( diff --git a/app/services/reservations/reserve.rb b/app/services/reservations/reserve.rb index 128262b63..d55a376c7 100644 --- a/app/services/reservations/reserve.rb +++ b/app/services/reservations/reserve.rb @@ -10,7 +10,7 @@ class Reservations::Reserve end def pay_and_save(reservation, payment_method, coupon) - reservation.statistic_profile_id = User.find(user_id).statistic_profile.id + reservation.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id if payment_method == :local reservation.save_with_local_payment(operator_id, coupon) elsif payment_method == :stripe diff --git a/app/services/statistic_service.rb b/app/services/statistic_service.rb index 2a89239a2..85d65ffad 100644 --- a/app/services/statistic_service.rb +++ b/app/services/statistic_service.rb @@ -305,7 +305,7 @@ class StatisticService def projects_list(options = default_options) result = [] Project.where('projects.published_at >= :start_date AND projects.published_at <= :end_date', options) - .eager_load(:licence, :themes, :components, :machines, :project_users, author: %i[profile group]) + .eager_load(:licence, :themes, :components, :machines, :project_users, author: %i[group]) .each do |p| result.push OpenStruct.new({ date: options[:start_date].to_date diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index 74ea491f7..dd5b5a065 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -12,7 +12,7 @@ class Subscriptions::Subscribe def pay_and_save(subscription, payment_method, coupon, invoice) return false if user_id.nil? - subscription.statistic_profile_id = User.find(user_id).statistic_profile.id + subscription.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id if payment_method == :local subscription.save_with_local_payment(operator_id, invoice, coupon) elsif payment_method == :stripe diff --git a/app/views/api/members/show.json.jbuilder b/app/views/api/members/show.json.jbuilder index 7cf56bfc2..335136632 100644 --- a/app/views/api/members/show.json.jbuilder +++ b/app/views/api/members/show.json.jbuilder @@ -1,3 +1,5 @@ +# frozen_string_literal: true + requested_current = (current_user and current_user.id == @member.id) json.partial! 'api/members/member', member: @member @@ -19,7 +21,8 @@ end json.all_projects @member.all_projects do |project| if requested_current || project.state == 'published' - json.extract! project, :id, :name, :description, :author_id, :licence_id, :slug, :state + json.extract! project, :id, :name, :description, :licence_id, :slug, :state + json.author_id project.author.user_id json.url project_url(project, format: :json) json.project_image project.project_image.attachment.large.url if project.project_image json.machine_ids project.machine_ids @@ -27,7 +30,6 @@ json.all_projects @member.all_projects do |project| json.id m.id json.name m.name end - json.author_id project.author_id json.user_ids project.user_ids json.component_ids project.component_ids json.components project.components do |c| @@ -69,7 +71,7 @@ json.invoices @member.invoices.order('reference DESC') do |i| json.reference i.reference json.type i.invoiced_type json.invoiced_id i.invoiced_id - json.total (i.total / 100.00) + json.total i.total / 100.00 json.is_avoir i.is_a?(Avoir) json.date i.is_a?(Avoir) ? i.avoir_date : i.created_at end diff --git a/app/views/api/projects/_indexed.json.jbuilder b/app/views/api/projects/_indexed.json.jbuilder new file mode 100644 index 000000000..afdfa520d --- /dev/null +++ b/app/views/api/projects/_indexed.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +json.id project.id +json.state project.state +json.author_id project.author.user_id +json.user_ids project.user_ids +json.machine_ids project.machine_ids +json.theme_ids project.theme_ids +json.component_ids project.component_ids +json.tags project.tags +json.name project.name +json.description project.description +json.project_steps project.project_steps do |project_step| + json.title project_step.title + json.description project_step.description +end +json.created_at project.created_at +json.updated_at project.updated_at diff --git a/app/views/api/projects/index.json.jbuilder b/app/views/api/projects/index.json.jbuilder index d9bfd00ed..6be508e95 100644 --- a/app/views/api/projects/index.json.jbuilder +++ b/app/views/api/projects/index.json.jbuilder @@ -1,8 +1,10 @@ +# frozen_string_literal: true + json.projects @projects do |project| - json.extract! project, :id, :name, :description, :author_id, :licence_id, :slug, :state + json.extract! project, :id, :name, :description, :licence_id, :slug, :state + json.author_id project.author.user_id json.url project_url(project, format: :json) json.project_image project.project_image.attachment.medium.url if project.project_image - json.author_id project.author_id json.user_ids project.user_ids end diff --git a/app/views/api/projects/last_published.json.jbuilder b/app/views/api/projects/last_published.json.jbuilder index 608448810..97199d440 100644 --- a/app/views/api/projects/last_published.json.jbuilder +++ b/app/views/api/projects/last_published.json.jbuilder @@ -1,3 +1,5 @@ +# frozen_string_literal: true + json.array!(@projects) do |project| json.extract! project, :id, :name, :description, :slug json.url project_url(project, format: :json) diff --git a/app/views/api/projects/show.json.jbuilder b/app/views/api/projects/show.json.jbuilder index a5fe8fe8f..5f79f3e98 100644 --- a/app/views/api/projects/show.json.jbuilder +++ b/app/views/api/projects/show.json.jbuilder @@ -1,17 +1,22 @@ -json.extract! @project, :id, :name, :description, :tags, :created_at, :updated_at, :author_id, :licence_id, :slug +# frozen_string_literal: true + +json.extract! @project, :id, :name, :description, :tags, :created_at, :updated_at, :licence_id, :slug +json.author_id @project.author.user_id json.project_image @project.project_image.attachment.large.url if @project.project_image json.project_full_image @project.project_image.attachment.url if @project.project_image json.author do - json.id @project.author_id - json.first_name @project.author.profile.first_name - json.last_name @project.author.profile.last_name - json.full_name @project.author.profile.full_name - json.user_avatar do - json.id @project.author.profile.user_avatar.id - json.attachment_url @project.author.profile.user_avatar.attachment_url - end if @project.author.profile.user_avatar - json.username @project.author.username - json.slug @project.author.slug + json.id @project.author.user_id + json.first_name @project.author&.user&.profile&.first_name + json.last_name @project.author&.user&.profile&.last_name + json.full_name @project.author&.user&.profile&.full_name + if @project.author&.user&.profile&.user_avatar + json.user_avatar do + json.id @project.author&.user&.profile&.user_avatar&.id + json.attachment_url @project.author&.user&.profile&.user_avatar&.attachment_url + end + end + json.username @project.author&.user&.username + json.slug @project.author&.user&.slug end json.project_caos_attributes @project.project_caos do |f| json.id f.id @@ -39,10 +44,12 @@ json.project_users @project.project_users do |pu| json.first_name pu.user.profile.first_name json.last_name pu.user.profile.last_name json.full_name pu.user.profile.full_name - json.user_avatar do - json.id pu.user.profile.user_avatar.id - json.attachment_url pu.user.profile.user_avatar.attachment_url - end if pu.user.profile.user_avatar + if pu.user.profile.user_avatar + json.user_avatar do + json.id pu.user.profile.user_avatar.id + json.attachment_url pu.user.profile.user_avatar.attachment_url + end + end json.username pu.user.username json.slug pu.user.slug json.is_valid pu.is_valid @@ -60,9 +67,8 @@ json.project_steps_attributes @project.project_steps.order('project_steps.step_n json.step_nb s.step_nb end json.state @project.state -json.licence do - json.name @project.licence.name -end if @project.licence.present? -#json.project_steps_attributes @project.project_steps do |s| - #json.set! s.id, {id: s.id, description: s.description} -#end +if @project.licence.present? + json.licence do + json.name @project.licence.name + end +end diff --git a/app/workers/availability_indexer_worker.rb b/app/workers/availability_indexer_worker.rb index c26acadad..7466904a9 100644 --- a/app/workers/availability_indexer_worker.rb +++ b/app/workers/availability_indexer_worker.rb @@ -9,20 +9,20 @@ class AvailabilityIndexerWorker logger.debug [operation, "ID: #{record_id}"] case operation.to_s - when /index/ - begin - record = Availability.find(record_id) - Client.index index: Availability.index_name, type: Availability.document_type, id: record.id, body: record.as_indexed_json - rescue ActiveRecord::RecordNotFound - STDERR.puts "Availability id(#{record_id}) will not be indexed in ElasticSearch as it does not exists anymore in database" - end - when /delete/ - begin - Client.delete index: Availability.index_name, type: Availability.document_type, id: record_id - rescue Elasticsearch::Transport::Transport::Errors::NotFound - STDERR.puts "Availability id(#{record_id}) will not be deleted form ElasticSearch as it has not been already indexed" - end - else raise ArgumentError, "Unknown operation '#{operation}'" + when /index/ + begin + record = Availability.find(record_id) + Client.index index: Availability.index_name, type: Availability.document_type, id: record.id, body: record.as_indexed_json + rescue ActiveRecord::RecordNotFound + STDERR.puts "Availability id(#{record_id}) will not be indexed in ElasticSearch as it does not exists anymore in database" + end + when /delete/ + begin + Client.delete index: Availability.index_name, type: Availability.document_type, id: record_id + rescue Elasticsearch::Transport::Transport::Errors::NotFound + STDERR.puts "Availability id(#{record_id}) will not be deleted form ElasticSearch as it has not been already indexed" + end + else raise ArgumentError, "Unknown operation '#{operation}'" end end end diff --git a/config/locales/devise.fr.yml b/config/locales/devise.fr.yml index a1477eb79..344dcf26d 100644 --- a/config/locales/devise.fr.yml +++ b/config/locales/devise.fr.yml @@ -3,7 +3,7 @@ fr: devise: confirmations: - confirmed: "Votre compte a été confirmé avec succès. Vous êtes désormais connecté(e)." + confirmed: "Votre compte a été confirmé avec succès." send_instructions: "Vous allez recevoir sous quelques minutes un e-mail comportant des instructions pour confirmer votre compte." send_paranoid_instructions: "Si votre e-mail existe sur notre base de données, vous recevrez sous quelques minutes un message avec des instructions pour confirmer votre compte." failure: diff --git a/db/migrate/20190603150642_create_statistic_profile.rb b/db/migrate/20190603150642_create_statistic_profile.rb index 79c990837..256bd66bb 100644 --- a/db/migrate/20190603150642_create_statistic_profile.rb +++ b/db/migrate/20190603150642_create_statistic_profile.rb @@ -10,5 +10,8 @@ class CreateStatisticProfile < ActiveRecord::Migration add_reference :reservations, :statistic_profile, index: true, foreign_key: true add_reference :subscriptions, :statistic_profile, index: true, foreign_key: true + + add_column :projects, :author_statistic_profile_id, :integer, index: true + add_foreign_key :projects, :statistic_profiles, column: :author_statistic_profile_id end end diff --git a/db/migrate/20190604070903_migrate_projet_to_statistic_profile.rb b/db/migrate/20190604070903_migrate_projet_to_statistic_profile.rb new file mode 100644 index 000000000..945ea2610 --- /dev/null +++ b/db/migrate/20190604070903_migrate_projet_to_statistic_profile.rb @@ -0,0 +1,19 @@ +class MigrateProjetToStatisticProfile < ActiveRecord::Migration + def up + Project.all.each do |p| + author = User.find(p.author_id) + p.update_column( + 'author_statistic_profile_id', author.statistic_profile.id + ) + end + end + + def down + Project.all.each do |p| + statistic_profile = User.find(p.author_statistic_profile_id) + p.update_column( + 'author_id', statistic_profile.user_id + ) + end + end +end diff --git a/db/migrate/20190604075717_remove_statistic_columns.rb b/db/migrate/20190604075717_remove_statistic_columns.rb index 20c64033a..edd95b978 100644 --- a/db/migrate/20190604075717_remove_statistic_columns.rb +++ b/db/migrate/20190604075717_remove_statistic_columns.rb @@ -4,5 +4,6 @@ class RemoveStatisticColumns < ActiveRecord::Migration remove_column :profiles, :birthday, :date remove_column :reservations, :user_id remove_column :subscriptions, :user_id + remove_column :projects, :author_id end end diff --git a/db/migrate/20190606074050_migrate_user_trainings_to_statistic_profile_trainings.rb b/db/migrate/20190606074050_migrate_user_trainings_to_statistic_profile_trainings.rb index e25b3955c..b500142d0 100644 --- a/db/migrate/20190606074050_migrate_user_trainings_to_statistic_profile_trainings.rb +++ b/db/migrate/20190606074050_migrate_user_trainings_to_statistic_profile_trainings.rb @@ -5,16 +5,28 @@ class MigrateUserTrainingsToStatisticProfileTrainings < ActiveRecord::Migration user_trainings.each do |ut| user = User.find(ut['user_id']) # here we use raw sql to prevent the notify_user callback the email the whole DB - execute("INSERT INTO statistic_profile_trainings (statistic_profile_id, training_id, created_at, updated_at) - VALUES (#{user.statistic_profile.id}, #{ut['training_id']}, '#{ut['created_at'].utc}', '#{DateTime.now.utc}')") + spt_id = insert("INSERT INTO statistic_profile_trainings (statistic_profile_id, training_id, created_at, updated_at) + VALUES (#{user.statistic_profile.id}, #{ut['training_id']}, '#{ut['created_at']}', '#{DateTime.now.utc}')") + + # update notifications + execute("UPDATE notifications SET + attached_object_type = 'StatisticProfileTraining', + attached_object_id = #{spt_id}, + updated_at = '#{DateTime.now.utc}' + WHERE attached_object_id = #{ut['id']} AND attached_object_type = 'UserTraining'") end end def down StatisticProfileTraining.all.each do |spt| statistic_profile = StatisticProfile.find(spt.statistic_profile_id) - execute("INSERT INTO user_trainings (user_id, training_id, created_at, updated_at) - VALUES (#{statistic_profile.user_id}, #{spt.training_id}, '#{spt.created_at.utc}', '#{DateTime.now.utc}')") + ut_id = execute("INSERT INTO user_trainings (user_id, training_id, created_at, updated_at) + VALUES (#{statistic_profile.user_id}, #{spt.training_id}, '#{spt.created_at.utc}', '#{DateTime.now.utc}')") + execute("UPDATE notifications SET + attached_object_type = 'UserTraining', + attached_object_id = #{ut_id}, + updated_at = '#{DateTime.now.utc}' + WHERE attached_object_id = #{spt.id} AND attached_object_type = 'StatisticProfileTraining'") end end end diff --git a/db/schema.rb b/db/schema.rb index 026c62216..e5cdc7b68 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -496,16 +496,16 @@ ActiveRecord::Schema.define(version: 20190606074801) do add_index "project_users", ["user_id"], name: "index_project_users_on_user_id", using: :btree create_table "projects", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name", limit: 255 t.text "description" t.datetime "created_at" t.datetime "updated_at" - t.integer "author_id" t.text "tags" t.integer "licence_id" - t.string "state", limit: 255 - t.string "slug", limit: 255 + t.string "state", limit: 255 + t.string "slug", limit: 255 t.datetime "published_at" + t.integer "author_statistic_profile_id" end add_index "projects", ["slug"], name: "index_projects_on_slug", using: :btree @@ -914,6 +914,7 @@ ActiveRecord::Schema.define(version: 20190606074801) do add_foreign_key "organizations", "invoicing_profiles" add_foreign_key "prices", "groups" add_foreign_key "prices", "plans" + add_foreign_key "projects", "statistic_profiles", column: "author_statistic_profile_id" add_foreign_key "projects_spaces", "projects" add_foreign_key "projects_spaces", "spaces" add_foreign_key "reservations", "statistic_profiles" diff --git a/test/fixtures/projects.yml b/test/fixtures/projects.yml index d371acfd9..07e635e93 100644 --- a/test/fixtures/projects.yml +++ b/test/fixtures/projects.yml @@ -6,7 +6,7 @@ project_1: pommes de terre ou d'outres légumes afin de confectionner de la purée.

" created_at: 2016-04-04 15:39:08.224918000 Z updated_at: 2016-04-04 15:39:08.224918000 Z - author_id: 5 + author_statistic_profile_id: 5 tags: cuisine licence_id: 1 state: published