Skip to content

Commit 5aa0eba

Browse files
authored
Merge pull request #6 from blocknotes/docs/improve-documentation-2
docs: improve documentation 2
2 parents 9b7006a + 0fdf95c commit 5aa0eba

File tree

7 files changed

+245
-3
lines changed

7 files changed

+245
-3
lines changed

README.md

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Tiny Admin
22

3+
[![Gem Version](https://badge.fury.io/rb/tiny_admin.svg)](https://badge.fury.io/rb/tiny_admin) [![Linters](https://github.com/blocknotes/tiny_admin/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/tiny_admin/actions/workflows/linters.yml) [![Specs Rails 7.0](https://github.com/blocknotes/tiny_admin/actions/workflows/specs_rails_70.yml/badge.svg)](https://github.com/blocknotes/tiny_admin/actions/workflows/specs_rails_70.yml)
4+
35
A compact and composable dashboard component for Ruby.
46

57
The main features are:
@@ -14,7 +16,7 @@ Please ⭐ if you like it.
1416

1517
## Install
1618

17-
- Add to your Gemfile: `gem 'tiny_admin'`
19+
- Add to your Gemfile: `gem 'tiny_admin', '~> 0.1'`
1820
- Mount the app in a route (check some examples with: Hanami, Rails, Roda and standalone in [extra](extra))
1921
- Configure the dashboard using `TinyAdmin.configure` and/or `TinyAdmin.configure_from_file` (see [configuration](#configuration) below)
2022

@@ -53,10 +55,98 @@ Components available:
5355
## Configuration
5456

5557
TinyAdmin can be configured using a YAML file and/or programmatically.
56-
5758
See [extra](extra) folder for some usage examples.
5859

59-
Sample:
60+
The following options are supported:
61+
62+
`root` (Hash): define the root section of the admin, properties:
63+
- `title` (String): root section's title;
64+
- `page` (String): a view object to render;
65+
- `redirect` (String): alternative to _page_ option - redirects to a specific slug;
66+
67+
Example:
68+
69+
```yml
70+
root:
71+
title: MyAdmin
72+
redirect: posts
73+
```
74+
75+
`authentication` (Hash): define the authentication method, properties:
76+
- `plugin` (String): a plugin class to use (ex. `TinyAdmin::Plugins::SimpleAuth`);
77+
- `password` (String): a password hash used by _SimpleAuth_ plugin (generated with `Digest::SHA512.hexdigest("some password")`).
78+
79+
Example:
80+
81+
```yml
82+
authentication:
83+
plugin: TinyAdmin::Plugins::SimpleAuth
84+
password: 'f1891cea80fc05e433c943254c6bdabc159577a02a7395dfebbfbc4f7661d4af56f2d372131a45936de40160007368a56ef216a30cb202c66d3145fd24380906'
85+
```
86+
87+
`sections` (Array of hashes): define the admin sections, properties:
88+
- `slug` (String): section reference identifier;
89+
- `name` (String): section's title;
90+
- `type` (String): the type of section: `url`, `page` or `resource`;
91+
- other properties depends on the section's type.
92+
93+
For _url_ sections:
94+
- `url` (String): the URL to load when clicking on the section's menu item;
95+
- `options` (Hash): properties:
96+
+ `target` (String): link _target_ attributes (ex. `_blank`).
97+
98+
Example:
99+
100+
```yml
101+
slug: google
102+
name: Google.it
103+
type: url
104+
url: https://www.google.it
105+
options:
106+
target: '_blank'
107+
```
108+
109+
For _page_ sections:
110+
- `page` (String): a view object to render.
111+
112+
Example:
113+
114+
```yml
115+
slug: stats
116+
name: Stats
117+
type: page
118+
page: Admin::Stats
119+
```
120+
121+
For _resource_ sections:
122+
- `model` (String): the class to use to fetch the data on an item of a collection;
123+
- `repository` (String): the class to get the properties related to the model;
124+
- `index` (Hash): collection's action options;
125+
- `show` (Hash): detail's action options;
126+
- `collection_actions` (Array of hashes): custom collection's actions;
127+
- `member_actions` (Array of hashes): custom details's actions;
128+
- `only` (Array of strings): list of supported actions (ex. `index`);
129+
- `options` (Array of strings): resource options (ex. `hidden`).
130+
131+
Example:
132+
133+
```yml
134+
slug: posts
135+
name: Posts
136+
type: resource
137+
model: Post
138+
```
139+
140+
`style_links` (Array of hashes): list of styles files to include, properties:
141+
- `href` (String): URL for the style file;
142+
- `rel` (String): type of style file.
143+
144+
`scripts` (Array of hashes): list of scripts to include, properties:
145+
- `src` (String): source URL for the script.
146+
147+
`extra_styles` (String): inline CSS styles.
148+
149+
### Sample
60150

61151
```rb
62152
# config/initializers/tiny_admin.rb
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
COLUMNS = %i[id first_col second_col third_col].freeze
5+
6+
Column = Struct.new(:name, :title, :type, :options)
7+
Item = Struct.new(*COLUMNS)
8+
9+
RECORDS = [
10+
Item.new(1, 'value a1', 'value a2', 'value a3'),
11+
Item.new(2, 'value b1', 'value b2', 'value b3'),
12+
Item.new(3, 'value c1', 'value c2', 'value c3')
13+
].freeze
14+
15+
class ItemsRepo < TinyAdmin::Plugins::BaseRepository
16+
def fields(options: nil)
17+
COLUMNS.map do |name|
18+
Column.new(name, name.to_s.tr('_', ' '), :string, {})
19+
end
20+
end
21+
22+
def index_record_attrs(record, fields: nil)
23+
record.to_h
24+
end
25+
26+
def index_title
27+
"Items"
28+
end
29+
30+
def list(page: 1, limit: 10, filters: nil, sort: ['id'])
31+
[
32+
RECORDS,
33+
RECORDS.size
34+
]
35+
end
36+
37+
# ---
38+
39+
def find(reference)
40+
RECORDS.find { _1.id == reference.to_i } || raise(TinyAdmin::Plugins::BaseRepository::RecordNotFound)
41+
end
42+
43+
def show_record_attrs(record, fields: nil)
44+
record.to_h
45+
end
46+
47+
def show_title(_record)
48+
"Item"
49+
end
50+
end
51+
end
52+
53+
TinyAdmin.configure do |settings|
54+
(settings.sections ||= []).push(
55+
slug: 'items',
56+
name: 'Items',
57+
type: :resource,
58+
model: Admin::Item,
59+
repository: Admin::ItemsRepo
60+
)
61+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
TinyAdmin.configure do |settings|
4+
(settings.sections ||= []).push(
5+
slug: 'missing-page',
6+
name: 'Missing Page',
7+
type: :url,
8+
url: '/missing-page'
9+
)
10+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
class SamplePage < TinyAdmin::Views::DefaultLayout
5+
def template
6+
super do
7+
h1 { 'Sample page' }
8+
p { 'This is a sample page' }
9+
end
10+
end
11+
end
12+
end
13+
14+
TinyAdmin.configure do |settings|
15+
(settings.sections ||= []).push(
16+
slug: 'sample-page',
17+
name: 'Sample Page',
18+
type: :page,
19+
page: Admin::SamplePage
20+
)
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
class SamplePage2 < TinyAdmin::Views::DefaultLayout
5+
def template
6+
super do
7+
h1 { 'Sample page 2' }
8+
p { 'This is another sample page' }
9+
end
10+
end
11+
end
12+
end
13+
14+
TinyAdmin.configure do |settings|
15+
(settings.sections ||= []).push(
16+
slug: 'sample-page-2',
17+
name: 'Sample Page 2',
18+
type: :page,
19+
page: Admin::SamplePage2
20+
)
21+
end

extra/sample_features_app/app.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# --- dependencies -------------------------------------------------------------
4+
begin
5+
require 'bundler/inline'
6+
rescue LoadError => e
7+
abort "#{e} - Bundler version 1.10 or later is required. Please update your Bundler"
8+
end
9+
10+
gemfile(true) do
11+
source 'https://rubygems.org'
12+
13+
gem 'rack'
14+
gem 'rackup'
15+
gem 'webrick'
16+
17+
gem 'tiny_admin', path: '../../'
18+
end
19+
20+
# --- Sample features application ----------------------------------------------
21+
Dir[File.expand_path('admin/**/*.rb', __dir__)].each { |f| require f }
22+
23+
TinyAdmin.configure_from_file('./tiny_admin.yml')
24+
25+
Rackup::Server.new(app: TinyAdmin::Router, Port: 3000).start
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
root_path: '/'
3+
root:
4+
redirect: sample-page
5+
extra_styles: >
6+
.navbar {
7+
background-color: var(--bs-teal);
8+
}
9+
.main-content {
10+
background-color: var(--bs-gray-100);
11+
}
12+
.main-content a {
13+
text-decoration: none;
14+
}

0 commit comments

Comments
 (0)