Class: QuickAdmin::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(model_name) ⇒ Resource

Initializes a new Resource.

Parameters:

  • the name of the model



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_nameString

human-friendly name for the resource

Returns:

  • the current value of display_name



11
12
13
# File 'lib/quick_admin/resource.rb', line 11

def display_name
  @display_name
end

#editable_fieldsArray<String>

fields that can be edited

Returns:

  • the current value of editable_fields



11
12
13
# File 'lib/quick_admin/resource.rb', line 11

def editable_fields
  @editable_fields
end

#filterable_fieldsArray<String>

fields that can be filtered

Returns:

  • the current value of filterable_fields



11
12
13
# File 'lib/quick_admin/resource.rb', line 11

def filterable_fields
  @filterable_fields
end

#model_nameString

the name of the model

Returns:

  • the current value of model_name



11
12
13
# File 'lib/quick_admin/resource.rb', line 11

def model_name
  @model_name
end

#param_keyString

the field to use for finding records (default: id)

Returns:

  • the current value of param_key



11
12
13
# File 'lib/quick_admin/resource.rb', line 11

def param_key
  @param_key
end

#searchable_fieldsArray<String>

fields that can be searched

Returns:

  • the current value of searchable_fields



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.

Parameters:

  • optional field names to set

Returns:

  • the list of editable field names



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.

Examples:

r.fields :name, :email, :created_at  # setter
r.fields                              # getter

Parameters:

  • optional field names to set

Returns:

  • the list of field names



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.

Parameters:

  • optional field names to set

Returns:

  • the list of filterable field names



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_fieldString

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.

Returns:

  • the field name to use for finding records



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_classClass?

Returns the actual model class for this resource. Attempts to constantize the model name.

Returns:

  • the model class or nil if not found



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.message}"
  nil
end

#name(display_name = nil) ⇒ String

Gets or sets the display name for this resource.

Parameters:

  • (defaults to: nil)

    optional display name to set

Returns:

  • the display name



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).

Examples:

r.param :slug  # Use slug field for finding records

Parameters:

  • (defaults to: nil)

    optional field name to set

Returns:

  • the param key field name



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.

Parameters:

  • optional field names to set

Returns:

  • the list of searchable field names



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