cm-admin 0.8.4 → 0.8.6

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: a8341533113fd9b7a9192af76438144a864b42449e6a2a6f6f805a36b187bae7
4
- data.tar.gz: 4a48bf48899e59f0734f81a836eb22534f39528c5d007abd9ec5c62aba6de7bf
3
+ metadata.gz: 47fc71a05e69092828694cc38ea2722580571bff16dfd6a9e52a59fd1177820f
4
+ data.tar.gz: 547bbce4e9127b0c411a9a869cdb0621ce3dcd191d24846e64bdec0415403d26
5
5
  SHA512:
6
- metadata.gz: b7f866ab3ecdca38fe1296549271b69d49745f001c57f3119d96af0626cd1d966da51a4f0a3d0ac31c034a3fbb4ee788072c8278d8c63697230fce034e7e6002
7
- data.tar.gz: 42a81533540eadfc057d86d199a1cf8116e7cdb5e18a37a1a8a3c0ecb44e4d63c2e76c65061a196b3a25ef9bef5f99d9aeb1fe82a0a01d860d43fa429300d2ba
6
+ metadata.gz: 13a83281f4010e9f7921089047e5c7ca60577ce61587413f2475cb0747b801bfdc9beb03744f8595a94743ad15a187b48f80547be5a8bd8b5217c40595551532
7
+ data.tar.gz: 8ac891bb57c06f67ede8e325196a693a78c79ef4b7a19d0f2ba72e53ce65f0273e7872916ce7fbe0883dad8c959b2c11ee6195962a81f9c098210e24ca61eb67
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cm-admin (0.8.4)
4
+ cm-admin (0.8.6)
5
5
  caxlsx_rails
6
6
  cocoon (~> 1.2.15)
7
7
  csv-importer (~> 0.8.2)
@@ -62,11 +62,12 @@ module CmAdmin
62
62
 
63
63
  def cm_destroy(params)
64
64
  @ar_object = @model.ar_model.name.classify.constantize.find(params[:id])
65
+ redirect_url = request.referrer || cm_admin.send("#{@model.name.underscore}_index_path")
65
66
  respond_to do |format|
66
67
  if @ar_object.destroy
