Class: Compony::ModelFields::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/compony/model_fields/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_attrsObject (readonly)



7
8
9
# File 'lib/compony/model_fields/base.rb', line 7

def extra_attrs
  @extra_attrs
end

#model_classObject (readonly)



5
6
7
# File 'lib/compony/model_fields/base.rb', line 5

def model_class
  @model_class
end

#nameObject (readonly)



4
5
6
# File 'lib/compony/model_fields/base.rb', line 4

def name
  @name
end

#schema_keyObject (readonly)



6
7
8
# File 'lib/compony/model_fields/base.rb', line 6

def schema_key
  @schema_key
end

Instance Method Details

#association?Boolean

Returns:



13
14
15
# File 'lib/compony/model_fields/base.rb', line 13

def association?
  !!@association
end

#labelObject

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

#multi?Boolean

Returns:



9
10
11
# File 'lib/compony/model_fields/base.rb', line 9

def multi?
  !!@multi
end

#schema_lineObject

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