cm-admin 0.6.1 → 0.6.2

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: 2e28d75f3c079c84bbf3e36a395b277390810ae103b87f2678b15c5c12210257
4
- data.tar.gz: f207f2665af3246e6f285c626dcfc23bd2f6160b893e665311e4e0babf42f919
3
+ metadata.gz: cb9d2bfcdbec704c6259f45f51e317e32ad05d5ca4990a52fca17af831f62566
4
+ data.tar.gz: 6a357cfe4458e4da3409c8e95e51cda3e36d6d8ebbd00c4518a6928f1815926e
5
5
  SHA512:
6
- metadata.gz: 2c1834f17fa138e419d25cd20140ee6e9b78594a3ce8d37f1834eb113f9e88def0b9532b09e3d4c0f0c3e5d1334ddb88d298274b4e70e12779d9e349732d215e
7
- data.tar.gz: 0e2781411e73d8ba757462451992f65292a47bb9d9042546a0b70486e7d38e6a60209bf4130ca08d47dc6617ecec75558f15f586c21c90187d998ba9ec74f699
6
+ metadata.gz: 242b5d01155399a9c9038b1a0811db95c82824ace0c9e4b592c98d9dae5a51b23cf28a80cb89db771f0d647685b5b5b9c83e0ac6774269eb0c88d4c90c792c83
7
+ data.tar.gz: 17cd6516e61f6e3ff3aa6fc7515807c3a5fb423d0fa016992aba51bbe2b2f40b885aa6e200c8aefa71b713dd2477fcb3bbb7f63e0ba7de96ca6e2351ae058356
@@ -7,5 +7,11 @@ module CmAdmin
7
7
  def current_webpacker_instance
8
8
  CmAdmin.webpacker
9
9
  end
10
+
11
+ # Allow if policy is not defined.
12
+ def has_valid_policy(model_name, action_name)
13
+ return true unless policy([:cm_admin, model_name.classify.constantize]).methods.include?(:"#{action_name}?")
14
+ policy([:cm_admin, model_name.classify.constantize]).send(:"#{action_name}?")
15
+ end
10
16
  end
11
17
  end
@@ -47,7 +47,7 @@
47
47
  = link_to "#{page_url('edit', ar_object)}" do
48
48
  .popup-option Edit
49
49
  - custom_actions.each do |custom_action|
50
- - if custom_action.name.present? && policy([:cm_admin, @model.name.classify.constantize]).send(:"#{custom_action.name}?")
50
+ - if custom_action.name.present? && has_valid_policy(@model.name, custom_action.name)
51
51
  - if custom_action.display_if.call(ar_object)
52
52
  = link_to custom_action.path.gsub(':id', ar_object.id.to_s), method: custom_action.verb do
53
53
  .popup-option = custom_action.name.titleize
@@ -24,14 +24,14 @@
24
24
  - if new_action.any? && policy([:cm_admin, @model.name.classify.constantize]).new?
25
25
  = link_to 'Add', "#{page_url('new')}", class: 'primary-btn ml-2'
26
26
  - @model.available_actions.select{|act| act if act.route_type == 'collection'}.each do |custom_action|
27
- - if custom_action.name.present? && policy([:cm_admin, @model.name.classify.constantize]).send(:"#{custom_action.name}?")
27
+ - if custom_action.name.present? && has_valid_policy(@model.name, custom_action.name)
28
28
  - if custom_action.display_type == :button
29
29
  = link_to custom_action.name.titleize, @model.ar_model.table_name + '/' + custom_action.path, class: 'primary-btn ml-2', method: custom_action.verb
30
30
  - elsif custom_action.display_type == :modal
31
31
  = link_to custom_action.name.titleize, '', class: 'primary-btn ml-2', data: { bs_toggle: "modal", bs_target: "##{custom_action.name.classify}Modal" }
32
32
  - elsif @model.current_action.name == 'show'
33
33
  - @model.available_actions.select{|act| act if act.route_type == 'member'}.each do |custom_action|
