Class: HakumiComponents::Rails::AttributeIntrospection

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hakumi_components/rails/attribute_introspection.rb

Class Method Summary collapse

Class Method Details

.column_for(object, attribute) ⇒ Object



54
55
56
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 54

def column_for(object, attribute)
  ModelReflection.for(object).column_for(attribute)
end

.column_limit(object, attribute) ⇒ Object



81
82
83
84
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 81

def column_limit(object, attribute)
  column = column_for(object, attribute)
  column&.limit
end

.hint(object, attribute) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 27

def hint(object, attribute)
  return nil unless defined?(I18n)
  model_name = ModelReflection.for(object).model_i18n_key
  return nil unless model_name

  key = "helpers.hint.#{model_name}.#{attribute}"

  I18n.t(key, default: "").presence
end

.human_attribute_name(object, attribute) ⇒ Object



11
12
13
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 11

def human_attribute_name(object, attribute)
  ModelReflection.for(object).human_attribute_name(attribute)
end

.input_type_from_column(object, attribute) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 38

def input_type_from_column(object, attribute)
  column = ModelReflection.for(object).column_for(attribute)
  return :text unless column&.type

  case column.type
  when :integer, :decimal, :float then :number
  when :date then :date
  when :datetime, :timestamp then :datetime_local
  when :time then :time
  when :boolean then :checkbox
  when :text then :textarea
  else :text
  end
end

.numeric_column?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 59

def numeric_column?(object, attribute)
  column = column_for(object, attribute)
  return false unless column&.type

  case column.type
  when :integer, :decimal, :float then true
  else false
  end
end

.placeholder(object, attribute) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 16

def placeholder(object, attribute)
  return nil unless defined?(I18n)
  model_name = ModelReflection.for(object).model_i18n_key
  return nil unless model_name

  key = "helpers.placeholder.#{model_name}.#{attribute}"

  I18n.t(key, default: "").presence
end

.text_column?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/hakumi_components/rails/attribute_introspection.rb', line 70

def text_column?(object, attribute)
  column = column_for(object, attribute)
  return true unless column&.type

  case column.type
  when :string, :text then true
  else false
  end
end