Skip to content

Commit

Permalink
Move the base helpers back to core as several are used in backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Dutil authored and Jeff Dutil committed Dec 2, 2014
1 parent d466afc commit 3142c64
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
module Spree
module BaseHelper

def available_countries
checkout_zone = Zone.find_by(name: Spree::Config[:checkout_zone])

if checkout_zone && checkout_zone.kind == 'country'
countries = checkout_zone.country_list
else
countries = Country.all
end

countries.collect do |country|
country.name = Spree.t(country.iso, scope: 'country_names', default: country.name)
country
end.sort_by { |c| c.name.parameterize }
end

# Defined because Rails' current_page? helper is not working when Spree is mounted at root.
def current_spree_page?(url)
path = request.fullpath.gsub(/^\/\//, '/')
Expand All @@ -12,9 +27,30 @@ def current_spree_page?(url)
return false
end

# human readable list of variant options
def variant_options(v, options={})
v.options_text
def display_price(product_or_variant)
product_or_variant.price_in(current_currency).display_price.to_html
end

def gem_available?(name)
Gem::Specification.find_by_name(name)
rescue Gem::LoadError
false
rescue
Gem.available?(name)
end

def link_to_tracking(shipment, options = {})
return unless shipment.tracking && shipment.shipping_method

if shipment.tracking_url
link_to(shipment.tracking, shipment.tracking_url, options)
else
content_tag(:span, shipment.tracking)
end
end

def logo(image_path=Spree::Config[:logo])
link_to image_tag(image_path), spree.root_path
end

def meta_data
Expand Down Expand Up @@ -43,51 +79,6 @@ def meta_data_tags
end.join("\n")
end

def body_class
@body_class ||= content_for?(:sidebar) ? 'two-col' : 'one-col'
@body_class
end

def logo(image_path=Spree::Config[:logo])
link_to image_tag(image_path), spree.root_path
end

def available_countries
checkout_zone = Zone.find_by(name: Spree::Config[:checkout_zone])

if checkout_zone && checkout_zone.kind == 'country'
countries = checkout_zone.country_list
else
countries = Country.all
end

countries.collect do |country|
country.name = Spree.t(country.iso, scope: 'country_names', default: country.name)
country
end.sort_by { |c| c.name.parameterize }
end

def seo_url(taxon)
return spree.nested_taxons_path(taxon.permalink)
end

def gem_available?(name)
Gem::Specification.find_by_name(name)
rescue Gem::LoadError
false
rescue
Gem.available?(name)
end

def display_price(product_or_variant)
product_or_variant.price_in(current_currency).display_price.to_html
end

def pretty_time(time)
[I18n.l(time.to_date, format: :long),
time.strftime("%l:%M %p")].join(" ")
end

def method_missing(method_name, *args, &block)
if image_style = image_style_from_method_name(method_name)
define_image_method(image_style)
Expand All @@ -97,26 +88,21 @@ def method_missing(method_name, *args, &block)
end
end

def link_to_tracking(shipment, options = {})
return unless shipment.tracking && shipment.shipping_method

if shipment.tracking_url
link_to(shipment.tracking, shipment.tracking_url, options)
else
content_tag(:span, shipment.tracking)
end
def pretty_time(time)
[I18n.l(time.to_date, format: :long), time.strftime("%l:%M %p")].join(" ")
end

private
def seo_url(taxon)
return spree.nested_taxons_path(taxon.permalink)
end

# Returns style of image or nil
def image_style_from_method_name(method_name)
if method_name.to_s.match(/_image$/) && style = method_name.to_s.sub(/_image$/, '')
possible_styles = Spree::Image.attachment_definitions[:attachment][:styles]
style if style.in? possible_styles.with_indifferent_access
end
# human readable list of variant options
def variant_options(v, options={})
v.options_text
end

private

def create_product_image_tag(image, product, options, style)
options.reverse_merge! alt: image.alt.blank? ? product.name : image.alt
image_tag image.attachment.url(style), options
Expand All @@ -140,5 +126,13 @@ def define_image_method(style)
end
end
end

# Returns style of image or nil
def image_style_from_method_name(method_name)
if method_name.to_s.match(/_image$/) && style = method_name.to_s.sub(/_image$/, '')
possible_styles = Spree::Image.attachment_definitions[:attachment][:styles]
style if style.in? possible_styles.with_indifferent_access
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
it "should raise NoMethodError when style is not exists" do
expect { another_strange_image(product) }.to raise_error(NoMethodError)
end

end

context "link_to_tracking" do
Expand Down Expand Up @@ -128,7 +127,6 @@ def link_to_tracking_html(options = {})
it "should raise NoMethodError when statement with name equal to style name called" do
expect { foobar(product) }.to raise_error(NoMethodError)
end

end

context "pretty_time" do
Expand Down
5 changes: 5 additions & 0 deletions frontend/app/helpers/spree/frontend_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module Spree
module FrontendHelper
def body_class
@body_class ||= content_for?(:sidebar) ? 'two-col' : 'one-col'
@body_class
end

def breadcrumbs(taxon, separator=" ")
return "" if current_page?("/") || taxon.nil?
separator = raw(separator)
Expand Down

0 comments on commit 3142c64

Please sign in to comment.