34
- - if custom_action.name.present? && policy([:cm_admin, @model.name.classify.constantize]).send(:"#{custom_action.name}?")
34
+ - if custom_action.name.present? && has_valid_policy(@model.name, custom_action.name)
35
35
  - if custom_action.display_type == :button && custom_action.display_if.call(@ar_object)
36
36
  = link_to custom_action.name.titleize, custom_action.path.gsub(':id', params[:id]), class: 'primary-btn ml-2', method: custom_action.verb
37
37
  - elsif custom_action.display_type == :modal && custom_action.display_if.call(@ar_object)
@@ -45,7 +45,6 @@ module CmAdmin
45
45
  actions unless @actions_set
46
46
  $available_actions = @available_actions.dup
47
47
  self.class.all_actions.push(@available_actions)
48
- define_policy
49
48
  define_controller
50
49
  end
51
50
 
@@ -101,23 +100,6 @@ module CmAdmin
101
100
 
102
101
  private
103
102
 
104
- def define_policy
105
- klass = Class.new(ApplicationPolicy) do
106
- def initialize(user, record)
107
- @user = user
108
- @record = record
109
- end
110
-
111
- $available_actions.each do |action|
112
- define_method :"#{action.name}?" do
113
- accessible_by = action.accessible_by.map { |role| "user.#{role}" }.join(' || ')
114
- eval(accessible_by)
115
- end
116
- end
117
- end
118
- CmAdmin.const_set "#{@name}Policy", klass
119
- end
120
-
121
103
  # Controller defined for each model
122
104
  # If model is User, controller will be UsersController
123
105
  def define_controller
@@ -135,7 +117,7 @@ module CmAdmin
135
117
  @model.current_action = @action
136
118
  @ar_object = @model.try(@action.parent || action_name, params)
137
119
  @ar_object, @associated_model, @associated_ar_object = @model.custom_controller_action(action_name, params.permit!) if !@ar_object.present? && params[:id].present?
138
- authorize controller_name.classify.constantize, policy_class: "CmAdmin::#{controller_name.classify}Policy".constantize
120
+ authorize controller_name.classify.constantize, policy_class: "CmAdmin::#{controller_name.classify}Policy".constantize if defined? "CmAdmin::#{controller_name.classify}Policy".constantize
139
121
  aar_model = request.url.split('/')[-2].classify.constantize if params[:aar_id]
140
122
  @associated_ar_object = aar_model.find(params[:aar_id]) if params[:aar_id]
141
123
  nested_tables = @model.available_fields[:new].except(:fields).keys
@@ -6,7 +6,7 @@ module CmAdmin
6
6
  include Actions::Blocks
7
7
  attr_accessor :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
- :display_type, :action_type, :redirection_url, :sort_direction, :sort_column, :accessible_by
9
+ :display_type, :action_type, :redirection_url, :sort_direction, :sort_column
10
10
 
11
11
  VALID_SORT_DIRECTION = Set[:asc, :desc].freeze
12
12
 
@@ -34,14 +34,12 @@ module CmAdmin
34
34
  self.action_type = :default
35
35
  self.sort_column = :created_at
36
36
  self.sort_direction = :desc
37
- self.accessible_by = CmAdmin.authorized_roles
38
37
  end
39
38
 
40
- def set_values(page_title, page_description, partial, accessible_by)
39
+ def set_values(page_title, page_description, partial)
41
40
  self.page_title = page_title
42
41
  self.page_description = page_description
43
42
  self.partial = partial
44
- self.accessible_by = (CmAdmin.authorized_roles.dup << accessible_by).flatten.compact.uniq if accessible_by
45
43
  end
46
44
 
47
45
  class << self
@@ -5,7 +5,8 @@ module CmAdmin
5
5
 
6
6
  def show(params)
7
7
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
8
- @ar_object = @ar_model.name.classify.constantize.find(params[:id])
8
+ scoped_model = "CmAdmin::#{self.name}Policy::Scope".constantize.new(Current.user, self.name.constantize).resolve
9
+ @ar_object = scoped_model.find(params[:id])
9
10
  end
10
11
 
11
12
  def index(params)
@@ -40,7 +41,8 @@ module CmAdmin
40
41
  sort_column = "created_at"
