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

Add decorators #4

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions app/helpers/spree/global_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Spree
module GlobalHelper

def search_url(taxon=nil, keywords='', options={})
products_path(options.merge(taxon: taxon, keywords: keywords))
end

def get_taxons(taxon=nil, depth=nil)
taxons = taxon.present? ? taxon.self_and_descendants : Spree::Taxon.all
taxons = taxons.where('depth < ?', depth) if taxons.any? && depth.present?
end

def get_products(per_page=Spree::Config[:products_per_page], page_number=1, taxon=nil)
products = taxon.present? ? taxon.products : Spree::Product
products.page(page_number).per(per_page)
end

def previous_orders
try_spree_current_user.orders.complete.order(completed_at: :desc) if try_spree_current_user.present?
end

def product_option_types(product)
product.option_types
end

def cart_info(text = nil)
css_class = nil

if simple_current_order.nil? or simple_current_order.item_count.zero?
text = "<span class='glyphicon glyphicon-shopping-cart'></span> 0"
css_class = 'empty'
else
text = "<span class='glyphicon glyphicon-shopping-cart'></span> #{simple_current_order.item_count}"
css_class = 'full'
end

text.html_safe
end

def first_level_taxons(taxon)
taxon.children.where(depth: 1)
end

def fetch_products_count_by_taxon(taxon)
Spree::Classification.where(taxon_id: taxon.self_and_descendants.pluck(:id)).count
end
end
end
31 changes: 31 additions & 0 deletions app/helpers/spree/products_helper_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Spree
ProductsHelper.class_eval do
def cache_key_for_products
count = @products.try(:count)
max_updated_at = (@products.try(:maximum, :updated_at) || Date.today).to_s(:number)
"#{I18n.locale}/#{current_currency}/spree/products/all-#{params[:page]}-#{max_updated_at}-#{count}-#{Spree::Config[:theme_name]}-#{Spree::Taxon.pluck(:is_featured).to_sentence}"
end

def cache_key_for_taxons
max_updated_at = @taxons.maximum(:updated_at).to_i
parts = [@taxon.try(:id), max_updated_at].compact.join("-")
"#{I18n.locale}/taxons/#{parts}/#{Spree::Config[:theme_name]}"
end

def color_option_value(variant)
variant.option_values.joins(:option_type).find_by(spree_option_types: { presentation: 'Color' })
end

def non_color_option_types(product)
product.option_types.where.not(presentation: 'Color')
end

def short_product_descritpion(product)
truncate(product.description, length: 100, omission: '')
end

def empty_product_properties_count(product_properties)
product_properties.where.not(value: '').size
end
end
end
27 changes: 27 additions & 0 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Spree
Product.class_eval do
def uniq_options(option_type)
sorted_option_values(option_type) do |value, _uniq_options, _variant|
_uniq_options[_variant.id] = { variant: _variant, option_value_presentation: value.presentation } if value
end
end

def uniq_color_options
sorted_option_values('Color') do |value, _uniq_options, _variant|
_uniq_options[value.id] = { variant: _variant, option_value_presentation: value.presentation } if value
end
end

private
def sorted_option_values(option_type)
_uniq_options = {}
variants.reload.map do |_variant|
value = _variant.option_values.joins(:option_type).where(spree_option_types: { presentation: option_type }).sort do |a, b|
a.option_type.position <=> b.option_type.position
end[0]
yield(value, _uniq_options, _variant)
end
_uniq_options
end
end
end
5 changes: 5 additions & 0 deletions app/models/spree/taxon_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Spree
Taxon.class_eval do
scope :non_roots, -> { where.not(parent_id: nil).order(:position) }
end
end
2 changes: 0 additions & 2 deletions app/views/spree/admin/themes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@
</div>

<% end %>


2 changes: 1 addition & 1 deletion lib/extensions/dir.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Dir

def self.human_entries(path)
Dir.entries(path).reject{ |_path| (_path == '.' || _path == '..') }
Dir.entries(path).reject{ |_path| _path.starts_with? '.' }
end

end
23 changes: 21 additions & 2 deletions lib/spree_themes/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ class Engine < Rails::Engine
end

initializer "asset_paths" do |app|
Rails.application.config.assets.paths.insert(8,
Rails.root.join('vendor', 'themes').to_s
Dir.human_entries(Rails.root.join('vendor', 'themes')).each do |theme_name|
Rails.application.config.assets.paths.insert(8,
Rails.root.join('vendor', 'themes', theme_name, 'javascripts').to_s,
Rails.root.join('vendor', 'themes', theme_name, 'stylesheets').to_s,
Rails.root.join('vendor', 'themes', theme_name, 'images').to_s,
Rails.root.join('vendor', 'themes', theme_name, 'fonts').to_s,
Rails.root.join('vendor', 'themes', theme_name, 'logo').to_s
)
end
end

initializer "asset_paths" do |app|
Rails.application.config.assets.paths.insert(8,
Rails.root.join('vendor', 'themes').to_s)
end

initializer "spree_themes.assets.precompile" do |app|
Expand All @@ -22,7 +33,10 @@ class Engine < Rails::Engine
app.config.assets.precompile += Dir.glob(Rails.root.join('vendor', 'themes', theme_name, 'stylesheets', 'spree', '**', '*.css'))
app.config.assets.precompile += Dir.glob(Rails.root.join('vendor', 'themes', theme_name, 'stylesheets', 'spree', '**', '*.scss'))
app.config.assets.precompile += Dir.glob(Rails.root.join('vendor', 'themes', theme_name, 'javascripts', 'spree', '**', '*.js'))
app.config.assets.precompile += Dir.glob(Rails.root.join('vendor', 'themes', theme_name, 'javascripts', 'spree', '**', '*.js.coffee'))
app.config.assets.precompile += Dir.glob(Rails.root.join('vendor', 'themes', theme_name, 'logo', '*.png'))
end
config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2|gif|png|jpg)\z/
end

def self.activate
Expand All @@ -31,6 +45,11 @@ def self.activate
end
end

config.to_prepare do
Spree::StoreController.helper Spree::GlobalHelper
end

config.to_prepare &method(:activate).to_proc
Devise.parent_controller = 'Spree::StoreController'
end
end