Class: Spree::Base
Constant Summary
Constants included
from PrefixedId
PrefixedId::SQIDS
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from PrefixedId
#assign_attributes, decode_prefixed_id, #prefixed_id, prefixed_id?, #to_param
#event_context, #event_payload, #event_prefix, #event_serializer_class, #publish_event, #touch
#store_integration, #store_integrations
#serialized_preferences
#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_change, #preference_default, #preference_deprecated, #preference_type, #preferences_of_type, #restore_preferences_for, #set_preference
Class Method Details
.api_type ⇒ Object
Public-API shorthand for this class, used as the type value on the
wire (e.g. "currency" for Spree::Promotion::Rules::Currency,
"flat_rate" for Spree::Calculator::FlatRate). Defaults to the
demodulized + underscored leaf; override on a subclass when the wire
format should stay stable across class renames.
85
86
87
|
# File 'app/models/spree/base.rb', line 85
def self.api_type
to_s.demodulize.underscore
end
|
.api_type_for(type) ⇒ String
.api_type for an STI type column value, without instantiating the
subclass — use this in serializers instead of record.class.api_type,
which reports the loaded class and so returns the base type for a record
read through the parent (a plain Spree::Export row, a factory that sets
type as an attribute, a query on the base relation).
Resolves against available_types where the class defines it, so an
unrecognized column value passes through untouched rather than being
constantized.
107
108
109
110
111
112
113
114
|
# File 'app/models/spree/base.rb', line 107
def self.api_type_for(type)
return api_type if type.blank?
type = type.to_s
return type unless respond_to?(:available_types)
available_types.find { |klass| klass.to_s == type }&.api_type || type
end
|
.belongs_to_required_by_default ⇒ Object
30
31
32
|
# File 'app/models/spree/base.rb', line 30
def self.belongs_to_required_by_default
false
end
|
.for_store(store) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'app/models/spree/base.rb', line 34
def self.for_store(store)
plural_model_name = model_name.plural.gsub(/spree_/, '').to_sym
if store.respond_to?(plural_model_name)
store.send(plural_model_name)
else
self
end
end
|
.has_many_inversing ⇒ Object
53
54
55
|
# File 'app/models/spree/base.rb', line 53
def self.has_many_inversing
false
end
|
.json_api_columns ⇒ Object
66
67
68
|
# File 'app/models/spree/base.rb', line 66
def self.json_api_columns
column_names.reject { |c| c.match(/_id$|id|preferences|(.*)password|(.*)token|(.*)api_key|^original_(.*)/) }
end
|
.json_api_permitted_attributes ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'app/models/spree/base.rb', line 70
def self.json_api_permitted_attributes
skipped_attributes = %w[id]
if included_modules.include?(CollectiveIdea::Acts::NestedSet::Model)
skipped_attributes.push('lft', 'rgt', 'depth')
end
column_names.reject { |c| skipped_attributes.include?(c.to_s) }
end
|
.json_api_type ⇒ Object
Backwards-compatible alias for .api_type. Delegates so subclass
overrides of api_type are honored.
91
92
93
|
# File 'app/models/spree/base.rb', line 91
def self.json_api_type
api_type
end
|
.page(num) ⇒ Object
19
20
21
|
# File 'app/models/spree/base.rb', line 19
def self.page(num)
send Kaminari.config.page_method_name, num
end
|
.polymorphic_api_type(type) ⇒ String?
Shorthand for a polymorphic *_type column (owner_type,
viewable_type, …), where the value names an arbitrary model rather than a
subclass of the serialized one — so api_type_for's registry lookup does
not apply. Demodulizes and underscores the same way api_type does, giving
"Spree::Product" => "product".
124
125
126
127
128
|
# File 'app/models/spree/base.rb', line 124
def self.polymorphic_api_type(type)
return nil if type.blank?
type.to_s.demodulize.underscore
end
|
.spree_base_scopes ⇒ Object
44
45
46
|
# File 'app/models/spree/base.rb', line 44
def self.spree_base_scopes
where(nil)
end
|
.spree_base_uniqueness_scope ⇒ Object
48
49
50
|
# File 'app/models/spree/base.rb', line 48
def self.spree_base_uniqueness_scope
ApplicationRecord.try(:spree_base_uniqueness_scope) || []
end
|
.to_tom_select_json ⇒ Object
130
131
132
133
134
135
136
137
|
# File 'app/models/spree/base.rb', line 130
def self.to_tom_select_json
pluck(:name, :id).map do |name, id|
{
id: id,
name: name
}
end.as_json
end
|
Instance Method Details
#can_be_deleted? ⇒ Boolean
this can overridden in subclasses to disallow deletion
58
59
60
|
# File 'app/models/spree/base.rb', line 58
def can_be_deleted?
true
end
|
#mysql_adapter? ⇒ Boolean
62
63
64
|
# File 'app/models/spree/base.rb', line 62
def mysql_adapter?
ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql')
end
|
#slug_candidates ⇒ Object
Try building a slug based on the following fields in increasing order of specificity.
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'app/models/spree/base.rb', line 144
def slug_candidates
if defined?(deleted_at) && deleted_at.present?
[
['deleted', :name],
['deleted', :name, :uuid_for_friendly_id]
]
else
[
[:name],
[:name, :uuid_for_friendly_id]
]
end
end
|
#uuid_for_friendly_id ⇒ Object
139
140
141
|
# File 'app/models/spree/base.rb', line 139
def uuid_for_friendly_id
SecureRandom.uuid
end
|