klastera 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bd206c969548ea6d299cb57564aa67e9f7b88a5ba753ad5d0c112b7670ad4f0
4
- data.tar.gz: 62580be564273b2a461808c9edbe2e9ef6c1cefa88f8b813ec9d2d02441a0836
3
+ metadata.gz: 3d4f51a31ad550a707ac854779af0d6335062752af2bf79eeb41e23eefc0ddce
4
+ data.tar.gz: 4abd8e21893258c0dd037d7596168a9266247441be74a751c0d6ee8ae2231796
5
5
  SHA512:
6
- metadata.gz: d5f9073b9968cab43d2a88be5d1bcb9ac4cb6254670b480ac2adeadbe9c00bef78cd35a022f52626c75157844f9ad428c26afaab33b73b55e51b1bcfd0f0c682
7
- data.tar.gz: 900d61f55072bfc19110dc4efda73c996dd7ec57e327165279e2c15b5eb86352c1f1aa98a28130b2b1f87a7d753a7891b388badee4a7771e6249fd758b662f5e
6
+ metadata.gz: 29c527c10bbb047607183891488a773655ff8d394ca28961e9745dd067cf77f0a8aa6012f096d334c3209a70e4adb1359b0623b4f3a28dd3d9dca08594328783
7
+ data.tar.gz: d55827e7deaa63de06c32a27a0ece3df1217d8eb4a553e1cdedee6b0efd882d33c77e9bcc9a574e89f0c7c7ae4ba4c7380c1ac74e3db79eef5f7892b4c6d62b4
@@ -11,10 +11,48 @@ module Klastera::Concerns::ClusterUser
11
11
  validates :cluster_id, presence: true
12
12
 
13
13
  scope :of, -> (user,organization) {
14
+ Rails.logger.warn("DON'T USE THIS SCOPE DIRECTLY: Use ::ClusterUser.get_from class method instead!")
14
15
  includes(:cluster).where(user_id: user, "clusters.organization_id": organization)
15
16
  }
16
17
  end
17
18
 
18
19
  module ClassMethods
20
+
21
+ ##
22
+ # Returns a hash with the users with cluster relation
23
+ #
24
+ def get_user_clusters_from(organization)
25
+ users_hash = {}
26
+ organization_cluster_ids = ::Cluster.of(organization).map(&:id)
27
+ ::ClusterUser.includes(:user).where(cluster_id: organization_cluster_ids).each do |cluster|
28
+ user_id = cluster.user.id
29
+ users_hash[user_id] ||= [];
30
+ users_hash[user_id] << cluster.id
31
+ end
32
+ users_hash
33
+ end
34
+
35
+ ##
36
+ # Returns a Cluster::ActiveRecord_Relation
37
+ #
38
+ ##
39
+ def get_clusters_from(user,organization)
40
+ clusters = []
41
+ unless user.can_admin_clusters?
42
+ # We could return cu.clusters but you may quering this result and a array can't handle order, where and etc.
43
+ # So we take only the cluster_ids and perfome a new search in order to get a Cluster::ActiveRecord_Relation
44
+ # like the else block.
45
+ # TODO: resolve it without quering twice
46
+ clusters_id = ::ClusterUser.of(user,organization).map(&:cluster_id)
47
+ clusters = ::Cluster.where(id: clusters_id)
48
+ # Add a empty cluster instance to handle models without a cluster assignation. Only for use and show modes
49
+ if organization.optional_mode?
50
+ clusters << ::Cluster.new({nid: :without_cluster, name: I18n.t('klastera.without_cluster')})
51
+ end
52
+ else
53
+ clusters = ::Cluster.of(organization)
54
+ end
55
+ clusters
56
+ end
19
57
  end
20
58
  end
@@ -3,7 +3,12 @@ module Klastera::Concerns::Clusterizable
3
3
  included do
4
4
  attr_accessor :lonely_cluster
5
5
  belongs_to :cluster
6
+
6
7
  validates :cluster_id, presence: true, if: proc { self.organization.required_suborganization_mode? }
8
+
9
+ scope :related_clusters, ->() {
10
+ includes(:cluster).where("#{self.table_name}.cluster_id IS NOT NULL")
11
+ }
7
12
  end
8
13
 
9
14
  module ClassMethods
@@ -4,7 +4,7 @@
4
4
  %>
5
5
  <% if cluster_organization.is_in_cluster_mode? || other_visibility_reason %>
6
6
  <%
7
- cluster_collection = cluster_clusters
7
+ cluster_collection = @cluster_clusters || cluster_clusters
8
8
  label = t('klastera.cluster.title')
9
9
  %>
10
10
  <% if f.nil? %>
@@ -1,3 +1,3 @@
1
1
  module Klastera
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
data/lib/klastera.rb CHANGED
@@ -7,6 +7,21 @@ module Klastera
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  class << self
10
+
11
+ ##
12
+ # Returns a ::Cluster::ActiveRecord_Relation from a given scope
13
+ #
14
+ def clusters_from!(user,organization,scope,includes=[])
15
+ _scope_result = scope.respond_to?(:map) ? scope : cluster_scope!(user,organization,scope,includes)
16
+ ::Cluster.where(id: _scope_result.related_clusters.map(&:cluster_id))
17
+ end
18
+
19
+ ##
20
+ # Returns a scope with filtered by clusters or
21
+ # the scope filterd its organization if
22
+ # the organization haven't active the cluster mode
23
+ #
24
+ #
10
25
  def cluster_scope!(user,organization,scope,includes=[])
