Module: SlashAdmin::ApplicationHelper
- Includes:
- Pagy::Frontend
- Defined in:
- app/helpers/slash_admin/application_helper.rb
Instance Method Summary collapse
-
#access?(s) ⇒ Boolean
is the current has access to at least one node.
-
#access_model?(sub) ⇒ Boolean
is the current has access to model.
- #admin_custom_field(form, attribute) ⇒ Object
-
#admin_field(form, attribute) ⇒ Object
Form helper for generic field.
- #class_name_from_association(obj, field_name) ⇒ Object
- #errors?(form, field_name) ⇒ Boolean
-
#guess_field_type(object, attr) ⇒ Object
Automatic retrieve of field type object params can be a Model Class or a Model Instance boolean integer number decimal string text date datetime has_many belongs_to.
-
#object_label(a) ⇒ Object
Default label for object to string, title and name can be an attribute, a string or the model_class.
- #object_label_methods ⇒ Object
-
#orderable?(object, attr) ⇒ Boolean
Default available field_type handeled.
- #page_sub_title(content) ⇒ Object
- #page_title(content) ⇒ Object
- #required?(obj, field_name) ⇒ Boolean
- #serialized_attributes_for(model) ⇒ Object
- #serialized_json_field?(model, attribute) ⇒ Boolean
- #show_errors(form, field_name) ⇒ Object
- #show_hidden_errors ⇒ Object
- #show_object(a) ⇒ Object
-
#sortable?(object, attr) ⇒ Boolean
By default all sortable fields are orderable.
-
#toastr_bootstrap ⇒ Object
Type must be ‘warning’ or ‘success’ or ‘error’.
Instance Method Details
#access?(s) ⇒ Boolean
is the current has access to at least one node
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/helpers/slash_admin/application_helper.rb', line 16 def access?(s) access = false if s[:sub_menu].present? s[:sub_menu].each do |sub| access = true if access_model?(sub) end end if s[:path].present? if can? s[:role], s[:role] access = true end if s[:role].present? if can? s[:role], s[:role] access = true end end end access end |
#access_model?(sub) ⇒ Boolean
is the current has access to model
39 40 41 42 |
# File 'app/helpers/slash_admin/application_helper.rb', line 39 def access_model?(sub) return false if cannot? :index, sub[:model] true end |
#admin_custom_field(form, attribute) ⇒ Object
207 208 209 210 |
# File 'app/helpers/slash_admin/application_helper.rb', line 207 def admin_custom_field(form, attribute) type = attribute[attribute.keys.first][:type].to_s render partial: "slash_admin/custom_fields/#{type}", locals: {f: form, a: attribute} end |
#admin_field(form, attribute) ⇒ Object
Form helper for generic field
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'app/helpers/slash_admin/application_helper.rb', line 213 def admin_field(form, attribute) object_class = form.object.class belongs_to_fields = object_class.reflect_on_all_associations(:belongs_to).map(&:name) has_many_fields = object_class.reflect_on_all_associations(:has_many).map(&:name) has_one_fields = object_class.reflect_on_all_associations(:has_one).map(&:name) # Handle custom field first and default after if attribute.is_a?(Hash) admin_custom_field(form, attribute) elsif belongs_to_fields.include?(attribute.to_sym) if form.object.class..key?(attribute.to_sym) render partial: "slash_admin/fields/nested_belongs_to", locals: {f: form, a: attribute} else render partial: "slash_admin/fields/belongs_to", locals: {f: form, a: attribute} end elsif has_many_fields.include?(attribute.to_sym) # if has nested_attributes_options for has_many field if form.object.class..key?(attribute.to_sym) render partial: "slash_admin/fields/nested_has_many", locals: {f: form, a: attribute} else render partial: "slash_admin/fields/has_many", locals: {f: form, a: attribute} end elsif has_one_fields.include?(attribute.to_sym) if form.object.class..key?(attribute.to_sym) render partial: "slash_admin/fields/nested_has_one", locals: {f: form, a: attribute} else render partial: "slash_admin/fields/has_one", locals: {f: form, a: attribute} end elsif form.object.class&.uploaders&.key?(attribute.to_sym) render partial: "slash_admin/fields/carrierwave", locals: {f: form, a: attribute} else type = form.object.class.type_for_attribute(attribute.to_s).type.to_s if type == "date" || type == "datetime" render partial: "slash_admin/fields/date", locals: {f: form, a: attribute} else raise Exception.new("Unable to guess field_type for attribute: #{attribute} in model: #{object_class}") if type.blank? render partial: "slash_admin/fields/#{type}", locals: {f: form, a: attribute} end end end |
#class_name_from_association(obj, field_name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'app/helpers/slash_admin/application_helper.rb', line 44 def class_name_from_association(obj, field_name) [:belongs_to, :has_many, :has_one].each do |relation| obj.class.reflect_on_all_associations(relation).each do |association| return association.class_name if association.name == field_name.to_sym end end raise Exception.new("Unable to guess association for attribute: #{field_name} in model: #{obj}") end |
#errors?(form, field_name) ⇒ Boolean
83 84 85 86 87 88 89 90 |
# File 'app/helpers/slash_admin/application_helper.rb', line 83 def errors?(form, field_name) object = form.object if field_name.is_a?(Hash) field_name = field_name.keys.first end return false if object.errors.empty? return true unless object.errors.[field_name].blank? end |
#guess_field_type(object, attr) ⇒ Object
Automatic retrieve of field type object params can be a Model Class or a Model Instance boolean integer number decimal string text date datetime has_many belongs_to
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/helpers/slash_admin/application_helper.rb', line 153 def guess_field_type(object, attr) object_class = if object.class === Class object else object.class end belongs_to_fields = object_class.reflect_on_all_associations(:belongs_to).map(&:name) has_many_fields = object_class.reflect_on_all_associations(:has_many).map(&:name) has_one_fields = object_class.reflect_on_all_associations(:has_one).map(&:name) return if attr.is_a? Hash type = if object_class&.uploaders&.key?(attr.to_sym) "image" elsif belongs_to_fields.include?(attr.to_sym) "belongs_to" elsif has_many_fields.include?(attr.to_sym) "has_many" elsif has_one_fields.include?(attr.to_sym) "has_one" else object_class.type_for_attribute(attr.to_s).type.to_s end # Virtual field default string eg password return "string" if object_class.new.respond_to?(attr) && type.blank? # Raise exception if no type fouded raise Exception.new("Unable to guess field_type for attribute: #{attr} in model: #{object_class}") if type.blank? type end |
#object_label(a) ⇒ Object
Default label for object to string, title and name can be an attribute, a string or the model_class
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/helpers/slash_admin/application_helper.rb', line 119 def object_label(a) constantized_model = if a.is_a? Hash a.keys.first.to_s.singularize.classify.constantize elsif a.is_a? ActiveRecord::Base a else a.to_s.singularize.classify.constantize end constantized_model = constantized_model.new method = "to_s" object_label_methods.each do |m| method = m if constantized_model.respond_to?(m) end method end |
#object_label_methods ⇒ Object
113 114 115 |
# File 'app/helpers/slash_admin/application_helper.rb', line 113 def object_label_methods [:title, :name] end |
#orderable?(object, attr) ⇒ Boolean
Default available field_type handeled
103 104 105 106 |
# File 'app/helpers/slash_admin/application_helper.rb', line 103 def orderable?(object, attr) field_type = guess_field_type(object, attr) %w[boolean integer number decimal string text date datetime].include?(field_type) end |
#page_sub_title(content) ⇒ Object
11 12 13 |
# File 'app/helpers/slash_admin/application_helper.rb', line 11 def page_sub_title(content) content_for :page_sub_title, content end |
#page_title(content) ⇒ Object
7 8 9 |
# File 'app/helpers/slash_admin/application_helper.rb', line 7 def page_title(content) content_for :page_title, content end |
#required?(obj, field_name) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/slash_admin/application_helper.rb', line 54 def required?(obj, field_name) if field_name.is_a?(Hash) field_hash = field_name field_name = field_hash.keys.first if field_hash[field_name].key?(:required) return field_hash[field_name][:required] end end obj.class.validators_on(field_name.to_s).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator } end |
#serialized_attributes_for(model) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/helpers/slash_admin/application_helper.rb', line 188 def serialized_attributes_for(model) serialized_columns = model.columns.select { |column| model.type_for_attribute(column.name).is_a?( ::ActiveRecord::Type::Serialized, ) } serialized_columns.each_with_object({}) do |column, hash| hash[column.name.to_s] = model.type_for_attribute(column.name).coder end end |
#serialized_json_field?(model, attribute) ⇒ Boolean
202 203 204 205 |
# File 'app/helpers/slash_admin/application_helper.rb', line 202 def serialized_json_field?(model, attribute) hash = serialized_attributes_for(model) hash.key?(attribute) && hash[attribute] == ::ActiveRecord::Coders::JSON end |
#show_errors(form, field_name) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/helpers/slash_admin/application_helper.rb', line 71 def show_errors(form, field_name) object = form.object if field_name.is_a?(Hash) field_name = field_name.keys.first end return [] if object.errors.empty? unless object.errors.[field_name].blank? return object.errors.[field_name] end [] end |
#show_hidden_errors ⇒ Object
67 68 69 |
# File 'app/helpers/slash_admin/application_helper.rb', line 67 def show_hidden_errors @model.errors..except(*@update_params) end |
#show_object(a) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/helpers/slash_admin/application_helper.rb', line 138 def show_object(a) method = "to_s" unless a.blank? object_label_methods.each do |m| method = m if a.respond_to?(m) end a.send(method) end end |
#sortable?(object, attr) ⇒ Boolean
By default all sortable fields are orderable
109 110 111 |
# File 'app/helpers/slash_admin/application_helper.rb', line 109 def sortable?(object, attr) orderable?(object, attr) end |
#toastr_bootstrap ⇒ Object
Type must be ‘warning’ or ‘success’ or ‘error’
93 94 95 96 97 98 99 100 |
# File 'app/helpers/slash_admin/application_helper.rb', line 93 def toastr_bootstrap = [] flash.each do |type, | text = '<script data-turbolinks-eval="false">toastr.' + type + '("' + + '");</script>' << text.html_safe if end .join("\n").html_safe end |