Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active May 8, 2019 19:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save iloveitaly/3187793 to your computer and use it in GitHub Desktop.
Save iloveitaly/3187793 to your computer and use it in GitHub Desktop.
Snippets for Spree Commerce Development
# with my git configuration the spree repo was giving me issues with line endings
# this fixed the issue for me: marking a file as binary causes git to ignore it completely
core/vendor/assets/javascripts/jquery.alerts/jquery.alerts.css.erb binary
core/vendor/assets/javascripts/jquery.alerts/jquery.alerts.js binary
core/vendor/assets/javascripts/jquery.jstree/themes/apple/style.css binary
sample/db/sample/spree/line_items.yml binary
sample/db/sample/spree/products.yml binary
# order/show
Deface::Override.new(:virtual_path => "spree/shared/_order_details",
:name => "add_digital_downloads_to_invoice",
:insert_bottom => "td[data-hook='order_item_description']",
:text => %q{
<% if order.state == 'complete' and item.variant.digital? %>
<% item.digital_links.sort { |a, b| a.digital.attachment.path.end_with?('mobi') ? -1 : 1 }.each do |digital_link| %>
<% format = File.extname(digital_link.digital.attachment.path).downcase %>
<div style="width:50%; float:left;">
<%== "<p><b>" + t("digital_instructions#{format}") +"</b></p>" if t("digital_instructions#{format}").present? %>
<%= link_to t(:digital_download, :type => t("digital_format#{format}"), :type => format), digital_url(:host => Spree::Config.get(:site_url), :secret => digital_link.secret), :class => "btn btn-success #{format}" %>
</div>
<% end %>
<% end %>
},
:disabled => false)
# `gem install sequel` or add sequel to Gemfile
# this will copy over your dev DB to a sqlite test db
# assumes your dev DB is a mysql DB and cwd is the project dir
rm $PWD/db/test.sqlite3; sequel mysql2://root:root@localhost/spree -C sqlite://$PWD/db/test.sqlite3
# create global zone containing everything but USA
global_zone = Spree::Zone.find_or_initialize_by_name 'Global Zone' do |z|
excluded_countries = ['US']
member_list = Spree::Country.find(:all, :conditions => ['iso not in (?)', excluded_countries.map { |c| "'#{c}'" }.join(',')])
puts "Creating Global Zone With Members: #{member_list.map(&:iso)}"
member_list.each do |member|
z.zone_members.build :zoneable_type => 'Spree::Country', :zoneable_id => member.id
end
z.description = "Global Zone (excluding US)"
z.save!
end
Spree::Payment.class_eval do
def gateway_error(error)
if error.is_a? ActiveMerchant::Billing::Response
text = error.params['message'] || error.params['response_reason_text'] || error.message
elsif error.is_a? ActiveMerchant::ConnectionError
text = I18n.t(:unable_to_connect_to_gateway)
else
text = error.to_s
end
logger.error(I18n.t(:gateway_error))
logger.error(" #{error.to_yaml}")
exc = Spree::Core::GatewayError.new(text)
Airbrake.notify exc, :parameters => (error.is_a? ActiveMerchant::Billing::Response)? error.params : {}
raise exc
end
end
# log active merchant errors; working off of 1.1.3 source code
Spree::Payment.class_eval do
def gateway_error(error)
if error.is_a? ActiveMerchant::Billing::Response
text = error.params['message'] || error.params['response_reason_text'] || error.message
elsif error.is_a? ActiveMerchant::ConnectionError
text = I18n.t(:unable_to_connect_to_gateway)
else
text = error.to_s
end
logger.error(I18n.t(:gateway_error))
logger.error(" #{error.to_yaml}")
exc = Spree::Core::GatewayError.new(text)
Airbrake.notify exc, :parameters => (error.is_a? ActiveMerchant::Billing::Response)? error.params : {}
raise exc
end
end
# for the whenever gem; fixes the rake execution issues
# be sure to create the /var/log/cron.log and `chown spree:spree`
# server is configured with spree deployment service
env :PATH, ENV['PATH']
set :output, "/var/log/cron.log"
# updates shipments with approximate shipped date
# manual DB migration for: https://github.com/spree/spree/commit/cc10ec3aa390b8581cddc343ae4409e89996e783
Spree::Shipment.where("state = 'shipped' and shipped_at IS NULL").select { |s| s.order.state == 'complete' }.each { |s| s.update_column(:shipped_at, s.order.completed_at) }
# when we are testing, we want to see the errors...
if Rails.env.test? or Rails.env.development?
Spree::OrdersController.class_eval do
private
def handle_shipping_error(e)
raise e
end
end
Spree::CheckoutController.class_eval do
private
def handle_shipping_error(e)
raise e
end
end
else
# in production, push all errors to airbrake
Spree::OrdersController.class_eval do
private
def handle_shipping_error(e)
Airbrake.notify e
flash[:error] = e.message
redirect_to checkout_state_path(:address)
end
end
Spree::CheckoutController.class_eval do
private
def handle_shipping_error(e)
Airbrake.notify e
flash[:error] = e.message
redirect_to checkout_state_path(:address)
end
end
end
Spree::Digital.class_eval do
has_attached_file :attachment, :path => '/data/spree/shared/uploaded-files/digitals/:id/:basename.:extension'
end
@tomash
Copy link

tomash commented Oct 16, 2015

In second gist (for spree_digital override) Spree::Config.get(:site_url) should be replaced (since Spree 2.3) with current_store.url.
Source: https://spreecommerce.com/blog/spree-2-3-released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment