Class: Kabk::Resource
- Inherits:
-
Object
- Object
- Kabk::Resource
- Defined in:
- lib/kabk/resource.rb
Overview
Metadata value object for a registered resource
Instance Attribute Summary collapse
-
#api_path ⇒ String
The base API path where the resource is mounted (e.g., "/api/admin/news").
-
#audit_fields ⇒ Array<String>
Configures standard audit fields (created_at, updated_at).
-
#concurrency_field ⇒ String
The field used for Optimistic Concurrency Control (OCC), usually "updated_at".
-
#default_sort ⇒ String
The default sort column and direction (e.g., "-created_at").
-
#display_in_sidebar ⇒ Boolean
Whether to display this resource in the admin sidebar menu.
-
#fields ⇒ Array<Kabk::Field>
The array of registered field objects.
-
#filterable_fields ⇒ Array<String>
List of field names that can be filtered on.
-
#group ⇒ String
The menu group category for this resource.
-
#icon ⇒ String
The identifier of the icon to render in the sidebar.
-
#model_class ⇒ Class
The underlying Sequel Model class (e.g., NewsItem).
-
#name ⇒ String
The technical singular name of the resource (e.g., "news_item").
-
#order ⇒ Integer
The explicit ordering weight in the sidebar.
-
#per_page_default ⇒ Integer
Default number of records per page for pagination.
-
#permissions ⇒ Hash
The baseline CRUD permissions (can_view, can_edit, etc).
-
#plural_name ⇒ String
The plural name used in routing (e.g., "news").
-
#searchable_fields ⇒ Array<String>
List of field names that support full-text search.
-
#sortable_fields ⇒ Array<String>
List of field names that can be sorted by.
-
#title ⇒ Hash
Localization hash for the resource title (e.g., { fa: "اخبار", en: "News" }).
Instance Method Summary collapse
-
#initialize(name:, model_class:) ⇒ Resource
constructor
A new instance of Resource.
- #to_h ⇒ Object
Constructor Details
#initialize(name:, model_class:) ⇒ Resource
Returns a new instance of Resource.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kabk/resource.rb', line 43 def initialize(name:, model_class:) @name = name.to_s @model_class = model_class @fields = [] @display_in_sidebar = true @per_page_default = 15 @permissions = { can_view: true, can_insert: true, can_edit: true, can_delete: true, can_export: true } end |
Instance Attribute Details
#api_path ⇒ String
Returns The base API path where the resource is mounted (e.g., "/api/admin/news").
15 16 17 |
# File 'lib/kabk/resource.rb', line 15 def api_path @api_path end |
#audit_fields ⇒ Array<String>
Returns Configures standard audit fields (created_at, updated_at).
35 36 37 |
# File 'lib/kabk/resource.rb', line 35 def audit_fields @audit_fields end |
#concurrency_field ⇒ String
Returns The field used for Optimistic Concurrency Control (OCC), usually "updated_at".
33 34 35 |
# File 'lib/kabk/resource.rb', line 33 def concurrency_field @concurrency_field end |
#default_sort ⇒ String
Returns The default sort column and direction (e.g., "-created_at").
23 24 25 |
# File 'lib/kabk/resource.rb', line 23 def default_sort @default_sort end |
#display_in_sidebar ⇒ Boolean
Returns Whether to display this resource in the admin sidebar menu.
17 18 19 |
# File 'lib/kabk/resource.rb', line 17 def @display_in_sidebar end |
#fields ⇒ Array<Kabk::Field>
Returns The array of registered field objects.
39 40 41 |
# File 'lib/kabk/resource.rb', line 39 def fields @fields end |
#filterable_fields ⇒ Array<String>
Returns List of field names that can be filtered on.
31 32 33 |
# File 'lib/kabk/resource.rb', line 31 def filterable_fields @filterable_fields end |
#group ⇒ String
Returns The menu group category for this resource.
19 20 21 |
# File 'lib/kabk/resource.rb', line 19 def group @group end |
#icon ⇒ String
Returns The identifier of the icon to render in the sidebar.
13 14 15 |
# File 'lib/kabk/resource.rb', line 13 def icon @icon end |
#model_class ⇒ Class
Returns The underlying Sequel Model class (e.g., NewsItem).
41 42 43 |
# File 'lib/kabk/resource.rb', line 41 def model_class @model_class end |
#name ⇒ String
Returns The technical singular name of the resource (e.g., "news_item").
7 8 9 |
# File 'lib/kabk/resource.rb', line 7 def name @name end |
#order ⇒ Integer
Returns The explicit ordering weight in the sidebar.
21 22 23 |
# File 'lib/kabk/resource.rb', line 21 def order @order end |
#per_page_default ⇒ Integer
Returns Default number of records per page for pagination.
25 26 27 |
# File 'lib/kabk/resource.rb', line 25 def per_page_default @per_page_default end |
#permissions ⇒ Hash
Returns The baseline CRUD permissions (can_view, can_edit, etc).
37 38 39 |
# File 'lib/kabk/resource.rb', line 37 def @permissions end |
#plural_name ⇒ String
Returns The plural name used in routing (e.g., "news").
9 10 11 |
# File 'lib/kabk/resource.rb', line 9 def plural_name @plural_name end |
#searchable_fields ⇒ Array<String>
Returns List of field names that support full-text search.
27 28 29 |
# File 'lib/kabk/resource.rb', line 27 def searchable_fields @searchable_fields end |
#sortable_fields ⇒ Array<String>
Returns List of field names that can be sorted by.
29 30 31 |
# File 'lib/kabk/resource.rb', line 29 def sortable_fields @sortable_fields end |
#title ⇒ Hash
Returns Localization hash for the resource title (e.g., { fa: "اخبار", en: "News" }).
11 12 13 |
# File 'lib/kabk/resource.rb', line 11 def title @title end |
Instance Method Details
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/kabk/resource.rb', line 58 def to_h hash = { name: @name, plural_name: @plural_name, title: @title, icon: @icon, api_path: @api_path } hash[:display_in_sidebar] = @display_in_sidebar unless @display_in_sidebar.nil? hash[:group] = @group if @group hash[:order] = @order if @order hash[:default_sort] = @default_sort if @default_sort hash[:per_page_default] = @per_page_default if @per_page_default hash[:searchable_fields] = @searchable_fields if @searchable_fields hash[:sortable_fields] = @sortable_fields if @sortable_fields hash[:filterable_fields] = @filterable_fields if @filterable_fields hash[:concurrency_field] = @concurrency_field if @concurrency_field hash[:audit_fields] = @audit_fields if @audit_fields hash[:permissions] = @permissions if @permissions hash[:fields] = @fields.map(&:to_h) hash end |