Class: Avo::Fields::LocationField

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

Defined Under Namespace

Classes: EditComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from BaseField

#action, #as_avatar, #autocomplete, #block, #computable, #computed, #computed_value, #default, #for_attribute, #for_presentation_only, #format_using, #help, #id, #null_values, #nullable, #panel_name, #readonly, #record, #required, #sortable, #stacked, #summarizable, #user

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

Attributes included from Concerns::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_preview, #show_on_show

Attributes included from Concerns::IsVisible

#visible

Attributes included from Concerns::IsResourceItem

#resource, #view

Instance Method Summary collapse

Methods inherited from BaseField

#apply_update_using, #custom?, #custom_name?, #database_id, #default_name, #execute_block, #form_field_label, #has_attribute?, #has_own_panel?, #hidden_in_reflection?, #meta, #name, #options_for_filter, #placeholder, #plural_name, #record_errors, #resolve_attribute, #table_header_label, #translated_name, #translated_plural_name, #translation_key, #type, #updatable, #visible_in_reflection?

Methods included from Concerns::UseViewComponents

#component_for_view, #view_component_name, #view_component_namespace

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::HasHelpers

#helpers

Methods included from Concerns::VisibleInDifferentViews

#except_on, #hide_on, #initialize_views, #only_on, #post_initialize, #show_on, #show_on_create, #show_on_update, #visible_in_view?

Methods included from Concerns::IsVisible

#visible?

Methods included from Concerns::HasItemType

#is_field?, #is_heading?, #is_main_panel?, #is_panel?, #is_row?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?

Methods included from Concerns::IsResourceItem

#visible?

Methods included from Concerns::Hydration

#hydrate

Constructor Details

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

Returns a new instance of LocationField.



8
9
10
11
12
13
14
# File 'lib/avo/fields/location_field.rb', line 8

def initialize(id, **args, &block)
  hide_on :index
  super(id, **args, &block)

  @stored_as = args[:stored_as].present? ? args[:stored_as] : nil # You can pass it an array of db columns [:latitude, :longitude]
  @zoom = args[:zoom].present? ? args[:zoom].to_i : 15
end

Instance Attribute Details

#stored_asObject (readonly)

Returns the value of attribute stored_as.



6
7
8
# File 'lib/avo/fields/location_field.rb', line 6

def stored_as
  @stored_as
end

#zoomObject (readonly)

Returns the value of attribute zoom.



6
7
8
# File 'lib/avo/fields/location_field.rb', line 6

def zoom
  @zoom
end

Instance Method Details

#as_lat_long_field_id(get) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/avo/fields/location_field.rb', line 20

def as_lat_long_field_id(get)
  if get == :lat
    stored_as.first
  elsif get == :long
    stored_as.last
  end
end

#as_lat_long_placeholder(get) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/avo/fields/location_field.rb', line 28

def as_lat_long_placeholder(get)
  if get == :lat
    "Enter #{stored_as.first}"
  elsif get == :long
    "Enter #{stored_as.last}"
  end
end

#as_lat_long_value(get) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/avo/fields/location_field.rb', line 36

def as_lat_long_value(get)
  if get == :lat
    record.send(stored_as.first)
  elsif get == :long
    record.send(stored_as.last)
  end
end

#assign_value(record:, value:) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/avo/fields/location_field.rb', line 77

def assign_value(record:, value:)
  return super if stored_as.blank?

  stored_as.each_with_index do |database_id, index|
    record.send(:"#{database_id}=", value[index])
  end
end

#fill_field(record, key, value, params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/avo/fields/location_field.rb', line 44

def fill_field(record, key, value, params)
  if value_as_array?
    latitude_field, longitude_field = stored_as
    record.send(:"#{latitude_field}=", value[latitude_field])
    record.send(:"#{longitude_field}=", value[longitude_field])
    record
  else
    super(record, key, value.split(","), params)
  end
end

#to_permitted_paramObject



55
56
57
58
59
60
61
# File 'lib/avo/fields/location_field.rb', line 55

def to_permitted_param
  if value_as_array?
    [:"#{id}", "#{id}": {}]
  else
    super
  end
end

#valueObject



63
64
65
66
67
68
69
# File 'lib/avo/fields/location_field.rb', line 63

def value
  if value_as_array?
    [@record.send(stored_as.first), @record.send(stored_as.last)]
  else
    super
  end
end

#value_as_array?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/avo/fields/location_field.rb', line 16

def value_as_array?
  stored_as.is_a?(Array) && stored_as.count == 2
end

#value_present?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/avo/fields/location_field.rb', line 71

def value_present?
  return value.first.present? && value.second.present? if value.is_a?(Array) && value.count == 2

  value.present?
end