Module: QuickAdmin::ApplicationHelper
- Defined in:
- app/helpers/quick_admin/application_helper.rb
Instance Method Summary collapse
- #current_resource_config ⇒ Object
- #current_resource_name ⇒ Object
- #display_field_value(resource, field) ⇒ Object
- #file_accept_types ⇒ Object
- #filter_options_for_field(model_class, field) ⇒ Object
-
#quick_admin_current_user ⇒ Object?
Returns the current user using the configured method.
-
#quick_admin_sign_out_path ⇒ String?
Returns the sign out path if available.
-
#quick_admin_user_display ⇒ String
Returns a display string for the current user.
- #render_form_field(form, resource, field) ⇒ Object
- #resource_name ⇒ Object
-
#resource_param_value(resource) ⇒ String, Integer
Returns the param value to use in URLs for a resource.
- #sort_direction(field) ⇒ Object
Instance Method Details
#current_resource_config ⇒ Object
193 194 195 |
# File 'app/helpers/quick_admin/application_helper.rb', line 193 def current_resource_config @resource_config end |
#current_resource_name ⇒ Object
189 190 191 |
# File 'app/helpers/quick_admin/application_helper.rb', line 189 def current_resource_name resource_name end |
#display_field_value(resource, field) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/helpers/quick_admin/application_helper.rb', line 3 def display_field_value(resource, field) # Check if this is a belongs_to association (field ends with _id) if field.to_s.end_with?('_id') association_name = field.to_s.sub(/_id$/, '') if resource.class.reflect_on_association(association_name.to_sym) associated = resource.send(association_name) if associated # Try common display methods return associated.try(:name) || associated.try(:title) || associated.try(:to_s) || associated.id else return content_tag(:span, "—", class: "null-value") end end end value = resource.send(field) case value when NilClass content_tag(:span, "—", class: "null-value") when TrueClass, FalseClass content_tag(:span, value ? "Yes" : "No", class: "boolean-value #{value}") when Time, DateTime value.strftime("%Y-%m-%d %H:%M") when Date value.strftime("%Y-%m-%d") when ActiveStorage::Attached::One if value.attached? blob = value.blob if value.image? thumb = image_tag(main_app.rails_blob_path(blob, only_path: true), class: "thumbnail", size: "50x50") link_to(thumb, quick_admin.(resource_name, resource.id, field, blob.signed_id), data: { turbo_frame: "modal" }) else link_to(blob.filename, main_app.rails_blob_path(blob, only_path: true), target: "_blank", class: "file-link") end else content_tag(:span, "No file", class: "null-value") end when ActiveStorage::Attached::Many if value.attached? content_tag(:div, class: "attachments") do value.map do || blob = .blob if .image? thumb = image_tag(main_app.rails_blob_path(blob, only_path: true), class: "thumbnail", size: "50x50") link_to(thumb, quick_admin.(resource_name, resource.id, field, blob.signed_id), data: { turbo_frame: "modal" }) else link_to(blob.filename, main_app.rails_blob_path(blob, only_path: true), target: "_blank", class: "file-link") end end.join(" ").html_safe end else content_tag(:span, "No files", class: "null-value") end when String if value.length > 100 content_tag(:div, class: "truncated-text") do content_tag(:span, truncate(value, length: 100)) + content_tag(:button, "Show more", class: "btn btn-link btn-sm", data: { action: "click->text-expander#toggle" }) end else simple_format(value) end else value.to_s end end |
#file_accept_types ⇒ Object
181 182 183 |
# File 'app/helpers/quick_admin/application_helper.rb', line 181 def file_accept_types "image/*,application/pdf,.doc,.docx,.txt" end |
#filter_options_for_field(model_class, field) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/helpers/quick_admin/application_helper.rb', line 130 def (model_class, field) return [] unless model_class.respond_to?(:column_names) # Check if this is a belongs_to association (field ends with _id) if field.to_s.end_with?('_id') association_name = field.to_s.sub(/_id$/, '') association = model_class.reflect_on_association(association_name.to_sym) if association associated_class = association.klass # Get all records from the associated model and display their name/title return associated_class.all.map do |record| display = record.try(:name) || record.try(:title) || record.to_s [display, record.id.to_s] end end end column = model_class.columns_hash[field] case column&.type when :boolean [['True', 'true'], ['False', 'false']] when :string model_class.distinct.pluck(field).compact.uniq.map { |v| [v.to_s, v.to_s] } when :date, :datetime, :timestamp dates = model_class.where.not(field => nil).order(field => :desc).limit(200).pluck(field) days = dates.map { |d| d.to_date }.uniq.first(10) days.map { |day| [day.strftime('%Y-%m-%d'), day.strftime('%Y-%m-%d')] } when :integer # Check if this is an enum field enum_name = field.to_s.pluralize if model_class.respond_to?(enum_name) return model_class.send(enum_name).keys.map { |k| [k.humanize, k] } end values = model_class.distinct.pluck(field).compact.uniq values.map { |v| [v.to_s, v.to_s] } else values = model_class.distinct.pluck(field).compact.uniq values.map { |v| [v.to_s, v.to_s] } end rescue [] end |
#quick_admin_current_user ⇒ Object?
Returns the current user using the configured method.
211 212 213 214 215 |
# File 'app/helpers/quick_admin/application_helper.rb', line 211 def quick_admin_current_user method_name = QuickAdmin.config.resolved_current_user_method return nil unless respond_to?(method_name, true) send(method_name) end |
#quick_admin_sign_out_path ⇒ String?
Returns the sign out path if available.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'app/helpers/quick_admin/application_helper.rb', line 229 def quick_admin_sign_out_path # First check explicit configuration configured_path = QuickAdmin.config.sign_out_path return configured_path if configured_path.present? # Try to use Rails route helpers (more reliable than string paths) if defined?(Devise) begin # Try destroy_admin_session_path first return main_app.destroy_admin_session_path if main_app.respond_to?(:destroy_admin_session_path) # Fall back to destroy_user_session_path return main_app.destroy_user_session_path if main_app.respond_to?(:destroy_user_session_path) rescue => e Rails.logger.debug "QuickAdmin: Could not resolve sign out path: #{e.}" end end nil end |
#quick_admin_user_display ⇒ String
Returns a display string for the current user. Tries email, name, username, or falls back to "Admin".
220 221 222 223 224 225 |
# File 'app/helpers/quick_admin/application_helper.rb', line 220 def quick_admin_user_display user = quick_admin_current_user return "Admin" unless user user.try(:email) || user.try(:name) || user.try(:username) || "Admin" end |
#render_form_field(form, resource, field) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/helpers/quick_admin/application_helper.rb', line 73 def render_form_field(form, resource, field) # Check if this is a belongs_to association (field ends with _id) if field.to_s.end_with?('_id') association_name = field.to_s.sub(/_id$/, '') association = resource.class.reflect_on_association(association_name.to_sym) if association associated_class = association.klass = associated_class.all.map do |record| display = record.try(:name) || record.try(:title) || record.to_s [display, record.id] end return form.select(field, , { include_blank: "Select #{association_name.humanize}" }, class: "form-control") end end # Check if this is an enum field if resource.class.respond_to?(:defined_enums) && resource.class.defined_enums.key?(field.to_s) = resource.class.send(field.to_s.pluralize).keys.map { |k| [k.humanize, k] } return form.select(field, , { include_blank: "Select #{field.humanize}" }, class: "form-control") end column = resource.class.columns_hash[field] case column&.type when :boolean content_tag(:div, class: "form-check") do form.check_box(field, class: "form-check-input") + form.label(field, field.humanize, class: "form-check-label") end when :text if QuickAdmin.config.trix_enabled? && QuickAdmin.config.text_editor == :trix form.rich_text_area(field, class: "form-control") else form.text_area(field, class: "form-control", rows: 4) end when :date form.date_field(field, class: "form-control") when :datetime, :timestamp form.datetime_local_field(field, class: "form-control") when :integer, :bigint form.number_field(field, class: "form-control") when :decimal, :float form.number_field(field, step: :any, class: "form-control") else # Check if it's a file field (Active Storage) if resource.class.(field) if resource.class.(field).macro == :has_many_attached form.file_field(field, multiple: true, class: "form-control", accept: file_accept_types) else form.file_field(field, class: "form-control", accept: file_accept_types) end else form.text_field(field, class: "form-control") end end end |
#resource_name ⇒ Object
185 186 187 |
# File 'app/helpers/quick_admin/application_helper.rb', line 185 def resource_name params[:resource_name] || params[:id] end |
#resource_param_value(resource) ⇒ String, Integer
Returns the param value to use in URLs for a resource. Uses the configured find_by_field to get the correct value.
202 203 204 205 206 207 |
# File 'app/helpers/quick_admin/application_helper.rb', line 202 def resource_param_value(resource) return resource.id unless @resource_config find_field = @resource_config.find_by_field find_field == 'id' ? resource.id : resource.send(find_field) end |
#sort_direction(field) ⇒ Object
173 174 175 176 177 178 179 |
# File 'app/helpers/quick_admin/application_helper.rb', line 173 def sort_direction(field) if params[:sort] == field && params[:direction] == 'asc' 'desc' else 'asc' end end |