diff --git a/.circleci/config.yml b/.circleci/config.yml index 12bb10e..158e80f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,6 +1,9 @@ version: 2.1 orbs: + # Required for feature specs. + browser-tools: circleci/browser-tools@1.1 + # Always take the latest version of the orb, this allows us to # run specs against Solidus supported versions only without the need # to change this configuration every time a Solidus version is released @@ -8,28 +11,63 @@ orbs: solidusio_extensions: solidusio/extensions@volatile jobs: - run-specs-with-postgres: - executor: solidusio_extensions/postgres + run-specs: + parameters: + solidus: + type: string + default: main + db: + type: string + default: "postgres" + ruby: + type: string + default: "3.2" + executor: + name: solidusio_extensions/<< parameters.db >> + ruby_version: << parameters.ruby >> steps: - - solidusio_extensions/run-tests - run-specs-with-mysql: - executor: solidusio_extensions/mysql + - checkout + - browser-tools/install-chrome + - solidusio_extensions/run-tests-solidus-<< parameters.solidus >> + + lint-code: + executor: + name: solidusio_extensions/sqlite + ruby_version: "3.2" steps: - - solidusio_extensions/run-tests + - solidusio_extensions/lint-code workflows: "Run specs on supported Solidus versions": jobs: - - run-specs-with-postgres - - run-specs-with-mysql - "Weekly run specs against master": + - run-specs: + name: &name "run-specs-solidus-<< matrix.solidus >>-ruby-<< matrix.ruby >>-db-<< matrix.db >>" + matrix: + parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] } + - run-specs: + name: *name + matrix: + parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] } + - run-specs: + name: *name + matrix: + parameters: { solidus: ["older"], ruby: ["3.1"], db: ["sqlite"] } + - lint-code + + "Weekly run specs against main": triggers: - schedule: cron: "0 0 * * 4" # every Thursday filters: branches: only: - - master + - main jobs: - - run-specs-with-postgres - - run-specs-with-mysql + - run-specs: + name: *name + matrix: + parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] } + - run-specs: + name: *name + matrix: + parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] } diff --git a/.rubocop.yml b/.rubocop.yml index 6207a64..a49247d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,38 @@ +inherit_from: .rubocop_todo.yml + require: - solidus_dev_support/rubocop + +FactoryBot/CreateList: + Enabled: false + +FactoryBot/AssociationStyle: + Enabled: false + +FactoryBot/FactoryNameStyle: + Enabled: false + +FactoryBot/ConsistentParenthesesStyle: + Enabled: false + +FactoryBot/FactoryClassName: + Enabled: false + +FactoryBot/RedundantFactoryOption: + Enabled: false + +Gemspec/RequiredRubyVersion: + Enabled: false + +Capybara/RSpec/PredicateMatcher: + Enabled: false + +Capybara/NegationMatcher: + Enabled: false + +Capybara/ClickLinkOrButtonStyle: + Enabled: false + +AllCops: + NewCops: disable + TargetRubyVersion: 3.2 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..2bd1211 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,73 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2025-03-18 11:13:14 UTC using RuboCop version 1.74.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Lint/RedundantCopDisableDirective: + Exclude: + - 'lib/generators/solidus_static_content/install/install_generator.rb' + +# Offense count: 3 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/features/spree/static_content_spec.rb' + - 'spec/models/spree/page_spec.rb' + +# Offense count: 1 +# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. +# Include: **/*_spec*rb*, **/spec/**/* +RSpec/FilePath: + Exclude: + - 'spec/helpers/spree/page_helper_spec.rb' + +# Offense count: 4 +# Configuration parameters: AssignmentOnly. +RSpec/InstanceVariable: + Exclude: + - 'spec/models/spree/page_spec.rb' + +# Offense count: 4 +RSpec/MultipleExpectations: + Max: 4 + +# Offense count: 4 +# Configuration parameters: EnforcedStyle, IgnoreSharedExamples. +# SupportedStyles: always, named_only +RSpec/NamedSubject: + Exclude: + - 'spec/controllers/spree/admin/pages_controller_spec.rb' + - 'spec/lib/solidus_static_content/route_matcher_spec.rb' + +# Offense count: 1 +# Configuration parameters: Include. +# Include: app/models/**/*.rb +Rails/HasAndBelongsToMany: + Exclude: + - 'app/models/spree/page.rb' + +# Offense count: 1 +Rails/OutputSafety: + Exclude: + - 'app/helpers/spree/pages_helper.rb' + +# Offense count: 4 +# Configuration parameters: ForbiddenMethods, AllowedMethods. +# ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all +Rails/SkipsModelValidations: + Exclude: + - 'db/migrate/20090625125735_extend_pages.rb' + - 'db/migrate/20090814113100_add_visible_to_pages.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings. +# URISchemes: http, https +Layout/LineLength: + Max: 150 diff --git a/Gemfile b/Gemfile index 661491e..50e6757 100644 --- a/Gemfile +++ b/Gemfile @@ -3,30 +3,43 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -branch = ENV.fetch('SOLIDUS_BRANCH', 'master') -solidus_git, solidus_frontend_git = if (branch == 'master') || (branch >= 'v3.2') - %w[solidusio/solidus solidusio/solidus_frontend] - else - %w[solidusio/solidus] * 2 - end -gem 'solidus', github: solidus_git, branch: branch -gem 'solidus_frontend', github: solidus_frontend_git, branch: branch - -# Needed to help Bundler figure out how to resolve dependencies, -# otherwise it takes forever to resolve them. -# See https://github.com/bundler/bundler/issues/6677 -gem 'rails', '>0.a' - -# Provides basic authentication functionality for testing parts of your engine -gem 'solidus_auth_devise' - -case ENV['DB'] +branch = ENV.fetch('SOLIDUS_BRANCH', 'main') +gem 'solidus', github: 'solidusio/solidus', branch: branch + +# The solidus_frontend gem has been pulled out since v3.2 +if branch >= 'v3.2' + gem 'solidus_frontend' +elsif branch == 'main' + gem 'solidus_frontend', github: 'solidusio/solidus_frontend' +else + gem 'solidus_frontend', github: 'solidusio/solidus', branch: branch +end + +rails_version = ENV.fetch('RAILS_VERSION', '~> 7.0') +gem 'rails', rails_version + +# Extract the minimum Rails version from the version requirement. +# For example, a requirement of "~> 7.0" translates to ">= 7.0" and "< 8.0". +rails_req = Gem::Requirement.new(rails_version) +# Find the minimum version specified by a ">=" constraint, if any. +min_rails_version = rails_req.requirements.find { |op, _| op == '>=' }&.last || Gem::Version.new('0') + +# Determine the sqlite3 version based on the minimum Rails version. +# If the minimum Rails version is less than 7.2, use "~> 1.4"; otherwise, use "~> 2.0". +sqlite_version = + if min_rails_version < Gem::Version.new('7.2') + "~> 1.4" + else + "~> 2.0" + end + +case ENV.fetch('DB', nil) when 'mysql' gem 'mysql2' when 'postgresql' gem 'pg' else - gem 'sqlite3' + gem 'sqlite3', sqlite_version end gemspec diff --git a/Guardfile b/Guardfile index a5bd54e..363a8c8 100644 --- a/Guardfile +++ b/Guardfile @@ -1,8 +1,10 @@ +# frozen_string_literal: true + guard "rspec", cmd: "bundle exec rspec" do watch("spec/spec_helper.rb") { "spec" } watch("config/routes.rb") { "spec/controllers" } watch("app/controllers/application_controller.rb") { "spec/controllers" } - watch(%r{^spec/(.+)_spec\.rb$}) { |m| "spec/#{m[1]}_spec.rb"} + watch(%r{^spec/(.+)_spec\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^app/(.+)_decorator\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^(app|lib)/(.+)(\.rb|\.erb)$}) { |m| "spec/#{m[2]}_spec.rb" } watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb" } diff --git a/README.md b/README.md index c657fa4..26d5f97 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,27 @@ Good, clean content management of pages for Solidus. You can use it to: Add solidus_static_content to your Gemfile: -```ruby -gem 'solidus_static_content' -``` +### Starter Frontend Bundle your dependencies and run the installation generator: ```shell bundle -bundle exec rails g solidus_static_content:install +bin/rails generate solidus_static_content:install --frontend=starter +``` + +Please, be aware that the installation only works with the default +implementation of the starter frontend. Any customization to the files that +will be modified by the installer might break the installation procedure. +If that happens, try to adapt the installed code on top of the customizations +of the store. + +### Legacy Frontend + +If you are using the legacy `solidus_frontend` gem, please run this command instead: + +```shell +bin/rails generate solidus_static_content:install ``` ## Usage diff --git a/app/helpers/spree/pages_helper.rb b/app/helpers/spree/pages_helper.rb index 62efbaf..8175add 100644 --- a/app/helpers/spree/pages_helper.rb +++ b/app/helpers/spree/pages_helper.rb @@ -1,6 +1,10 @@ -module Spree::PagesHelper - def render_snippet(slug) - page = Spree::Page.find_by_slug(slug) - raw page.body if page +# frozen_string_literal: true + +module Spree + module PagesHelper + def render_snippet(slug) + page = Spree::Page.find_by(slug: slug) + raw page.body if page + end end -end \ No newline at end of file +end diff --git a/app/models/spree/page.rb b/app/models/spree/page.rb index a761eaf..d638685 100644 --- a/app/models/spree/page.rb +++ b/app/models/spree/page.rb @@ -1,49 +1,53 @@ -class Spree::Page < ActiveRecord::Base - acts_as_list - default_scope -> { order("position ASC") } +# frozen_string_literal: true - has_and_belongs_to_many :stores, join_table: 'spree_pages_stores' +module Spree + class Page < ApplicationRecord + acts_as_list + default_scope -> { order("position ASC") } - validates_presence_of :title - validates_presence_of [:slug, :body], :if => :not_using_foreign_link? - validates_presence_of :layout, :if => :render_layout_as_partial? - validates_presence_of :stores + has_and_belongs_to_many :stores, join_table: 'spree_pages_stores' - validates :slug, :uniqueness => true, :if => :not_using_foreign_link? - validates :foreign_link, :uniqueness => true, :allow_blank => true + validates :title, presence: true + validates :slug, :body, presence: true, if: :not_using_foreign_link? + validates :layout, presence: { if: :render_layout_as_partial? } + validates :stores, presence: true - before_validation :generate_slug, if: :title? + validates :slug, uniqueness: true, if: :not_using_foreign_link? + validates :foreign_link, uniqueness: true, allow_blank: true - scope :visible, -> { where(:visible => true) } - scope :header_links, -> { where(:show_in_header => true).visible } - scope :footer_links, -> { where(:show_in_footer => true).visible } - scope :sidebar_links, -> { where(:show_in_sidebar => true).visible } - scope :ordered_by_position, -> { order(:position) } + before_validation :generate_slug, if: :title? - scope :by_store, lambda { |store| joins(:stores).where("spree_pages_stores.store_id = ?", store) } + scope :visible, -> { where(visible: true) } + scope :header_links, -> { where(show_in_header: true).visible } + scope :footer_links, -> { where(show_in_footer: true).visible } + scope :sidebar_links, -> { where(show_in_sidebar: true).visible } + scope :ordered_by_position, -> { order(:position) } - before_save :normalize_slug + scope :by_store, lambda { |store| joins(:stores).where("spree_pages_stores.store_id = ?", store) } - def link - foreign_link.blank? ? slug : foreign_link - end + before_save :normalize_slug - def meta_title - super.presence || title - end + def link + foreign_link.presence || slug + end -private + def meta_title + super.presence || title + end - def normalize_slug - # ensure that all slugs start with a slash - slug.prepend('/') if not_using_foreign_link? and not slug.start_with? '/' - end + private - def not_using_foreign_link? - foreign_link.blank? - end + def normalize_slug + # ensure that all slugs start with a slash + slug.prepend('/') if not_using_foreign_link? && (!slug.start_with? '/') + end + + def not_using_foreign_link? + foreign_link.blank? + end - def generate_slug - self.slug ||= title.parameterize + def generate_slug + self.slug ||= title.parameterize + end end end diff --git a/app/overrides/pages_in_footer.rb b/app/overrides/pages_in_footer.rb deleted file mode 100644 index d87dcad..0000000 --- a/app/overrides/pages_in_footer.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/shared/_footer", - :name => "pages_in_footer", - :insert_bottom => "#footer-right", - :partial => "spree/static_content/static_content_footer", - :disabled => false) \ No newline at end of file diff --git a/app/overrides/pages_in_header.rb b/app/overrides/pages_in_header.rb deleted file mode 100644 index 42414cb..0000000 --- a/app/overrides/pages_in_header.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/shared/_main_nav_bar", - :name => "pages_in_header", - :insert_bottom => "#main-nav-bar", - :partial => "spree/static_content/static_content_header", - :disabled => false) \ No newline at end of file diff --git a/app/overrides/pages_in_sidebar.rb b/app/overrides/pages_in_sidebar.rb deleted file mode 100644 index dff7751..0000000 --- a/app/overrides/pages_in_sidebar.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/shared/_sidebar", - :name => "pages_in_sidebar", - :insert_bottom => "#sidebar", - :partial => "spree/static_content/static_content_sidebar", - :disabled => false) diff --git a/app/overrides/solidus_static_content/pages_in_footer.rb b/app/overrides/solidus_static_content/pages_in_footer.rb new file mode 100644 index 0000000..6e1747b --- /dev/null +++ b/app/overrides/solidus_static_content/pages_in_footer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module SolidusStaticContent + module PagesInFooter + Deface::Override.new(virtual_path: "spree/shared/_footer", + name: "pages_in_footer", + insert_bottom: "#footer-right", + partial: "spree/static_content/static_content_footer", + disabled: false) + end +end diff --git a/app/overrides/solidus_static_content/pages_in_header.rb b/app/overrides/solidus_static_content/pages_in_header.rb new file mode 100644 index 0000000..83adba1 --- /dev/null +++ b/app/overrides/solidus_static_content/pages_in_header.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module SolidusStaticContent + module PagesInHeader + Deface::Override.new(virtual_path: "spree/shared/_main_nav_bar", + name: "pages_in_header", + insert_bottom: "#main-nav-bar", + partial: "spree/static_content/static_content_header", + disabled: false) + end +end diff --git a/app/overrides/solidus_static_content/pages_in_sidebar.rb b/app/overrides/solidus_static_content/pages_in_sidebar.rb new file mode 100644 index 0000000..72c81f2 --- /dev/null +++ b/app/overrides/solidus_static_content/pages_in_sidebar.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module SolidusStaticContent + module PagesInSidebar + Deface::Override.new(virtual_path: "spree/shared/_sidebar", + name: "pages_in_sidebar", + insert_bottom: "#sidebar", + partial: "spree/static_content/static_content_sidebar", + disabled: false) + end +end diff --git a/bin/r b/bin/r index c149948..78578b4 100755 --- a/bin/r +++ b/bin/r @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true warn %{ DEPRECATION: bin/r has been replaced by bin/rails-engine, please use that diff --git a/bin/rails b/bin/rails index 6dbbbc3..3833db4 100755 --- a/bin/rails +++ b/bin/rails @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true if %w[g generate].include? ARGV.first exec "#{__dir__}/rails-engine", *ARGV diff --git a/bin/rails-engine b/bin/rails-engine index ae96890..3d5c3b5 100755 --- a/bin/rails-engine +++ b/bin/rails-engine @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + # This command will automatically be run when you run "rails" with Rails gems # installed from the root of your application. diff --git a/bin/rails-sandbox b/bin/rails-sandbox index c89b2ae..1a8861b 100755 --- a/bin/rails-sandbox +++ b/bin/rails-sandbox @@ -1,11 +1,12 @@ #!/usr/bin/env ruby +# frozen_string_literal: true app_root = 'sandbox' unless File.exist? "#{app_root}/bin/rails" warn 'Creating the sandbox app...' Dir.chdir "#{__dir__}/.." do - system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr + system "#{__dir__}/sandbox" or begin warn 'Automatic creation of the sandbox app failed' exit 1 end diff --git a/bin/sandbox b/bin/sandbox index ec62207..cd1fdc1 100755 --- a/bin/sandbox +++ b/bin/sandbox @@ -22,19 +22,19 @@ if [ ! -z $SOLIDUS_BRANCH ] then BRANCH=$SOLIDUS_BRANCH else - BRANCH="master" + BRANCH="main" fi extension_name="solidus_static_content" # Stay away from the bundler env of the containing extension. function unbundled { - ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@ + ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- "$@" } rm -rf ./sandbox -unbundled bundle exec rails new sandbox --database="$RAILSDB" \ - --skip-bundle \ +rails new sandbox \ + --database="$RAILSDB" \ --skip-git \ --skip-keeps \ --skip-rc \ @@ -47,37 +47,30 @@ if [ ! -d "sandbox" ]; then exit 1 fi +# 🔥 Fix for Sprockets error: Ensure the manifest.js file exists +mkdir -p sandbox/app/assets/config +cat < sandbox/app/assets/config/manifest.js +//= link_tree ../images +//= link_directory ../javascripts .js +//= link_directory ../stylesheets .css +EOF + cd ./sandbox cat <> Gemfile -gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH' -gem 'solidus_auth_devise', '>= 2.1.0' -gem 'rails-i18n' -gem 'solidus_i18n' - -gem '$extension_name', path: '..' - group :test, :development do platforms :mri do - gem 'pry-byebug' + gem 'pry' end end RUBY - -unbundled bundle install --gemfile Gemfile - +unbundled bundle add solidus --github solidusio/solidus --branch "${BRANCH:-main}" --version '> 0.a' unbundled bundle exec rake db:drop db:create - -unbundled bundle exec rails generate spree:install \ - --auto-accept \ - --user_class=Spree::User \ - --enforce_available_locales=true \ - --with-authentication=false \ - $@ - -unbundled bundle exec rails generate solidus:auth:install +unbundled bundle exec rails generate solidus:install --payment-method=none --auto-accept "$@" +unbundled bundle add ${extension_name} --path '../' +unbundled bundle exec rails generate $extension_name:install --frontend=starter --auto-run-migrations echo -echo "🚀 Sandbox app successfully created for $extension_name!" +echo "🚀 Sandbox app successfully created for ${extension_name}!" echo "🚀 Using $RAILSDB and Solidus $BRANCH" echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter" echo "🚀 Use 'export SOLIDUS_BRANCH=' to control the Solidus version" diff --git a/bin/sandbox_rails b/bin/sandbox_rails index 024315c..bf2d0e8 100755 --- a/bin/sandbox_rails +++ b/bin/sandbox_rails @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true warn %{ DEPRECATION: bin/sandbox_rails has been replaced by bin/rails-sandbox, please diff --git a/config/routes.rb b/config/routes.rb index e086809..2ea81d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,10 @@ end end - constraints(SolidusStaticContent::RouteMatcher) do - get '/(*path)', :to => 'static_content#show', :as => 'static' + # Check if the layout is not the default layout to avoid conflicts with the starter_frontend routes + if Spree::Config.layout != 'layouts/storefront' + constraints(SolidusStaticContent::RouteMatcher) do + get '/(*path)', to: 'static_content#show', as: 'static' + end end end diff --git a/db/migrate/20081216193152_create_pages.rb b/db/migrate/20081216193152_create_pages.rb index b66d051..03c0df3 100644 --- a/db/migrate/20081216193152_create_pages.rb +++ b/db/migrate/20081216193152_create_pages.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreatePages < SolidusSupport::Migration[4.2] def self.up create_table :spree_pages do |t| diff --git a/db/migrate/20090625125735_extend_pages.rb b/db/migrate/20090625125735_extend_pages.rb index da9c553..1863b77 100644 --- a/db/migrate/20090625125735_extend_pages.rb +++ b/db/migrate/20090625125735_extend_pages.rb @@ -1,19 +1,20 @@ +# frozen_string_literal: true + class ExtendPages < SolidusSupport::Migration[4.2] - class Page < ActiveRecord::Base + class Page < ApplicationRecord end def self.up change_table :spree_pages do |t| - t.boolean :show_in_header, :default => false, :null => false - t.boolean :show_in_footer, :default => false, :null => false + t.boolean :show_in_header, default: false, null: false + t.boolean :show_in_footer, default: false, null: false t.string :foreign_link - t.integer :position, :default => 1, :null => false + t.integer :position, default: 1, null: false if Page.table_exists? - Page.order(:updated_at).each_with_index{|page,x| page.update_attribute(:position, x+1)} + Page.order(:updated_at).each_with_index{ |page, x| page.update_attribute(:position, x + 1) } else - Spree::Page.order(:updated_at).each_with_index{|page,x| page.update_attribute(:position, x+1)} + Spree::Page.order(:updated_at).each_with_index{ |page, x| page.update_attribute(:position, x + 1) } end - end end diff --git a/db/migrate/20090814113100_add_visible_to_pages.rb b/db/migrate/20090814113100_add_visible_to_pages.rb index baf56bb..d70732b 100644 --- a/db/migrate/20090814113100_add_visible_to_pages.rb +++ b/db/migrate/20090814113100_add_visible_to_pages.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + class AddVisibleToPages < SolidusSupport::Migration[4.2] - class Page < ActiveRecord::Base + class Page < ApplicationRecord end def self.up add_column :spree_pages, :visible, :boolean if Page.table_exists? - Page.update_all :visible => true + Page.update_all visible: true else - Spree::Page.update_all :visible => true + Spree::Page.update_all visible: true end end diff --git a/db/migrate/20090814142845_add_default_true_to_visible_for_page.rb b/db/migrate/20090814142845_add_default_true_to_visible_for_page.rb index 7fcfd4b..287a49d 100644 --- a/db/migrate/20090814142845_add_default_true_to_visible_for_page.rb +++ b/db/migrate/20090814142845_add_default_true_to_visible_for_page.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + class AddDefaultTrueToVisibleForPage < SolidusSupport::Migration[4.2] def self.up - change_column :spree_pages, :visible, :boolean, :default=> true + change_column :spree_pages, :visible, :boolean, default: true end - def self.down - end -end \ No newline at end of file + def self.down; end +end diff --git a/db/migrate/20090829000527_add_index_for_page.rb b/db/migrate/20090829000527_add_index_for_page.rb index 239deca..6a02e8b 100644 --- a/db/migrate/20090829000527_add_index_for_page.rb +++ b/db/migrate/20090829000527_add_index_for_page.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddIndexForPage < SolidusSupport::Migration[4.2] def self.up add_index(:spree_pages, :slug) @@ -7,4 +9,3 @@ def self.down remove_index(:spree_pages, :slug) end end - diff --git a/db/migrate/20091219021134_add_meta_fields_to_pages.rb b/db/migrate/20091219021134_add_meta_fields_to_pages.rb index d728a2f..a7c3f39 100644 --- a/db/migrate/20091219021134_add_meta_fields_to_pages.rb +++ b/db/migrate/20091219021134_add_meta_fields_to_pages.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true + class AddMetaFieldsToPages < SolidusSupport::Migration[4.2] def self.up add_column :spree_pages, :meta_keywords, :string add_column :spree_pages, :meta_description, :string end - def self.down - end + def self.down; end end diff --git a/db/migrate/20100204105222_add_layout_to_pages.rb b/db/migrate/20100204105222_add_layout_to_pages.rb index fb0cfc7..facd176 100644 --- a/db/migrate/20100204105222_add_layout_to_pages.rb +++ b/db/migrate/20100204105222_add_layout_to_pages.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddLayoutToPages < SolidusSupport::Migration[4.2] def self.up add_column :spree_pages, :layout, :string @@ -6,4 +8,4 @@ def self.up def self.down remove_column :spree_pages, :layout end -end \ No newline at end of file +end diff --git a/db/migrate/20100323085528_add_show_in_sidebar_option_to_pages.rb b/db/migrate/20100323085528_add_show_in_sidebar_option_to_pages.rb index 48873f1..3dd2efd 100644 --- a/db/migrate/20100323085528_add_show_in_sidebar_option_to_pages.rb +++ b/db/migrate/20100323085528_add_show_in_sidebar_option_to_pages.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + class AddShowInSidebarOptionToPages < SolidusSupport::Migration[4.2] def self.up - add_column :spree_pages, :show_in_sidebar, :boolean, :default=> false, :null=>false + add_column :spree_pages, :show_in_sidebar, :boolean, default: false, null: false end def self.down remove_column :spree_pages, :show_in_sidebar end -end \ No newline at end of file +end diff --git a/db/migrate/20110717103112_add_meta_title_to_page.rb b/db/migrate/20110717103112_add_meta_title_to_page.rb index ec37c4e..f5d42be 100644 --- a/db/migrate/20110717103112_add_meta_title_to_page.rb +++ b/db/migrate/20110717103112_add_meta_title_to_page.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddMetaTitleToPage < SolidusSupport::Migration[4.2] def self.up add_column :spree_pages, :meta_title, :string diff --git a/db/migrate/20120723144115_add_render_as_partial_for_layout_for_spree_pages.rb b/db/migrate/20120723144115_add_render_as_partial_for_layout_for_spree_pages.rb index 1cbaa91..6dd4329 100644 --- a/db/migrate/20120723144115_add_render_as_partial_for_layout_for_spree_pages.rb +++ b/db/migrate/20120723144115_add_render_as_partial_for_layout_for_spree_pages.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + class AddRenderAsPartialForLayoutForSpreePages < SolidusSupport::Migration[4.2] def up - unless column_exists? :spree_pages, :render_layout_as_partial - add_column :spree_pages, :render_layout_as_partial, :boolean, :default => false - end + return if column_exists? :spree_pages, :render_layout_as_partial + + add_column :spree_pages, :render_layout_as_partial, :boolean, default: false end def down - if column_exists? :spree_pages, :render_layout_as_partial - remove_column :spree_pages, :render_layout_as_partial - end + return unless column_exists? :spree_pages, :render_layout_as_partial + + remove_column :spree_pages, :render_layout_as_partial end end diff --git a/db/migrate/20140926121757_add_pages_stores.rb b/db/migrate/20140926121757_add_pages_stores.rb index 5ed703a..e33636e 100644 --- a/db/migrate/20140926121757_add_pages_stores.rb +++ b/db/migrate/20140926121757_add_pages_stores.rb @@ -1,7 +1,8 @@ -class AddPagesStores < SolidusSupport::Migration[4.2] +# frozen_string_literal: true +class AddPagesStores < SolidusSupport::Migration[4.2] def change - create_table :spree_pages_stores, :id => false do |t| + create_table :spree_pages_stores, id: false do |t| t.integer :store_id t.integer :page_id t.timestamps @@ -9,6 +10,5 @@ def change add_index :spree_pages_stores, :store_id add_index :spree_pages_stores, :page_id - end end diff --git a/db/migrate/20180305193240_update_spree_page_position.rb b/db/migrate/20180305193240_update_spree_page_position.rb index 7e25571..fd4742b 100644 --- a/db/migrate/20180305193240_update_spree_page_position.rb +++ b/db/migrate/20180305193240_update_spree_page_position.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + class UpdateSpreePagePosition < SolidusSupport::Migration[4.2] def up - if column_exists?(:spree_pages, :position) - change_column :spree_pages, :position, :integer, default: 0 - end + return unless column_exists?(:spree_pages, :position) + + change_column :spree_pages, :position, :integer, default: 0 end def down - if column_exists?(:spree_pages, :position) - change_column :spree_pages, :position, :boolean, default: false - end + return unless column_exists?(:spree_pages, :position) + + change_column :spree_pages, :position, :boolean, default: false end end diff --git a/lib/controllers/backend/spree/admin/pages_controller.rb b/lib/controllers/backend/spree/admin/pages_controller.rb index a81d7f5..8f44870 100644 --- a/lib/controllers/backend/spree/admin/pages_controller.rb +++ b/lib/controllers/backend/spree/admin/pages_controller.rb @@ -1,16 +1,22 @@ -class Spree::Admin::PagesController < Spree::Admin::ResourceController - private +# frozen_string_literal: true - # Set some default attributes - def build_resource - super.tap do |resource| - if resource.stores.blank? - resource.stores << Spree::Store.default +module Spree + module Admin + class PagesController < Spree::Admin::ResourceController + private + + # Set some default attributes + def build_resource + super.tap do |resource| + if resource.stores.blank? + resource.stores << Spree::Store.default + end + end end - end - end - def collection - super.ordered_by_position + def collection + super.ordered_by_position + end + end end end diff --git a/lib/controllers/frontend/spree/static_content_controller.rb b/lib/controllers/frontend/spree/static_content_controller.rb index 2f23797..eb24b9c 100644 --- a/lib/controllers/frontend/spree/static_content_controller.rb +++ b/lib/controllers/frontend/spree/static_content_controller.rb @@ -1,24 +1,32 @@ -class Spree::StaticContentController < Spree::StoreController - helper 'spree/products' - layout :determine_layout +# frozen_string_literal: true - def show - @page = Spree::Page.by_store(current_store).visible.find_by_slug!(request.path_info) +module Spree + class StaticContentController < Spree::StoreController + helper 'spree/products' + layout :determine_layout - # Assign static_content to let solidus recognize it as the current - # controller resource, this is used by meta tags and in other places. - @static_content = @page + def show + @page = Spree::Page.by_store(current_store).visible.find_by!(slug: request.path_info) - @taxonomies = Spree::Taxonomy.includes(root: :children) - end + # Assign static_content to let solidus recognize it as the current + # controller resource, this is used by meta tags and in other places. + @static_content = @page + + @taxonomies = Spree::Taxonomy.includes(root: :children) + end + + private - private def determine_layout - return @page.layout if @page and @page.layout.present? and not @page.render_layout_as_partial? + return @page.layout if @page && @page.layout.present? && !@page.render_layout_as_partial? + Spree::Config.layout end def accurate_title - @page ? (@page.meta_title.present? ? @page.meta_title : @page.title) : nil + return unless @page + + @page.meta_title.presence || @page.title end + end end diff --git a/lib/generators/solidus_static_content/install/install_generator.rb b/lib/generators/solidus_static_content/install/install_generator.rb index a15b649..2910320 100644 --- a/lib/generators/solidus_static_content/install/install_generator.rb +++ b/lib/generators/solidus_static_content/install/install_generator.rb @@ -3,7 +3,10 @@ module SolidusStaticContent module Generators class InstallGenerator < Rails::Generators::Base + source_root File.expand_path('templates', __dir__) class_option :auto_run_migrations, type: :boolean, default: false + # Either 'starter' or 'classic' + class_option :frontend, type: :string, default: 'classic' def add_stylesheets inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_static_content\n", before: %r{\*/}, verbose: true # rubocop:disable Metrics/LineLength @@ -13,6 +16,31 @@ def add_migrations run 'bin/rails railties:install:migrations FROM=solidus_static_content' end + def add_javascripts + empty_directory 'app/assets/javascripts' + end + + def copy_starter_frontend_files + return if options[:frontend] != 'starter' + + copy_file 'app/controllers/static_content_controller.rb' + copy_file 'app/views/static_content/show.html.erb' + + insert_into_file 'config/initializers/spree.rb', after: "Spree.config do |config|\n" do + <<~RUBY.indent(2) + config.layout = 'layouts/storefront' + + RUBY + end + route <<~ROUTES + if Spree::Config.layout == 'layouts/storefront' + constraints(SolidusStaticContent::RouteMatcher) do + get '/(*path)', to: 'static_content#show', as: 'static' + end + end + ROUTES + end + def run_migrations run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Metrics/LineLength if run_migrations diff --git a/lib/generators/solidus_static_content/install/templates/app/controllers/static_content_controller.rb b/lib/generators/solidus_static_content/install/templates/app/controllers/static_content_controller.rb new file mode 100644 index 0000000..3d2eae3 --- /dev/null +++ b/lib/generators/solidus_static_content/install/templates/app/controllers/static_content_controller.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class StaticContentController < StoreController + helper 'spree/products' + layout :determine_layout + + def show + @page = Spree::Page.by_store(current_store).visible.find_by!(slug: request.path_info) + + # Assign static_content to let solidus recognize it as the current + # controller resource, this is used by meta tags and in other places. + @static_content = @page + end + + private + + def determine_layout + return @page.layout if @page && @page.layout.present? && !@page.render_layout_as_partial? + + Spree::Config.layout + end + + def accurate_title + return unless @page + + @page.meta_title.presence || @page.title + end +end diff --git a/lib/generators/solidus_static_content/install/templates/app/views/static_content/show.html.erb b/lib/generators/solidus_static_content/install/templates/app/views/static_content/show.html.erb new file mode 100644 index 0000000..813d31a --- /dev/null +++ b/lib/generators/solidus_static_content/install/templates/app/views/static_content/show.html.erb @@ -0,0 +1,28 @@ +<% if @page.layout.present? and @page.render_layout_as_partial? %> + <%= render partial: @page.layout %> +<% else %> + <% content_for :head do -%> + <%- if @page.meta_title.present? -%> + + <%- else -%> + + <%- end -%> + + + <% end -%> + + <% content_for :sidebar do %> + <% if (defined?(@products) && @products) && (defined?(@taxon) && @taxon) %> + <%= render partial: 'spree/shared/filters' %> + <% elsif defined?(@taxonomies) && @taxonomies %> + <%= render partial: 'spree/shared/taxonomies' %> + <% end %> + <% end %> + +
+

