Class: Spree::Admin::Table::Column

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, Visibility
Defined in:
app/models/spree/admin/table/column.rb

Constant Summary collapse

TYPES =
%w[string number date datetime money status link boolean image custom association].freeze
FILTER_TYPES =
%w[string number date datetime money status boolean autocomplete select].freeze
DEFAULT_OPERATORS =
{
  'string' => %i[eq not_eq cont not_cont start end in not_in null not_null],
  'number' => %i[eq not_eq gt gteq lt lteq in not_in null not_null],
  'money' => %i[eq not_eq gt gteq lt lteq in not_in null not_null],
  'date' => %i[eq not_eq gt gteq lt lteq null not_null],
  'datetime' => %i[eq not_eq gt gteq lt lteq null not_null],
  'boolean' => %i[eq],
  'status' => %i[eq not_eq in not_in],
  'select' => %i[eq not_eq in not_in],
  'association' => %i[in not_in],
  'tags' => %i[in not_in],
  'autocomplete' => %i[in not_in]
}.freeze

Instance Method Summary collapse

Methods included from Visibility

#visible?

Constructor Details

#initialize(attributes = {}) ⇒ Column

Returns a new instance of Column.



60
61
62
63
# File 'app/models/spree/admin/table/column.rb', line 60

def initialize(attributes = {})
  super
  set_defaults
end

Instance Method Details

#custom_sort?Boolean

Check if column uses custom sort scopes instead of ransack

Returns:

  • (Boolean)


66
67
68
# File 'app/models/spree/admin/table/column.rb', line 66

def custom_sort?
  sort_scope_asc.present? || sort_scope_desc.present?
end

#deep_cloneObject

Deep clone the column



89
90
91
# File 'app/models/spree/admin/table/column.rb', line 89

def deep_clone
  self.class.new(**attributes.symbolize_keys)
end

#inspectObject



93
94
95
# File 'app/models/spree/admin/table/column.rb', line 93

def inspect
  "#<Spree::Admin::Table::Column key=#{key} type=#{type} default=#{self.default}>"
end

#resolve_labelObject

Resolve label (handles i18n keys)



71
72
73
74
75
76
77
# File 'app/models/spree/admin/table/column.rb', line 71

def resolve_label
  return I18n.t(label, default: label.split('.').last.humanize) if label.is_a?(String) && label.include?('.')
  return label if label.is_a?(String) && label.present?

  key_to_translate = label || key
  Spree.t(key_to_translate, default: key_to_translate.to_s.humanize)
end

#resolve_value(record, context = nil) ⇒ Object

Resolve value from record



80
81
82
83
84
85
86
# File 'app/models/spree/admin/table/column.rb', line 80

def resolve_value(record, context = nil)
  if self.method.respond_to?(:call)
    context&.respond_to?(:instance_exec) ? context.instance_exec(record, &self.method) : self.method.call(record)
  elsif record.respond_to?(self.method)
    record.send(self.method)
  end
end