cm-admin 4.0.0 → 4.1.0

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: 758deeaf34976fadccc5e68a438f0ba2cc3fe7f519431ffe750c740c162b92c7
4
- data.tar.gz: 770661bd71b01b11ec2f50c4a88adf8a182d6d082e0b8415ef099df8559e8279
3
+ metadata.gz: 19fd7ec610f3f8b02370549dfac1e314ece5c44f57466727666058c83f4f5905
4
+ data.tar.gz: fe9a9c73585456a365364b1e44305e229a242a19bf832da849117bc1fb3f47a6
5
5
  SHA512:
6
- metadata.gz: e87eb00d34c7c2e601078d815d6137ef4eddb22d543294d33301a102b3f9ff2931f46e353a832db435441df81cb32d198d06824bd8cef82984440fc0ef5186d5
7
- data.tar.gz: 594fcf9d069ef492dfb469e22cb4b609ea3b6d4c6acbf1404080c3d9e8b18ed2fa416ef2122a7035c9a53dc8dc915ae6724a768e1aa74fec16bd5735c9879cef
6
+ metadata.gz: 8fef781ecc5b1bfc28f04fdc4bbdea3c6463cddb39aa4cddddfcb84d359af8528ce77c8f33eb98a505e95214c468883c89185f61cb7b320b614c91813f1bef10
7
+ data.tar.gz: '068f711e3e85d6019af1d40256c1bff7a11fd69016e11be5285e48598d13dd652d1ae7447cae179279063f2f7edd1f85398f4a140a83bf41bc494c4332d1318c'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cm-admin (4.0.0)
4
+ cm-admin (4.1.0)
5
5
  caxlsx_rails
6
6
  cocoon (~> 1.2.15)
7
7
  csv-importer (~> 0.8.2)
@@ -6,12 +6,19 @@ module CmAdmin
6
6
  helper CmAdmin::ViewHelpers
7
7
 
8
8
  skip_before_action :verify_authenticity_token, only: :reset_sort_columns
9
+ before_action :set_current_user_permission
10
+
11
+
12
+ def set_current_user_permission
13
+ CmCurrent.user_permissions = Current.user.cm_role.cm_permissions if Current.user.cm_role.present?
14
+ end
9
15
 
10
16
  def cm_index(params)
11
17
  @current_action = CmAdmin::Models::Action.find_by(@model, name: 'index')
12
18
  # Based on the params the filter and pagination object to be set
13
19
  authorize @ar_object, policy_class: "CmAdmin::#{controller_name.classify}Policy".constantize if defined? "CmAdmin::#{controller_name.classify}Policy".constantize
14
20
  records = "CmAdmin::#{@model.name}Policy::IndexScope".constantize.new(Current.user, @model.name.constantize).resolve
21
+ records = records.includes(@current_action.eager_load_associations) if @current_action.eager_load_associations.present?
15
22
  records = apply_scopes(records)
16
23
  @ar_object = if %w[table card].include?(params[:view_type]) || %i[table card].include?(@current_action.view_type)
17
24
  filter_by(params, records, filter_params: @model.filter_params(params))
@@ -163,6 +170,7 @@ module CmAdmin
163
170
  @current_action = @action
164
171
  if @action.parent == 'index'
165
172
  records = apply_scopes(records)
173
+ records = records.includes(@model.eager_load_associations) if @model.eager_load_associations.present?
166
174
  @ar_object = filter_by(params, records, filter_params: @model.filter_params(params))
167
175
  else
168
176
  resource_identifier
@@ -304,6 +312,7 @@ module CmAdmin
304
312
  return @ar_object unless @current_action.child_records
305
313
 
306
314
  child_records = @ar_object.send(@current_action.child_records)
315
+ child_records = child_records.includes(@current_action.eager_load_associations) if @current_action.eager_load_associations.present?
307
316
  child_records = apply_scopes(child_records)
308
317
  @reflection = @model.ar_model.reflect_on_association(@current_action.child_records)
309
318
  @associated_model = if @reflection.klass.column_names.include?('type')
