From a2867b0cfe3087f88e5f15cd8bcca6c0ea72d729 Mon Sep 17 00:00:00 2001 From: Julien Lestavel Date: Wed, 2 May 2018 13:42:33 +0200 Subject: [PATCH 1/3] Change Rails locked version to 5.2 --- Gemfile.lock | 157 ++++++++++++++++++++++++++++----------------------- 1 file changed, 86 insertions(+), 71 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 68e32d7..e0bc75c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,55 +7,63 @@ PATH GEM remote: https://rubygems.org/ specs: - actionmailer (4.2.9) - actionpack (= 4.2.9) - actionview (= 4.2.9) - activejob (= 4.2.9) + actioncable (5.2.0) + actionpack (= 5.2.0) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.0) + actionpack (= 5.2.0) + actionview (= 5.2.0) + activejob (= 5.2.0) mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 1.0, >= 1.0.5) - actionpack (4.2.9) - actionview (= 4.2.9) - activesupport (= 4.2.9) - rack (~> 1.6) - rack-test (~> 0.6.2) - rails-dom-testing (~> 1.0, >= 1.0.5) + rails-dom-testing (~> 2.0) + actionpack (5.2.0) + actionview (= 5.2.0) + activesupport (= 5.2.0) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (4.2.9) - activesupport (= 4.2.9) + actionview (5.2.0) + activesupport (= 5.2.0) builder (~> 3.1) - erubis (~> 2.7.0) - rails-dom-testing (~> 1.0, >= 1.0.5) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (4.2.9) - activesupport (= 4.2.9) - globalid (>= 0.3.0) - activemodel (4.2.9) - activesupport (= 4.2.9) - builder (~> 3.1) - activerecord (4.2.9) - activemodel (= 4.2.9) - activesupport (= 4.2.9) - arel (~> 6.0) - activesupport (4.2.9) - i18n (~> 0.7) + activejob (5.2.0) + activesupport (= 5.2.0) + globalid (>= 0.3.6) + activemodel (5.2.0) + activesupport (= 5.2.0) + activerecord (5.2.0) + activemodel (= 5.2.0) + activesupport (= 5.2.0) + arel (>= 9.0) + activestorage (5.2.0) + actionpack (= 5.2.0) + activerecord (= 5.2.0) + marcel (~> 0.3.1) + activesupport (5.2.0) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (6.0.4) + arel (9.0.0) builder (3.2.3) codeclimate-test-reporter (1.0.8) simplecov (<= 0.13) coderay (1.1.1) concurrent-ruby (1.0.5) + crass (1.0.4) database_cleaner (1.6.1) diff-lcs (1.3) docile (1.1.5) - erubis (2.7.0) + erubi (1.7.1) factory_girl (4.8.0) activesupport (>= 3.0.0) ffi (1.9.18) formatador (0.2.5) - globalid (0.4.0) + globalid (0.4.1) activesupport (>= 4.2.0) guard (2.14.1) formatador (>= 0.2.4) @@ -74,27 +82,31 @@ GEM guard (~> 2.1) guard-compat (~> 1.1) rspec (>= 2.99.0, < 4.0) - i18n (0.8.4) + i18n (1.0.1) + concurrent-ruby (~> 1.0) json (2.1.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - loofah (2.0.3) + loofah (2.2.2) + crass (~> 1.0.2) nokogiri (>= 1.5.9) lumberjack (1.0.12) - mail (2.6.6) - mime-types (>= 1.16, < 4) + mail (2.7.0) + mini_mime (>= 0.1.1) + marcel (0.3.2) + mimemagic (~> 0.3.2) method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.2.0) - minitest (5.10.2) + mimemagic (0.3.2) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.11.3) mysql2 (0.4.7) nenv (0.3.0) - nokogiri (1.8.0) - mini_portile2 (~> 2.2.0) + nio4r (2.3.0) + nokogiri (1.8.2) + mini_portile2 (~> 2.3.0) notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) @@ -102,34 +114,34 @@ GEM coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - rack (1.6.8) - rack-test (0.6.3) - rack (>= 1.0) - rails (4.2.9) - actionmailer (= 4.2.9) - actionpack (= 4.2.9) - actionview (= 4.2.9) - activejob (= 4.2.9) - activemodel (= 4.2.9) - activerecord (= 4.2.9) - activesupport (= 4.2.9) - bundler (>= 1.3.0, < 2.0) - railties (= 4.2.9) - sprockets-rails - rails-deprecated_sanitizer (1.0.3) - activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.8) - activesupport (>= 4.2.0.beta, < 5.0) - nokogiri (~> 1.6) - rails-deprecated_sanitizer (>= 1.0.1) - rails-html-sanitizer (1.0.3) - loofah (~> 2.0) - railties (4.2.9) - actionpack (= 4.2.9) - activesupport (= 4.2.9) + rack (2.0.4) + rack-test (1.0.0) + rack (>= 1.0, < 3) + rails (5.2.0) + actioncable (= 5.2.0) + actionmailer (= 5.2.0) + actionpack (= 5.2.0) + actionview (= 5.2.0) + activejob (= 5.2.0) + activemodel (= 5.2.0) + activerecord (= 5.2.0) + activestorage (= 5.2.0) + activesupport (= 5.2.0) + bundler (>= 1.3.0) + railties (= 5.2.0) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.4) + loofah (~> 2.2, >= 2.2.2) + railties (5.2.0) + actionpack (= 5.2.0) + activesupport (= 5.2.0) + method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.0.0) + rake (12.3.1) rb-fsevent (0.10.2) rb-inotify (0.9.10) ffi (>= 0.5.0, < 2) @@ -168,14 +180,17 @@ GEM sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - thor (0.19.4) + thor (0.20.0) thread_safe (0.3.6) - tzinfo (1.2.3) + tzinfo (1.2.5) thread_safe (~> 0.1) + websocket-driver (0.7.0) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) PLATFORMS ruby From 0bd9d9a94d24adf8eb99fe726ff56b7059e3195a Mon Sep 17 00:00:00 2001 From: Julien Lestavel Date: Wed, 2 May 2018 14:18:46 +0200 Subject: [PATCH 2/3] Replacing the global AR config `belongs_to_required_by_default = false` by individual `optional: true` on every `belongs_to` association We don't want the gem Engine to change the config of its parent Rails app --- app/models/wordpress/attachmentmeta.rb | 2 +- app/models/wordpress/pagemeta.rb | 2 +- app/models/wordpress/postmeta.rb | 2 +- app/models/wordpress/relationship.rb | 4 ++-- app/models/wordpress/usermeta.rb | 2 +- app/models/wordpress/wp_post.rb | 4 ++-- lib/rails_wordpress.rb | 1 - lib/rails_wordpress/railtie.rb | 14 -------------- 8 files changed, 8 insertions(+), 23 deletions(-) delete mode 100644 lib/rails_wordpress/railtie.rb diff --git a/app/models/wordpress/attachmentmeta.rb b/app/models/wordpress/attachmentmeta.rb index 66d824a..9f2c3bd 100644 --- a/app/models/wordpress/attachmentmeta.rb +++ b/app/models/wordpress/attachmentmeta.rb @@ -2,6 +2,6 @@ module Wordpress class Attachmentmeta < WpBase self.table_name = self.prefix_table_name('postmeta') - belongs_to :attachment, foreign_key: "post_id", class_name: "Attachment" + belongs_to :attachment, foreign_key: "post_id", class_name: "Attachment", optional: true end end diff --git a/app/models/wordpress/pagemeta.rb b/app/models/wordpress/pagemeta.rb index da8b108..3f01fff 100644 --- a/app/models/wordpress/pagemeta.rb +++ b/app/models/wordpress/pagemeta.rb @@ -2,6 +2,6 @@ module Wordpress class Pagemeta < WpBase self.table_name = self.prefix_table_name('postmeta') - belongs_to :page, foreign_key: "post_id" + belongs_to :page, foreign_key: "post_id", optional: true end end diff --git a/app/models/wordpress/postmeta.rb b/app/models/wordpress/postmeta.rb index a40b862..42afe7c 100644 --- a/app/models/wordpress/postmeta.rb +++ b/app/models/wordpress/postmeta.rb @@ -2,6 +2,6 @@ module Wordpress class Postmeta < WpBase self.table_name = self.prefix_table_name('postmeta') - belongs_to :post + belongs_to :post, optional: true end end diff --git a/app/models/wordpress/relationship.rb b/app/models/wordpress/relationship.rb index 785c3e3..039438d 100644 --- a/app/models/wordpress/relationship.rb +++ b/app/models/wordpress/relationship.rb @@ -12,8 +12,8 @@ class Relationship < WpBase after_save :increment_term_use_count before_destroy :decrement_term_use_count - belongs_to :post, class_name: "Wordpress::Post", foreign_key: "object_id" - belongs_to :taxonomy, class_name: "Wordpress::Taxonomy", foreign_key: "term_taxonomy_id" + belongs_to :post, class_name: "Wordpress::Post", foreign_key: "object_id", optional: true + belongs_to :taxonomy, class_name: "Wordpress::Taxonomy", foreign_key: "term_taxonomy_id", optional: true def increment_term_use_count self.taxonomy.update_attribute(:count, self.taxonomy.count + 1) if self.taxonomy.present? diff --git a/app/models/wordpress/usermeta.rb b/app/models/wordpress/usermeta.rb index 24d828d..35330b3 100644 --- a/app/models/wordpress/usermeta.rb +++ b/app/models/wordpress/usermeta.rb @@ -3,6 +3,6 @@ class Usermeta < WpBase self.primary_key = "umeta_id" self.table_name = self.prefix_table_name('usermeta') - belongs_to :user + belongs_to :user, optional: true end end diff --git a/app/models/wordpress/wp_post.rb b/app/models/wordpress/wp_post.rb index f197947..8dc9c22 100644 --- a/app/models/wordpress/wp_post.rb +++ b/app/models/wordpress/wp_post.rb @@ -11,14 +11,14 @@ class WpPost < WpBase scope :descending, -> { order(post_modified: :desc, id: :desc) } scope :recent, -> (count = 10) { descending.limit(count) } - belongs_to :parent, class_name: "Post", foreign_key: "post_parent" + belongs_to :parent, class_name: "Post", foreign_key: "post_parent", optional: true has_many :relationships, foreign_key: "object_id" has_many :tags, class_name: "PostTag", through: :relationships, source: :taxonomy, :dependent => :destroy has_many :categories, class_name: "Category", through: :relationships, source: :taxonomy, :dependent => :destroy has_many :metas, class_name: "Postmeta", foreign_key: "post_id" - belongs_to :author, class_name: "User", foreign_key: "post_author" + belongs_to :author, class_name: "User", foreign_key: "post_author", optional: true def self.find_sti_class type_name "wordpress/#{type_name}".camelize.constantize diff --git a/lib/rails_wordpress.rb b/lib/rails_wordpress.rb index 5acf4c9..9f8d748 100644 --- a/lib/rails_wordpress.rb +++ b/lib/rails_wordpress.rb @@ -1,5 +1,4 @@ require "rails_wordpress/engine" -require "rails_wordpress/railtie" if defined?(Rails) module Wordpress end diff --git a/lib/rails_wordpress/railtie.rb b/lib/rails_wordpress/railtie.rb deleted file mode 100644 index aa86f1f..0000000 --- a/lib/rails_wordpress/railtie.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Wordpress - class Railtie < ::Rails::Railtie - initializer :wordpress do |app| - if Rails::VERSION::MAJOR >= 5 - options = app.config.active_record - options.belongs_to_required_by_default = false - - ActiveSupport.on_load(:active_record) do - options.each { |k,v| send("#{k}=", v) } - end - end - end - end -end From 05bf1e1a3919d7a854dfcd0b20a904013bed4093 Mon Sep 17 00:00:00 2001 From: Julien Lestavel Date: Wed, 2 May 2018 15:27:30 +0200 Subject: [PATCH 3/3] Add a test on optional `belongs_to` association : a Post can be saved without parent, it will use the column's default value 0 --- spec/dummy/config/application.rb | 3 +++ spec/models/wordpress/post_spec.rb | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index dddcb59..9dcf920 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -7,6 +7,9 @@ module Dummy class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 5.2 + # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. diff --git a/spec/models/wordpress/post_spec.rb b/spec/models/wordpress/post_spec.rb index 1281cf7..1e07954 100644 --- a/spec/models/wordpress/post_spec.rb +++ b/spec/models/wordpress/post_spec.rb @@ -83,6 +83,15 @@ module Wordpress expect(post.post_categories).to eq "Foo,Bar" end end - + + context "without parent" do + let(:post) { build(:post) } + + it "is valid and can be saved" do + expect(post.post_parent).to eq 0 + expect(post.parent).to be_nil + expect(post.save).to eq true + end + end end end