lib/admin_it/field/field.rb in admin_it-1.1.0 vs lib/admin_it/field/field.rb in admin_it-1.2.0
- old
+ new
@@ -17,21 +17,21 @@
extend ExtendIt::Dsl
extend DisplayableName
include ExtendIt::Callbacks
TYPES = %i(unknown integer float string date datetime time relation enum
- array hash range regexp symbol binary)
+ array hash range regexp symbol binary image)
+ EDITORS = %i(text combo radio image hidden)
define_callbacks :initialize
dsl do
dsl_accessor :type, default: TYPES[0]
-
dsl_accessor :placeholder
-
- dsl_boolean :readable, :writable, :visible, :sortable
-
+ dsl_accessor :partial
+ dsl_accessor :editor, default: EDITORS[0]
+ dsl_boolean :readable, :writable, :visible, :sortable, :show_label
dsl_block :read, :write, :render, :display
def hide
dsl_set(:visible, false)
end
@@ -40,11 +40,11 @@
dsl_set(:visible, true)
end
end
class << self
- attr_reader :read, :write, :render, :display, :type
+ attr_reader :read, :write, :render, :display, :type, :partial
protected
def default_display_name
field_name
@@ -65,10 +65,14 @@
def self.sortable?
@sortable.nil? ? @sortable = true : @sortable == true
end
+ def self.show_label?
+ @show_label.nil? ? @show_label = true : @show_label == true
+ end
+
inherited_class_reader :field_name, :entity_class
def self.create(name, _entity_class, **opts)
base = self
Class.new(base) do
@@ -76,39 +80,54 @@
import_data_module(base)
@readable = opts[:readable].nil? ? true : opts[:readable] == true
@writable = opts[:writable].nil? ? true : opts[:writable] == true
@visible = opts[:visible].nil? ? true : opts[:visible] == true
@sortable = opts[:sortable].nil? ? true : opts[:sortable] == true
+ @show_label = opts[:show_label].nil? ? true : opts[:show_label] == true
self.type = opts[:type]
+ self.editor = opts[:editor] unless opts[:editor].nil?
end
end
def self.type=(value)
@type = value.ensure_symbol(values: TYPES, default: TYPES[0])
end
+ def self.editor=(value)
+ @editor = value.ensure_symbol(values: EDITORS, default: EDITORS[0])
+ end
+
def self.placeholder
@placeholder ||= display_name
end
+ def self.partial
+ @partial ||= nil
+ end
+
def self.hide
@visible = false
end
def self.show
@visible = true
end
- class_attr_reader :entity_class, :display_name, :type
+ def self.editor
+ @editor ||= EDITORS[0]
+ end
+
+ class_attr_reader :entity_class, :display_name, :type, :partial, :editor
attr_writer :visible, :readable, :writable
- def initialize(readable: nil, writable: nil, visible: nil, sortable: nil)
+ def initialize(readable: nil, writable: nil, visible: nil, sortable: nil, show_label: nil)
run_callbacks :initialize do
@readable = readable.nil? ? self.class.readable? : readable == true
@writable = writable.nil? ? self.class.writable? : writable == true
@visible = visible.nil? ? self.class.visible? : visible == true
@sortable = sortable.nil? ? self.class.sortable? : sortable == true
+ @show_label = show_label.nil? ? self.class.show_label? : show_label == true
end
end
def name
@name ||= self.class.field_name
@@ -124,14 +143,22 @@
def visible?
@visible == true
end
+ def hidden?
+ @visible != true
+ end
+
def sortable?
@sortable == true
end
+ def show_label?
+ @show_label == true
+ end
+
def read(entity)
unless readable?
fail FieldReadError, "Attempt to read write-only field #{name}"
end
self.class.read.nil? ? read_value(entity) : self.class.read.call(entity)
@@ -217,18 +244,26 @@
names.each { |name| hash[name].show if hash.key?(name) }
end
end
def fields(scope: :visible)
+ values = @fields.values
+ if scope.is_a?(Hash)
+ if scope.key?(:editor)
+ return values.select { |f| f.editor == scope[:editor] }
+ end
+ end
case scope
- when nil, :all then @fields.values
- when :visible then @fields.values.select { |f| f.visible? }
- when :hidden then @fields.values.select { |f| !f.visible? }
- when :readable then @fields.values.select { |f| f.readable? }
- when :writable then @fields.values.select { |f| f.writable? }
- when :sortable then @fields.values.select { |f| f.sortable? }
- when Field::TYPES then @fields.values.select { |f| f.type == scope }
- else @fields.values
+ when nil, :all then values
+ when :visible then values.select { |f| f.visible? }
+ when :hidden then values.select { |f| !f.visible? }
+ when :readable then values.select { |f| f.readable? }
+ when :writable then values.select { |f| f.writable? }
+ when :sortable then values.select { |f| f.sortable? }
+ when :with_labels then values.select { |f| f.show_label? }
+ when :without_labels then values.select { |f| !f.show_label? }
+ when Field::TYPES then values.select { |f| f.type == scope }
+ else values
end
end
def field(name)
@fields[name.ensure_symbol]