Class: Airview::Field
- Inherits:
-
Object
- Object
- Airview::Field
- Defined in:
- lib/airview/field.rb,
sig/airview.rbs
Constant Summary collapse
- TYPES =
%i[ string text integer float decimal boolean date datetime select belongs_to has_many attachment json ].freeze
Instance Attribute Summary collapse
-
#label ⇒ String
readonly
Returns the value of attribute label.
-
#label_method ⇒ Symbol
readonly
Returns the value of attribute label_method.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Symbol
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Symbol
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #association? ⇒ Boolean
- #attachment? ⇒ Boolean
- #attribute_name ⇒ Symbol
- #collection_association? ⇒ Boolean
- #default_visible? ⇒ Boolean
- #display_only? ⇒ Boolean
- #editable? ⇒ Boolean
- #filterable? ⇒ Boolean
-
#initialize(name, type:, label: nil, readonly: false, options: nil, model: nil, label_method: nil, default_visible: true) ⇒ Field
constructor
A new instance of Field.
- #model_class ⇒ Object
- #option_values ⇒ Array[untyped]
- #readonly? ⇒ Boolean
- #sortable? ⇒ Boolean
- #validate! ⇒ void
Constructor Details
#initialize(name, type:, label: nil, readonly: false, options: nil, model: nil, label_method: nil, default_visible: true) ⇒ Field
Returns a new instance of Field.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/airview/field.rb', line 11 def initialize(name, type:, label: nil, readonly: false, options: nil, model: nil, label_method: nil, default_visible: true) @name = name.to_sym @label = label.presence || name.to_s.humanize @type = type.to_sym @readonly = readonly @options = @model = model @label_method = label_method || :to_s @default_visible = default_visible end |
Instance Attribute Details
#label ⇒ String (readonly)
Returns the value of attribute label.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def label @label end |
#label_method ⇒ Symbol (readonly)
Returns the value of attribute label_method.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def label_method @label_method end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def model @model end |
#name ⇒ Symbol (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def @options end |
#type ⇒ Symbol (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/airview/field.rb', line 9 def type @type end |
Instance Method Details
#association? ⇒ Boolean
35 36 37 |
# File 'lib/airview/field.rb', line 35 def association? type.in?(%i[belongs_to has_many]) end |
#attachment? ⇒ Boolean
43 44 45 |
# File 'lib/airview/field.rb', line 43 def type == :attachment end |
#attribute_name ⇒ Symbol
59 60 61 |
# File 'lib/airview/field.rb', line 59 def attribute_name type == :belongs_to ? :"#{name}_id" : name end |
#collection_association? ⇒ Boolean
39 40 41 |
# File 'lib/airview/field.rb', line 39 def collection_association? type == :has_many end |
#default_visible? ⇒ Boolean
31 32 33 |
# File 'lib/airview/field.rb', line 31 def default_visible? @default_visible end |
#display_only? ⇒ Boolean
47 48 49 |
# File 'lib/airview/field.rb', line 47 def display_only? collection_association? || end |
#editable? ⇒ Boolean
27 28 29 |
# File 'lib/airview/field.rb', line 27 def editable? !readonly? && !display_only? end |
#filterable? ⇒ Boolean
51 52 53 |
# File 'lib/airview/field.rb', line 51 def filterable? !display_only? end |
#model_class ⇒ Object
69 70 71 72 73 |
# File 'lib/airview/field.rb', line 69 def model_class return nil unless model model.to_s.constantize end |
#option_values ⇒ Array[untyped]
75 76 77 78 79 |
# File 'lib/airview/field.rb', line 75 def option_values return [] unless .respond_to?(:call) ? .call : end |
#readonly? ⇒ Boolean
23 24 25 |
# File 'lib/airview/field.rb', line 23 def readonly? @readonly end |
#sortable? ⇒ Boolean
55 56 57 |
# File 'lib/airview/field.rb', line 55 def sortable? !display_only? end |
#validate! ⇒ void
This method returns an undefined value.
63 64 65 66 67 |
# File 'lib/airview/field.rb', line 63 def validate! return if TYPES.include?(type) raise ConfigurationError, "Unsupported Airview field type #{type.inspect} for #{name}" end |