Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Spanish translations and scopes. Support scss in generators. #88

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5394d0b
Extend generator to support scss files
Jun 10, 2015
7fb41f3
Fixes error in Spanish and Italian translations and in translations s…
Jun 10, 2015
e4634f1
Fixes error in italian translation
Jun 10, 2015
f9974a0
Generate digital links after order is completed, not after saving line
Jun 10, 2015
c0b6344
Change SpreeDigital configuration preferences for better integeration…
Jun 10, 2015
586a6f9
Added authentication(devise) and authorization(cancancan) for uenable…
Jun 11, 2015
4e44592
Improves readme
Jun 11, 2015
90080b7
Improve order mailer translations and added html format
Jun 11, 2015
5004b1e
Add missing translations
Jun 11, 2015
971bf05
Fixes error in translation
Jun 11, 2015
6c783cb
Spree::Product digital scope¡
Jun 12, 2015
33606f8
Increase version
Jun 12, 2015
650766a
Adds new preference `per_user_attachment` to generate an individual f…
Jun 15, 2015
d3f4b1c
Adds new preference `per_user_attachment` to generate an individual f…
Jun 15, 2015
f2c131c
Adds per_user_attachment_process method to execute before creating pe…
Jun 16, 2015
351211d
Fixes merge conflict
Jun 16, 2015
8a81ebc
Temporary disable ssl
Jun 16, 2015
1268aae
Added contributor
Jun 16, 2015
a01bf6a
Added contributor
Jun 16, 2015
39a7154
Change user.digital_signature to include user email
Jun 18, 2015
8e2ba1f
Tweaks in dowload links style
Jul 9, 2015
1aab0bd
Adds digital downloads links in new order_mailer partials
Jul 9, 2015
85545c2
Fixes error in authentication helpers
Aug 19, 2015
dfb2eef
Improve digital download links in order_mailer. New partial available…
Aug 19, 2015
58fdfe8
Adds new method in Spree::Order not_digital
Sep 10, 2015
9cbbafe
Order methods for digital and not_digital line_items
Sep 11, 2015
f8b71ef
Change order_mailer digital link insertion
Sep 11, 2015
26756a1
Change download link location
Sep 14, 2015
31ea86d
Change download link location
Sep 14, 2015
95bf63f
Changes in digital_links generation. Generate links if not present on…
Oct 23, 2015
a9e0796
Refs #6675: strip strange characters in digital file name
Oct 23, 2015
d04e7b2
Add link to product show in order/digital_links
Feb 25, 2016
5b453ad
Change digital copy file name to append secret at the end
Feb 25, 2016
3cc22b5
Improves variant digital methods
May 8, 2017
618b260
Improves variant digital methods
May 9, 2017
4de3071
Sort digital by updated_at desc
Sep 28, 2017
bb70600
Sort digital by updated_at desc
Sep 28, 2017
f7ff6fb
Sort digital by updated_at desc
Sep 28, 2017
8b73c17
Adds content-length header in send_file
Aug 21, 2018
394ca14
Improves variant check digital methods. Adds scopes for line_item and…
Jun 17, 2020
f0adef9
Upgrade to version 1.2.0
Jun 17, 2020
fcd9647
Search in deleted variants for digital_links
Oct 13, 2020
45a747b
New line_item scope for compatibility with spree_advanced_reporting
Feb 14, 2021
7631931
Upgrade to spree 4.4.x
Jan 31, 2022
66b836e
Upgrade to spree 4.4.x
Jan 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
source 'https://rubygems.org'

gem 'spree', github: 'spree/spree', branch: '3-0-stable'
source :rubygems

gemspec

group :test do
if RUBY_PLATFORM.downcase.include? "darwin"
gem 'guard-rspec'
gem 'rb-fsevent'
gem 'growl'
end
end
3 changes: 2 additions & 1 deletion Versionfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
"2.2.x" => { :branch => "2-2-stable" }
"2.3.x" => { :branch => "2-3-stable" }
"3.0.x" => { :branch => "3-0-stable" }
"3.1.x" => { :branch => "master" }
"3.1.x" => { :branch => "3-1-stable" }
"4.0.x" => { :branch => "master" }
3 changes: 0 additions & 3 deletions app/assets/stylesheets/spree/frontend/spree_digital.css.scss

This file was deleted.

14 changes: 14 additions & 0 deletions app/assets/stylesheets/spree/frontend/spree_digital.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
div.download-links {
margin-top: 1em;
ul {
list-style: none;
margin-top: 0.5em;
padding-left: 0em;
li a {
color: #27aae2;
&:hover {
text-decoration: underline;
}
}
}
}
22 changes: 16 additions & 6 deletions app/controllers/spree/admin/orders_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
Spree::Admin::OrdersController.class_eval do
def reset_digitals
load_order
@order.reset_digital_links!
flash[:notice] = Spree.t(:downloads_reset, scope: 'digitals')
redirect_to spree.admin_order_path(@order)
module Spree
module Admin
module OrdersControllerDecorator
def self.prepended(base)
base.before_action :load_data, only: :reset_digitals
end

