require "klastera/engine" module Klastera mattr_accessor :organization_class class RelatedClusterEntityComplianceError < StandardError; end extend ActiveSupport::Concern class << self def cluster_scope!(user,organization,scope,includes=[]) scope_klass = scope_class(scope) raise RelatedClusterEntityComplianceError, "attribute cluster_id was not found in #{scope_klass}" unless scope_klass.try(:column_names).try(:include?,'cluster_id') scope_klass = scope_klass.includes(includes) if includes.present? scope_klass = scope_klass.where(organization_id: organization) session_clusters(user,organization) do |clusters| clusters = clusters.reject{|c|c.id.blank?} with_clusters = nil; without_clusters = nil if organization.required_suborganization_mode? || clusters.present? with_clusters = "#{scope.table_name}.cluster_id IN (#{clusters.map(&:id).join(',')})" end if organization.optional_mode? without_clusters = "#{with_clusters.blank? ? '' : ' OR ' }#{scope.table_name}.cluster_id IS NULL" end scope_klass = scope_klass.where("#{with_clusters}#{without_clusters}") end scope_klass end ## # TODO: # Implement a validation to ensure that # object is a ActiveRecord::Base class # ## def scope_class(object) object end ## # # ## def session_clusters(user,organization) clusters = [] if organization.is_in_cluster_mode? unless user.can_admin_clusters? # TODO: 1: return a Cluster::ActiveRecord_Relation and then remove to_a in ::Cluster.of line # TODO: 2: fix call to ::Cluster instead of Klastera::Cluster clusters = ::ClusterUser.of(user,organization).map{|cu|::Cluster.find(cu.cluster_id)} clusters << ::Cluster.new({nid: :without_cluster, name: I18n.t('klastera.without_cluster')}) if organization.optional_mode? else # TODO: remove "to_a" after ClusterUser.of result became a Cluster::ActiveRecord_Relation object clusters = ::Cluster.of(organization).to_a end yield(clusters) if block_given? end clusters end end included do if respond_to?(:helper_method) helper_method :cluster_scope helper_method :user_cluster helper_method :cluster_user helper_method :cluster_organization helper_method :cluster_clusters # helper_method :cluster_clusters_ids end if respond_to?(:hide_action) hide_action :cluster_scope hide_action :cluster_scope= hide_action :user_cluster hide_action :user_cluster= hide_action :cluster_user hide_action :cluster_user= hide_action :cluster_organization hide_action :cluster_organization= hide_action :cluster_clusters hide_action :cluster_clusters= # hide_action :cluster_clusters_ids # hide_action :cluster_clusters_ids= end before_action :set_the_lonely_cluster, only: %i[ create update ] end def set_the_lonely_cluster form_model = params[:controller].singularize parameters = params.require( form_model ) rescue nil lonely_cluster = parameters.blank? ? false : parameters.permit( :lonely_cluster ).present? if lonely_cluster params[form_model][:cluster_id] = ::ClusterUser.of(cluster_user,cluster_organization).first.try(:cluster_id) end end def cluster_scope(scope,includes=[]) Klastera.cluster_scope!(cluster_user,cluster_organization,scope,includes) end def cluster_user current_user end def cluster_organization current_organization end def cluster_clusters Klastera.session_clusters(cluster_user,cluster_organization) end # def cluster_clusters_ids # cluster_clusters.map(&:id) # end def set_cluster_filter @filter = ::ClusterFilter.new(cluster_filter_params) end def cluster_filter_params parameters = params.require(:cluster_filter) rescue nil return {} if parameters.blank? parameters.permit(*cluster_filter_permit_params) end def cluster_filter_permit_params [ :cluster_id ].concat( ::ClusterFilter.attributes ) end end