Class: Compony::ModelFields::Base
- Inherits:
-
Object
- Object
- Compony::ModelFields::Base
- Defined in:
- lib/compony/model_fields/base.rb
Direct Known Subclasses
Anchormodel, Association, Attachment, Boolean, Color, Currency, Date, Datetime, Decimal, Email, Float, Integer, Percentage, Phone, RichText, String, Text, Time, Url
Instance Attribute Summary collapse
- #extra_attrs ⇒ Object readonly
- #model_class ⇒ Object readonly
- #name ⇒ Object readonly
- #schema_key ⇒ Object readonly
Instance Method Summary collapse
- #association? ⇒ Boolean
-
#initialize(name, model_class, **extra_attrs) ⇒ Base
constructor
A new instance of Base.
-
#label ⇒ Object
Use this to display the label for this field, e.g.
- #multi? ⇒ Boolean
-
#schema_line ⇒ Object
Used for auto-providing Schemacop schemas.
-
#simpleform_input(form, _component, name: nil, **input_opts) ⇒ Object
Used in form helper.
-
#simpleform_input_hidden(form, _component, name: nil, **input_opts) ⇒ Object
Used in form helper Given a simpleform instance, returns a suitable hidden input for thetype.
-
#transform_and_join(data, controller:, &transform_block) ⇒ Object
protected
If given a scalar, calls the block on the scalar.
-
#value_for(data, controller: nil, **_) ⇒ Object
Use this to display the value for this field applied to data.
Constructor Details
#initialize(name, model_class, **extra_attrs) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 |
# File 'lib/compony/model_fields/base.rb', line 17 def initialize(name, model_class, **extra_attrs) @name = name.to_sym @model_class = model_class @schema_key = name.to_sym @extra_attrs = extra_attrs end |
Instance Attribute Details
#extra_attrs ⇒ Object (readonly)
7 8 9 |
# File 'lib/compony/model_fields/base.rb', line 7 def extra_attrs @extra_attrs end |
#model_class ⇒ Object (readonly)
5 6 7 |
# File 'lib/compony/model_fields/base.rb', line 5 def model_class @model_class end |
#name ⇒ Object (readonly)
4 5 6 |
# File 'lib/compony/model_fields/base.rb', line 4 def name @name end |
#schema_key ⇒ Object (readonly)
6 7 8 |
# File 'lib/compony/model_fields/base.rb', line 6 def schema_key @schema_key end |
Instance Method Details
#association? ⇒ Boolean
13 14 15 |
# File 'lib/compony/model_fields/base.rb', line 13 def association? !!@association end |
#label ⇒ Object
Use this to display the label for this field, e.g. for columns, forms etc.
25 26 27 |
# File 'lib/compony/model_fields/base.rb', line 25 def label @model_class.human_attribute_name(@name) end |
#schema_line ⇒ Object
Used for auto-providing Schemacop schemas. Returns a proc that is meant for instance_exec within a Schemacop3 hash block
37 38 39 40 41 |
# File 'lib/compony/model_fields/base.rb', line 37 def schema_line # Default behavior local_schema_key = @schema_key # Capture schema_key as it will not be available within the lambda return proc { obj? local_schema_key } end |
#simpleform_input(form, _component, name: nil, **input_opts) ⇒ Object
Used in form helper. Given a simpleform instance, returns the corresponding input to be supplied to the view.
45 46 47 |
# File 'lib/compony/model_fields/base.rb', line 45 def simpleform_input(form, _component, name: nil, **input_opts) return form.input name || @name, **input_opts end |
#simpleform_input_hidden(form, _component, name: nil, **input_opts) ⇒ Object
Used in form helper Given a simpleform instance, returns a suitable hidden input for thetype
51 52 53 |
# File 'lib/compony/model_fields/base.rb', line 51 def simpleform_input_hidden(form, _component, name: nil, **input_opts) return form.input name || @name, as: :hidden, **input_opts end |
#transform_and_join(data, controller:, &transform_block) ⇒ Object (protected)
If given a scalar, calls the block on the scalar. If given a list, calls the block on every member and joins the result with “,”.
58 59 60 61 62 63 64 65 66 |
# File 'lib/compony/model_fields/base.rb', line 58 def transform_and_join(data, controller:, &transform_block) if data.is_a?(Enumerable) data = data.compact.map(&transform_block) if block_given? return controller.helpers.safe_join(data.compact, ', ') else data = transform_block.call(data) if block_given? return data end end |
#value_for(data, controller: nil, **_) ⇒ Object
Use this to display the value for this field applied to data
30 31 32 33 |
# File 'lib/compony/model_fields/base.rb', line 30 def value_for(data, controller: nil, **_) # Default behavior return transform_and_join(data.send(@name), controller:) end |