11
26
  scope_klass = scope_class(scope)
12
27
  raise RelatedClusterEntityComplianceError, "attribute cluster_id was not found in #{scope_klass}" unless scope_klass.try(:column_names).try(:include?,'cluster_id')
@@ -15,15 +30,13 @@ module Klastera
15
30
 
16
31
  session_clusters(user,organization) do |clusters|
17
32
  clusters = clusters.reject{|c|c.id.blank?}
18
-
19
33
  if organization.required_suborganization_mode?
20
34
  scope_klass = scope_klass.where("#{scope.table_name}.cluster_id IN (?)",clusters)
21
- elsif organization.optional_mode?
35
+ elsif organization.optional_mode? && user.is_a_cluster_user?
22
36
  with_clusters = clusters.present? ? "#{scope.table_name}.cluster_id IN (#{clusters.map(&:id).join(',')})" : nil
23
37
  without_clusters = "#{with_clusters.blank? ? '' : ' OR ' }#{scope.table_name}.cluster_id IS NULL"
24
38
  scope_klass = scope_klass.where("#{with_clusters}#{without_clusters}")
25
39
  end
26
-
27
40
  end
28
41
 
29
42
  scope_klass
@@ -40,23 +53,15 @@ module Klastera
40
53
  end
41
54
 
42
55
  ##
56
+ # Returns a cluster array if organization is using the cluster mode
43
57
  #
58
+ # Use this only to get current_user/organization clusters
59
+ # understanding this wont be useful out of a cluster mode context.
44
60
  #
45
61
  ##
46
62
  def session_clusters(user,organization)
47
- clusters = []
48
- if organization.is_in_cluster_mode?
49
- unless user.can_admin_clusters?
50
- # TODO: 1: return a Cluster::ActiveRecord_Relation and then remove to_a in ::Cluster.of line
51
- # TODO: 2: fix call to ::Cluster instead of Klastera::Cluster
52
- clusters = ::ClusterUser.of(user,organization).map{|cu|::Cluster.find(cu.cluster_id)}
53
- clusters << ::Cluster.new({nid: :without_cluster, name: I18n.t('klastera.without_cluster')}) if organization.optional_mode?
54
- else
55
- # TODO: remove "to_a" after ClusterUser.of result became a Cluster::ActiveRecord_Relation object
56
- clusters = ::Cluster.of(organization).to_a
57
- end
58
- yield(clusters) if block_given?
59
- end
63
+ clusters = organization.is_in_cluster_mode? ? ::ClusterUser.get_clusters_from(user,organization) : []
64
+ yield(clusters) if block_given?
60
65
  clusters
61
66
  end
62
67
  end
@@ -69,6 +74,8 @@ module Klastera
69
74
  helper_method :cluster_organization
70
75
  helper_method :cluster_clusters
71
76
  # helper_method :cluster_clusters_ids
77
+ helper_method :user_has_more_than_one_cluster
78
+ helper_method :clusters_from
72
79
  end
73
80
  if respond_to?(:hide_action)
74
81
  hide_action :cluster_scope
@@ -83,16 +90,20 @@ module Klastera
83
90
  hide_action :cluster_clusters=
84
91
  # hide_action :cluster_clusters_ids
85
92
  # hide_action :cluster_clusters_ids=
93
+ hide_action :user_has_more_than_one_cluster
94
+ hide_action :user_has_more_than_one_cluster=
95
+ hide_action :clusters_from
96
+ hide_action :clusters_from=
86
97
  end
87
98
  before_action :set_the_lonely_cluster, only: %i[ create update ]
88
99
  end
89
100
 
90
101
  def set_the_lonely_cluster
91
- form_model = params[:controller].singularize
102
+ form_model = @form_record ? model_name_from_record_or_class(@form_record).param_key : params[:controller].singularize
92
103
  parameters = params.require( form_model ) rescue nil
93
104
  lonely_cluster = parameters.blank? ? false : parameters.permit( :lonely_cluster ).present?
94
105
  if lonely_cluster
95
- params[form_model][:cluster_id] = ::ClusterUser.of(cluster_user,cluster_organization).first.try(:cluster_id)
106
+ params[form_model][:cluster_id] = cluster_clusters.first.try(:id)
96
107
  end
97
108
  end
98
109
 
@@ -116,6 +127,15 @@ module Klastera
116
127
  # cluster_clusters.map(&:id)
117
128
  # end
118
129
 
130
+ def user_has_more_than_one_cluster
131
+ @cluster_clusters ||= cluster_clusters
132
+ @cluster_clusters.size > 1
133
+ end
134
+
135
+ def clusters_from(scope,includes=[])
136
+ Klastera.clusters_from!(cluster_user,cluster_organization,scope,includes)
137
+ end
138
+
119
139
  def set_cluster_filter
120
140
  @filter = ::ClusterFilter.new(cluster_filter_params)
121
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klastera
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gino Barahona
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-08 00:00:00.000000000 Z
11
+ date: 2020-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails