klastera 1.2.4.1 → 1.2.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 149afa24eba1225396134f74b791f7d1e960480af16f5785d7f1a76570eea5d3
4
- data.tar.gz: e801acbf180a965cf9a922bfc0e7ad3006df21a780b7d9cbb279cad0c33a4f22
3
+ metadata.gz: 8780a6928f5249deb34be42cac1f52a226113865b9f29ddb12e1a36f018141ed
4
+ data.tar.gz: 1d99b709cea49a08533192ad8a1df621d78ea4d4ee59e1c266f06e952e6d8063
5
5
  SHA512:
6
- metadata.gz: e43ab98889edd3d9ece42a1f496bfc614ee77ac4721344694adf1e084d05192bcc97a43e69be19247095d9b18c3131a5d86d123be9a69ef85544965af6bb419b
7
- data.tar.gz: e994dfd1ae541212674faed964c526a38ee3ef14020af970893f54f04d054bf6b9abe36822cf5456ade0cb47a5570576c0cd790604b5d0e70ef71e41f12ae039
6
+ metadata.gz: 62c72441e00d9edb6c17525453cde71d864389c9342dbe54df3538609814aef0cdcbc3f7c49f49134d526e2da1f99ff1cc258b750cd024a7031afd3a80977d2d
7
+ data.tar.gz: 806978a3fae34b0a2ddda331ce37b90de2e66f9c77bd707859b721ac7c83980f706b5eb432a00875db8b26b4c8901b11307ef62ab063952d013eef30eca69772
@@ -8,7 +8,7 @@ module Klastera
8
8
  KLSTR_HELPERS = %i[
9
9
  cluster_user cluster_organization user_has_more_than_one_cluster
10
10
  cluster_scope cluster_clusters cluster_scope_filtered clusters_from
11
- user_clusters_string_list
11
+ user_clusters_string_list set_collection_before_group_by_entity
12
12
  ]
13
13
 
14
14
  class << self
@@ -36,13 +36,15 @@ module Klastera
36
36
  end
37
37
 
38
38
  #
39
- # I like to wrap methods
39
+ # In cases you don't have the active_record_collection object to filter, this method helps you
40
+ # calling cluster_scope! before and pass the collction to filter_clusterized_collection_with!,
41
+ # but at the end of the day, I just like to wrap methods.
40
42
  #
41
- def cluster_scope_filtered!(scope,cluster_id,user,organization,includes=[])
43
+ def cluster_scope_filtered!(scope,cluster_id,user,organization)
42
44
  self.filter_clusterized_collection_with!(
43
- organization,
44
45
  cluster_id,
45
- self.cluster_scope!(user,organization,scope,includes)
46
+ self.cluster_scope!(scope,user,organization),
47
+ organization
46
48
  )
47
49
  end
48
50
 
@@ -50,61 +52,75 @@ module Klastera
50
52
  # In order to this works, active_record_collection argument
51
53
  # should be passed through cluster_scope! method before.
52
54
  #
53
- def filter_clusterized_collection_with!(cluster_organization,cluster_id,active_record_collection)
55
+ def filter_clusterized_collection_with!(cluster_id,active_record_collection,cluster_organization)
54
56
  if cluster_organization.is_in_cluster_mode?
57
+
58
+ # IMPORTANT
59
+ # The next block was commented on because cluster_scope! method is not returning
60
+ # the cluster_entities within the scope anymore and it doesn't make any sense trying
61
+ # to filter based on force/use/show logic. Nevertheless, someday we will need it again.
62
+
63
+ # # If cluster_id is nil we will try to filter including unclustered entities active_record_collection wsas
64
+ # unless cluster_id.present?
65
+ # # Based on force/use/show definition we don't really need this validation. A force cluster organization won't return an entity that
66
+ # # doesn't belong to a cluster. Nevertheless we don't know if active_record_collection argument is fully definition-compliant.
67
+ # if cluster_organization.optional_suborganization_mode?
68
+ # cluster_array << nil
69
+ # end
70
+ # end
71
+
55
72
  if cluster_id.present?
56
- cluster_array = [cluster_id]
57
- # Based on force/use/show definition we don't really need this validation.
58
- # A force cluster organization won't return an entity out of a cluster, but
59
- # we don't know if the clusterized entities are fully definition-compliant.
60
- cluster_array << nil if cluster_organization.optional_suborganization_mode?
73
+ cluster_array = [cluster_id] unless cluster_id.is_a?(Array)
74
+ # The ActiveRecordCollection argument should have previously eager-loaded the cluster entities, thus the join statement is unnecessary.
61
75
  active_record_collection = active_record_collection.joins(:cluster_entities).where("cluster_entities.cluster_id": cluster_array)
62
76
  end
63
- # you may use a block only with clusterizable data
77
+
78
+ # You should use a block with clusterable data only
64
79
  yield(active_record_collection) if block_given?
65
80
  end
66
81
  active_record_collection
67
82
  end
68
83
 
69
- #
70
- #
71
- #
72
- def set_collection_before_group_by_entity!(cluster_organization,active_record_collection,model_class,entity_params)
84
+ ##
85
+ #
86
+ #
87
+ ##
88
+ def set_collection_before_group_by_entity!(active_record_collection,entity_params,cluster_organization)
73
89
  entity_params_keys = [:entity_name,:entity_attribute,:entity_id,:entity_id_attribute,:unamed]
