Skip to content

Commit

Permalink
Merge pull request #688 from kbrock/cops
Browse files Browse the repository at this point in the history
rubocop fixes
  • Loading branch information
kbrock authored Oct 23, 2024
2 parents 78d63a8 + 98d831b commit 6cc9398
Show file tree
Hide file tree
Showing 43 changed files with 392 additions and 225 deletions.
77 changes: 77 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 3.3

Gemspec/DevelopmentDependencies:
Enabled: false
Layout/HashAlignment:
Enabled: false
Layout/LineLength:
Enabled: false
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
Layout/SpaceAroundOperators:
EnforcedStyleForExponentOperator: space

Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/ModuleLength:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false

Naming/PredicateName:
Enabled: false
Naming/VariableNumber:
Enabled: false

Style/Documentation:
Enabled: false
Style/ExpandPathArguments:
Enabled: false
Style/GuardClause:
Enabled: false
Style/HashSyntax:
Enabled: false
Style/IfUnlessModifier:
Enabled: false
Style/Lambda:
Enabled: false
Style/MultilineIfModifier:
Enabled: false
Style/NegatedIf:
Enabled: false
Style/Next:
Enabled: false
Style/NumericPredicate:
Enabled: false
Style/ParallelAssignment:
Enabled: false
Style/PercentLiteralDelimiters:
Enabled: false
Style/StringLiterals:
Enabled: false
Style/StringLiteralsInInterpolation:
Enabled: false
Style/SymbolArray:
Enabled: false

# specs

Layout/ExtraSpacing:
Exclude: [ "test/**/*" ]
Style/TrailingUnderscoreVariable:
Exclude: [ "test/**/*" ]
Layout/SpaceBeforeFirstArg:
Exclude: [ "test/**/*" ]
Layout/SpaceAroundOperators:
Exclude: [ "test/**/*" ]
2 changes: 2 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# on a mac using:
# bundle config --global build.mysql2 "--with-mysql-dir=$(brew --prefix mysql)"

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rake/testtask'
Expand Down
13 changes: 8 additions & 5 deletions ancestry.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'ancestry/version'

Gem::Specification.new do |s|
Expand All @@ -22,6 +24,7 @@ EOF
"changelog_uri" => "https://github.com/stefankroes/ancestry/blob/master/CHANGELOG.md",
"source_code_uri" => "https://github.com/stefankroes/ancestry/",
"bug_tracker_uri" => "https://github.com/stefankroes/ancestry/issues",
"rubygems_mfa_required" => "true"
}
s.version = Ancestry::VERSION

Expand All @@ -30,19 +33,19 @@ EOF
s.homepage = 'https://github.com/stefankroes/ancestry'
s.license = 'MIT'

s.files = Dir[
s.files = Dir[
"{lib}/**/*",
'CHANGELOG.md',
'MIT-LICENSE',
'README.md'
]
s.require_paths = ["lib"]
s.required_ruby_version = '>= 2.5'

s.required_ruby_version = '>= 2.5'
s.add_runtime_dependency 'activerecord', '>= 5.2.6'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'minitest'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'yard'
end
1 change: 1 addition & 0 deletions gemfiles/gemfile_52.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/gemfile_60.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/gemfile_61.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/gemfile_70.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/gemfile_71.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/gemfile_72.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file was generated by Appraisal
# frozen_string_literal: true

source "https://rubygems.org"

Expand Down
4 changes: 3 additions & 1 deletion lib/ancestry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'ancestry/version'
require_relative 'ancestry/class_methods'
require_relative 'ancestry/instance_methods'
Expand All @@ -8,7 +10,7 @@
require_relative 'ancestry/materialized_path_pg'

I18n.load_path += Dir[File.join(File.expand_path(File.dirname(__FILE__)),
'ancestry', 'locales', '*.{rb,yml}').to_s]
'ancestry', 'locales', '*.{rb,yml}').to_s]

module Ancestry
@@default_update_strategy = :ruby
Expand Down
95 changes: 46 additions & 49 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Ancestry
module ClassMethods
# Fetch tree node if necessary
def to_node object
def to_node(object)
if object.is_a?(ancestry_base_class)
object
else
Expand All @@ -10,13 +12,13 @@ def to_node object
end

# Scope on relative depth options
def scope_depth depth_options, depth
def scope_depth(depth_options, depth)
depth_options.inject(ancestry_base_class) do |scope, option|
scope_name, relative_depth = option
if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
scope.send scope_name, depth + relative_depth
else
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_depth_option", scope_name: scope_name))
raise Ancestry::AncestryException, I18n.t("ancestry.unknown_depth_option", scope_name: scope_name)
end
end
end
Expand All @@ -27,11 +29,11 @@ def scope_depth depth_options, depth
# To order your hashes pass the order to the arrange method instead of to the scope

# Get all nodes and sort them into an empty hash
def arrange options = {}
def arrange(options = {})
if (order = options.delete(:order))
arrange_nodes ancestry_base_class.order(order).where(options)
arrange_nodes(ancestry_base_class.order(order).where(options))
else
arrange_nodes ancestry_base_class.where(options)
arrange_nodes(ancestry_base_class.where(options))
end
end

Expand Down Expand Up @@ -63,10 +65,10 @@ def flatten_arranged_nodes(arranged, nodes = [])
nodes
end

