diff --git a/app/helpers/spree/global_helper.rb b/app/helpers/spree/global_helper.rb new file mode 100644 index 0000000..d0593dc --- /dev/null +++ b/app/helpers/spree/global_helper.rb @@ -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 = " 0" + css_class = 'empty' + else + text = " #{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 diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb new file mode 100644 index 0000000..e92c0cf --- /dev/null +++ b/app/helpers/spree/products_helper_decorator.rb @@ -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 diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb new file mode 100644 index 0000000..c74b961 --- /dev/null +++ b/app/models/spree/product_decorator.rb @@ -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 diff --git a/app/models/spree/taxon_decorator.rb b/app/models/spree/taxon_decorator.rb new file mode 100644 index 0000000..a45679a --- /dev/null +++ b/app/models/spree/taxon_decorator.rb @@ -0,0 +1,5 @@ +module Spree + Taxon.class_eval do + scope :non_roots, -> { where.not(parent_id: nil).order(:position) } + end +end diff --git a/app/views/spree/admin/themes/show.html.erb b/app/views/spree/admin/themes/show.html.erb index e4537d3..814fcc3 100644 --- a/app/views/spree/admin/themes/show.html.erb +++ b/app/views/spree/admin/themes/show.html.erb @@ -27,5 +27,3 @@ <% end %> - - diff --git a/lib/extensions/dir.rb b/lib/extensions/dir.rb index 47cc8ff..a4dec51 100644 --- a/lib/extensions/dir.rb +++ b/lib/extensions/dir.rb @@ -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 diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index c979bf0..f881a87 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -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| @@ -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 @@ -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