Skip to content
6 changes: 6 additions & 0 deletions lib/ransack/adapters/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def self.extended(base)
alias :search :ransack unless base.respond_to? :search
base.class_eval do
class_attribute :_ransackers
class_attribute :_ransacker_aliases
self._ransackers ||= {}
self._ransacker_aliases ||= {}
end
end

Expand All @@ -19,6 +21,10 @@ def ransacker(name, opts = {}, &block)
self._ransackers = _ransackers.merge name.to_s => Ransacker
.new(self, name, opts, &block)
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

def ransacker_alias(alias_name, normal_name)
self._ransacker_aliases[alias_name.to_s] = normal_name.to_s
end

# Ransackable_attributes, by default, returns all column names
# and any defined ransackers as an array of strings.
Expand Down
8 changes: 7 additions & 1 deletion lib/ransack/helpers/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ module ActionView::Helpers::Tags
class Base
private
def value(object)
object.send @method_name if object # use send instead of public_send
search_attr = @method_name.to_s.sub(/_#{Ransack::Predicate.detect_from_string(@method_name.to_s)}$/, Ransack::Constants::EMPTY)

Choose a reason for hiding this comment

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

Line is too long. [133/80]

search_pred = @method_name.to_s.sub(/^#{search_attr}_/, Ransack::Constants::EMPTY)

Choose a reason for hiding this comment

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

Line is too long. [88/80]

if object.klass._ransacker_aliases.has_key?(search_attr)
object.send "#{object.klass._ransacker_aliases[search_attr]}_#{search_pred}" if object

Choose a reason for hiding this comment

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

Line is too long. [94/80]
Use 2 (not 3) spaces for indentation.

else

Choose a reason for hiding this comment

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

Align else with if.

object.send @method_name if object # use send instead of public_send
end

Choose a reason for hiding this comment

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

end at 16, 6 is not aligned with if at 12, 5

end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ def self.sane_adapter?
expect { s.result.first }.to_not raise_error
end
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

describe '#ransack_alias' do
s1 = Person.ransack(cmnt_bd_cont: "some_words")
s2 = Person.ransack(comments_body_cont: "some_words")
expect(s1.result.to_sql).to eq(s2.result.to_sql)
end

describe '#ransackable_attributes' do
context 'when auth_object is nil' do
Expand Down
2 changes: 2 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Person < ActiveRecord::Base
SQL
Arel.sql(query)
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

ransacker_alias :cmnt_bd, :comments_body

def self.ransackable_attributes(auth_object = nil)
if auth_object == :admin
Expand Down