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.



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

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)


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

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

#deep_cloneObject

Deep clone the column



103
104
105
# File 'app/models/spree/admin/table/column.rb', line 103

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

#inspectObject



107
108
109
# File 'app/models/spree/admin/table/column.rb', line 107

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

#resolve_labelObject

Resolve label (handles i18n keys)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/spree/admin/table/column.rb', line 76

def resolve_label
  if label.is_a?(String) && label.include?('.')
    # Dotted keys may live under the `spree` namespace (e.g. `admin.num_orders`,
    # `price_list_statuses.active`) or at the I18n root (e.g. `activerecord.attributes.*`).
    # Prefer the `spree`-scoped lookup, then fall back to the root lookup.
    scoped = Spree.t(label, default: '')
    return scoped if scoped.present?

    return I18n.t(label, default: label.split('.').last.humanize)
  end

  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



94
95
96
97
98
99
100
# File 'app/models/spree/admin/table/column.rb', line 94

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