Class: Kabk::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/kabk/resource.rb

Overview

Metadata value object for a registered resource

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathString

Returns The base API path where the resource is mounted (e.g., "/api/admin/news").

Returns:

  • (String)

    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_fieldsArray<String>

Returns Configures standard audit fields (created_at, updated_at).

Returns:

  • (Array<String>)

    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_fieldString

Returns The field used for Optimistic Concurrency Control (OCC), usually "updated_at".

Returns:

  • (String)

    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_sortString

Returns The default sort column and direction (e.g., "-created_at").

Returns:

  • (String)

    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_sidebarBoolean

Returns Whether to display this resource in the admin sidebar menu.

Returns:

  • (Boolean)

    Whether to display this resource in the admin sidebar menu.



17
18
19
# File 'lib/kabk/resource.rb', line 17

def display_in_sidebar
  @display_in_sidebar
end

#fieldsArray<Kabk::Field>

Returns The array of registered field objects.

Returns:

  • (Array<Kabk::Field>)

    The array of registered field objects.



39
40
41
# File 'lib/kabk/resource.rb', line 39

def fields
  @fields
end

#filterable_fieldsArray<String>

Returns List of field names that can be filtered on.

Returns:

  • (Array<String>)

    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

#groupString

Returns The menu group category for this resource.

Returns:

  • (String)

    The menu group category for this resource.



19
20
21
# File 'lib/kabk/resource.rb', line 19

def group
  @group
end

#iconString

Returns The identifier of the icon to render in the sidebar.

Returns:

  • (String)

    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_classClass

Returns The underlying Sequel Model class (e.g., NewsItem).

Returns:

  • (Class)

    The underlying Sequel Model class (e.g., NewsItem).



41
42
43
# File 'lib/kabk/resource.rb', line 41

def model_class
  @model_class
end

#nameString

Returns The technical singular name of the resource (e.g., "news_item").

Returns:

  • (String)

    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

#orderInteger

Returns The explicit ordering weight in the sidebar.

Returns:

  • (Integer)

    The explicit ordering weight in the sidebar.



21
22
23
# File 'lib/kabk/resource.rb', line 21

def order
  @order
end

#per_page_defaultInteger

Returns Default number of records per page for pagination.

Returns:

  • (Integer)

    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

#permissionsHash

Returns The baseline CRUD permissions (can_view, can_edit, etc).

Returns:

  • (Hash)

    The baseline CRUD permissions (can_view, can_edit, etc).



37
38
39
# File 'lib/kabk/resource.rb', line 37

def permissions
  @permissions
end

#plural_nameString

Returns The plural name used in routing (e.g., "news").

Returns:

  • (String)

    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_fieldsArray<String>

Returns List of field names that support full-text search.

Returns:

  • (Array<String>)

    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_fieldsArray<String>

Returns List of field names that can be sorted by.

Returns:

  • (Array<String>)

    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

#titleHash

Returns Localization hash for the resource title (e.g., { fa: "اخبار", en: "News" }).

Returns:

  • (Hash)

    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_hObject



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