Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extra/sample_features_app/admin/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Admin

class ItemsRepo < ::TinyAdmin::Plugins::BaseRepository
def fields(options: nil)
COLUMNS.map do |name|
Column.new(name, name.to_s.tr('_', ' '), :string, {})
COLUMNS.each_with_object({}) do |name, result|
result[name] = TinyAdmin::Field.create_field(name: name)
end
end

Expand Down
7 changes: 0 additions & 7 deletions lib/tiny_admin/actions/basic_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ module Actions
class BasicAction
include Utils

attr_reader :params, :repository

def initialize(repository, params:)
@repository = repository
@params = params
end

def attribute_options(options)
options&.each_with_object({}) do |field, result|
field_data =
Expand Down
16 changes: 13 additions & 3 deletions lib/tiny_admin/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
module TinyAdmin
module Actions
class Index < BasicAction
attr_reader :current_page, :fields_options, :filters_list, :pagination, :pages, :query_string, :sort
attr_reader :current_page,
:fields_options,
:filters_list,
:pagination,
:pages,
:params,
:query_string,
:repository,
:sort

def call(app:, context:, options:, actions:)
evaluate_options(options)
Expand All @@ -28,6 +36,8 @@ def call(app:, context:, options:, actions:)

def evaluate_options(options)
@fields_options = attribute_options(options[:attributes])
@params = context.request.params
@repository = context.repository
@filters_list = options[:filters]
@pagination = options[:pagination] || 10
@sort = options[:sort] || ['id']
Expand All @@ -40,8 +50,8 @@ def prepare_filters(fields, filters_list)
filters = (filters_list || []).map { _1.is_a?(Hash) ? _1 : { field: _1 } }
filters = filters.each_with_object({}) { |filter, result| result[filter[:field]] = filter }
values = (params['q'] || {})
fields.each_with_object({}) do |field, result|
result[field] = { value: values[field.name], filter: filters[field.name] } if filters.key?(field.name)
fields.each_with_object({}) do |(name, field), result|
result[field] = { value: values[name], filter: filters[name] } if filters.key?(name)
end
end

Expand Down
14 changes: 10 additions & 4 deletions lib/tiny_admin/actions/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
module TinyAdmin
module Actions
class Show < BasicAction
attr_reader :repository

def call(app:, context:, options:, actions:)
@repository = context.repository
fields_options = attribute_options(options[:attributes])
record = repository.find(context.reference)
prepare_record = ->(record_data) { repository.show_record_attrs(record_data, fields: fields_options) }
fields = repository.fields(options: fields_options)

prepare_page(Views::Actions::Show) do |page|
page.setup_record(record: record, fields: fields, prepare_record: prepare_record)
page.update_attributes(actions: actions, title: repository.show_title(record))
page.update_attributes(
actions: actions,
fields: repository.fields(options: fields_options),
prepare_record: ->(record_data) { repository.show_record_attrs(record_data, fields: fields_options) },
record: record,
title: repository.show_title(record)
)
end
rescue Plugins::BaseRepository::RecordNotFound => _e
prepare_page(options[:record_not_found_page] || Views::Pages::RecordNotFound)
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module TinyAdmin
class Context
include Singleton

attr_accessor :reference, :router, :settings, :slug
attr_accessor :reference, :repository, :request, :router, :settings, :slug
end
end
9 changes: 7 additions & 2 deletions lib/tiny_admin/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ def initialize(name:, title:, type:, options: {})

class << self
def create_field(name:, title: nil, type: nil, options: {})
field_title = title || name.respond_to?(:humanize) ? name.humanize : name.tr('_', ' ').capitalize
new(name: name, title: field_title, type: type || :string, options: options)
field_name = name.to_s
new(
name: field_name,
title: title || field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize,
type: type || :string,
options: options
)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tiny_admin/plugins/active_record_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def index_title
def fields(options: nil)
if options
types = model.columns.to_h { [_1.name, _1.type] }
options.map do |name, field_options|
TinyAdmin::Field.create_field(name: name, type: types[name], options: field_options)
options.each_with_object({}) do |(name, field_options), result|
result[name] = TinyAdmin::Field.create_field(name: name, type: types[name], options: field_options)
end
else
model.columns.map do |column|
TinyAdmin::Field.create_field(name: column.name, type: column.type)
model.columns.each_with_object({}) do |column, result|
result[column.name] = TinyAdmin::Field.create_field(name: column.name, type: column.type)
end
end
end
Expand Down
18 changes: 11 additions & 7 deletions lib/tiny_admin/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,30 @@ def setup_resource_routes(router, slug, options:)
end

def setup_collection_routes(router, options:)
repository = options[:repository].new(options[:model])
context.repository = options[:repository].new(options[:model])
action_options = options[:index] || {}

# Custom actions
custom_actions = setup_custom_actions(
router,
options[:collection_actions],
repository: repository,
repository: context.repository,
options: action_options
)

