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

Remove column name validations #16

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project tries to adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.0] - 2024-08-28

### Changed

* Raise an error if `nil` is passed for the `:field` option. (#16)
* Calling `rank!` with an invalid column name, by either specifying the `:field` or `:group_by` option, will not issue a warning anymore. This also means the gem does not require an active database connection when loading classes. (#16)

## [0.2.0] - 2024-08-16

### Major Changes
Expand Down
33 changes: 9 additions & 24 deletions lib/lexorank/ranking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
class Lexorank::Ranking
include Lexorank

attr_reader :record_class, :original_field, :field, :original_group_by, :group_by, :advisory_lock_config
attr_reader :record_class, :field, :group_by, :advisory_lock_config

def initialize(record_class:, field:, group_by:, advisory_lock:)
@record_class = record_class
@original_field = field
@field = process_column_name(field)
@original_group_by = group_by
@field = field
@group_by = process_group_by_column_name(group_by)
@advisory_lock_config = { enabled: record_class.respond_to?(:with_advisory_lock) }.merge(advisory_lock)
end
Expand All @@ -23,13 +21,11 @@ def validate!
)
end

unless @field
# TODO: Make this raise an error. Supplying an invalid column should raise.
warn "The supplied ranking column \"#{@original_field}\" is not a column of the model!"
end

if original_group_by && !group_by
warn "The supplied grouping by \"#{original_group_by}\" is neither a column nor an association of the model!"
unless field
raise(
Lexorank::InvalidConfigError,
'The supplied ":field" option cannot be "nil"!'
)
end
end

Expand Down Expand Up @@ -110,23 +106,12 @@ def advisory_locks_enabled?

private

def process_column_name(name)
return unless name

# This requires an active connection... do we want this?
if record_class.columns.map(&:name).include?(name.to_s)
name
end
end

def process_group_by_column_name(name)
processed_name = process_column_name(name)

# This requires rank! to be after the specific association
if name && !processed_name && (association = record_class.reflect_on_association(name))
if name && (association = record_class.reflect_on_association(name))
association.foreign_key.to_sym
else
processed_name
name
end
end
end
11 changes: 0 additions & 11 deletions test/group_by_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ class Paragraph2 < Base
assert_equal :page_id, Paragraph2.lexorank_ranking.group_by
end

should 'warn on invalid ranking field' do
_, err = capture_io do
class Paragraph3 < Base
self.table_name = 'paragraphs'
rank!(group_by: :foo)
end
end
assert_equal "The supplied grouping by \"foo\" is neither a column nor an association of the model!\n", err
assert_nil Paragraph3.lexorank_ranking.group_by
end

describe 'moving to a different group' do
should 'insert into middle' do
page_1, page_2 = create_sample_pages(count: 2)
Expand Down
15 changes: 8 additions & 7 deletions test/ranking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ class Page1 < Base
assert_not page.rank
end

should 'report warning on invalid field' do
_, err = capture_io do
class Page2 < Base
self.table_name = 'pages'
rank!(field: :foo)
should 'report warning if field is nil' do
error =
assert_raises Lexorank::InvalidConfigError do
class Page2 < Base
self.table_name = 'pages'
rank!(field: nil)
end
end
end
assert_equal "The supplied ranking column \"foo\" is not a column of the model!\n", err
assert_equal 'The supplied ":field" option cannot be "nil"!', error.message
assert_not Page2.respond_to?(:ranked)
assert_not Page2.method_defined?(:move_to)
end
Expand Down
Loading