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, #as_description, #as_label, #block, #computable, #computed, #computed_value, #default, #format_using, #help, #id, #index_text_align, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #stacked, #updatable, #user, #view, #visible

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

Attributes included from FieldExtensions::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_show

Instance Method Summary collapse

Methods inherited from BaseField

#component_for_view, #custom?, #custom_name?, #database_id, #default_name, #has_own_panel?, #hidden_in_reflection?, #hydrate, #model_errors, #name, #placeholder, #plural_name, #resolve_attribute, #translation_key, #type, #value, #view_component_name, #visible?, #visible_in_reflection?

Methods included from FieldExtensions::HasFieldName

#field_name, #get_field_name

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on, #show_on_create, #show_on_update, #visible_on?

Methods included from Concerns::IsResourceItem

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

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]
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

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
    "#{id}[#{stored_as.first}]"
  elsif get == :long
    "#{id}[#{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
    model.send(stored_as.first)
  elsif get == :long
    model.send(stored_as.last)
  end
end

#fill_field(model, 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(model, key, value, params)
  if value_as_array?
    latitude_field, longitude_field = stored_as
    model.send("#{latitude_field}=", value[latitude_field])
    model.send("#{longitude_field}=", value[longitude_field])
    model
  else
    super(model, 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

#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)


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

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

  value.present?
end