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| or_without_cluster = organization.required_suborganization_mode? ? "" : " OR cluster_id IS NULL " scope_klass = scope_klass.where("cluster_id IN (?)#{or_without_cluster}",clusters.map{|c|c.id}) 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: return a Cluster::ActiveRecord_Relation and then remove to_a in ::Cluster.of line clusters = ::ClusterUser.of(user,organization).map{|cu|cu.cluster} 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 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= 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).map{|c|[c.name,c.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