Class: JSAdmin::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/js_admin/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, column) ⇒ Field

Returns a new instance of Field.



5
6
7
8
# File 'lib/js_admin/field.rb', line 5

def initialize(resource, column)
  @resource = resource
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



3
4
5
# File 'lib/js_admin/field.rb', line 3

def column
  @column
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/js_admin/field.rb', line 3

def resource
  @resource
end

Instance Method Details

#association_optionsObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/js_admin/field.rb', line 76

def association_options
  association = belongs_to_association
  return [] unless association

  association_resource = Resource.new(association.klass)
  association.klass
    .limit(JSAdmin.configuration.association_limit)
    .map { |record| [ association_resource.display_name(record), record.id ] }
rescue ActiveRecord::ActiveRecordError, NameError
  []
end

#belongs_to_associationObject



68
69
70
71
72
73
74
# File 'lib/js_admin/field.rb', line 68

def belongs_to_association
  @belongs_to_association ||= resource.model_class
    .reflect_on_all_associations(:belongs_to)
    .find { |association| association.foreign_key.to_s == name }
rescue NameError
  nil
end

#editable?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/js_admin/field.rb', line 30

def editable?
  !primary_key? &&
    !timestamp? &&
    !inheritance_column? &&
    !readonly_attribute? &&
    !generated_column?
end

#enum?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/js_admin/field.rb', line 60

def enum?
  enum_values.any?
end

#enum_valuesObject



64
65
66
# File 'lib/js_admin/field.rb', line 64

def enum_values
  resource.model_class.defined_enums.fetch(name, {}).keys
end

#formatted_value(record) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/js_admin/field.rb', line 96

def formatted_value(record)
  value = value_for(record)

  case value
  when nil
    "NULL"
  when true
    "true"
  when false
    "false"
  else
    value.to_s
  end
end

#human_nameObject



14
15
16
# File 'lib/js_admin/field.rb', line 14

def human_name
  resource.model_class.human_attribute_name(name)
end

#input_kindObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/js_admin/field.rb', line 38

def input_kind
  return :association_select if belongs_to_association
  return :enum_select if enum?

  case type
  when :text
    :text_area
  when :boolean
    :check_box
  when :integer, :bigint, :float, :decimal
    :number_field
  when :date
    :date_field
  when :datetime
    :datetime_field
  when :time
    :time_field
  else
    :text_field
  end
end

#nameObject



10
11
12
# File 'lib/js_admin/field.rb', line 10

def name
  column.name
end

#number_stepObject



88
89
90
# File 'lib/js_admin/field.rb', line 88

def number_step
  %i[float decimal].include?(type) ? "any" : 1
end

#primary_key?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/js_admin/field.rb', line 22

def primary_key?
  name == resource.model_class.primary_key
end

#timestamp?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/js_admin/field.rb', line 26

def timestamp?
  %w[created_at updated_at].include?(name)
end

#typeObject



18
19
20
# File 'lib/js_admin/field.rb', line 18

def type
  column.type
end

#value_for(record) ⇒ Object



92
93
94
# File 'lib/js_admin/field.rb', line 92

def value_for(record)
  record.public_send(name)
end