41
42
  sort_direction = %w[asc desc].include?(sort_params[:sort_direction]) ? sort_params[:sort_direction] : "asc"
42
43
  sort_params = {sort_column: sort_column, sort_direction: sort_direction}
43
- records = self.name.constantize.where(nil) if records.nil?
44
+
45
+ records = "CmAdmin::#{self.name}Policy::Scope".constantize.new(Current.user, self.name.constantize).resolve if records.nil?
44
46
  records = records.order("#{current_action.sort_column} #{current_action.sort_direction}")
45
47
 
46
48
  final_data = CmAdmin::Models::Filter.filtered_data(filter_params, records, @filters)
@@ -10,28 +10,28 @@ module CmAdmin
10
10
  # @current_action = CmAdmin::Models::CustomAction.find_by(self, name: name)
11
11
  end
12
12
 
13
- def cm_index(page_title: nil, page_description: nil, partial: nil, accessible_by: nil, &block)
13
+ def cm_index(page_title: nil, page_description: nil, partial: nil, &block)
14
14
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'index')
15
- @current_action.set_values(page_title, page_description, partial, accessible_by)
15
+ @current_action.set_values(page_title, page_description, partial)
16
16
  yield
17
17
  # action.instance_eval(&block)
18
18
  end
19
19
 
20
- def cm_show(page_title: nil, page_description: nil, partial: nil, accessible_by: nil, &block)
20
+ def cm_show(page_title: nil, page_description: nil, partial: nil, &block)
21
21
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
22
- @current_action.set_values(page_title, page_description, partial, accessible_by)
22
+ @current_action.set_values(page_title, page_description, partial)
23
23
  yield
24
24
  end
25
25
 
26
- def cm_edit(page_title: nil,page_description: nil, partial: nil, accessible_by: nil, &block)
26
+ def cm_edit(page_title: nil,page_description: nil, partial: nil, &block)
27
27
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'edit')
28
- @current_action.set_values(page_title, page_description, partial, accessible_by)
28
+ @current_action.set_values(page_title, page_description, partial)
29
29
  yield
30
30
  end
31
31
 
32
- def cm_new(page_title: nil,page_description: nil, partial: nil, accessible_by: nil, &block)
32
+ def cm_new(page_title: nil,page_description: nil, partial: nil, &block)
33
33
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'new')
34
- @current_action.set_values(page_title, page_description, partial, accessible_by)
34
+ @current_action.set_values(page_title, page_description, partial)
35
35
  yield
36
36
  end
37
37
 
@@ -47,16 +47,14 @@ module CmAdmin
47
47
  end
48
48
  end
49
49
 
50
- def tab(tab_name, custom_action, associated_model: nil, layout_type: nil, layout: nil, partial: nil, accessible_by: nil, &block)
50
+ def tab(tab_name, custom_action, associated_model: nil, layout_type: nil, layout: nil, partial: nil, &block)
51
51
  if custom_action.to_s == ''
52
52
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
53
- @current_action.accessible_by = (CmAdmin.authorized_roles.dup << accessible_by).flatten.compact.uniq if accessible_by
54
53
  @available_tabs << CmAdmin::Models::Tab.new(tab_name, '', &block)
55
54
  else
56
55
  action = CmAdmin::Models::Action.new(name: custom_action.to_s, verb: :get, path: ':id/'+custom_action,
57
56
  layout_type: layout_type, layout: layout, partial: partial, child_records: associated_model,
58
57
  action_type: :custom, display_type: :page)
59
- action.accessible_by = (CmAdmin.authorized_roles.dup << accessible_by).flatten.compact.uniq if accessible_by
60
58
  @available_actions << action
61
59
  @current_action = action
62
60
  @available_tabs << CmAdmin::Models::Tab.new(tab_name, custom_action, &block)
@@ -117,12 +115,11 @@ module CmAdmin
117
115
  # end
118
116
  # end
119
117
  # end
