Module: RailsAdmin::Config::HasFields
- Included in:
- Sections::Base
- Defined in:
- lib/rails_admin/config/has_fields.rb
Overview
Provides accessors and autoregistering of model’s fields.
Instance Method Summary collapse
-
#all_fields ⇒ Object
Accessor for all fields.
-
#configure(name, type = nil, &block) ⇒ Object
configure a field without adding it.
-
#exclude_fields(*field_names, &block) ⇒ Object
(also: #exclude_fields_if)
exclude fields by name or by condition (block).
-
#field(name, type = nil, add_to_section = true, &block) ⇒ Object
Defines a configuration for a field.
-
#fields(*field_names, &block) ⇒ Object
Returns all field configurations for the model configuration instance.
-
#fields_of_type(type, &block) ⇒ Object
Defines configuration for fields by their type.
- #include_all_fields ⇒ Object
-
#include_fields(*field_names, &block) ⇒ Object
(also: #include_fields_if)
include fields by name and apply an optionnal block to each (through a call to fields), or include fields by conditions if no field names.
-
#visible_fields ⇒ Object
Get all fields defined as visible, in the correct order.
Instance Method Details
#all_fields ⇒ Object
Accessor for all fields
104 105 106 107 108 109 |
# File 'lib/rails_admin/config/has_fields.rb', line 104 def all_fields ((ro_fields = _fields(true)).select(&:defined).presence || ro_fields).collect do |f| f.section = self f end end |
#configure(name, type = nil, &block) ⇒ Object
configure a field without adding it.
39 40 41 |
# File 'lib/rails_admin/config/has_fields.rb', line 39 def configure(name, type = nil, &block) field(name, type, false, &block) end |
#exclude_fields(*field_names, &block) ⇒ Object Also known as: exclude_fields_if
exclude fields by name or by condition (block)
59 60 61 62 63 |
# File 'lib/rails_admin/config/has_fields.rb', line 59 def exclude_fields(*field_names, &block) block ||= lambda { |f| field_names.include?(f.name) } _fields.each { |f| f.defined = true } if _fields.select(&:defined).empty? _fields.select { |f| f.instance_eval(&block) }.each { |f| f.defined = false } end |
#field(name, type = nil, add_to_section = true, &block) ⇒ Object
Defines a configuration for a field.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_admin/config/has_fields.rb', line 6 def field(name, type = nil, add_to_section = true, &block) field = _fields.detect { |f| name == f.name } # some fields are hidden by default (belongs_to keys, has_many associations in list views.) # unhide them if config specifically defines them if field field.show unless field.instance_variable_get("@#{field.name}_registered").is_a?(Proc) end # Specify field as virtual if type is not specifically set and field was not # found in default stack if field.nil? && type.nil? field = (_fields << RailsAdmin::Config::Fields::Types.load(:string).new(self, name, nil)).last # Register a custom field type if one is provided and it is different from # one found in default stack elsif type && type != (field.nil? ? nil : field.type) _fields.delete(field) unless field.nil? properties = abstract_model.properties.detect { |p| name == p.name } field = (_fields << RailsAdmin::Config::Fields::Types.load(type).new(self, name, properties)).last end # If field has not been yet defined add some default properties if add_to_section && !field.defined field.defined = true field.order = _fields.select(&:defined).length end # If a block has been given evaluate it and sort fields after that field.instance_eval(&block) if block field end |
#fields(*field_names, &block) ⇒ Object
Returns all field configurations for the model configuration instance. If no fields have been defined returns all fields. Defined fields are sorted to match their order property. If order was not specified it will match the order in which fields were defined.
If a block is passed it will be evaluated in the context of each field
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rails_admin/config/has_fields.rb', line 79 def fields(*field_names, &block) return all_fields if field_names.empty? && !block if field_names.empty? defined = _fields.select { |f| f.defined } defined = _fields if defined.empty? else defined = field_names.collect { |field_name| _fields.detect { |f| f.name == field_name } } end defined.collect do |f| unless f.defined f.defined = true f.order = _fields.select(&:defined).length end f.instance_eval(&block) if block f end end |
#fields_of_type(type, &block) ⇒ Object
Defines configuration for fields by their type.
99 100 101 |
# File 'lib/rails_admin/config/has_fields.rb', line 99 def fields_of_type(type, &block) _fields.select { |f| type == f.type }.map! { |f| f.instance_eval(&block) } if block end |
#include_all_fields ⇒ Object
69 70 71 |
# File 'lib/rails_admin/config/has_fields.rb', line 69 def include_all_fields include_fields_if { true } end |
#include_fields(*field_names, &block) ⇒ Object Also known as: include_fields_if
include fields by name and apply an optionnal block to each (through a call to fields), or include fields by conditions if no field names
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rails_admin/config/has_fields.rb', line 45 def include_fields(*field_names, &block) if field_names.empty? _fields.select { |f| f.instance_eval(&block) }.each do |f| unless f.defined f.defined = true f.order = _fields.select(&:defined).length end end else fields(*field_names, &block) end end |
#visible_fields ⇒ Object
Get all fields defined as visible, in the correct order.
112 113 114 115 |
# File 'lib/rails_admin/config/has_fields.rb', line 112 def visible_fields i = 0 all_fields.collect { |f| f.with(bindings) }.select(&:visible?).sort_by { |f| [f.order, i += 1] } # stable sort, damn end |