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

string-functions: fix a subtle bug on [] calls of Table #125

Merged
merged 2 commits into from
Jan 4, 2025
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [unreleased]

- Fix a subtle bug on table access as in `table[:col]` in certain situations.

## Release v2.3.1/v1.5.1 (20-12-2024)

- Fallback to Ruby's Warning module if ActiveSupport doesn't exist. Relevant for old Rails versions.
Expand Down
25 changes: 19 additions & 6 deletions lib/arel_extensions/string_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,27 @@ def substring start, len = nil
ArelExtensions::Nodes::Substring.new [self, start, len]
end

def [](start, ind = nil)
start += 1 if start.is_a?(Integer)
if start.is_a?(Range)
# Return a [ArelExtensions::Nodes::Substring] if `start` is a [Range] or an
# [Integer].
#
# Return the result to `self.send(start)` if it's a [String|Symbol]. The
# assumption is that you're trying to reach an [Arel::Table]'s
# [Arel::Attribute].
#
# @note `ind` should be an [Integer|NilClass] if `start` is an [Integer].
# It's ignored in all other cases.
def [](start, end_ = nil)
if start.is_a?(String) || start.is_a?(Symbol)
self.send(start)
elsif start.is_a?(Range)
ArelExtensions::Nodes::Substring.new [self, start.begin + 1, start.end - start.begin + 1]
elsif start.is_a?(Integer) && !ind
ArelExtensions::Nodes::Substring.new [self, start, 1]
elsif start.is_a?(Integer) && !end_
ArelExtensions::Nodes::Substring.new [self, start + 1, 1]
elsif start.is_a?(Integer)
start += 1
ArelExtensions::Nodes::Substring.new [self, start, end_ - start + 1]
else
ArelExtensions::Nodes::Substring.new [self, start, ind - start + 1]
raise ArgumentError, 'unrecognized argument types; can accept integers, ranges, or strings.'
end
end

Expand Down
Loading