# Arrangement to nested array for serialization
# You can also supply your own serialization logic using blocks
# also allows you to pass the order just as you can pass it to the arrange method
def arrange_serializable options={}, nodes=nil, &block
# Arrangement to nested array for serialization
# You can also supply your own serialization logic using blocks
# also allows you to pass the order just as you can pass it to the arrange method
def arrange_serializable(options = {}, nodes = nil, &block)
nodes = arrange(options) if nodes.nil?
nodes.map do |parent, children|
if block_given?
Expand All @@ -78,13 +80,13 @@ def arrange_serializable options={}, nodes=nil, &block
end

def tree_view(column, data = nil)
data = arrange unless data
data ||= arrange
data.each do |parent, children|
if parent.depth == 0
puts parent[column]
else
num = parent.depth - 1
indent = " "*num
indent = " " * num
puts " #{"|" if parent.depth > 1}#{indent}|_ #{parent[column]}"
end
tree_view(column, children) if children
Expand All @@ -93,7 +95,7 @@ def tree_view(column, data = nil)

# Pseudo-preordered array of nodes. Children will always follow parents,
# This is deterministic unless the parents are missing *and* a sort block is specified
def sort_by_ancestry(nodes, &block)
def sort_by_ancestry(nodes)
arranged = nodes if nodes.is_a?(Hash)

unless arranged
Expand All @@ -113,48 +115,43 @@ def sort_by_ancestry(nodes, &block)
# compromised tree integrity is unlikely without explicitly setting cyclic parents or invalid ancestry and circumventing validation
# just in case, raise an AncestryIntegrityException if issues are detected
# specify :report => :list to return an array of exceptions or :report => :echo to echo any error messages
def check_ancestry_integrity! options = {}
def check_ancestry_integrity!(options = {})
parents = {}
exceptions = [] if options[:report] == :list

unscoped_where do |scope|
# For each node ...
scope.find_each do |node|
begin
# ... check validity of ancestry column
if !node.sane_ancestor_ids?
raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.invalid_ancestry_column",
:node_id => node.id,
:ancestry_column => "#{node.read_attribute node.class.ancestry_column}"
))
end
# ... check that all ancestors exist
node.ancestor_ids.each do |ancestor_id|
unless exists? ancestor_id
raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.reference_nonexistent_node",
:node_id => node.id,
:ancestor_id => ancestor_id
))
end
end
# ... check that all node parents are consistent with values observed earlier
node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id|
parents[node_id] = parent_id unless parents.has_key? node_id
unless parents[node_id] == parent_id
raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.conflicting_parent_id",
:node_id => node_id,
:parent_id => parent_id || 'nil',
:expected => parents[node_id] || 'nil'
))
end
# ... check validity of ancestry column
if !node.sane_ancestor_ids?
raise Ancestry::AncestryIntegrityException, I18n.t("ancestry.invalid_ancestry_column",
:node_id => node.id,
:ancestry_column => node.read_attribute(node.class.ancestry_column))
end
# ... check that all ancestors exist
node.ancestor_ids.each do |ancestor_id|
unless exists?(ancestor_id)
raise Ancestry::AncestryIntegrityException, I18n.t("ancestry.reference_nonexistent_node",
:node_id => node.id,
:ancestor_id => ancestor_id)
end
rescue Ancestry::AncestryIntegrityException => integrity_exception
case options[:report]
when :list then exceptions << integrity_exception
when :echo then puts integrity_exception
else raise integrity_exception
end
# ... check that all node parents are consistent with values observed earlier
node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id|
parents[node_id] = parent_id unless parents.key?(node_id)
unless parents[node_id] == parent_id
raise Ancestry::AncestryIntegrityException, I18n.t("ancestry.conflicting_parent_id",
:node_id => node_id,
:parent_id => parent_id || 'nil',
:expected => parents[node_id] || 'nil')
end
end
rescue Ancestry::AncestryIntegrityException => e
case options[:report]
when :list then exceptions << e
when :echo then puts e
else raise e
end
end
end
exceptions if options[:report] == :list
Expand Down Expand Up @@ -201,7 +198,7 @@ def restore_ancestry_integrity!
end

# Build ancestry from parent ids for migration purposes
def build_ancestry_from_parent_ids! column=:parent_id, parent_id = nil, ancestor_ids = []
def build_ancestry_from_parent_ids!(column = :parent_id, parent_id = nil, ancestor_ids = [])
unscoped_where do |scope|
scope.where(column => parent_id).find_each do |node|
node.without_ancestry_callbacks do
Expand All @@ -214,7 +211,7 @@ def build_ancestry_from_parent_ids! column=:parent_id, parent_id = nil, ancestor

# Rebuild depth cache if it got corrupted or if depth caching was just turned on
def rebuild_depth_cache!
raise Ancestry::AncestryException.new(I18n.t("ancestry.cannot_rebuild_depth_cache")) unless respond_to? :depth_cache_column
raise(Ancestry::AncestryException, I18n.t("ancestry.cannot_rebuild_depth_cache")) unless respond_to?(:depth_cache_column)

ancestry_base_class.transaction do
unscoped_where do |scope|
Expand All @@ -232,7 +229,7 @@ def rebuild_depth_cache_sql!
end

def rebuild_counter_cache!
if %w(mysql mysql2).include?(connection.adapter_name.downcase)
if %w(mysql mysql2).include?(connection.adapter_name.downcase)
connection.execute %{
UPDATE #{table_name} AS dest
LEFT JOIN (
Expand Down
4 changes: 3 additions & 1 deletion lib/ancestry/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Ancestry
class AncestryException < RuntimeError
end

class AncestryIntegrityException < AncestryException
end
end
end
Loading

0 comments on commit 6cc9398

Please sign in to comment.