Class: Avo::Fields::DateTimeField

Inherits:
DateField show all
Defined in:
lib/avo/fields/date_time_field.rb

Direct Known Subclasses

TimeField

Defined Under Namespace

Classes: EditComponent, IndexComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from DateField

#disable_mobile, #first_day_of_week, #picker_options

Attributes inherited from TextField

#as_html, #link_to_resource, #protocol

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::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, #to_permitted_param, #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::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) ⇒ DateTimeField

Returns a new instance of DateTimeField.



10
11
12
13
14
15
16
17
18
# File 'lib/avo/fields/date_time_field.rb', line 10

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

  add_boolean_prop args, :time_24hr
  add_string_prop args, :picker_format, "Y-m-d H:i:S"
  add_string_prop args, :format, "yyyy-LL-dd TT"
  add_string_prop args, :timezone
  add_boolean_prop args, :relative, true
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/avo/fields/date_time_field.rb', line 4

def format
  @format
end

#picker_formatObject (readonly)

Returns the value of attribute picker_format.



5
6
7
# File 'lib/avo/fields/date_time_field.rb', line 5

def picker_format
  @picker_format
end

#relativeObject (readonly)

Returns the value of attribute relative.



8
9
10
# File 'lib/avo/fields/date_time_field.rb', line 8

def relative
  @relative
end

#time_24hrObject (readonly)

Returns the value of attribute time_24hr.



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

def time_24hr
  @time_24hr
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



7
8
9
# File 'lib/avo/fields/date_time_field.rb', line 7

def timezone
  @timezone
end

Instance Method Details

#edit_formatted_valueObject



26
27
28
29
30
# File 'lib/avo/fields/date_time_field.rb', line 26

def edit_formatted_value
  return nil if value.nil?

  value.utc.iso8601
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/avo/fields/date_time_field.rb', line 32

def fill_field(model, key, value, params)
  if value.in?(["", nil])
    model[id] = value

    return model
  end

  return model if value.blank?

  model[id] = utc_time(value)

  model
end

#formatted_valueObject



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

def formatted_value
  return nil if value.nil?

  value.utc.to_time.iso8601
end

#utc_time(value) ⇒ Object



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

def utc_time(value)
  if timezone.present?
    ActiveSupport::TimeZone.new(timezone).local_to_utc(Time.parse(value))
  else
    value
  end
end