@@ -0,0 +1,14 @@
1
+ module CmAdmin
2
+ module PermissionHelper
3
+
4
+ # Used in mode.rb while creating policies.
5
+ def has_access_to?(ar_model, action)
6
+ find_permission_by(ar_model, action).present?
7
+ end
8
+
9
+ # Checks if the current user permission has access to the specified model and action
10
+ def find_permission_by(ar_model, action)
11
+ CmCurrent.user_permissions.find { |permission| permission.ar_model_name == ar_model.name && permission.action_name == action.name }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class CmCurrent < ActiveSupport::CurrentAttributes
2
+ attribute :user_permissions
3
+ end
@@ -19,4 +19,5 @@ class CmPermission < ApplicationRecord
19
19
  CmPermission.where(action_name: 'update', ar_model_name:, cm_role_id:).first&.destroy
20
20
  end
21
21
  end
22
+
22
23
  end
@@ -188,12 +188,14 @@ module CmAdmin
188
188
  def define_pundit_policy(ar_model)
189
189
  if $available_actions.present?
190
190
  klass = Class.new(ApplicationPolicy) do
191
+ include CmAdmin::PermissionHelper
191
192
  $available_actions.each do |action|
192
193
  define_method "#{action.name}?".to_sym do
193
194
  return false unless Current.user.respond_to?(:cm_role_id)
194
195
  return false if Current.user.cm_role.nil?
196
+ return false if CmCurrent.user_permissions.empty?
195
197
 
196
- Current.user.cm_role.cm_permissions.where(action_name: action.name, ar_model_name: ar_model.name).present?
198
+ has_access_to?(ar_model, action)
197
199
  end
198
200
  end
199
201
  end
@@ -211,7 +213,7 @@ module CmAdmin
211
213
 
212
214
  define_method :resolve do
213
215
  # action_name = Current.request_params.dig("action")
214
- permission = Current.user.cm_role.cm_permissions.find_by(action_name: action.name, ar_model_name: ar_model.name)
216
+ permission = find_permission_by(ar_model, action)
215
217
  if permission.present? && permission.scope_name.present?
216
218
  scope.send(permission.scope_name)
217
219
  else
@@ -7,7 +7,7 @@ module CmAdmin
7
7
  attr_accessor :name, :display_name, :verb, :layout_type, :layout, :partial, :path, :page_title, :page_description,
8
8
  :child_records, :is_nested_field, :nested_table_name, :parent, :display_if, :route_type, :code_block,
9
9
  :display_type, :action_type, :redirection_url, :sort_direction, :sort_column, :icon_name, :scopes, :view_type,
10
- :kanban_attr, :model_name, :redirect_to, :execution_mode
10
+ :kanban_attr, :model_name, :redirect_to, :execution_mode, :eager_load_associations
11
11
 
12
12
  VALID_SORT_DIRECTION = Set[:asc, :desc].freeze
13
13
 
@@ -41,6 +41,7 @@ module CmAdmin
41
41
  self.route_type = nil
42
42
  self.display_type = nil
43
43
  self.view_type = :table
44
+ self.eager_load_associations = []
44
45
  self.kanban_attr = {}
45
46
  end
46
47
 
@@ -402,6 +402,17 @@ module CmAdmin
402
402
  @default_sort_column = default_column[:column]
403
403
  @default_sort_direction = default_column[:default_direction] if default_column[:default_direction].present?
404
404
  end
405
+
406
+ # Configure Eager load associations for action
407
+ # This will help us avoid N+1 queries
408
+ #
409
+ # @example Eager load associations
410
+ # eager_load_assocations [:association_name]
411
+ # eager_load_assocations [:constant]
412
+ #
413
+ def eager_load_associations(associations=[])
414
+ @current_action.eager_load_associations = associations if @current_action
415
+ end
405
416
  end
406
417
  end
407
418
  end
@@ -1,3 +1,3 @@
1
1
  module CmAdmin
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cm-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: exe
16
16
  cert_chain: []
17
- date: 2024-11-25 00:00:00.000000000 Z
17
+ date: 2024-11-26 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: caxlsx_rails
@@ -344,7 +344,9 @@ files:
344
344
  - app/controllers/cm_admin/static_controller.rb
345
345
  - app/helpers/cm_admin/application_helper.rb
346
346
  - app/helpers/cm_admin/custom_helper.rb
347
+ - app/helpers/cm_admin/permission_helper.rb
347
348
  - app/jobs/file_import_processor_job.rb
349
+ - app/models/cm_current.rb
348
350
  - app/models/cm_permission.rb
349
351
  - app/models/cm_role.rb
350
352
  - app/models/concerns/cm_admin/bulk_action_processor.rb