def reset_digitals
# load_order
@order.reset_digital_links!
flash[:notice] = Spree.t(:downloads_reset, scope: 'digitals')
redirect_to spree.admin_order_path(@order)
end
end
end
end

::Spree::Admin::OrdersController.prepend Spree::Admin::OrdersControllerDecorator if ::Spree::Admin::OrdersController.included_modules.exclude?(Spree::Admin::OrdersControllerDecorator)
51 changes: 34 additions & 17 deletions app/controllers/spree/digitals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
module Spree
class DigitalsController < Spree::StoreController
force_ssl only: :show, if: :ssl_configured?
#force_ssl only: :show, if: :ssl_configured?
rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
skip_before_filter :require_no_authentication, if: Proc.new { SpreeDigital::Config[:authentication_required] }

def show
if attachment.present?
# don't authorize the link unless the file exists
# the logger error will help track down customer issues easier
if attachment_is_file?
if digital_link.authorize!
if Paperclip::Attachment.default_options[:storage] == :s3
redirect_to attachment.expiring_url(Spree::DigitalConfiguration[:s3_expiration_seconds]) and return
else
send_file attachment.path, :filename => attachment.original_filename, :type => attachment.content_type and return
end
end
if attachment.present?
if attachment_file_present?
authorize_and_send_file!
else
Rails.logger.error "Missing Digital Item: #{attachment.path}"
if digital_link.regenerate_attachment_file = true and digital_link.save
Rails.logger.info "Digital link #{attachment.path} regenerated"
authorize_and_send_file!
else
Rails.logger.error "Error generating file: #{attachment.path} #{digital_link.inspect}"
redirect_to :back, error: Spree.t('error_in_attachment', scope: 'digitals') and return
end
end
else
Rails.logger.error "Missing Digital Item: #{attachment.path}"
redirect_to :back, error: Spree.t('error_in_attachment', scope: 'digitals') and return
end

render :unauthorized
end

private

def attachment_is_file?
def attachment_file_present?
if Paperclip::Attachment.default_options[:storage] == :s3
attachment.exists?
else
Expand All @@ -38,7 +38,7 @@ def digital_link
end

def attachment
@attachment ||= digital_link.digital.try(:attachment) if digital_link.present?
@attachment ||= digital_link.try(:attachment) if digital_link.present?
end

def resource_not_found
Expand All @@ -48,5 +48,22 @@ def resource_not_found
def ssl_configured?
Rails.env.production?
end

def authorize_download!
!SpreeDigital::Config[:authentication_required] || !SpreeDigital::Config[:authorization_required] || authorize!(:download, digital_link)
end

def authorize_and_send_file!
if authorize_download! && digital_link.downloadable?
if Paperclip::Attachment.default_options[:storage] == :s3
redirect_to attachment.expiring_url(Spree::DigitalConfiguration[:s3_expiration_seconds]) and return
else
response.headers['Content-Length'] = attachment.size.to_s
send_file attachment.path, :filename => attachment.original_filename, :type => attachment.content_type and return
end
else
render :unauthorized
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/spree/calculator/shipping/digital_delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DigitalDelivery < ShippingCalculator
preference :currency, :string, default: ->{ Spree::Config[:currency] }

def self.description
Spree.t(:digital_delivery, scope: 'digital')
Spree.t(:digital_delivery, scope: 'digitals')
end

def compute_package(package=nil)
Expand Down
13 changes: 13 additions & 0 deletions app/models/spree/digital.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ module Spree
class Digital < ActiveRecord::Base
belongs_to :variant
has_many :digital_links, dependent: :destroy
after_save :delete_digital_links_attachments, if: :attachment_changed?

has_attached_file :attachment, path: ":rails_root/private/digitals/:id/:basename.:extension"
before_post_process :strip_strange_characters_from_attachments
do_not_validate_attachment_file_type :attachment
validates_attachment_presence :attachment

if Paperclip::Attachment.default_options[:storage] == :s3
attachment_definitions[:attachment][:s3_permissions] = :private
attachment_definitions[:attachment][:s3_headers] = { :content_disposition => 'attachment' }
end

def attachment_changed?
self.attachment_file_name_changed? || self.attachment_content_type_changed? || self.attachment_file_size_changed?
end

private
def delete_digital_links_attachments
self.digital_links.each do |dl|
dl.remove_attachment_file = 1 and dl.save
end
end
end
end
13 changes: 13 additions & 0 deletions app/models/spree/digital_ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Spree
class DigitalAbility
include CanCan::Ability