# Index
actions = options[:only]
if !actions || actions.include?(:index) || actions.include?('index')
router.is do
index_action = TinyAdmin::Actions::Index.new(repository, params: request.params)
context.request = request
index_action = TinyAdmin::Actions::Index.new
render_page index_action.call(app: self, context: context, options: action_options, actions: custom_actions)
end
end
end

def setup_member_routes(router, options:)
repository = options[:repository].new(options[:model])
context.repository = options[:repository].new(options[:model])
action_options = (options[:show] || {}).merge(record_not_found_page: settings.record_not_found)

router.on String do |reference|
Expand All @@ -108,28 +109,31 @@ def setup_member_routes(router, options:)
custom_actions = setup_custom_actions(
router,
options[:member_actions],
repository: repository,
repository: context.repository,
options: action_options
)

# Show
actions = options[:only]
if !actions || actions.include?(:show) || actions.include?('show')
router.is do
show_action = TinyAdmin::Actions::Show.new(repository, params: request.params)
context.request = request
show_action = TinyAdmin::Actions::Show.new
render_page show_action.call(app: self, context: context, options: action_options, actions: custom_actions)
end
end
end
end

def setup_custom_actions(router, custom_actions, repository:, options:)
context.repository = repository
(custom_actions || []).each_with_object({}) do |custom_action, result|
action_slug, action = custom_action.first
action_class = action.is_a?(String) ? Object.const_get(action) : action

router.get action_slug.to_s do
custom_action = action_class.new(repository, params: request.params)
context.request = request
custom_action = action_class.new
render_page custom_action.call(app: self, context: context, options: options)
end

Expand Down
29 changes: 15 additions & 14 deletions lib/tiny_admin/views/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,19 @@ class Index < DefaultLayout
attr_accessor :actions, :fields, :filters, :pagination_component, :prepare_record, :records

def template
@fields = fields.each_with_object({}) { |field, result| result[field.name] = field }
@filters ||= {}

super do
div(class: 'index') {
div(class: 'row') {
div(class: 'col-4') {
h1(class: 'title') { title }
}
div(class: 'col-8') {
ul(class: 'nav justify-content-end') {
(actions || {}).each do |action, action_class|
li(class: 'nav-item mx-1') {
href = route_for(context.slug, action: action)
title = action_class.respond_to?(:title) ? action_class.title : action
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
}
end
}
actions_buttons
}
}

div(class: 'row') {
div_class = filters.any? ? 'col-9' : 'col-12'
div_class = filters&.any? ? 'col-9' : 'col-12'
div(class: div_class) {
table(class: 'table') {
table_header if fields.any?
Expand All @@ -39,7 +28,7 @@ def template
}
}

if filters.any?
if filters&.any?
div(class: 'col-3') {
filters_form_attrs = { section_path: route_for(context.slug), filters: filters }
render TinyAdmin::Views::Components::FiltersForm.new(**filters_form_attrs)
Expand Down Expand Up @@ -88,6 +77,18 @@ def table_body
end
}
end

def actions_buttons
ul(class: 'nav justify-content-end') {
(actions || {}).each do |action, action_class|
li(class: 'nav-item mx-1') {
href = route_for(context.slug, action: action)
title = action_class.respond_to?(:title) ? action_class.title : action
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
}
end
}
end
end
end
end
Expand Down
37 changes: 18 additions & 19 deletions lib/tiny_admin/views/actions/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ module TinyAdmin
module Views
module Actions
class Show < DefaultLayout
attr_reader :fields, :prepare_record, :record
attr_accessor :actions

def setup_record(record:, fields:, prepare_record:)
@record = record
@fields = fields
@prepare_record = prepare_record
end
attr_accessor :actions, :fields, :prepare_record, :record

def template
super do
Expand All @@ -21,20 +14,12 @@ def template
h1(class: 'title') { title }
}
div(class: 'col-8') {
ul(class: 'nav justify-content-end') {
(actions || {}).each do |action, action_class|
li(class: 'nav-item mx-1') {
href = route_for(context.slug, reference: context.reference, action: action)
title = action_class.respond_to?(:title) ? action_class.title : action
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
}
end
}
actions_buttons
}
}

prepare_record.call(record).each_with_index do |(_key, value), index|
field = fields[index]
prepare_record.call(record).each do |key, value|
field = fields[key]
div(class: "field-#{field.name} row lh-lg") {
if field
div(class: 'field-header col-2') { field.title }
Expand All @@ -52,6 +37,20 @@ def template
}
end
end

private

def actions_buttons
ul(class: 'nav justify-content-end') {
(actions || {}).each do |action, action_class|
li(class: 'nav-item mx-1') {
href = route_for(context.slug, reference: context.reference, action: action)
title = action_class.respond_to?(:title) ? action_class.title : action
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
}
end
}
end
end
end
end
Expand Down