Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Style/MultilineTernaryOperator offenses #754

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,3 @@ Style/ModuleFunction:
Exclude:
- 'lib/paper_trail/serializers/json.rb'
- 'lib/paper_trail/serializers/yaml.rb'

# Offense count: 4
Style/MultilineTernaryOperator:
Exclude:
- 'lib/paper_trail/config.rb'
- 'lib/paper_trail/has_paper_trail.rb'
- 'lib/paper_trail/reifier.rb'
6 changes: 4 additions & 2 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def serialized_attributes=(_)
end

def track_associations
@track_associations.nil? ?
PaperTrail::VersionAssociation.table_exists? :
if @track_associations.nil?
PaperTrail::VersionAssociation.table_exists?
else
@track_associations
end
end
alias_method :track_associations?, :track_associations

Expand Down
13 changes: 9 additions & 4 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ def versions_between(start_time, end_time, _reify_options = {})

# Returns the object (not a Version) as it was most recently.
def previous_version
preceding_version = source_version ?
source_version.previous :
send(self.class.versions_association_name).last
preceding_version.reify if preceding_version
preceding_version.try(:reify)
end

# Returns the object (not a Version) as it became next.
Expand Down Expand Up @@ -306,6 +303,14 @@ def source_version
send self.class.version_association_name
end

def preceding_version
if source_version
source_version.previous
else
send(self.class.versions_association_name).last
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I said I'd prefer to extract private methods, but we can't do that here because we already have a problem with polluting people's AR models with a ton of methods, and I don't want to make that problem worse. See discussion in #719


def record_create
if paper_trail_switched_on?
data = {
Expand Down
9 changes: 3 additions & 6 deletions lib/paper_trail/reifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def reify(version, options)
unversioned_attributes: :nil
)

attrs = version.class.object_col_is_json? ?
version.object :
PaperTrail.serializer.load(version.object)
attrs = version.send(:object_deserialized)

# Normally a polymorphic belongs_to relationship allows us to get the
# object we belong to by calling, in this case, `item`. However this
Expand Down Expand Up @@ -284,9 +282,8 @@ def reify_has_many_through(transaction_id, associations, model, options = {})
# table, this method would return the constant `Fruit`.
def version_reification_class(version, attrs)
inheritance_column_name = version.item_type.constantize.inheritance_column
class_name = attrs[inheritance_column_name].blank? ?
version.item_type :
attrs[inheritance_column_name]
class_name = attrs[inheritance_column_name] || version.item_type

class_name.constantize
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do people ever set inheritance_column to "" ? That's the only case in which this would break...

Happy to revert this change if needed. Looking through git, it's been blank? since the code was first written in 2009 (so not in response to a bug report from someone using inheritance_column = "").

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do people ever set inheritance_column to "" ?

Seems unlikely, but we should not be changing behavior in a linter commit. If we are going to make that change, please make a separate PR. Thanks.

end

Expand Down
9 changes: 9 additions & 0 deletions lib/paper_trail/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ def load_changeset
{}
end

# @api private
def object_deserialized
if self.class.object_col_is_json?
object
else
PaperTrail.serializer.load(object)
end
end

# @api private
def object_changes_deserialized
if self.class.object_changes_col_is_json?
Expand Down