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

Attributes inherited from BaseField

#action, #as_avatar, #autocomplete, #block, #computable, #computed, #computed_value, #copyable, #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.



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

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

  # You can pass it an array of db columns [:latitude, :longitude]
  @stored_as = args[:stored_as]
end

Instance Method Details

#as_lat_long_field_id(get) ⇒ Object



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

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



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

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



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

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



102
103
104
105
106
107
108
# File 'lib/avo/fields/location_field.rb', line 102

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

#default_mapkick_options(render_static_map) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/avo/fields/location_field.rb', line 24

def default_mapkick_options(render_static_map)
  default_options = if render_static_map
    {
      width: 300,
      height: 300
    }
  else
    {
      id: "location-map",
      zoom: @args[:zoom]&.to_i || 15,
      controls: true
    }
  end

  default_options.merge(@args[:mapkick_options] || {})
end

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/avo/fields/location_field.rb', line 69

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

#render_map(params) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/avo/fields/location_field.rb', line 14

def render_map(params)
  return "" if !value_present?

  render_static_map = params[:action] == "preview" || @args[:static]

  Avo::Current.view_context.send render_static_map ? :static_map : :js_map,
    [{latitude: value[0], longitude: value[1]}],
     **default_mapkick_options(render_static_map)
end

#to_permitted_paramObject



80
81
82
83
84
85
86
# File 'lib/avo/fields/location_field.rb', line 80

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

#valueObject



88
89
90
91
92
93
94
# File 'lib/avo/fields/location_field.rb', line 88

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)


41
42
43
# File 'lib/avo/fields/location_field.rb', line 41

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

#value_present?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/avo/fields/location_field.rb', line 96

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

  value.present?
end