Class: Spree::CustomFieldDefinition

Inherits:
Object
  • Object
show all
Includes:
SearchCapabilities, SingleStoreResource
Defined in:
app/models/spree/custom_field_definition.rb,
app/models/spree/custom_field_definition/search_capabilities.rb

Defined Under Namespace

Modules: SearchCapabilities

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchCapabilities

#filter_key

Class Method Details

.api_type_resolver(attribute) ⇒ Object

Both columns store class names; filters take the short names clients see.



48
49
50
51
52
53
54
# File 'app/models/spree/custom_field_definition.rb', line 48

def self.api_type_resolver(attribute)
  case attribute
  when 'resource_type' then ->(api_type) { normalize_value_for(:resource_type, api_type) }
  when 'field_type' then ->(api_type) { Spree::CustomField::TYPE_TOKENS[api_type] }
  else super
  end
end

.available_resources ⇒ Array<Class>

Returns the available resources

Returns:

  • (Array<Class>)


156
157
158
# File 'app/models/spree/custom_field_definition.rb', line 156

def self.available_resources
  Spree.custom_fields.enabled_resources
end

.available_types ⇒ Array<Class>

Returns the available types

Returns:

  • (Array<Class>)


150
151
152
# File 'app/models/spree/custom_field_definition.rb', line 150

def self.available_types
  Spree.custom_fields.types
end

.enabled_resource_types ⇒ Array<Hash{Symbol => String}>

What a definition can be attached to: every registered resource, named as the API names it, so a client renders a picker rather than carrying its own list that drifts from the registry.

Returns:

  • (Array<Hash{Symbol => String}>) —

    resource_type shorthand + name



127
128
129
130
131
132
133
134
# File 'app/models/spree/custom_field_definition.rb', line 127

def self.enabled_resource_types
  # Through `available_resources`, not the registry directly: that is the
  # accessor the rest of the class reads, so discovery and validation
  # cannot disagree about what a host app has made available.
  Array(available_resources).compact.map do |resource|
    { resource_type: Spree::Base.polymorphic_api_type(resource), name: resource.to_s.demodulize.titleize.pluralize }
  end.uniq { |entry| entry[:resource_type] }.sort_by { |entry| entry[:name] }
end

Instance Method Details

#csv_header_name ⇒ String

Returns the CSV header name for this custom field

Returns:

  • (String) —

    eg. custom_field.custom.id



144
145
146
# File 'app/models/spree/custom_field_definition.rb', line 144

def csv_header_name
  "custom_field.#{full_key}"
end

#display_on ⇒ Object

The tri-state display_on collapsed into the storefront_visible boolean; 'front_end' folds into visible since it never meant hidden-from-storefront.



112
113
114
115
# File 'app/models/spree/custom_field_definition.rb', line 112

def display_on
  Spree::Deprecation.warn('#display_on is deprecated and will be removed in Spree 6.3. Use #storefront_visible instead.')
  storefront_visible? ? 'both' : 'back_end'
end

#display_on=(value) ⇒ Object



117
118
119
120
# File 'app/models/spree/custom_field_definition.rb', line 117

def display_on=(value)
  Spree::Deprecation.warn('#display_on= is deprecated and will be removed in Spree 6.3. Use #storefront_visible= instead.')
  self.storefront_visible = value.to_s != 'back_end'
end

#field_type ⇒ Object

API-facing token for the STI subclass name stored in the field_type column. Reader returns the registered token (short_text); writer accepts either the token or the class-name form, which is what the column itself holds.



61
62
63
# File 'app/models/spree/custom_field_definition.rb', line 61

def field_type
  Spree::CustomField::TYPE_CLASS_TO_TOKEN[self[:field_type]] || self[:field_type]
end

#field_type=(value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/custom_field_definition.rb', line 65

def field_type=(value)
  string_value = value.to_s
  mapped = Spree::CustomField::TYPE_TOKENS[string_value]
  # An input is "recognized" when it's a known token (mapped to a class) or
  # any registered class name — extensions register their own types, which
  # have no token. Anything else gets surfaced as an error on `field_type`
  # so API clients get a token-vocabulary message instead of a raw
  # class-name inclusion error.
  @field_type_input_recognized = !mapped.nil? || valid_available_types.include?(string_value)
  super(mapped || value)
end

#field_type_class_name ⇒ String

The raw STI class name stored in the column, as opposed to the API token returned by #field_type.

Returns:

  • (String)


80
81
82
# File 'app/models/spree/custom_field_definition.rb', line 80

def field_type_class_name
  self[:field_type]
end

#full_key ⇒ String

Returns the full key with namespace

Returns:

  • (String) —

    eg. custom.id



138
139
140
# File 'app/models/spree/custom_field_definition.rb', line 138

def full_key
  "#{namespace}.#{key}"
end

#metafield_type ⇒ Object



97
98
99
100
101
102
103
# File 'app/models/spree/custom_field_definition.rb', line 97

def metafield_type
  Spree::Deprecation.warn(
    '#metafield_type is deprecated and will be removed in Spree 6.3. ' \
    'Use #field_type (which returns an API token) or #field_type_class_name instead.'
  )
  field_type_class_name
end

#metafield_type=(value) ⇒ Object



105
106
107
108
# File 'app/models/spree/custom_field_definition.rb', line 105

def metafield_type=(value)
  Spree::Deprecation.warn('#metafield_type= is deprecated and will be removed in Spree 6.3. Use #field_type= instead.')
  self.field_type = value
end

#name ⇒ Object

Deprecated 6.0 column names, removed in 6.3



87
88
89
90
# File 'app/models/spree/custom_field_definition.rb', line 87

def name
  Spree::Deprecation.warn('#name is deprecated and will be removed in Spree 6.3. Use #label instead.')
  label
end

#name=(value) ⇒ Object



92
93
94
95
# File 'app/models/spree/custom_field_definition.rb', line 92

def name=(value)
  Spree::Deprecation.warn('#name= is deprecated and will be removed in Spree 6.3. Use #label= instead.')
  self.label = value
end