Class: QuickAdmin::Resource
- Inherits:
-
Object
- Object
- QuickAdmin::Resource
- Defined in:
- lib/quick_admin/resource.rb
Overview
Resource represents a model configuration for the admin interface. It defines which fields to display, search, filter, and edit.
Instance Attribute Summary collapse
-
#display_name ⇒ String
human-friendly name for the resource.
-
#editable_fields ⇒ Array<String>
fields that can be edited.
-
#filterable_fields ⇒ Array<String>
fields that can be filtered.
-
#model_name ⇒ String
the name of the model.
-
#param_key ⇒ String
the field to use for finding records (default: id).
-
#searchable_fields ⇒ Array<String>
fields that can be searched.
Instance Method Summary collapse
-
#editable(*field_names) ⇒ Array<String>
Gets or sets the editable fields.
-
#fields(*field_names) ⇒ Array<String>
Gets or sets the fields to display in the index view.
-
#filterable(*field_names) ⇒ Array<String>
Gets or sets the filterable fields.
-
#find_by_field ⇒ String
Returns the field to use for finding records.
-
#initialize(model_name) ⇒ Resource
constructor
Initializes a new Resource.
-
#model_class ⇒ Class?
Returns the actual model class for this resource.
-
#name(display_name = nil) ⇒ String
Gets or sets the display name for this resource.
-
#param(field = nil) ⇒ String
Gets or sets the param key used for finding records.
-
#searchable(*field_names) ⇒ Array<String>
Gets or sets the searchable fields.
Constructor Details
#initialize(model_name) ⇒ Resource
Initializes a new Resource.
18 19 20 21 22 23 24 25 26 |
# File 'lib/quick_admin/resource.rb', line 18 def initialize(model_name) @model_name = model_name.to_s @fields = [] @searchable_fields = [] @filterable_fields = [] @editable_fields = [] @display_name = model_name.to_s.humanize.pluralize @param_key = nil # Will auto-detect or default to :id end |
Instance Attribute Details
#display_name ⇒ String
human-friendly name for the resource
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def display_name @display_name end |
#editable_fields ⇒ Array<String>
fields that can be edited
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def editable_fields @editable_fields end |
#filterable_fields ⇒ Array<String>
fields that can be filtered
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def filterable_fields @filterable_fields end |
#model_name ⇒ String
the name of the model
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def model_name @model_name end |
#param_key ⇒ String
the field to use for finding records (default: id)
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def param_key @param_key end |
#searchable_fields ⇒ Array<String>
fields that can be searched
11 12 13 |
# File 'lib/quick_admin/resource.rb', line 11 def searchable_fields @searchable_fields end |
Instance Method Details
#editable(*field_names) ⇒ Array<String>
Gets or sets the editable fields.
77 78 79 80 |
# File 'lib/quick_admin/resource.rb', line 77 def editable(*field_names) @editable_fields = field_names.map(&:to_s) if field_names.any? @editable_fields end |
#fields(*field_names) ⇒ Array<String>
Gets or sets the fields to display in the index view.
47 48 49 50 51 52 53 |
# File 'lib/quick_admin/resource.rb', line 47 def fields(*field_names) if field_names.any? @fields = field_names.map(&:to_s) else @fields.empty? ? default_fields : @fields end end |
#filterable(*field_names) ⇒ Array<String>
Gets or sets the filterable fields.
68 69 70 71 |
# File 'lib/quick_admin/resource.rb', line 68 def filterable(*field_names) @filterable_fields = field_names.map(&:to_s) if field_names.any? @filterable_fields end |
#find_by_field ⇒ String
Returns the field to use for finding records. If param_key is set, uses that. Otherwise, auto-detects if model overrides to_param and tries to find the matching field.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/quick_admin/resource.rb', line 109 def find_by_field return @param_key if @param_key.present? # Auto-detect: check if model overrides to_param if model_class && model_class.instance_methods(false).include?(:to_param) # Common patterns: slug, uuid, token, etc. %w[slug uuid token permalink].each do |field| return field if model_class.column_names.include?(field) end end 'id' end |
#model_class ⇒ Class?
Returns the actual model class for this resource. Attempts to constantize the model name.
32 33 34 35 36 37 |
# File 'lib/quick_admin/resource.rb', line 32 def model_class @model_class ||= model_name.classify.constantize rescue NameError => e Rails.logger.warn "QuickAdmin: Could not load model class for #{model_name}: #{e.}" nil end |
#name(display_name = nil) ⇒ String
Gets or sets the display name for this resource.
86 87 88 89 |
# File 'lib/quick_admin/resource.rb', line 86 def name(display_name = nil) @display_name = display_name if display_name @display_name end |
#param(field = nil) ⇒ String
Gets or sets the param key used for finding records. Use this when your model overrides to_param (e.g., uses slug instead of id).
99 100 101 102 |
# File 'lib/quick_admin/resource.rb', line 99 def param(field = nil) @param_key = field.to_s if field @param_key end |
#searchable(*field_names) ⇒ Array<String>
Gets or sets the searchable fields.
59 60 61 62 |
# File 'lib/quick_admin/resource.rb', line 59 def searchable(*field_names) @searchable_fields = field_names.map(&:to_s) if field_names.any? @searchable_fields end |