Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: improve fields data structure
  • Loading branch information
Mattia Roccoberton committed Apr 17, 2023
commit a734bed0661bc961e51c088e152d1b7cd582a287
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
4 changes: 2 additions & 2 deletions lib/tiny_admin/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,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
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
7 changes: 2 additions & 5 deletions lib/tiny_admin/views/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ 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') {
Expand All @@ -30,7 +27,7 @@ def template
}

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 +36,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
13 changes: 3 additions & 10 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 @@ -33,8 +26,8 @@ def template
}
}

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 Down