Skip to content

Commit

Permalink
WIP: Mongoid 7 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jun 19, 2018
1 parent fe8b1f7 commit 3f35679
Show file tree
Hide file tree
Showing 26 changed files with 505 additions and 273 deletions.
28 changes: 12 additions & 16 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-01-16 15:37:33 -0500 using RuboCop version 0.48.1.
# on 2018-06-19 17:36:18 -0400 using RuboCop version 0.48.1.
# 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: 6
# Offense count: 7
# Configuration parameters: Include.
# Include: **/Gemfile, **/gems.rb
Bundler/DuplicatedGem:
Expand All @@ -26,16 +26,17 @@ Lint/HandleExceptions:
Exclude:
- 'spec/unit/trackable_spec.rb'

# Offense count: 2
# Offense count: 3
Lint/ParenthesesAsGroupedExpression:
Exclude:
- 'spec/integration/integration_spec.rb'
- 'spec/integration/nested_embedded_polymorphic_documents_spec.rb'

# Offense count: 21
Metrics/AbcSize:
Max: 45
Max: 52

# Offense count: 114
# Offense count: 112
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 807
Expand All @@ -45,11 +46,11 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 114

# Offense count: 5
# Offense count: 6
Metrics/CyclomaticComplexity:
Max: 10
Max: 13

# Offense count: 457
# Offense count: 478
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand All @@ -63,11 +64,11 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 180
Max: 182

# Offense count: 5
# Offense count: 6
Metrics/PerceivedComplexity:
Max: 12
Max: 15

# Offense count: 12
Style/Documentation:
Expand Down Expand Up @@ -98,11 +99,6 @@ Style/FileName:
- 'Dangerfile'
- 'lib/mongoid-history.rb'

# Offense count: 1
Style/IfInsideElse:
Exclude:
- 'lib/mongoid/history/trackable.rb'

# Offense count: 1
Style/MultilineBlockChain:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
- MONGOID_VERSION=4
- MONGOID_VERSION=5
- MONGOID_VERSION=6
- MONGOID_VERSION=7
- MONGOID_VERSION=HEAD

rvm:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.8.1 (Next)

* [#221](https://github.com/mongoid/mongoid-history/pull/221): Mongoid 7 support - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.8.0 (2018/01/16)
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ source 'https://rubygems.org'

gemspec

case version = ENV['MONGOID_VERSION'] || '~> 6.0.0'
case version = ENV['MONGOID_VERSION'] || '~> 7.0.0'
when 'HEAD'
gem 'mongoid', github: 'mongodb/mongoid'
when /7/
gem 'mongoid', '~> 7.0.0'
when /6/
gem 'mongoid', '~> 6.0.0'
when /5/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This gem also implements multi-user undo, which allows users to undo any history
Install
-------

This gem supports Mongoid 3, 4, 5 on Ruby 1.9.3 or newer and Mongoid 6 on Ruby 2.2.2+. Add it to your `Gemfile` or run `gem install mongoid-history`.
This gem supports Mongoid 3, 4, 5 on Ruby 1.9.3 or newer and Mongoid 6 and 7 on Ruby 2.2.2+. Add it to your `Gemfile` or run `gem install mongoid-history`.

```ruby
gem 'mongoid-history'
Expand Down
26 changes: 20 additions & 6 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ def related_scope
scope = _parent.collection_name.to_s.singularize.to_sym if scope.is_a?(Array)
if Mongoid::Compatibility::Version.mongoid3?
scope = metadata.inverse_class_name.tableize.singularize.to_sym if metadata.present? && scope == metadata.as
else
elsif Mongoid::Compatibility::Version.mongoid6_or_older?
scope = relation_metadata.inverse_class_name.tableize.singularize.to_sym if relation_metadata.present? && scope == relation_metadata.as
elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
scope = _association.inverse_class_name.tableize.singularize.to_sym if _association.present? && scope == _association.as
end
end

Expand All @@ -173,15 +175,18 @@ def traverse_association_chain(node = self)

def association_hash(node = self)
# We prefer to look up associations through the parent record because
# we're assured, through the object creation, it'll exist. Whereas we're not guarenteed
# we're assured, through the object creation, it'll exist. Whereas we're not guaranteed
# the child to parent (embedded_in, belongs_to) relation will be defined
if node._parent
meta = node._parent.relations.values.find do |relation|
if Mongoid::Compatibility::Version.mongoid3?
relation.class_name == node.metadata.class_name.to_s && relation.name == node.metadata.name
else
elsif Mongoid::Compatibility::Version.mongoid6_or_older?
relation.class_name == node.relation_metadata.class_name.to_s &&
relation.name == node.relation_metadata.name
elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
relation.class_name == node._association.class_name.to_s &&
relation.name == node._association.name
end
end
end
Expand Down Expand Up @@ -316,7 +321,11 @@ def relation_class_of(field)
#
# @return [ Boolean ] true if there is an Embedded::One relation for the given embedded field.
def embeds_one?(field)
relation_of(field) == Mongoid::Relations::Embedded::One
relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
Mongoid::Association::Embedded::EmbedsOne::Proxy
else
Mongoid::Relations::Embedded::One
end
end

# Indicates whether there is an Embedded::Many relation for the given embedded field.
Expand All @@ -325,7 +334,11 @@ def embeds_one?(field)
#
# @return [ Boolean ] true if there is an Embedded::Many relation for the given embedded field.
def embeds_many?(field)
relation_of(field) == Mongoid::Relations::Embedded::Many
relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
Mongoid::Association::Embedded::EmbedsMany::Proxy
else
Mongoid::Relations::Embedded::Many
end
end

# Retrieves the database representation of an embedded field name, in case the :store_as option is used.
Expand Down Expand Up @@ -365,7 +378,8 @@ def relation_of(field)
# @return [ Hash < String, String > ] hash of embedded aliases (keys) to database representations (values)
def relation_aliases
@relation_aliases ||= relations.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
h[v[:store_as] || k] = k
store_as = Mongoid::Compatibility::Version.mongoid7_or_newer? ? v.store_as : v[:store_as]
h[store_as || k] = k
h
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/mongoid/history/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ def create_standalone

def create_on_parent
name = association_chain.last['name']

#
# TODO: modifier
# klass = trackable_parent.class.relation_class_of(name)
# klass.history_trackable_options[:modifier_field] if klass.respond_to?(:history_trackable_options)
#

if trackable_parent.class.embeds_one?(name)
trackable_parent._create_relation(name, localize_keys(original))
elsif trackable_parent.class.embeds_many?(name)
Expand Down Expand Up @@ -209,7 +216,9 @@ def traverse_association_chain
elsif doc.class.embeds_many?(name)
doc._get_relation(name).unscoped.where(_id: node['id']).first
else
raise 'This should never happen. Please report bug.'
relation_klass = doc.class.relation_class_of(name) if doc
relation_klass ||= 'nil'
raise "Unexpected relation for field '#{name}': #{relation_klass}. This should never happen. Please report bug."
end
documents << doc
break if chain.empty?
Expand Down
4 changes: 4 additions & 0 deletions spec/integration/embedded_in_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RealState

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
Expand All @@ -31,6 +32,7 @@ class Company

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
Expand All @@ -46,6 +48,7 @@ class Embone

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
Expand All @@ -64,6 +67,7 @@ class Contact

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
Expand Down
Loading

0 comments on commit 3f35679

Please sign in to comment.