def initialize(user)
user ||= Spree.user_class.new
alias_action :create, :update, :destroy, :read, to: :download
can :download, Spree::DigitalLink do |digital_link|
digital_link.user_id == user.id
end
end
end
end
116 changes: 107 additions & 9 deletions app/models/spree/digital_link.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,143 @@
require 'fileutils'

module Spree
class DigitalLink < ActiveRecord::Base

belongs_to :digital
validates :digital, :presence => true

belongs_to :line_item
# delegate :variant, to: :digital

belongs_to :line_item
belongs_to :user

validates_length_of :secret, :is => 30
validates_uniqueness_of :secret

has_attached_file :attachment, path: ":rails_root/private/digitals/:digital_id/:basename.:extension"
do_not_validate_attachment_file_type :attachment
# validates_attachment_presence :attachment
attr_accessor :remove_attachment_file
attr_accessor :regenerate_attachment_file

before_validation :set_defaults, :on => :create
before_save :copy_attachment_file!, if: :should_regenerate_attachment_file
before_save :check_remove_attachment_file

def should_regenerate_attachment_file
self.new_record? || self.regenerate_attachment_file
end

def regenerate_attachment_file
@regenerate_attachment_file || false
end

def regenerate_attachment_file=(value)
@regenerate_attachment_file = value
end

# Can this link stil be used? It is valid if it's less than 24 hours old and was not accessed more than 3 times
def authorizable?
def downloadable?
!(expired? || access_limit_exceeded?)
end

def expired?
self.created_at <= Spree::DigitalConfiguration[:authorized_days].day.ago
SpreeDigital::Config[:expirable_links] && (self.created_at <= SpreeDigital::Config[:authorized_days].day.ago)
end

def access_limit_exceeded?
self.access_counter >= Spree::DigitalConfiguration[:authorized_clicks]
SpreeDigital::Config[:expirable_links] && (self.access_counter >= SpreeDigital::Config[:authorized_clicks])
end

# This method should be called when a download is initiated.
# It returns +true+ or +false+ depending on whether the authorization is granted.
def authorize!
authorizable? && increment!(:access_counter) ? true : false
def download!
downloadable? && increment!(:access_counter) ? true : false
end

def reset!
update_column :access_counter, 0
update_column :created_at, Time.now
end

def original_attachment
self.digital.present? and self.digital.attachment
end

def attachment_file_name
file_name, file_ext = self.original_attachment.original_filename.split('.')
super || (SpreeDigital::Config[:per_user_attachment] ? [[file_name, self.secret].join('_'), file_ext].join('.') : self.original_attachment.original_filename)
end

def attachment_dir
self.original_attachment.path.gsub(self.original_attachment.original_filename, '')
end

def attachment_alias
SpreeDigital::Config[:per_user_attachment] ? self.attachment : self.original_attachment
end

# include deleted ones
def variant
self.digital.try(:variant) || Spree::Variant.unscoped.find(self.digital.try(:variant_id))
end

private

# def copy_digital
# if SpreeDigital::Config[:per_user_attachment]
# begin
# new_copy = File.join(self.attachment_dir, self.attachment_file_name)
# if SpreeDigital::Config.per_user_attachment_process.present?
# puts "Executing Spree::Digital per_user_attachment_process..."
# SpreeDigital::Config.per_user_attachment_process.call(self.original_attachment.path, new_copy, self)
# else
# FileUtils.cp(self.original_attachment.path, new_copy)
# end
# self.attachment = File.open(new_copy)
# rescue => e
# logger.error "There was a problem copying file #{self.attachment_file_name} #{e}"
# return false
# end
# else
# return true
# end
# end

def copy_attachment_file!
if SpreeDigital::Config[:per_user_attachment]
begin
new_copy = File.join(self.attachment_dir, self.attachment_file_name)
if SpreeDigital::Config.per_user_attachment_process.present?
puts "Executing Spree::Digital per_user_attachment_process..."
SpreeDigital::Config.per_user_attachment_process.call(self.original_attachment.path, new_copy, self)
else
FileUtils.cp(self.original_attachment.path, new_copy)
end
self.attachment = File.open(new_copy)
rescue => e
logger.error "There was a problem copying file #{self.attachment_file_name} #{e}"
return false
end
else
return true
end
end

# Populating the secret automatically and zero'ing the access_counter (otherwise it might turn out to be NULL)
def set_defaults
self.secret = SecureRandom.hex(15)
self.access_counter ||= 0
end

def check_remove_attachment_file
if !SpreeDigital::Config[:keep_digitals] && self.remove_attachment_file.eql?(1)
begin
FileUtils.rm(self.attachment.path)
rescue => e
logger.error "There was a problem deleting file #{self.attachment_file_name} #{e}"
return false
end
return true
end
end

end
end
Loading