Class: Spree::DigitalLink

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

Instance Method Summary collapse

Instance Method Details

#access_limit_exceeded?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'app/models/spree/digital_link.rb', line 38

def access_limit_exceeded?
  if line_item.order.store.preferred_limit_digital_download_count
    access_counter >= line_item.order.store.preferred_digital_asset_authorized_clicks
  else
    false
  end
end

#authorizable?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/spree/digital_link.rb', line 26

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.



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

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

#expired?Boolean

Returns:

  • (Boolean)


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

def expired?
  if line_item.order.store.preferred_limit_digital_download_days
    created_at <= line_item.order.store.preferred_digital_asset_authorized_days.day.ago
  else
    false
  end
end

#reset!Object



54
55
56
57
58
# File 'app/models/spree/digital_link.rb', line 54

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