Class: Spree::Base

Inherits:
ApplicationRecord
  • Object
show all
Includes:
IntegrationsConcern, PreferenceSchema, Preferences::Preferable, PrefixedId, Publishable, RansackableAttributes, TranslatableResourceScopes, TypedAssociations
Defined in:
app/models/spree/base.rb

Direct Known Subclasses

Invitations::Store, UserIdentity

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

Methods included from Publishable

#event_context, #event_payload, #event_prefix, #event_serializer_class, #publish_event

Methods included from IntegrationsConcern

#store_integration, #store_integrations

Methods included from PreferenceSchema

#serialized_preferences

Methods included from Preferences::Preferable

#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_typeObject

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

.belongs_to_required_by_defaultObject



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_inversingObject



53
54
55
# File 'app/models/spree/base.rb', line 53

def self.has_many_inversing
  false
end

.json_api_columnsObject



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_attributesObject



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_typeObject

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

.spree_base_scopesObject



44
45
46
# File 'app/models/spree/base.rb', line 44

def self.spree_base_scopes
  where(nil)
end

.spree_base_uniqueness_scopeObject



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_jsonObject



95
96
97
98
99
100
101
102
# File 'app/models/spree/base.rb', line 95

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

Returns:

  • (Boolean)


58
59
60
# File 'app/models/spree/base.rb', line 58

def can_be_deleted?
  true
end

#mysql_adapter?Boolean

Returns:

  • (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_candidatesObject

Try building a slug based on the following fields in increasing order of specificity.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/spree/base.rb', line 109

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_idObject



104
105
106
# File 'app/models/spree/base.rb', line 104

def uuid_for_friendly_id
  SecureRandom.uuid
end