Class: Spree::DigitalLink

Inherits:
Object
  • Object
show all
Includes:
Security::DigitalLinks
Defined in:
app/models/spree/digital_link.rb

Instance Method Summary collapse

Instance Method Details

#access_limit_exceeded? ⇒ Boolean

Returns:

  • (Boolean)


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

def access_limit_exceeded?
  if store.preferred_limit_digital_download_count
    access_counter >= digital_asset.effective_authorized_clicks(store)
  else
    false
  end
end

#authorizable? ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/spree/digital_link.rb', line 40

def authorizable?
  !(expired? || access_limit_exceeded?)
end

#authorize! ⇒ Object

This method should be called when a download is initiated. It returns true or false depending on whether the authorization is granted.

The access-limit check and the counter increment run inside a row lock so concurrent requests sharing the same token cannot each pass the cap before any of them increments the counter (TOCTOU race).



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

def authorize!
  ActiveRecord::Base.connected_to(role: :writing) do
    with_lock do
      authorizable? && increment!(:access_counter, touch: true)
    end
  end
end

#digital ⇒ Object

Deprecated.

Use #digital_asset; removed in 6.3.



29
30
31
32
# File 'app/models/spree/digital_link.rb', line 29

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

#digital=(value) ⇒ Object

Deprecated.

Use #digital_asset=; removed in 6.3.



35
36
37
38
# File 'app/models/spree/digital_link.rb', line 35

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

#expired? ⇒ Boolean

Returns:

  • (Boolean)


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

def expired?
  expiry = expires_at
  expiry.present? && expiry <= Time.current
end

#expires_at ⇒ Time?

Returns when access lapses, or nil when day limits are off.

Returns:

  • (Time, nil) —

    when access lapses, or nil when day limits are off



78
79
80
81
82
# File 'app/models/spree/digital_link.rb', line 78

def expires_at
  return unless store.preferred_limit_digital_download_days

  created_at + digital_asset.effective_authorized_days(store).days
end

#reset! ⇒ Object



71
72
73
74
75
# File 'app/models/spree/digital_link.rb', line 71

def reset!
  self.access_counter = 0
  self.created_at = Time.current
  save!
end