From a9343f76e075a1d606e714f1e2b06061fa7976f3 Mon Sep 17 00:00:00 2001 From: Kimsrung Lov Date: Thu, 25 May 2023 14:24:34 +0700 Subject: [PATCH] Upgrade to support spree v4.5 --- .gitignore | 2 + Gemfile | 6 ++- app/assets/config/spree_reviews_manifest.js | 3 ++ .../spree/feedback_reviews_controller.rb | 2 +- app/controllers/spree/reviews_controller.rb | 2 +- .../spree/api/api_helpers_decorator.rb | 37 ------------------- config/initializers/asset.rb | 1 + .../spree_permitted_attributes.rb | 10 +++++ lib/spree_reviews.rb | 1 + lib/spree_reviews/engine.rb | 5 +++ spec/controllers/products_controller_spec.rb | 2 +- spree_reviews.gemspec | 4 +- 12 files changed, 32 insertions(+), 43 deletions(-) create mode 100644 app/assets/config/spree_reviews_manifest.js delete mode 100644 app/helpers/spree/api/api_helpers_decorator.rb create mode 100644 config/initializers/asset.rb create mode 100644 config/initializers/spree_permitted_attributes.rb diff --git a/.gitignore b/.gitignore index c6dcc215..9390b1bc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,9 @@ pkg/* spec/dummy coverage .rvmrc +.nvmrc .ruby-version .ruby-gemset +.tool-versions tmp gemfiles/*.gemfile.lock diff --git a/Gemfile b/Gemfile index abf1869b..e8790bca 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,10 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end -gem 'spree', github: 'spree/spree', branch: 'master' -gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: 'master' +spree_version = '>= 4.5' +gem 'spree', spree_version +gem 'spree_auth_devise', spree_version gem 'rails-controller-testing' + gemspec diff --git a/app/assets/config/spree_reviews_manifest.js b/app/assets/config/spree_reviews_manifest.js new file mode 100644 index 00000000..f6f1455c --- /dev/null +++ b/app/assets/config/spree_reviews_manifest.js @@ -0,0 +1,3 @@ +//= link_tree ../images +//= link_tree ../javascripts +//= link_directory ../stylesheets .css \ No newline at end of file diff --git a/app/controllers/spree/feedback_reviews_controller.rb b/app/controllers/spree/feedback_reviews_controller.rb index a7b48f6d..1b1cd829 100644 --- a/app/controllers/spree/feedback_reviews_controller.rb +++ b/app/controllers/spree/feedback_reviews_controller.rb @@ -27,7 +27,7 @@ def load_review end def permitted_feedback_review_attributes - [:rating, :comment] + permitted_attributes.feedback_review_attributes end def feedback_review_params diff --git a/app/controllers/spree/reviews_controller.rb b/app/controllers/spree/reviews_controller.rb index 1ee8e2e3..9a01aa5a 100644 --- a/app/controllers/spree/reviews_controller.rb +++ b/app/controllers/spree/reviews_controller.rb @@ -39,7 +39,7 @@ def load_product end def permitted_review_attributes - [:rating, :title, :review, :name, :show_identifier] + permitted_attributes.review_attributes end def review_params diff --git a/app/helpers/spree/api/api_helpers_decorator.rb b/app/helpers/spree/api/api_helpers_decorator.rb deleted file mode 100644 index 7472dd3c..00000000 --- a/app/helpers/spree/api/api_helpers_decorator.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Api - module ApiHelpersDecorator - def self.prepended(base) - base.module_eval do - @@review_attributes = [ - :id, :product_id, :name, :location, :rating, :title, :review, :approved, - :created_at, :updated_at, :user_id, :ip_address, :locale, :show_identifier - ] - - @@feedback_review_attributes = [ - :id, :user_id, :review_id, :rating, :comment, :created_at, :updated_at, :locale - ] - - def review_attributes - @@review_attributes - end - - def feedback_review_attributes - @@feedback_review_attributes - end - - reviews_fields = [:avg_rating, :reviews_count] - - reviews_fields.each do |attrib| - ::Spree::PermittedAttributes.product_attributes << attrib - class_variable_set(:@@product_attributes, class_variable_get(:@@product_attributes).push(attrib)) - end - end - end - - ::Spree::Api::ApiHelpers.prepend self if ::Spree::Api::ApiHelpers.included_modules.exclude?(self) - end - end -end diff --git a/config/initializers/asset.rb b/config/initializers/asset.rb new file mode 100644 index 00000000..8aab5511 --- /dev/null +++ b/config/initializers/asset.rb @@ -0,0 +1 @@ +Rails.application.config.assets.precompile << 'spree_reviews_manifest.js' \ No newline at end of file diff --git a/config/initializers/spree_permitted_attributes.rb b/config/initializers/spree_permitted_attributes.rb new file mode 100644 index 00000000..ca935a79 --- /dev/null +++ b/config/initializers/spree_permitted_attributes.rb @@ -0,0 +1,10 @@ +module Spree + module PermittedAttributes + ATTRIBUTES += %i[review_attributes feedback_review_attributes] + mattr_reader *ATTRIBUTES + + @@product_attributes += %i[avg_rating reviews_count] + @@review_attributes = [:rating, :title, :review, :name, :show_identifier] + @@feedback_review_attributes = [:rating, :comment] + end +end diff --git a/lib/spree_reviews.rb b/lib/spree_reviews.rb index 0a22952e..02e50b2b 100644 --- a/lib/spree_reviews.rb +++ b/lib/spree_reviews.rb @@ -1,5 +1,6 @@ require 'spree_core' require 'spree_extension' +require 'spree_backend' require 'spree_reviews/engine' require 'spree_reviews/version' require 'deface' diff --git a/lib/spree_reviews/engine.rb b/lib/spree_reviews/engine.rb index 9c87cbcc..01e30d25 100644 --- a/lib/spree_reviews/engine.rb +++ b/lib/spree_reviews/engine.rb @@ -4,12 +4,17 @@ class Engine < Rails::Engine isolate_namespace Spree engine_name 'spree_reviews' + config.autoload_paths += %W[#{config.root}/lib] # use rspec for tests config.generators do |g| g.test_framework :rspec end initializer 'spree_reviews.environment', before: :load_config_initializers do |_app| + Config = Configuration.new + end + + config.after_initialize do Spree::Reviews::Config = Spree::ReviewSetting.new end diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb index 53d3a36f..adc19a73 100644 --- a/spec/controllers/products_controller_spec.rb +++ b/spec/controllers/products_controller_spec.rb @@ -6,7 +6,7 @@ end it "adds #{attrib} to the set of available attributes from Spree API" do - expect(Spree::Api::ApiHelpers.product_attributes).to include(attrib) + expect(Spree::PermittedAttributes.product_attributes).to include(attrib) end end end diff --git a/spree_reviews.gemspec b/spree_reviews.gemspec index 6fa807ed..045a2ff3 100644 --- a/spree_reviews.gemspec +++ b/spree_reviews.gemspec @@ -20,13 +20,15 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - spree_version = '>= 3.2.0', '< 5.0' + spree_version = '>= 4.5' s.add_dependency 'deface', '~> 1.0' s.add_dependency 'spree_api', spree_version s.add_dependency 'spree_backend', spree_version + s.add_dependency 'spree_frontend', spree_version s.add_dependency 'spree_core', spree_version s.add_dependency 'spree_extension' s.add_development_dependency 'sass-rails' s.add_development_dependency 'spree_dev_tools' + s.add_development_dependency 'pg' end