Class: Avo::LexxyField::Fields::LexxyField

Inherits:
Fields::BaseField
  • Object
show all
Defined in:
lib/avo/lexxy_field/fields/lexxy_field.rb

Constant Summary collapse

EDITOR_OPTIONS =

Lexxy's per-editor options, which it reads off the element as dasherized attributes and JSON-parses. attachments is omitted on purpose — attachments_disabled below owns it. See https://lexxy.dev/docs/ for what each one takes.

%i[
  preset
  markdown
  rich_text
  multi_line
  headings
  toolbar
  highlight
  permitted_attachment_types
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, **args, &block) ⇒ LexxyField

Returns a new instance of LexxyField.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 22

def initialize(id, **args, &block)
  super(id, **args, &block)

  hide_on :index

  # A rich text editor needs the whole row. `full_width` on the wrapper
  # only widens it inside the side-by-side content column, so stack it
  # too — `stacked: false` still opts back out.
  @stacked = args.fetch(:stacked, true)

  @always_show = args[:always_show] || false
  @attachments_disabled = args[:attachments_disabled]
  @editor_options = args.slice(*EDITOR_OPTIONS)
end

Instance Attribute Details

#always_showObject (readonly)

Returns the value of attribute always_show.



20
21
22
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 20

def always_show
  @always_show
end

Instance Method Details

#attachments_disabledObject



52
53
54
55
56
57
58
59
60
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 52

def attachments_disabled
  # Return the value of attachments_disabled if explicitly provided
  return @attachments_disabled unless @attachments_disabled.nil?

  # Lexxy uploads through Active Storage direct uploads and relies on
  # Action Text to attach the blobs to the record on save. On plain
  # columns the uploads would remain orphaned blobs, so disable them.
  !is_action_text?
end

#component_for_view(view) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 66

def component_for_view(view)
  view = Avo::ViewInquirer.new(view)

  if view.edit? && (is_readonly? || is_disabled?)
    Avo::Fields::LexxyField::ShowComponent
  else
    super
  end
end

#editor_attributesObject

The editor options as element attributes. Anything that isn't already a string goes over as JSON, which is what Lexxy parses it back from.



39
40
41
42
43
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 39

def editor_attributes
  @editor_options.to_h do |name, value|
    [name.to_s.dasherize, value.is_a?(String) || value.is_a?(Symbol) ? value.to_s : value.to_json]
  end
end

#is_action_text?Boolean

Identify if field is bonded to a rich text model attribute

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 46

def is_action_text?
  return false if !defined?(ActionText::RichText) || record.nil? || !record.respond_to?(id)

  record.send(id).is_a?(ActionText::RichText)
end

#view_component_namespaceObject



62
63
64
# File 'lib/avo/lexxy_field/fields/lexxy_field.rb', line 62

def view_component_namespace
  "Avo::Fields::LexxyField"
end