120
- def custom_action(name: nil, verb: nil, layout: nil, layout_type: nil, partial: nil, path: nil, display_type: nil, display_if: lambda { |arg| return true }, route_type: nil, accessible_by: nil, &block)
118
+ def custom_action(name: nil, verb: nil, layout: nil, layout_type: nil, partial: nil, path: nil, display_type: nil, display_if: lambda { |arg| return true }, route_type: nil, &block)
121
119
  action = CmAdmin::Models::CustomAction.new(
122
120
  name: name, verb: verb, layout: layout, layout_type: layout_type, partial: partial, path: path,
123
121
  parent: self.current_action.name, display_type: display_type, display_if: display_if,
124
122
  action_type: :custom, route_type: route_type, &block)
125
- action.accessible_by = (CmAdmin.authorized_roles.dup << accessible_by).flatten.compact.uniq if accessible_by
126
123
  @available_actions << action
127
124
  # self.class.class_eval(&block)
128
125
  end
@@ -1,3 +1,3 @@
1
1
  module CmAdmin
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -9,6 +9,7 @@ module CmAdmin
9
9
  copy_file 'cm_admin_initializer.rb', 'config/initializers/cm_admin.rb'
10
10
  copy_file 'custom.js', 'app/assets/javascripts/custom.js'
11
11
  copy_file 'custom.css', 'app/assets/stylesheets/custom.css'
12
+ copy_file 'application_policy.rb', 'app/policies/application_policy.rb'
12
13
  route 'mount CmAdmin::Engine => "/admin"'
13
14
  end
14
15
  end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module CmAdmin
4
+ module Generators
5
+ class PolicyGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def copy_policy_files
9
+ template "policy.rb", "app/policies/cm_admin/#{file_name}_policy.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -17,7 +17,7 @@ class ApplicationPolicy
17
17
  end
18
18
 
19
19
  def create?
20
- false
20
+ true
21
21
  end
22
22
 
23
23
  def new?
@@ -43,7 +43,7 @@ class ApplicationPolicy
43
43
  end
44
44
 
45
45
  def resolve
46
- raise NotImplementedError, "You must define #resolve in #{self.class}"
46
+ scope.all
47
47
  end
48
48
 
49
49
  private
@@ -0,0 +1,8 @@
1
+ class CmAdmin::<%= class_name %>Policy < ApplicationPolicy
2
+ <%- available_action_names = (CmAdmin::Model.find_by({name: class_name}).available_actions.map{|action| action.name}.uniq - ['custom_action', 'new', 'edit']) %>
3
+ <%- available_action_names.each do |action_name| %>
4
+ def <%= action_name %>?
5
+ <%= CmAdmin.authorized_roles.map {|role| "@user.#{role}" }.join(' && ') %>
6
+ end
7
+ <% end %>
8
+ end
@@ -1 +1 @@
1
- d5850cd60f91abe79ff8a84a45a9e24c75cb0e4b
1
+ 07d558738c316293586496817b2bccbf6430b5bd
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: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sajinmp
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-03-25 00:00:00.000000000 Z
13
+ date: 2022-04-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: pagy
@@ -152,7 +152,6 @@ files:
152
152
  - app/javascript/packs/cm_admin/quick_search.js
153
153
  - app/javascript/packs/cm_admin/scaffolds.js
154
154
  - app/javascript/stylesheets/cm_admin/application.scss
155
- - app/policies/application_policy.rb
156
155
  - app/views/cm_admin/main/_associated_table.html.slim
157
156
  - app/views/cm_admin/main/_cm_pagy_nav.html.slim
158
157
  - app/views/cm_admin/main/_drawer.html.slim
@@ -222,9 +221,12 @@ files:
222
221
  - lib/cm_admin/view_helpers/navigation_helper.rb
223
222
  - lib/cm_admin/view_helpers/page_info_helper.rb
224
223
  - lib/generators/cm_admin/install_generator.rb
224
+ - lib/generators/cm_admin/policy_generator.rb
225
+ - lib/generators/cm_admin/templates/application_policy.rb
225
226
  - lib/generators/cm_admin/templates/cm_admin_initializer.rb
226
227
  - lib/generators/cm_admin/templates/custom.css
227
228
  - lib/generators/cm_admin/templates/custom.js
229
+ - lib/generators/cm_admin/templates/policy.rb
228
230
  - lib/tasks/webpack_install.rake
229
231
  - package.json
230
232
  - postcss.config.js