From 2bb5333e95d32d654f4b9164fc2b687ae9f8b094 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Wed, 30 Mar 2016 15:47:47 +0530 Subject: [PATCH 01/15] auto load vendor files --- lib/spree_themes/engine.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index c979bf0..bf41722 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -22,6 +22,7 @@ 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')) end end @@ -29,6 +30,10 @@ def self.activate Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c| Rails.configuration.cache_classes ? require(c) : load(c) end + + Dir.glob(File.join(Rails.root.join('vendor/themes/**/*_decorator*.rb'))) do |c| + Rails.configuration.cache_classes ? require(c) : load(c) + end end config.to_prepare &method(:activate).to_proc From 541cb3f9d58f8c1909cf44735fcc369fff43f9ae Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Thu, 31 Mar 2016 23:03:46 +0530 Subject: [PATCH 02/15] Add models and controller logic --- .../spree/products_helper_decorator.rb | 24 +++++++++++++++++++ app/models/spree/product_decorator.rb | 18 ++++++++++++++ app/models/spree/variant_decorator.rb | 15 ++++++++++++ .../add_max_retail_price.html.erb.deface | 6 +++++ ...655_add_maximum_retail_price_to_variant.rb | 13 ++++++++++ lib/extensions/dir.rb | 2 +- 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 app/helpers/spree/products_helper_decorator.rb create mode 100644 app/models/spree/product_decorator.rb create mode 100644 app/models/spree/variant_decorator.rb create mode 100644 app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface create mode 100644 db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb new file mode 100644 index 0000000..a8e51c1 --- /dev/null +++ b/app/helpers/spree/products_helper_decorator.rb @@ -0,0 +1,24 @@ +module Spree + ProductsHelper.class_eval do + def show_maximum_retail_price(product_or_variant) + Spree::Price.new(variant_id: product_or_variant.id, currency: product_or_variant.currency, price: product_or_variant.maximum_retail_price).display_price.to_html + end + + def percentage_diff(product_or_variant) + @percentage_diff = (((product_or_variant.maximum_retail_price - product_or_variant.price_in(product_or_variant.currency).price) / product_or_variant.maximum_retail_price) * 100).round(2) + "(-#{@percentage_diff}%)" if(@percentage_diff) + end + + def color_option_value(variant) + variant.option_values.joins(:option_type).find_by(spree_option_types: { presentation: 'Color' }) + end + + def size_variant_options(variant) + variant.size_options_text + end + + def color_variant_options(variant) + variant.color_options_text + 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..858f807 --- /dev/null +++ b/app/models/spree/product_decorator.rb @@ -0,0 +1,18 @@ +module Spree + Product.class_eval do + + delegate :maximum_retail_price, to: :master + + def uniq_color_options + _uniq_options = {} + variants.map do |v| + value = v.option_values.joins(:option_type).where(spree_option_types: { presentation: 'Color' }).sort do |a, b| + a.option_type.position <=> b.option_type.position + end[0] + _uniq_options[value.id] = value.presentation + end + _uniq_options + end + + end +end diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb new file mode 100644 index 0000000..484672e --- /dev/null +++ b/app/models/spree/variant_decorator.rb @@ -0,0 +1,15 @@ +module Spree + Variant.class_eval do + + before_create :set_maximum_retail_price, if: -> { maximum_retail_price.zero? } + + def size_options_text + value = self.option_values.joins(:option_type).find_by(spree_option_types: { presentation: 'Size' }) + value.try(:presentation) + end + + def set_maximum_retail_price + self.maximum_retail_price = price + end + end +end diff --git a/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface b/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface new file mode 100644 index 0000000..25b49c0 --- /dev/null +++ b/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface @@ -0,0 +1,6 @@ + + +
+ <%= f.label :maximum_retail_price, Spree.t(:maximum_retail_price) %> + <%= f.text_field :maximum_retail_price, :value => number_to_currency(@variant.maximum_retail_price, :unit => ''), :class => 'form-control' %> +
diff --git a/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb b/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb new file mode 100644 index 0000000..15a3540 --- /dev/null +++ b/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb @@ -0,0 +1,13 @@ +class AddMaximumRetailPriceToVariant < ActiveRecord::Migration + def up + add_column :spree_variants, :maximum_retail_price, :decimal, scale: 8, precision: 2, default: 0 + Spree::Variant.all.each do |variant| + variant.update_column(:maximum_retail_price, variant.price) + end + end + + def down + remove_column :spree_variants, :maximum_retail_price, :decimal, scale: 8, precision: 2, default: 0 + end + +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 From ef39ccac4fb52c6b5754d81017e258c9cf1a8409 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Fri, 1 Apr 2016 19:20:05 +0530 Subject: [PATCH 03/15] fix migration --- .../20160330125655_add_maximum_retail_price_to_variant.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb b/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb index 15a3540..3e3c9bb 100644 --- a/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb +++ b/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb @@ -1,13 +1,13 @@ class AddMaximumRetailPriceToVariant < ActiveRecord::Migration def up - add_column :spree_variants, :maximum_retail_price, :decimal, scale: 8, precision: 2, default: 0 + add_column :spree_variants, :maximum_retail_price, :decimal, precision: 8, scale: 2, default: 0 Spree::Variant.all.each do |variant| variant.update_column(:maximum_retail_price, variant.price) end end def down - remove_column :spree_variants, :maximum_retail_price, :decimal, scale: 8, precision: 2, default: 0 + remove_column :spree_variants, :maximum_retail_price, :decimal, precision: 8, scale: 2, default: 0 end end From 27b7fda4f8d98f7807671f543e6c84624a25171f Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Sun, 3 Apr 2016 22:12:08 +0530 Subject: [PATCH 04/15] complete logic to display option types --- .../spree/products_helper_decorator.rb | 8 ++---- app/models/spree/product_decorator.rb | 27 ++++++++++++++----- app/models/spree/variant_decorator.rb | 5 ---- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index a8e51c1..9bf2573 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -13,12 +13,8 @@ def color_option_value(variant) variant.option_values.joins(:option_type).find_by(spree_option_types: { presentation: 'Color' }) end - def size_variant_options(variant) - variant.size_options_text - end - - def color_variant_options(variant) - variant.color_options_text + def non_color_option_types(product) + product.option_types.where.not(presentation: 'Color') end end end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 858f807..c6392e3 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -3,16 +3,29 @@ module Spree delegate :maximum_retail_price, to: :master + 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 - _uniq_options = {} - variants.map do |v| - value = v.option_values.joins(:option_type).where(spree_option_types: { presentation: 'Color' }).sort do |a, b| - a.option_type.position <=> b.option_type.position - end[0] - _uniq_options[value.id] = value.presentation + sorted_option_values('Color') do |value, _uniq_options, _variant| + _uniq_options[value.id] = { variant: _variant, option_value_presentation: value.presentation } if value end - _uniq_options end + private + def sorted_option_values(option_type) + _uniq_options = {} + variants.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/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 484672e..b7b1cfd 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -3,11 +3,6 @@ module Spree before_create :set_maximum_retail_price, if: -> { maximum_retail_price.zero? } - def size_options_text - value = self.option_values.joins(:option_type).find_by(spree_option_types: { presentation: 'Size' }) - value.try(:presentation) - end - def set_maximum_retail_price self.maximum_retail_price = price end From d59e9c84ea68564d034185ee65a7a5f2730a0b23 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Mon, 4 Apr 2016 12:34:05 +0530 Subject: [PATCH 05/15] override link_to_cart in FrontendHelper --- app/helpers/spree/frontend_helper_decorator.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/helpers/spree/frontend_helper_decorator.rb diff --git a/app/helpers/spree/frontend_helper_decorator.rb b/app/helpers/spree/frontend_helper_decorator.rb new file mode 100644 index 0000000..5136e11 --- /dev/null +++ b/app/helpers/spree/frontend_helper_decorator.rb @@ -0,0 +1,17 @@ +module Spree + FrontendHelper.class_eval do + def link_to_cart(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 + + link_to text.html_safe, spree.cart_path, :class => "cart-info #{css_class}" + end + end +end From e203c00e6bc0c32a63551277c88d7bd1a5a5e692 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Mon, 4 Apr 2016 15:16:39 +0530 Subject: [PATCH 06/15] update assets_path --- app/helpers/spree/products_helper_decorator.rb | 8 +++++++- lib/spree_themes/engine.rb | 13 +++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 9bf2573..8ddef40 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -6,7 +6,7 @@ def show_maximum_retail_price(product_or_variant) def percentage_diff(product_or_variant) @percentage_diff = (((product_or_variant.maximum_retail_price - product_or_variant.price_in(product_or_variant.currency).price) / product_or_variant.maximum_retail_price) * 100).round(2) - "(-#{@percentage_diff}%)" if(@percentage_diff) + "(#{@percentage_diff}%)" if(@percentage_diff) end def color_option_value(variant) @@ -16,5 +16,11 @@ def color_option_value(variant) def non_color_option_types(product) product.option_types.where.not(presentation: 'Color') end + + def cache_key_for_products + count = @products.count + max_updated_at = (@products.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]}" + end end end diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index bf41722..7d47c25 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -11,9 +11,17 @@ 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 ) + 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| @@ -23,6 +31,7 @@ class Engine < Rails::Engine 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', 'yoda', 'fonts', 'spree', '*')) end end From d52640b7d179c3ab64c46df939be77bf5e06f37d Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Tue, 5 Apr 2016 12:09:19 +0530 Subject: [PATCH 07/15] add #short_product_description --- app/helpers/spree/frontend_helper_decorator.rb | 2 +- app/helpers/spree/products_helper_decorator.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/helpers/spree/frontend_helper_decorator.rb b/app/helpers/spree/frontend_helper_decorator.rb index 5136e11..82d87c3 100644 --- a/app/helpers/spree/frontend_helper_decorator.rb +++ b/app/helpers/spree/frontend_helper_decorator.rb @@ -1,6 +1,6 @@ module Spree FrontendHelper.class_eval do - def link_to_cart(text = nil) + def cart_info(text = nil) css_class = nil if simple_current_order.nil? or simple_current_order.item_count.zero? diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 8ddef40..85b6fe7 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -22,5 +22,9 @@ def cache_key_for_products max_updated_at = (@products.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]}" end + + def short_product_descritpion(product) + truncate(product.description, length: 100) + end end end From a5fd89fbe49a03424db66878df8d611aa0f14965 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Tue, 5 Apr 2016 16:05:29 +0530 Subject: [PATCH 08/15] add more formats to precompile --- app/helpers/spree/products_helper_decorator.rb | 2 +- lib/spree_themes/engine.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 85b6fe7..bf72abb 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -24,7 +24,7 @@ def cache_key_for_products end def short_product_descritpion(product) - truncate(product.description, length: 100) + truncate(product.description, length: 100, omission: '') end end end diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index 7d47c25..4a32079 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -14,7 +14,9 @@ class Engine < Rails::Engine 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, 'stylesheets').to_s, + Rails.root.join('vendor', 'themes', theme_name, 'images').to_s, + Rails.root.join('vendor', 'themes', theme_name, 'fonts').to_s ) end end @@ -31,8 +33,8 @@ class Engine < Rails::Engine 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', 'yoda', 'fonts', 'spree', '*')) end + config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2|gif|png)\z/ end def self.activate From fe3341edb5bb9de3d04a44ffada4e79d48a687a7 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Tue, 5 Apr 2016 23:18:41 +0530 Subject: [PATCH 09/15] override products#index method to add support for JS requests --- .../spree/products_controller_decorator.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 app/controllers/spree/products_controller_decorator.rb diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb new file mode 100644 index 0000000..5be5557 --- /dev/null +++ b/app/controllers/spree/products_controller_decorator.rb @@ -0,0 +1,19 @@ +module Spree + ProductsController.class_eval do + + + alias_method :orig_index, :index + + def index + orig_index + respond_to do |format| + format.html { } + format.js do + @products = @products.page(params[:page]).per(3) + render 'index.js' + end + end + end + + end +end From 7178bb6242a466f0e0178cc68c01d148863e4aab Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Wed, 6 Apr 2016 15:54:26 +0530 Subject: [PATCH 10/15] retract helpers for yoda theme to a new extension --- .../spree/admin/themes_controller.rb | 2 +- .../spree/products_controller_decorator.rb | 19 ------------ .../spree/frontend_helper_decorator.rb | 17 ---------- .../spree/products_helper_decorator.rb | 21 ------------- app/models/spree/product_decorator.rb | 31 ------------------- app/models/spree/variant_decorator.rb | 10 ------ .../add_max_retail_price.html.erb.deface | 6 ---- ...655_add_maximum_retail_price_to_variant.rb | 13 -------- lib/spree_themes/engine.rb | 4 --- 9 files changed, 1 insertion(+), 122 deletions(-) delete mode 100644 app/controllers/spree/products_controller_decorator.rb delete mode 100644 app/helpers/spree/frontend_helper_decorator.rb delete mode 100644 app/models/spree/product_decorator.rb delete mode 100644 app/models/spree/variant_decorator.rb delete mode 100644 app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface delete mode 100644 db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb diff --git a/app/controllers/spree/admin/themes_controller.rb b/app/controllers/spree/admin/themes_controller.rb index f50f8c0..ebee403 100644 --- a/app/controllers/spree/admin/themes_controller.rb +++ b/app/controllers/spree/admin/themes_controller.rb @@ -1,4 +1,4 @@ -module Spree + module Spree module Admin class ThemesController < Spree::Admin::BaseController diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb deleted file mode 100644 index 5be5557..0000000 --- a/app/controllers/spree/products_controller_decorator.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Spree - ProductsController.class_eval do - - - alias_method :orig_index, :index - - def index - orig_index - respond_to do |format| - format.html { } - format.js do - @products = @products.page(params[:page]).per(3) - render 'index.js' - end - end - end - - end -end diff --git a/app/helpers/spree/frontend_helper_decorator.rb b/app/helpers/spree/frontend_helper_decorator.rb deleted file mode 100644 index 82d87c3..0000000 --- a/app/helpers/spree/frontend_helper_decorator.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Spree - FrontendHelper.class_eval do - 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 - - link_to text.html_safe, spree.cart_path, :class => "cart-info #{css_class}" - end - end -end diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index bf72abb..03df692 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -1,30 +1,9 @@ module Spree ProductsHelper.class_eval do - def show_maximum_retail_price(product_or_variant) - Spree::Price.new(variant_id: product_or_variant.id, currency: product_or_variant.currency, price: product_or_variant.maximum_retail_price).display_price.to_html - end - - def percentage_diff(product_or_variant) - @percentage_diff = (((product_or_variant.maximum_retail_price - product_or_variant.price_in(product_or_variant.currency).price) / product_or_variant.maximum_retail_price) * 100).round(2) - "(#{@percentage_diff}%)" if(@percentage_diff) - 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 cache_key_for_products count = @products.count max_updated_at = (@products.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]}" end - - def short_product_descritpion(product) - truncate(product.description, length: 100, omission: '') - end end end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb deleted file mode 100644 index c6392e3..0000000 --- a/app/models/spree/product_decorator.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Spree - Product.class_eval do - - delegate :maximum_retail_price, to: :master - - 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.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/variant_decorator.rb b/app/models/spree/variant_decorator.rb deleted file mode 100644 index b7b1cfd..0000000 --- a/app/models/spree/variant_decorator.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Spree - Variant.class_eval do - - before_create :set_maximum_retail_price, if: -> { maximum_retail_price.zero? } - - def set_maximum_retail_price - self.maximum_retail_price = price - end - end -end diff --git a/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface b/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface deleted file mode 100644 index 25b49c0..0000000 --- a/app/overrides/spree/admin/variants/_form/add_max_retail_price.html.erb.deface +++ /dev/null @@ -1,6 +0,0 @@ - - -
- <%= f.label :maximum_retail_price, Spree.t(:maximum_retail_price) %> - <%= f.text_field :maximum_retail_price, :value => number_to_currency(@variant.maximum_retail_price, :unit => ''), :class => 'form-control' %> -
diff --git a/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb b/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb deleted file mode 100644 index 3e3c9bb..0000000 --- a/db/migrate/20160330125655_add_maximum_retail_price_to_variant.rb +++ /dev/null @@ -1,13 +0,0 @@ -class AddMaximumRetailPriceToVariant < ActiveRecord::Migration - def up - add_column :spree_variants, :maximum_retail_price, :decimal, precision: 8, scale: 2, default: 0 - Spree::Variant.all.each do |variant| - variant.update_column(:maximum_retail_price, variant.price) - end - end - - def down - remove_column :spree_variants, :maximum_retail_price, :decimal, precision: 8, scale: 2, default: 0 - end - -end diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index 4a32079..3dca6d7 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -41,10 +41,6 @@ def self.activate Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c| Rails.configuration.cache_classes ? require(c) : load(c) end - - Dir.glob(File.join(Rails.root.join('vendor/themes/**/*_decorator*.rb'))) do |c| - Rails.configuration.cache_classes ? require(c) : load(c) - end end config.to_prepare &method(:activate).to_proc From 0c51f656c439a8b1e0dad29bcde44ff66ecff138 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Thu, 14 Apr 2016 12:10:36 +0530 Subject: [PATCH 11/15] add jpg to precompile assets --- app/helpers/spree/products_helper_decorator.rb | 4 ++-- lib/spree_themes/engine.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 03df692..82d0cc6 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -1,8 +1,8 @@ module Spree ProductsHelper.class_eval do def cache_key_for_products - count = @products.count - max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number) + 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]}" end end diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index 3dca6d7..00d6963 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -34,7 +34,7 @@ class Engine < Rails::Engine 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')) end - config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2|gif|png)\z/ + config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2|gif|png|jpg)\z/ end def self.activate From 1a3da71a9059eddb7fb896424a75c3a9b9e352e1 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Tue, 19 Apr 2016 23:12:19 +0530 Subject: [PATCH 12/15] change base controller for devise --- app/helpers/spree/products_helper_decorator.rb | 2 +- app/views/spree/admin/themes/show.html.erb | 2 -- lib/spree_themes/engine.rb | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 82d0cc6..aa69b09 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -3,7 +3,7 @@ module Spree 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]}" + "#{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 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/spree_themes/engine.rb b/lib/spree_themes/engine.rb index 00d6963..42f9a84 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -44,5 +44,6 @@ def self.activate end config.to_prepare &method(:activate).to_proc + Devise.parent_controller = 'Spree::StoreController' end end From e3ec816ec14c85de9a6937f0f1f5ad3943be14cd Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Fri, 29 Apr 2016 17:20:40 +0530 Subject: [PATCH 13/15] move cache key to spree_themes --- app/helpers/spree/products_helper_decorator.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index aa69b09..fd166e0 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -5,5 +5,11 @@ def cache_key_for_products 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 end end From c2dc9ed444e8e2a6b2a67d93350307531c778310 Mon Sep 17 00:00:00 2001 From: sawan gupta Date: Thu, 5 May 2016 14:57:39 +0530 Subject: [PATCH 14/15] add logo image to assets precompile --- lib/spree_themes/engine.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index 42f9a84..d8e2e81 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -16,7 +16,8 @@ class Engine < Rails::Engine 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, 'fonts').to_s, + Rails.root.join('vendor', 'themes', theme_name, 'logo').to_s ) end end @@ -33,6 +34,7 @@ class Engine < Rails::Engine 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 From f8d92562f4e2d89bb7148a19ffbfea3ad0558040 Mon Sep 17 00:00:00 2001 From: Sawan Gupta Date: Tue, 24 May 2016 16:04:02 +0530 Subject: [PATCH 15/15] add helper methods to help develop themes --- .../spree/admin/themes_controller.rb | 2 +- app/helpers/spree/global_helper.rb | 48 +++++++++++++++++++ .../spree/products_helper_decorator.rb | 16 +++++++ app/models/spree/product_decorator.rb | 27 +++++++++++ app/models/spree/taxon_decorator.rb | 5 ++ lib/spree_themes/engine.rb | 4 ++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 app/helpers/spree/global_helper.rb create mode 100644 app/models/spree/product_decorator.rb create mode 100644 app/models/spree/taxon_decorator.rb diff --git a/app/controllers/spree/admin/themes_controller.rb b/app/controllers/spree/admin/themes_controller.rb index ebee403..f50f8c0 100644 --- a/app/controllers/spree/admin/themes_controller.rb +++ b/app/controllers/spree/admin/themes_controller.rb @@ -1,4 +1,4 @@ - module Spree +module Spree module Admin class ThemesController < Spree::Admin::BaseController 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 index fd166e0..e92c0cf 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -11,5 +11,21 @@ def cache_key_for_taxons 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/lib/spree_themes/engine.rb b/lib/spree_themes/engine.rb index d8e2e81..f881a87 100644 --- a/lib/spree_themes/engine.rb +++ b/lib/spree_themes/engine.rb @@ -45,6 +45,10 @@ 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