90
+ entity_params[:entity_id] ||= nil #Ensures the entity_id attribute presence even if there is no filter
74
91
  entity_params = entity_params.slice(*entity_params_keys).values
75
- if model_class.try(:reflections).try(:keys).try(:include?,entity_params[0])
92
+ model_class = active_record_collection.model.base_class
93
+ model_relations = model_class.reflections.keys
94
+ if model_relations.include?(entity_params[0])
76
95
  entity_params << "#{entity_params[0]}_id".to_sym
77
96
  if entity_params[0] == 'cluster'
78
97
  entity_params << :unclustered if cluster_organization.is_in_cluster_mode?
79
98
  active_record_collection = Klastera.filter_clusterized_collection_with!(
80
- cluster_organization,
81
99
  entity_params[2],
82
- active_record_collection
100
+ active_record_collection,
101
+ cluster_organization
83
102
  )
84
103
  end
85
- yield(
86
- active_record_collection,
87
- entity_params_keys.zip(entity_params).to_h)
104
+ yield( active_record_collection, entity_params_keys.zip(entity_params).to_h )
88
105
  end
89
106
  end
90
107
 
91
108
  ##
92
109
  # Returns a ::Cluster::ActiveRecord_Relation from a given scope
93
110
  #
94
- def clusters_from!(user,organization,scope,includes=[])
95
- cluster_scope!(user,organization,scope,includes).related_clusters
111
+ def clusters_from!(scope,user,organization)
112
+ cluster_scope!(scope,user,organization).related_clusters
96
113
  end
97
114
 
98
115
  ##
99
116
  # Returns a scope filtered by clusters or its
100
117
  # organization if the cluster mode is not active.
101
118
  #
102
- def cluster_scope!(user,organization,scope,includes=[])
119
+ def cluster_scope!(scope,user,organization)
103
120
  scope_klass = scope_class(scope).where(organization_id: organization)
104
121
  session_clusters(user,organization) do |clusters|
105
122
  if organization.is_in_cluster_mode?
106
123
  scope_klass = scope_klass.select("DISTINCT ON (#{scope.table_name}.id) #{scope.table_name}.id, #{scope.table_name}.*")
107
- scope_klass = scope_klass.includes(includes) if includes.present?
108
124
  cluster_ids = clusters.map(&:id)
109
125
  if organization.required_suborganization_mode?
110
126
  scope_klass = scope_klass.joins(:cluster_entities).where( cluster_entities: { cluster_id: cluster_ids } )
@@ -117,7 +133,7 @@ module Klastera
117
133
  ").where("cluster_entities.id IS NULL#{or_these_cluster_ids}")
118
134
  end
119
135
  # Provisional fix to avoid SQL clashes due to DISTINCT ON clause
120
- scope_klass = scope_class(scope).where(organization_id: organization).where(id: scope_klass.map(&:id))
136
+ scope_klass = scope_class(scope).eager_load(:cluster_entities).where(id: scope_klass.map(&:id), organization_id: organization)
121
137
  end
122
138
  end
123
139
  scope_klass
@@ -210,26 +226,29 @@ module Klastera
210
226
  [ :cluster_id ].concat( ::ClusterFilter.attributes )
211
227
  end
212
228
 
213
- def cluster_scope(scope,includes=[])
214
- Klastera.cluster_scope!(cluster_user,cluster_organization,scope,includes)
229
+ def filter_clusterized_collection_with(cluster_id,active_record_collection)
230
+ Klastera.filter_clusterized_collection_with!(cluster_id,active_record_collection,cluster_organization)
231
+ end
232
+
233
+ def cluster_scope(scope)
234
+ Klastera.cluster_scope!(scope,cluster_user,cluster_organization)
215
235
  end
216
236
 
217
237
  def cluster_clusters
218
238
  Klastera.session_clusters(cluster_user,cluster_organization)
219
239
  end
220
240
 
221
- def cluster_scope_filtered(scope,cluster_id,includes=[])
241
+ def cluster_scope_filtered(scope,cluster_id)
222
242
  Klastera.cluster_scope_filtered!(
223
243
  scope,
224
244
  cluster_id,
225
245
  cluster_user,
226
- cluster_organization,
227
- includes
246
+ cluster_organization
228
247
  )
229
248
  end
230
249
 
231
- def clusters_from(scope,includes=[])
232
- Klastera.clusters_from!(cluster_user,cluster_organization,scope,includes)
250
+ def clusters_from(scope)
251
+ Klastera.clusters_from!(scope,cluster_user,cluster_organization)
233
252
  end
234
253
 
235
254
  def user_clusters_string_list(object_entity,separator,attribute=:name)
@@ -242,6 +261,10 @@ module Klastera
242
261
  )
243
262
  end
244
263
 
264
+ def set_collection_before_group_by_entity(active_record_collection,entity_params,&block)
265
+ Klastera.set_collection_before_group_by_entity!(active_record_collection,params,cluster_organization,&block)
266
+ end
267
+
245
268
  included do
246
269
  Klastera::KLSTR_HELPERS.each do |action|
247
270
  if respond_to?(:helper_method)
@@ -1,3 +1,3 @@
1
1
  module Klastera
2
- VERSION = "1.2.4.1"
2
+ VERSION = "1.2.4.2"
3
3
  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.2.4.1
4
+ version: 1.2.4.2
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-06-12 00:00:00.000000000 Z
11
+ date: 2020-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails