cm-admin 0.8.5 → 0.8.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/controllers/cm_admin/resource_controller.rb +3 -2
- data/app/views/cm_admin/main/_actions_dropdown.html.slim +12 -5
- data/app/views/cm_admin/main/_associated_table.html.slim +7 -6
- data/app/views/cm_admin/main/_nested_fields.html.slim +4 -3
- data/app/views/cm_admin/main/_table.html.slim +12 -10
- data/app/views/cm_admin/main/show.html.slim +6 -5
- data/lib/cm_admin/models/cm_show_section.rb +3 -2
- data/lib/cm_admin/models/column.rb +2 -1
- data/lib/cm_admin/models/dsl_method.rb +2 -2
- data/lib/cm_admin/models/field.rb +2 -1
- data/lib/cm_admin/models/filter.rb +20 -6
- data/lib/cm_admin/models/form_field.rb +3 -1
- data/lib/cm_admin/version.rb +1 -1
- data/lib/cm_admin/view_helpers/field_display_helper.rb +1 -0
- data/lib/cm_admin/view_helpers/form_field_helper.rb +7 -6
- data/lib/cm_admin/view_helpers/form_helper.rb +1 -0
- data/lib/cm_admin/view_helpers.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c6cf3d2c260e9964ab1aa9865d14af11fe287e49b6216ae260ae575567c0e89
|
4
|
+
data.tar.gz: 495df145a9060198ca69bfd1eaeea836b6bba24287a804e69d100fab5d36cbd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03b3e0f445067d05f32313a7c4e519545ee6396eca549e75719de294380df8be3be2d5e95c6dfcf262d563eb5db4cd8e3ebabcae753075a4b61f69a6957c5c1d
|
7
|
+
data.tar.gz: 498e3dfb9d3580fa69bfaf959df282567590cbdee12d2ad3bc65fcbb7f7101c70fd9d7d5aba2e78ea6a09547afe0fc4161dd478495110bcfe8efe3887e0d7040
|
data/Gemfile.lock
CHANGED
@@ -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:
|
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:
|
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
|
-
|
22
|
-
.
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
@@ -3,9 +3,9 @@
|
|
3
3
|
- if @associated_model.filters.present? && @action.partial.nil?
|
4
4
|
.index-page__filters
|
5
5
|
== render partial: 'cm_admin/main/filters', locals: { filters: @associated_model.filters }
|
6
|
-
p.table-top__total-count = "#{@associated_ar_object.pagy.count
|
6
|
+
p.table-top__total-count = "#{humanized_ar_collection_count(@associated_ar_object.pagy.count, @action.child_records.to_s)}"
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
-
|
8
|
-
|
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
|
@@ -1,6 +1,6 @@
|
|
1
1
|
.admin-table-index
|
2
2
|
.table-top
|
3
|
-
p.table-top__total-count = "#{@ar_object.pagy.count
|
3
|
+
p.table-top__total-count = "#{humanized_ar_collection_count(@ar_object.pagy.count, @model.ar_model.table_name)}"
|
4
4
|
// .table-top__column-action
|
5
5
|
// button.secondary-btn.column-btn data-bs-target="#columnActionModal" data-bs-toggle="modal"
|
6
6
|
// span
|
@@ -16,7 +16,8 @@
|
|
16
16
|
/ span
|
17
17
|
/ input.cm-checkbox type="checkbox"
|
18
18
|
- @model.available_fields[:index].each do |column|
|
19
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
|
@@ -8,8 +8,9 @@
|
|
8
8
|
== render @action.partial
|
9
9
|
- else
|
10
10
|
- @model.available_fields[:show].each do |section|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
@@ -19,7 +19,7 @@ module CmAdmin
|
|
19
19
|
|
20
20
|
case filter_type
|
21
21
|
when :search
|
22
|
-
db_column_name = (Array.new << db_column_name).flatten.map(
|
22
|
+
db_column_name = (Array.new << db_column_name).flatten.map{|x| x.class.eql?(Hash) ? x : x.to_sym}
|
23
23
|
else
|
24
24
|
db_column_name = db_column_name.is_a?(Array) ? db_column_name[0].to_sym : db_column_name.to_sym
|
25
25
|
end
|
@@ -47,23 +47,37 @@ module CmAdmin
|
|
47
47
|
def cm_search_filter(scope_value, records, filters)
|
48
48
|
return nil if scope_value.blank?
|
49
49
|
table_name = records.table_name
|
50
|
-
|
51
50
|
filters.select{|x| x if x.filter_type.eql?(:search)}.each do |filter|
|
51
|
+
query_variables = []
|
52
|
+
filter.db_column_name.each do |col|
|
53
|
+
if col.is_a?(Symbol)
|
54
|
+
query_variables << "#{table_name.pluralize}.#{col}"
|
55
|
+
elsif col.is_a?(Hash)
|
56
|
+
col.map do |key, value|
|
57
|
+
value.map {|val| query_variables << "#{key.to_s.pluralize}.#{val}" }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
52
61
|
terms = scope_value.downcase.split(/\s+/)
|
53
62
|
terms = terms.map { |e|
|
54
63
|
(e.gsub('*', '%').prepend('%') + '%').gsub(/%+/, '%')
|
55
64
|
}
|
56
65
|
sql = ""
|
57
|
-
|
58
|
-
sql.concat("#{
|
59
|
-
sql.concat(' OR ') unless
|
66
|
+
query_variables.each.with_index do |column, i|
|
67
|
+
sql.concat("#{column} ILIKE ?")
|
68
|
+
sql.concat(' OR ') unless query_variables.size.eql?(i+1)
|
69
|
+
end
|
70
|
+
|
71
|
+
if filter.db_column_name.map{|x| x.is_a?(Hash)}.include?(true)
|
72
|
+
associations_hash = filter.db_column_name.select{|x| x if x.is_a?(Hash)}.last
|
73
|
+
records = records.joins(associations_hash.keys)
|
60
74
|
end
|
61
75
|
|
62
76
|
records = records.where(
|
63
77
|
terms.map { |term|
|
64
78
|
sql
|
65
79
|
}.join(' AND '),
|
66
|
-
*terms.map { |e| [e] *
|
80
|
+
*terms.map { |e| [e] * query_variables.size }.flatten
|
67
81
|
)
|
68
82
|
end
|
69
83
|
records
|
@@ -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
|
data/lib/cm_admin/version.rb
CHANGED
@@ -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
|
-
|
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
|
@@ -72,5 +72,10 @@ module CmAdmin
|
|
72
72
|
concat " #{column_path.to_s.gsub('/', '_').humanize}"
|
73
73
|
end
|
74
74
|
end
|
75
|
+
|
76
|
+
def humanized_ar_collection_count(count, model_name)
|
77
|
+
table_name = count == 1 ? model_name.singularize : model_name.pluralize
|
78
|
+
return "#{count} #{table_name.humanize.downcase} found"
|
79
|
+
end
|
75
80
|
end
|
76
81
|
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: 0.8.
|
4
|
+
version: 0.8.7
|
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-
|
13
|
+
date: 2022-10-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: caxlsx_rails
|