lib/admin_it/field/field.rb in admin_it-1.2.8 vs lib/admin_it/field/field.rb in admin_it-1.3.0

- old
+ new

@@ -26,10 +26,11 @@ dsl do dsl_accessor :type, default: TYPES[0] dsl_accessor :placeholder dsl_accessor :partial + dsl_accessor :options, default: {} dsl_accessor :editor, default: EDITORS[0] dsl_boolean :readable, :writable, :visible, :sortable, :show_label dsl_block :read, :write, :render, :display def hide @@ -40,11 +41,11 @@ dsl_set(:visible, true) end end class << self - attr_reader :read, :write, :render, :display, :type, :partial + attr_reader :read, :write, :render, :display, :type, :partial, :options protected def default_display_name field_name @@ -76,16 +77,21 @@ def self.create(name, _entity_class, **opts) base = self Class.new(base) do @field_name, @entity_class = name, _entity_class import_data_module(base) + self.type = opts[:type] @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 + @options = opts[:options].is_a?(Hash) ? opts[:options] : {} + if type == :image + @options[:s3] = {} unless @options[:s3].is_a?(Hash) + @options[:s3] = {}.merge(AdminIt.config.s3, @options[:s3]) + end @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) @@ -102,10 +108,14 @@ def self.partial @partial ||= nil end + def self.optionis + @options ||= {} + end + def self.hide @visible = false end def self.show @@ -118,14 +128,16 @@ return @editor = :combo if type == :enum return @editor = :geo_picker if type == :geo_point @editor = EDITORS[0] end - class_attr_reader :entity_class, :display_name, :type, :partial, :editor + class_attr_reader :entity_class, :display_name, :type, :partial, :editor, + :options attr_writer :visible, :readable, :writable - def initialize(readable: nil, writable: nil, visible: nil, sortable: nil, show_label: nil) + def initialize(readable: nil, writable: nil, visible: nil, sortable: nil, + show_label: nil, options: 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 @@ -224,10 +236,10 @@ # fail NotImplementedError, # "Attempt to write to field #{name} with unimplemented writer" # end def show_value(entity) - value = read_value(entity) + value = read(entity) if type == :enum value.text elsif type == :geo_point value.nil? ? '' : "#{value.x}, #{value.y}" else