Class: Spree::Base

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

Direct Known Subclasses

CustomDomain, Invitations::Store

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IntegrationsConcern

#store_integration, #store_integrations

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

.belongs_to_required_by_defaultObject



25
26
27
# File 'app/models/spree/base.rb', line 25

def self.belongs_to_required_by_default
  false
end

.for_store(store) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'app/models/spree/base.rb', line 29

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



48
49
50
# File 'app/models/spree/base.rb', line 48

def self.has_many_inversing
  false
end

.json_api_columnsObject



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

def self.json_api_columns
  column_names.reject { |c| c.match(/_id$|id|preferences|(.*)password|(.*)token|(.*)api_key|^original_(.*)/) }
end

.json_api_permitted_attributesObject



61
62
63
64
65
66
67
68
69
# File 'app/models/spree/base.rb', line 61

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



71
72
73
# File 'app/models/spree/base.rb', line 71

def self.json_api_type
  to_s.demodulize.underscore
end

.page(num) ⇒ Object



14
15
16
# File 'app/models/spree/base.rb', line 14

def self.page(num)
  send Kaminari.config.page_method_name, num
end

.spree_base_scopesObject



39
40
41
# File 'app/models/spree/base.rb', line 39

def self.spree_base_scopes
  where(nil)
end

.spree_base_uniqueness_scopeObject



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

def self.spree_base_uniqueness_scope
  ApplicationRecord.try(:spree_base_uniqueness_scope) || []
end

.to_tom_select_jsonObject



75
76
77
78
79
80
81
82
# File 'app/models/spree/base.rb', line 75

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)


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

def can_be_deleted?
  true
end

#slug_candidatesObject

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



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/spree/base.rb', line 89

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



84
85
86
# File 'app/models/spree/base.rb', line 84

def uuid_for_friendly_id
  SecureRandom.uuid
end