Class: JSAdmin::Field
- Inherits:
-
Object
- Object
- JSAdmin::Field
- Defined in:
- lib/js_admin/field.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #association_options ⇒ Object
- #belongs_to_association ⇒ Object
- #editable? ⇒ Boolean
- #enum? ⇒ Boolean
- #enum_values ⇒ Object
- #formatted_value(record) ⇒ Object
- #human_name ⇒ Object
-
#initialize(resource, column) ⇒ Field
constructor
A new instance of Field.
- #input_kind ⇒ Object
- #name ⇒ Object
- #number_step ⇒ Object
- #primary_key? ⇒ Boolean
- #timestamp? ⇒ Boolean
- #type ⇒ Object
- #value_for(record) ⇒ Object
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
#column ⇒ Object (readonly)
Returns the value of attribute column.
3 4 5 |
# File 'lib/js_admin/field.rb', line 3 def column @column end |
#resource ⇒ Object (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_options ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/js_admin/field.rb', line 76 def 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_association ⇒ Object
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
30 31 32 33 34 35 36 |
# File 'lib/js_admin/field.rb', line 30 def editable? !primary_key? && ! && !inheritance_column? && !readonly_attribute? && !generated_column? end |
#enum? ⇒ Boolean
60 61 62 |
# File 'lib/js_admin/field.rb', line 60 def enum? enum_values.any? end |
#enum_values ⇒ Object
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_name ⇒ Object
14 15 16 |
# File 'lib/js_admin/field.rb', line 14 def human_name resource.model_class.human_attribute_name(name) end |
#input_kind ⇒ Object
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 |
#name ⇒ Object
10 11 12 |
# File 'lib/js_admin/field.rb', line 10 def name column.name end |
#number_step ⇒ Object
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
22 23 24 |
# File 'lib/js_admin/field.rb', line 22 def primary_key? name == resource.model_class.primary_key end |
#timestamp? ⇒ Boolean
26 27 28 |
# File 'lib/js_admin/field.rb', line 26 def %w[created_at updated_at].include?(name) end |
#type ⇒ Object
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 |