<%= @page.title %>

+
+ <%= raw @page.body %> +
+
+<% end %> diff --git a/lib/solidus_static_content.rb b/lib/solidus_static_content.rb index dfea099..047d7e2 100644 --- a/lib/solidus_static_content.rb +++ b/lib/solidus_static_content.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true -require 'solidus_core' -require 'solidus_support' -require 'deface' - require 'solidus_static_content/version' require 'solidus_static_content/route_matcher' require 'solidus_static_content/engine' +require 'deface' diff --git a/lib/solidus_static_content/engine.rb b/lib/solidus_static_content/engine.rb index 8c5a536..d4d6752 100644 --- a/lib/solidus_static_content/engine.rb +++ b/lib/solidus_static_content/engine.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'spree/core' -require 'solidus_static_content' +require 'solidus_core' +require 'solidus_support' module SolidusStaticContent class Engine < Rails::Engine @@ -16,20 +16,18 @@ class Engine < Rails::Engine g.test_framework :rspec end - def self.menu_item - @menu_item ||= Spree::Backend::Config.class::MenuItem.new( - [:pages], - 'file-text', - condition: -> { can?(:admin, Spree::Page) }, - ) + initializer 'solidus_subscriptions.configure_backend' do + next unless ::Spree::Backend::Config.respond_to?(:menu_items) + + ::Spree::Backend::Config.configure do |config| + config.menu_items << config.class::MenuItem.new( + [:pages], + 'file-text', + url: :admin_pages_path, + condition: -> { can?(:admin, Spree::Page) }, + match_path: '/pages' + ) + end end - - def self.activate_menu_items - return if Spree::Backend::Config.menu_items.include?(menu_item) - - Spree::Backend::Config.menu_items << menu_item - end - - config.to_prepare(&method(:activate_menu_items)) end end diff --git a/lib/solidus_static_content/route_matcher.rb b/lib/solidus_static_content/route_matcher.rb index 2a680eb..ac7b8fc 100644 --- a/lib/solidus_static_content/route_matcher.rb +++ b/lib/solidus_static_content/route_matcher.rb @@ -1,11 +1,15 @@ -module SolidusStaticContent::RouteMatcher - EXCLUDED_PATHS = /^\/+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+/ +# frozen_string_literal: true - def self.matches?(request) - path = request.path_info +module SolidusStaticContent + module RouteMatcher + EXCLUDED_PATHS = %r{^/+(admin|account|cart|checkout|content|login|pg/|orders|products|s/|session|signup|shipments|states|t/|tax_categories|user)+} - return false if EXCLUDED_PATHS.match? path + def self.matches?(request) + path = request.path_info - Spree::Page.visible.where(slug: path).exists? + return false if EXCLUDED_PATHS.match? path + + Spree::Page.visible.where(slug: path).exists? + end end end diff --git a/lib/solidus_static_content/factories.rb b/lib/solidus_static_content/testing_support/factories.rb similarity index 100% rename from lib/solidus_static_content/factories.rb rename to lib/solidus_static_content/testing_support/factories.rb diff --git a/solidus_static_content.gemspec b/solidus_static_content.gemspec index 2cbea7a..9f215a1 100644 --- a/solidus_static_content.gemspec +++ b/solidus_static_content.gemspec @@ -30,8 +30,8 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency 'deface', '~> 1.0' - spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 4'] - spec.add_dependency 'solidus_support', '~> 0.6' + spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 5'] + spec.add_dependency 'solidus_support', '~> 0.8' - spec.add_development_dependency 'solidus_dev_support' + spec.add_development_dependency 'solidus_dev_support', '~> 2.7' end diff --git a/spec/controllers/spree/admin/pages_controller_spec.rb b/spec/controllers/spree/admin/pages_controller_spec.rb index 58c5a2a..bfbaf44 100644 --- a/spec/controllers/spree/admin/pages_controller_spec.rb +++ b/spec/controllers/spree/admin/pages_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Spree::Admin::PagesController do diff --git a/spec/controllers/spree/static_content_controller_spec.rb b/spec/controllers/spree/static_content_controller_spec.rb index b154ee8..cdc93fd 100644 --- a/spec/controllers/spree/static_content_controller_spec.rb +++ b/spec/controllers/spree/static_content_controller_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::StaticContentController, type: :controller do let!(:store) { create(:store, default: true) } - context '#show' do + describe '#show' do it 'accepts path as root' do page = create(:page, slug: '/', stores: [store]) request.path = page.slug diff --git a/spec/features/spree/admin/pages_spec.rb b/spec/features/spree/admin/pages_spec.rb index 6157bf5..38313cb 100644 --- a/spec/features/spree/admin/pages_spec.rb +++ b/spec/features/spree/admin/pages_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' -RSpec.feature 'Admin Static Content', js: true do +RSpec.describe 'Admin Static Content', js: true do stub_authorization! - background do + before do create :store, default: true end context 'when no page exists' do - background do + before do visit spree.admin_path click_link 'Pages' end - scenario 'can create new page' do + it 'can create new page' do expect(page).to have_text /No Pages found/i click_link 'New page' @@ -32,7 +34,7 @@ check 'page_show_in_header' check 'page_show_in_footer' - expect(find('#page_visible')).to be_checked + expect(find_by_id('page_visible')).to be_checked click_button 'Create' expect(page).to have_text 'successfully created!' @@ -40,14 +42,14 @@ end context 'when page exists' do - given!(:static_page) { create(:page) } + let!(:static_page) { create(:page) } - background do + before do visit spree.admin_path click_link 'Pages' end - scenario 'can be updated' do + it 'can be updated' do within_row(1) do click_icon :edit end @@ -60,7 +62,7 @@ expect(page).to have_text 'successfully updated!' end - scenario 'can be deleted' do + it 'can be deleted' do within_row(1) do accept_confirm { click_icon :trash } end diff --git a/spec/features/spree/static_content_spec.rb b/spec/features/spree/static_content_spec.rb index 7204d83..7aaeb27 100644 --- a/spec/features/spree/static_content_spec.rb +++ b/spec/features/spree/static_content_spec.rb @@ -1,43 +1,44 @@ -require 'spec_helper' +# frozen_string_literal: true -feature 'Static Content Page', js: true do +require 'spec_helper' +describe 'Static Content Page', js: true do let!(:store) { create(:store, default: true) } context 'render page' do - scenario 'is a query string' do + it 'is a query string' do create(:page, slug: '/page', title: 'Query Test', stores: [store]) visit '/page?test' expect(page).to have_text 'Query Test' end - scenario 'can have slug not starting by /' do + it 'can have slug not starting by /' do create(:page, slug: 'page2', title: 'No Slash Prefix Test', stores: [store]) visit '/page2' expect(page).to have_text 'No Slash Prefix Test' end - scenario 'can have slug with multiple /' do + it 'can have slug with multiple /' do create(:page, slug: '/hello/shoppers/page3', title: 'Multiple Slash Test', stores: [store]) visit '/hello/shoppers/page3' expect(page).to have_text 'Multiple Slash Test' end - scenario 'can be a custom root page' do + it 'can be a custom root page' do create(:page, slug: '/', title: 'Root Page Test', stores: [store]) visit '/' expect(page).to have_text 'Root Page Test' end - scenario 'is limited within its own constraints', js: false do - create(:taxon, permalink: 'test', name: 'The Taxon') - create(:page, slug: '/t/test', title: 'The Page', stores: [store]) - visit '/t/test' + it 'is limited within its own constraints' do + taxon = create(:taxon, permalink: 'test', name: 'The Taxon') + create(:page, slug: "/t/#{taxon.permalink}", title: 'The Page', stores: [store]) + visit "/t/#{taxon.permalink}" expect(page).to have_content('The Taxon') expect(page).not_to have_content('The Page') end - scenario 'fetch correct page' do + it 'fetch correct page' do create(:page, slug: '/', stores: [store]) create(:page, slug: 'hello', title: 'Hello', stores: [store]) create(:page, slug: 'somwhere', stores: [store]) @@ -46,7 +47,7 @@ expect(page).to have_text 'Hello' end - scenario 'do not effect the rendering of the rest of the site' do + it 'do not effect the rendering of the rest of the site' do product = create(:product) visit spree.product_path(product) expect(page).to have_text product.name diff --git a/spec/helpers/spree/page_helper_spec.rb b/spec/helpers/spree/page_helper_spec.rb index 0bcd07c..90c8337 100644 --- a/spec/helpers/spree/page_helper_spec.rb +++ b/spec/helpers/spree/page_helper_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Spree::PagesHelper, type: :helper do - context '.render_snippet' do + describe '.render_snippet' do it 'return body when page exists' do page = create(:page) expect(render_snippet(page.slug)).to eq page.body diff --git a/spec/lib/solidus_static_content/engine_spec.rb b/spec/lib/solidus_static_content/engine_spec.rb index 1b776e0..b6160c9 100644 --- a/spec/lib/solidus_static_content/engine_spec.rb +++ b/spec/lib/solidus_static_content/engine_spec.rb @@ -1,14 +1,26 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe SolidusStaticContent::Engine do - describe '.activate_menu_item' do - it 'adds the menu item only once' do - described_class.activate_menu_items - - expect { - described_class.activate_menu_items - described_class.activate_menu_items - }.not_to change(Spree::Backend::Config.menu_items, :size) + describe 'initializer' do + it 'adds the pages menu item to the backend configuration' do + expect(Spree::Backend::Config.menu_items.any? { |item| item.label.to_sym == :pages }).to be true + end + + it 'sets the correct icon for the pages menu item' do + menu_item = Spree::Backend::Config.menu_items.find { |item| item.label.to_sym == :pages } + expect(menu_item.icon).to eq('file-text') + end + + it 'sets the correct match_path for the pages menu item' do + menu_item = Spree::Backend::Config.menu_items.find { |item| item.label.to_sym == :pages } + expect(menu_item.match_path).to eq('/pages') + end + + it 'ensures the pages menu item is added only once' do + pages_menu_items = Spree::Backend::Config.menu_items.select { |item| item.label.to_sym == :pages } + expect(pages_menu_items.size).to eq(1) end end end diff --git a/spec/lib/solidus_static_content/route_matcher_spec.rb b/spec/lib/solidus_static_content/route_matcher_spec.rb index 8c2b572..03096e0 100644 --- a/spec/lib/solidus_static_content/route_matcher_spec.rb +++ b/spec/lib/solidus_static_content/route_matcher_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' describe SolidusStaticContent::RouteMatcher do subject { described_class } - context '.matches?' do + describe '.matches?' do it 'is true when valid page' do page = create(:page, slug: 'hello', visible: true) request = instance_double(Rack::Request, path_info: page.slug) diff --git a/spec/models/spree/page_spec.rb b/spec/models/spree/page_spec.rb index 32c70b5..c765e4a 100644 --- a/spec/models/spree/page_spec.rb +++ b/spec/models/spree/page_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::Page do @@ -9,7 +11,7 @@ end end - context '.link' do + describe '.link' do it 'return slug if foreign_link blank' do page = create(:page, slug: 'hello') expect(page.link).to eq page.slug @@ -22,16 +24,16 @@ end context "pages in stores" do - before(:each) do + before do @store = create(:store) - @page = create(:page, :stores => [@store]) + @page = create(:page, stores: [@store]) @page2 = create(:page) end - it 'should correctly find pages by store' do - pages_by_store = Spree::Page.by_store(@store) + it 'correctlies find pages by store' do + pages_by_store = described_class.by_store(@store) expect(pages_by_store).to include(@page) - expect(pages_by_store).to_not include(@page2) + expect(pages_by_store).not_to include(@page2) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bc06322..5adf4c1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,22 +6,27 @@ # Run Coverage report require 'solidus_dev_support/rspec/coverage' -require File.expand_path('dummy/config/environment.rb', __dir__).tap { |file| - # Create the dummy app if it's still missing. - system 'bin/rake extension:test_app' unless File.exist? file -} +# Create the dummy app if it's still missing. +dummy_env = "#{__dir__}/dummy/config/environment.rb" +system 'bin/rake extension:test_app' unless File.exist? dummy_env +require dummy_env # Requires factories and other useful helpers defined in spree_core. require 'solidus_dev_support/rspec/feature_helper' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } +Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f } -# Requires factories defined in lib/solidus_static_content/factories.rb -require 'solidus_static_content/factories' +# Requires factories defined in Solidus core and this extension. +# See: lib/solidus_static_content/testing_support/factories.rb +SolidusDevSupport::TestingSupport::Factories.load_for(SolidusStaticContent::Engine) RSpec.configure do |config| config.infer_spec_type_from_file_location! config.use_transactional_fixtures = false + + if Spree.solidus_gem_version < Gem::Version.new('2.11') + config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :system + end end