67
- format.html { redirect_back fallback_location: cm_admin.send("#{@model.name.underscore}_index_path"), notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is successful" }
68
+ format.html { redirect_back fallback_location: redirect_url, notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is successful" }
68
69
  else
69
- format.html { redirect_back fallback_location: cm_admin.send("#{@model.name.underscore}_index_path"), notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is unsuccessful" }
70
+ format.html { redirect_back fallback_location: redirect_url, notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is unsuccessful" }
70
71
  end
71
72
  end
72
73
  end
@@ -18,11 +18,18 @@
18
18
  i.fa.fa-edit
19
19
  | Edit
20
20
  - if destroy_action.present?
21
- = link_to cm_admin.send("#{current_model.name.underscore}_destroy_path", ar_object.id), method: :delete do
22
- .popup-option
23
- span
24
- i.fa.fa-trash
25
- | Destroy
21
+ - if @associated_model
22
+ = link_to cm_admin.send("#{current_model.name.underscore}_destroy_path", ar_object.id, referrer: request.path), method: :delete do
23
+ .popup-option
24
+ span
25
+ i.fa.fa-trash
26
+ | Destroy
27
+ - else
28
+ = link_to cm_admin.send("#{current_model.name.underscore}_destroy_path", ar_object.id), method: :delete do
29
+ .popup-option
30
+ span
31
+ i.fa.fa-trash
32
+ | Destroy
26
33
  - custom_actions.each do |custom_action|
27
34
  - if custom_action.display_if.call(ar_object)
28
35
  - case custom_action.display_type
@@ -5,7 +5,7 @@
5
5
  == render partial: 'cm_admin/main/filters', locals: { filters: @associated_model.filters }
6
6
  p.table-top__total-count = "#{@associated_ar_object.pagy.count} #{@action.child_records.to_s.gsub('_', ' ')} found"
7
7
  .table-top__column-action
8
- - if @associated_model && @associated_model.available_actions.map(&:name).include?('new')
8
+ - if @associated_model && @associated_model.available_actions.map(&:name).include?('new') && has_valid_policy(@associated_model.name, 'new')
9
9
  - association = @ar_object.class.reflect_on_all_associations.select{|x| x.name == @associated_model.name.tableize.to_sym }.first
10
10
  - polymorphic_name = (association && association.inverse_of && association.inverse_of.options[:polymorphic]) ? association.inverse_of.name : ''
11
11
  a href="#{CmAdmin::Engine.mount_path}/#{@associated_model.name.tableize}/new?associated_id=#{@ar_object.id}&associated_class=#{@ar_object.class.name.underscore}&polymorphic_name=#{polymorphic_name}&referrer=#{request.path}"
@@ -35,10 +35,11 @@
35
35
  / input.cm-checkbox type="checkbox"
36
36
  - @model.available_fields[@action.name.to_sym].each_with_index do |column, index|
37
37
  td class="text-ellipsis"
38
- - if index == 0 && @associated_model && @associated_model.available_actions.map(&:name).include?('show')
39
- a href="#{CmAdmin::Engine.mount_path}/#{@associated_model.name.tableize}/#{ar_object.id}" = show_field_value(ar_object, column)
40
- - else
41
- span class="#{column.cm_css_class}" = show_field_value(ar_object, column)
38
+ span class="#{column.cm_css_class}"
39
+ - if index == 0 && @associated_model && @associated_model.available_actions.map(&:name).include?('show')
40
+ a href="#{CmAdmin::Engine.mount_path}/#{@associated_model.name.tableize}/#{ar_object.id}" = show_field_value(ar_object, column)
41
+ - else
42
+ = show_field_value(ar_object, column)
42
43
  - if @associated_model
43
44
  == render partial: 'cm_admin/main/actions_dropdown', locals: { cm_model: @associated_model, ar_object: ar_object }
44
45
 
@@ -1,8 +1,9 @@
1
- .nested-fields
1
+ .nested-fields class=assoc_name
2
2
  - @model.available_fields[ action(action_name) ][assoc_name].each do |field|
3
3
  .row
4
4
  .col-sm-10
5
5
  = input_field_for_column(f, field)
6
6
  .col-sm-2
7
- - if @reflections.select {|x| x if x.name == assoc_name}.first.macro == :has_many
8
- = link_to_remove_association "x", f
7
+ - unless field.input_type == :hidden
8
+ - if @reflections.select {|x| x if x.name == assoc_name}.first.macro == :has_many
9
+ = link_to_remove_association "x", f
@@ -16,7 +16,8 @@
16
16
  / span
17
17
  / input.cm-checkbox type="checkbox"
18
18
  - @model.available_fields[:index].each do |column|
19
- th = column.header
19
+ - if column.display_if.call(Current.user)
20
+ th = column.header
20
21
  tbody.cm-table__body
21
22
  - @ar_object.data.each do |ar_object|
22
23
  tr.body-row
@@ -24,14 +25,15 @@
24
25
  / span
25
26
  / input.cm-checkbox type="checkbox"
26
27
  - @model.available_fields[:index].each_with_index do |column, index|
27
- td.text-ellipsis
28
- span class="#{column.field_type.to_s} #{column.cm_css_class} "
29
- - if index == 0 && !([:link, :custom, :attachment, :drawer, :image].include?(column.field_type))
30
- = link_to ar_object.send(column.field_name), cm_admin.send("#{ar_object.model_name.singular}_show_path", ar_object.id)
31
- - else
32
- = show_field_value(ar_object, column)
33
- - if column.field_type == :drawer
34
- = render partial: column.drawer_partial, locals: { ar_object: ar_object }
28
+ - if column.display_if.call(Current.user)
29
+ td.text-ellipsis
30
+ span class="#{column.field_type.to_s} #{column.cm_css_class} "
31
+ - if index == 0 && !([:link, :custom, :attachment, :drawer, :image].include?(column.field_type))
32
+ = link_to ar_object.send(column.field_name), cm_admin.send("#{ar_object.model_name.singular}_show_path", ar_object.id)
33
+ - else
34
+ = show_field_value(ar_object, column)
35
+ - if column.field_type == :drawer
36
+ = render partial: column.drawer_partial, locals: { ar_object: ar_object }
35
37
  - if @model
36
38
  == render partial: 'cm_admin/main/actions_dropdown', locals: { cm_model: @model, ar_object: ar_object }
37
39
 
@@ -23,7 +23,7 @@
23
23
  li
24
24
  .popup-option.pointer data-bs-toggle='modal' data-bs-target='#exportmodal'
25
25
  span Export
26
- - if @model.importer
26
+ - if @model.importer && has_valid_policy(@model.name, :importable)
27
27
  = link_to 'Import', cm_admin.send(:"#{@model.name.underscore}_import_path"), class: 'primary-btn ml-2'
28
28
  - new_action = @model.available_actions.select{|act| act if act.action_type.eql?(:default) && act.name.eql?('new')}
29
29
  - if new_action.any? && policy([:cm_admin, @model.name.classify.constantize]).new?
@@ -8,8 +8,9 @@
8
8
  == render @action.partial
9
9
  - else
10
10
  - @model.available_fields[:show].each do |section|
11
- .info-section
12
- p.section-heading = section.section_name
13
- .paper
14
- - section.available_section_fields.each do |field|
15
- = show_field(@ar_object, field)
11
+ - if section.display_if.call(@ar_object)
12
+ .info-section
13
+ p.section-heading = section.section_name
14
+ .paper
15
+ - section.available_section_fields.each do |field|
16
+ = show_field(@ar_object, field)
@@ -2,11 +2,12 @@ module CmAdmin
2
2
  module Models
3
3
  class CmShowSection
4
4
 
5
- attr_accessor :section_name, :available_section_fields
5
+ attr_accessor :section_name, :available_section_fields, :display_if
6
6
 
7
- def initialize(section_name, &block)
7
+ def initialize(section_name, display_if, &block)
8
8
  @available_section_fields = []
9
9
  @section_name = section_name
10
+ @display_if = display_if || lambda { |arg| return true }
10
11
  instance_eval(&block)
11
12
  end
12
13
 
@@ -2,7 +2,7 @@ module CmAdmin
2
2
  module Models
3
3
  class Column
4
4
  attr_accessor :field_name, :field_type, :header, :format, :prefix, :suffix, :exportable, :round, :height, :width,
5
- :cm_css_class, :link, :url, :custom_method, :helper_method, :managable, :lockable, :drawer_partial, :tag_class
5
+ :cm_css_class, :link, :url, :custom_method, :helper_method, :managable, :lockable, :drawer_partial, :tag_class, :display_if
6
6
 
7
7
  def initialize(field_name, attributes = {})
8
8
  @field_name = field_name
@@ -15,6 +15,7 @@ module CmAdmin
15
15
  self.send("header=", format_header)
16
16
  self.height = 50 if self.field_type == :image && self.height.nil?
17
17
  self.width = 50 if self.field_type == :image && self.width.nil?
18
+ self.display_if = lambda { |arg| return true } if self.display_if.nil?
18
19
  end
19
20
 
20
21
  #returns a string value as a header (either field_name or value present in header attribute)
@@ -54,9 +54,9 @@ module CmAdmin
54
54
  yield if block
55
55
  end
56
56
 
57
- def cm_show_section(section_name, &block)
57
+ def cm_show_section(section_name, display_if: nil, &block)
58
58
  @available_fields[@current_action.name.to_sym] ||= []
59
- @available_fields[@current_action.name.to_sym] << CmAdmin::Models::CmShowSection.new(section_name, &block)
59
+ @available_fields[@current_action.name.to_sym] << CmAdmin::Models::CmShowSection.new(section_name, display_if, &block)
60
60
  end
61
61
 
62
62
  def form_field(field_name, options={}, arg=nil)
@@ -3,7 +3,7 @@ module CmAdmin
3
3
  class Field
4
4
 
5
5
  attr_accessor :field_name, :label, :header, :field_type, :format, :precision, :height,
6
- :width, :helper_method, :preview, :custom_link, :precision, :prefix, :suffix, :tag_class
6
+ :width, :helper_method, :preview, :custom_link, :precision, :prefix, :suffix, :tag_class, :display_if
7
7
 
8
8
  def initialize(field_name, attributes = {})
9
9
  @field_name = field_name
@@ -13,6 +13,7 @@ module CmAdmin
13
13
  end
14
14
  self.height = 50 if self.field_type == :image && self.height.nil?
15
15
  self.width = 50 if self.field_type == :image && self.width.nil?
16
+ self.display_if = lambda { |arg| return true } if self.display_if.nil?
16
17
  end
17
18
 
18
19
  def set_default_values
@@ -1,7 +1,7 @@
1
1
  module CmAdmin
2
2
  module Models
3
3
  class FormField
4
- attr_accessor :field_name, :label, :header, :input_type, :collection, :disabled, :helper_method, :placeholder
4
+ attr_accessor :field_name, :label, :header, :input_type, :collection, :disabled, :helper_method, :placeholder, :display_if, :html_attr
5
5
  VALID_INPUT_TYPES = [:integer, :decimal, :string, :single_select, :multi_select, :date, :date_time, :text, :single_file_upload, :multi_file_upload, :hidden, :rich_text].freeze
6
6
 
7
7
  def initialize(field_name, input_type, attributes = {})
@@ -10,6 +10,7 @@ module CmAdmin
10
10
  attributes.each do |key, value|
11
11
  self.send("#{key.to_s}=", value)
12
12
  end
13
+ self.display_if = lambda { |arg| return true } if self.display_if.nil?
13
14
  raise ArgumentError, "Kindly select a valid input type like #{VALID_INPUT_TYPES.sort.to_sentence(last_word_connector: ', or ')} instead of #{self.input_type} for form field #{field_name}" unless VALID_INPUT_TYPES.include?(self.input_type.to_sym)
14
15
  end
15
16
 
@@ -18,6 +19,7 @@ module CmAdmin
18
19
  self.label = self.field_name.to_s.titleize
19
20
  self.input_type = :string
20
21
  self.placeholder = "Enter #{self.field_name.to_s.downcase.gsub('_', ' ')}"
22
+ self.html_attr = {}
21
23
  end
22
24
  end
23
25
  end
@@ -1,3 +1,3 @@
1
1
  module CmAdmin
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.6"
3
3
  end
@@ -2,6 +2,7 @@ module CmAdmin
2
2
  module ViewHelpers
3
3
  module FieldDisplayHelper
4
4
  def show_field(ar_object, field)
5
+ return unless field.display_if.call(ar_object)
5
6
  content_tag(:div, class: "info-split") do
6
7
  concat show_field_label(ar_object, field)
7
8
  concat value_with_prefix_and_suffix(ar_object, field)
@@ -2,7 +2,8 @@ module CmAdmin
2
2
  module ViewHelpers
3
3
  module FormFieldHelper
4
4
  def input_field_for_column(f, field)
5
- value = field.helper_method ? send(field.helper_method) : f.object.send(field.field_name)
5
+ return unless field.display_if.call(f.object)
6
+ value = field.helper_method ? send(field.helper_method, f.object, field.field_name) : f.object.send(field.field_name)
6
7
  is_required = f.object._validators[field.field_name].map(&:kind).include?(:presence)
7
8
  required_class = is_required ? 'required' : ''
8
9
  case field.input_type
@@ -13,9 +14,9 @@ module CmAdmin
13
14
  when :string
14
15
  return f.text_field field.field_name, class: "normal-input #{required_class}", disabled: field.disabled, value: value, placeholder: "Enter #{field.field_name.to_s.downcase.gsub('_', ' ')}"
15
16
  when :single_select
16
- return f.select field.field_name, options_for_select(select_collection_value(field), f.object.send(field.field_name)), {include_blank: field.placeholder.to_s}, class: "normal-input #{required_class} select-2", disabled: field.disabled
17
+ return f.select field.field_name, options_for_select(select_collection_value(f.object, field), f.object.send(field.field_name)), {include_blank: field.placeholder.to_s}, class: "normal-input #{required_class} select-2", disabled: field.disabled
17
18
  when :multi_select
18
- return f.select field.field_name, options_for_select(select_collection_value(field), f.object.send(field.field_name)), {include_blank: "Select #{field.field_name.to_s.downcase.gsub('_', ' ')}"}, class: "normal-input #{required_class} select-2", disabled: field.disabled, multiple: true
19
+ return f.select field.field_name, options_for_select(select_collection_value(f.object, field), f.object.send(field.field_name)), {include_blank: "Select #{field.field_name.to_s.downcase.gsub('_', ' ')}"}, class: "normal-input #{required_class} select-2", disabled: field.disabled, multiple: true
19
20
  when :date
20
21
  return f.text_field field.field_name, class: "normal-input #{required_class}", disabled: field.disabled, value: value&.strftime('%d-%m-%Y'), placeholder: "Enter #{field.field_name.to_s.downcase.gsub('_', ' ')}", data: { behaviour: 'date-only' }
21
22
  when :date_time
@@ -29,15 +30,15 @@ module CmAdmin
29
30
  when :multi_file_upload
30
31
  return f.file_field field.field_name, multiple: true, class: "normal-input #{required_class}"
31
32
  when :hidden
32
- return f.hidden_field field.field_name, value: value
33
+ return f.hidden_field field.field_name, value: value, name: field.html_attr[:name] || "#{f.object_name}[#{field.field_name}]"
33
34
  end
34
35
  end
35
36
 
36
37
  # Refactor: Collection argument can be removed.
37
38
  # helper_method argument will accept a method where value can be passed.
38
- def select_collection_value(field)
39
+ def select_collection_value(object, field)
39
40
  if field.helper_method
40
- collection = send(field.helper_method)
41
+ collection = send(field.helper_method, object, field.field_name)
41
42
  elsif field.collection
42
43
  collection = field.collection
43
44
  else
@@ -50,6 +50,7 @@ module CmAdmin
50
50
  available_fields_hash.each do |key, fields_array|
51
51
  if key == :fields
52
52
  fields_array.each do |field|
53
+ next unless field.display_if.call(f.object)
53
54
  if field.input_type.eql?(:hidden)
54
55
  concat input_field_for_column(f, field)
55
56
  else
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.8.4
4
+ version: 0.8.6
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-10-17 00:00:00.000000000 Z
13
+ date: 2022-10-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: caxlsx_rails