diff --git a/lib/ransack/adapters/active_record/3.0/compat.rb b/lib/ransack/adapters/active_record/3.0/compat.rb index 12ed3dd..3716842 100644 --- a/lib/ransack/adapters/active_record/3.0/compat.rb +++ b/lib/ransack/adapters/active_record/3.0/compat.rb @@ -135,16 +135,16 @@ module Arel "#{ o.name }(#{ - o.distinct ? DISTINCT : EMPTY_STRING + o.distinct ? Ransack::Constants::DISTINCT : Ransack::Constants::EMPTY }#{ - o.expressions.map { |x| visit x }.join(COMMA_SPACE) + o.expressions.map { |x| visit x }.join(Ransack::Constants::COMMA_SPACE) })#{ - o.alias ? " AS #{visit o.alias}" : EMPTY_STRING + o.alias ? " AS #{visit o.alias}" : Ransack::Constants::EMPTY }" end def visit_Arel_Nodes_And o - o.children.map { |x| visit x }.join ' AND ' + o.children.map { |x| visit x }.join(Ransack::Constants::SPACED_AND) end def visit_Arel_Nodes_Not o @@ -161,7 +161,7 @@ module Arel quote(value, attr && column_for(attr)) end } - .join(COMMA_SPACE) + .join(Ransack::Constants::COMMA_SPACE) })" end end diff --git a/lib/ransack/adapters/active_record/3.0/context.rb b/lib/ransack/adapters/active_record/3.0/context.rb index 7881204..1bdde28 100644 --- a/lib/ransack/adapters/active_record/3.0/context.rb +++ b/lib/ransack/adapters/active_record/3.0/context.rb @@ -29,9 +29,14 @@ module Ransack relation = relation.except(:order) .reorder(viz.accept(search.sorts)) end - opts[:distinct] ? - relation.select(DISTINCT + @klass.quoted_table_name + '.*'.freeze) : - relation + if opts[:distinct] + relation.select( + Ransack::Constants::DISTINCT + @klass.quoted_table_name + + '.*'.freeze + ) + else + relation + end end def attribute_method?(str, klass = @klass) @@ -45,11 +50,12 @@ module Ransack while !found_assoc && remainder.unshift(segments.pop) && segments.size > 0 do assoc, poly_class = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, klass) exists = attribute_method?( - remainder.join(UNDERSCORE), poly_class || found_assoc.klass + remainder.join(Ransack::Constants::UNDERSCORE), + poly_class || found_assoc.klass ) end end @@ -98,14 +104,14 @@ module Ransack while remainder.unshift(segments.pop) && segments.size > 0 && !found_assoc do assoc, klass = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, parent) join = build_or_find_association( found_assoc.name, parent, klass ) parent, attr_name = get_parent_and_attribute_name( - remainder.join(UNDERSCORE), join + remainder.join(Ransack::Constants::UNDERSCORE), join ) end end @@ -131,24 +137,31 @@ module Ransack buckets = relation.joins_values.group_by do |join| case join when String - STRING_JOIN + Ransack::Constants::STRING_JOIN when Hash, Symbol, Array - ASSOCIATION_JOIN + Ransack::Constants::ASSOCIATION_JOIN when ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation - STASHED_JOIN + Ransack::Constants::STASHED_JOIN when Arel::Nodes::Join - JOIN_NODE + Ransack::Constants::JOIN_NODE else raise 'unknown class: %s' % join.class.name end end - association_joins = buckets[ASSOCIATION_JOIN] || [] - stashed_association_joins = buckets[STASHED_JOIN] || [] - join_nodes = buckets[JOIN_NODE] || [] - string_joins = (buckets[STRING_JOIN] || []) - .map { |x| x.strip } - .uniq + association_joins = + buckets[Ransack::Constants::ASSOCIATION_JOIN] || [] + + stashed_association_joins = + buckets[Ransack::Constants::STASHED_JOIN] || [] + + join_nodes = + buckets[Ransack::Constants::JOIN_NODE] || [] + + string_joins = + (buckets[Ransack::Constants::STRING_JOIN] || []) + .map { |x| x.strip } + .uniq join_list = relation.send :custom_join_sql, (string_joins + join_nodes) diff --git a/lib/ransack/adapters/active_record/3.1/context.rb b/lib/ransack/adapters/active_record/3.1/context.rb index d8e55f2..3136a5a 100644 --- a/lib/ransack/adapters/active_record/3.1/context.rb +++ b/lib/ransack/adapters/active_record/3.1/context.rb @@ -28,9 +28,14 @@ module Ransack relation = relation.except(:order) .reorder(viz.accept(search.sorts)) end - opts[:distinct] ? - relation.select(DISTINCT + @klass.quoted_table_name + '.*'.freeze) : - relation + if opts[:distinct] + relation.select( + Ransack::Constants::DISTINCT + @klass.quoted_table_name + + '.*'.freeze + ) + else + relation + end end def attribute_method?(str, klass = @klass) @@ -44,11 +49,12 @@ module Ransack while !found_assoc && remainder.unshift(segments.pop) && segments.size > 0 do assoc, poly_class = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, klass) exists = attribute_method?( - remainder.join(UNDERSCORE), poly_class || found_assoc.klass + remainder.join(Ransack::Constants::UNDERSCORE), + poly_class || found_assoc.klass ) end end @@ -110,14 +116,14 @@ module Ransack while remainder.unshift(segments.pop) && segments.size > 0 && !found_assoc do assoc, klass = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, parent) join = build_or_find_association( found_assoc.name, parent, klass ) parent, attr_name = get_parent_and_attribute_name( - remainder.join(UNDERSCORE), join + remainder.join(Ransack::Constants::UNDERSCORE), join ) end end @@ -144,24 +150,31 @@ module Ransack buckets = relation.joins_values.group_by do |join| case join when String - STRING_JOIN + Ransack::Constants::STRING_JOIN when Hash, Symbol, Array - ASSOCIATION_JOIN + Ransack::Constants::ASSOCIATION_JOIN when ::ActiveRecord::Associations::JoinDependency::JoinAssociation - STASHED_JOIN + Ransack::Constants::STASHED_JOIN when Arel::Nodes::Join - JOIN_NODE + Ransack::Constants::JOIN_NODE else raise 'unknown class: %s' % join.class.name end end - association_joins = buckets[ASSOCIATION_JOIN] || [] - stashed_association_joins = buckets[STASHED_JOIN] || [] - join_nodes = buckets[JOIN_NODE] || [] - string_joins = (buckets[STRING_JOIN] || []) - .map { |x| x.strip } - .uniq + association_joins = + buckets[Ransack::Constants::ASSOCIATION_JOIN] || [] + + stashed_association_joins = + buckets[Ransack::Constants::STASHED_JOIN] || [] + + join_nodes = + buckets[Ransack::Constants::JOIN_NODE] || [] + + string_joins = + (buckets[Ransack::Constants::STRING_JOIN] || []) + .map { |x| x.strip } + .uniq join_list = relation.send :custom_join_ast, relation.table.from(relation.table), string_joins diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 21b2dac..954d3d7 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -34,8 +34,7 @@ module Ransack viz = Visitor.new relation = @object.where(viz.accept(search.base)) if search.sorts.any? - relation = relation.except(:order) - .reorder(viz.accept(search.sorts)) + relation = relation.except(:order).reorder(viz.accept(search.sorts)) end opts[:distinct] ? relation.distinct : relation end @@ -50,12 +49,13 @@ module Ransack while !found_assoc && remainder.unshift( segments.pop) && segments.size > 0 do assoc, poly_class = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, klass) - exists = attribute_method?(remainder.join(UNDERSCORE), + exists = attribute_method?( + remainder.join(Ransack::Constants::UNDERSCORE), poly_class || found_assoc.klass - ) + ) end end end @@ -139,14 +139,14 @@ module Ransack while remainder.unshift( segments.pop) && segments.size > 0 && !found_assoc do assoc, klass = unpolymorphize_association( - segments.join(UNDERSCORE) + segments.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, parent) join = build_or_find_association( found_assoc.name, parent, klass ) parent, attr_name = get_parent_and_attribute_name( - remainder.join(UNDERSCORE), join + remainder.join(Ransack::Constants::UNDERSCORE), join ) end end @@ -187,15 +187,19 @@ module Ransack end end - association_joins = buckets[ASSOCIATION_JOIN] || [] + association_joins = + buckets[Ransack::Constants::ASSOCIATION_JOIN] || [] - stashed_association_joins = buckets[STASHED_JOIN] || [] + stashed_association_joins = + buckets[Ransack::Constants::STASHED_JOIN] || [] - join_nodes = buckets[JOIN_NODE] || [] + join_nodes = + buckets[Ransack::Constants::JOIN_NODE] || [] - string_joins = (buckets[STRING_JOIN] || []) - .map { |x| x.strip } - .uniq + string_joins = + (buckets[Ransack::Constants::STRING_JOIN] || []) + .map { |x| x.strip } + .uniq join_list = relation.send :custom_join_ast, relation.table.from(relation.table), string_joins diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index b93474d..e50f78e 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -1,35 +1,6 @@ require 'ransack/constants' require 'ransack/predicate' -ASC = 'asc'.freeze -DESC = 'desc'.freeze -ASC_ARROW = '▲'.freeze -DESC_ARROW = '▼'.freeze -OR = 'or'.freeze -AND = 'and'.freeze -SORT = 'sort'.freeze -SORT_LINK = 'sort_link'.freeze -SEARCH = 'search'.freeze -DEFAULT_SEARCH_KEY = 'q'.freeze -ATTRIBUTE = 'attribute'.freeze -DISTINCT = 'DISTINCT '.freeze -COMBINATOR = 'combinator'.freeze -SPACE = ' '.freeze -COMMA_SPACE = ', '.freeze -UNDERSCORE = '_'.freeze -NON_BREAKING_SPACE = ' '.freeze -EMPTY_STRING = ''.freeze - -ASC_DESC = %w(asc desc).freeze -AND_OR = %w(and or).freeze -IN_NOT_IN = %w(in not_in).freeze -SUFFIXES = %w(_any _all).freeze - -STRING_JOIN = 'string_join'.freeze -ASSOCIATION_JOIN = 'association_join'.freeze -STASHED_JOIN = 'stashed_join'.freeze -JOIN_NODE = 'join_node'.freeze - module Ransack module Configuration @@ -53,7 +24,7 @@ module Ransack self.predicates[name] = Predicate.new(opts) - SUFFIXES.each do |suffix| + Ransack::Constants::SUFFIXES.each do |suffix| compound_name = name + suffix self.predicates[compound_name] = Predicate.new( opts.merge( diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 69edd61..ab369b6 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -1,17 +1,45 @@ module Ransack module Constants + ASC = 'asc'.freeze + DESC = 'desc'.freeze + ASC_ARROW = '▲'.freeze + DESC_ARROW = '▼'.freeze + OR = 'or'.freeze + AND = 'and'.freeze + SPACED_AND = ' AND '.freeze + SORT = 'sort'.freeze + SORT_LINK = 'sort_link'.freeze + SEARCH = 'search'.freeze + DEFAULT_SEARCH_KEY = 'q'.freeze + ATTRIBUTE = 'attribute'.freeze + DISTINCT = 'DISTINCT '.freeze + COMBINATOR = 'combinator'.freeze + SPACE = ' '.freeze + COMMA_SPACE = ', '.freeze + UNDERSCORE = '_'.freeze + NON_BREAKING_SPACE = ' '.freeze + EMPTY = ''.freeze + STRING_JOIN = 'string_join'.freeze + ASSOCIATION_JOIN = 'association_join'.freeze + STASHED_JOIN = 'stashed_join'.freeze + JOIN_NODE = 'join_node'.freeze + TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set BOOLEAN_VALUES = TRUE_VALUES + FALSE_VALUES - AREL_PREDICATES = %w( + ASC_DESC = %w(asc desc).freeze + AND_OR = %w(and or).freeze + IN_NOT_IN = %w(in not_in).freeze + SUFFIXES = %w(_any _all).freeze + AREL_PREDICATES = %w( eq not_eq matches does_not_match lt lteq gt gteq in not_in ).freeze - EQ = 'eq'.freeze - NOT_EQ = 'not_eq'.freeze - EQ_ANY = 'eq_any'.freeze - NOT_EQ_ALL = 'not_eq_all'.freeze + EQ = 'eq'.freeze + NOT_EQ = 'not_eq'.freeze + EQ_ANY = 'eq_any'.freeze + NOT_EQ_ALL = 'not_eq_all'.freeze DERIVED_PREDICATES = [ ['cont'.freeze, { @@ -91,7 +119,7 @@ module Ransack :compounds => false, :type => :boolean, :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, EMPTY_STRING] } + :formatter => proc { |v| [nil, Ransack::Constants::EMPTY] } } ], ['blank'.freeze, { @@ -99,7 +127,7 @@ module Ransack :compounds => false, :type => :boolean, :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, EMPTY_STRING] } + :formatter => proc { |v| [nil, Ransack::Constants::EMPTY] } } ], ['null'.freeze, { diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 8d1e036..0b9317d 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -95,7 +95,7 @@ module Ransack end def traverse(str, base = @base) - str ||= EMPTY_STRING + str ||= Ransack::Constants::EMPTY if (segments = str.split(/_/)).size > 0 remainder = [] @@ -103,11 +103,13 @@ module Ransack while !found_assoc && segments.size > 0 do # Strip the _of_Model_type text from the association name, but hold # onto it in klass, for use as the next base - assoc, klass = unpolymorphize_association(segments.join(UNDERSCORE)) + assoc, klass = unpolymorphize_association(segments + .join(Ransack::Constants::UNDERSCORE)) if found_assoc = get_association(assoc, base) base = traverse( - remainder.join(UNDERSCORE), klass || found_assoc.klass - ) + remainder.join( + Ransack::Constants::UNDERSCORE), klass || found_assoc.klass + ) end remainder.unshift segments.pop @@ -121,15 +123,16 @@ module Ransack def association_path(str, base = @base) base = klassify(base) - str ||= EMPTY_STRING + str ||= Ransack::Constants::EMPTY path = [] segments = str.split(/_/) association_parts = [] if (segments = str.split(/_/)).size > 0 - while segments.size > 0 && !base.columns_hash[segments.join(UNDERSCORE)] && + while segments.size > 0 && + !base.columns_hash[segments.join(Ransack::Constants::UNDERSCORE)] && association_parts << segments.shift do assoc, klass = unpolymorphize_association( - association_parts.join(UNDERSCORE) + association_parts.join(Ransack::Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, base) path += association_parts @@ -139,7 +142,7 @@ module Ransack end end - path.join(UNDERSCORE) + path.join(Ransack::Constants::UNDERSCORE) end def unpolymorphize_association(str) @@ -163,15 +166,15 @@ module Ransack klass.ransackable_scopes(auth_object).any? { |s| s.to_s == str } end - def searchable_attributes(str = EMPTY_STRING) + def searchable_attributes(str = Ransack::Constants::EMPTY) traverse(str).ransackable_attributes(auth_object) end - def sortable_attributes(str = EMPTY_STRING) + def sortable_attributes(str = Ransack::Constants::EMPTY) traverse(str).ransortable_attributes(auth_object) end - def searchable_associations(str = EMPTY_STRING) + def searchable_associations(str = Ransack::Constants::EMPTY) traverse(str).ransackable_associations(auth_object) end end diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 7d24bef..def9f00 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -3,7 +3,8 @@ require 'action_view' RANSACK_FORM_BUILDER = 'RANSACK_FORM_BUILDER'.freeze require 'simple_form' if - (ENV[RANSACK_FORM_BUILDER] || EMPTY_STRING).match('SimpleForm'.freeze) + (ENV[RANSACK_FORM_BUILDER] || Ransack::Constants::EMPTY) + .match('SimpleForm'.freeze) module Ransack module Helpers @@ -29,12 +30,13 @@ module Ransack def attribute_select(options = nil, html_options = nil, action = nil) options = options || {} html_options = html_options || {} - action = action || SEARCH + action = action || Ransack::Constants::SEARCH default = options.delete(:default) raise ArgumentError, formbuilder_error_message( "#{action}_select") unless object.respond_to?(:context) options[:include_blank] = true unless options.has_key?(:include_blank) - bases = [EMPTY_STRING] + association_array(options[:associations]) + bases = [Ransack::Constants::EMPTY] + + association_array(options[:associations]) if bases.size > 1 collection = attribute_collection_for_bases(action, bases) object.name ||= default if can_use_default?( @@ -57,7 +59,7 @@ module Ransack end def sort_select(options = {}, html_options = {}) - attribute_select(options, html_options, SORT) + + attribute_select(options, html_options, Ransack::Constants::SORT) + sort_direction_select(options, html_options) end @@ -119,7 +121,7 @@ module Ransack else only = Array.wrap(only).map(&:to_s) keys = keys.select { - |k| only.include? k.sub(/_(any|all)$/, EMPTY_STRING) + |k| only.include? k.sub(/_(any|all)$/, Ransack::Constants::EMPTY) } end end @@ -161,14 +163,23 @@ module Ransack end def sort_array - [[ASC, object.translate(ASC)], [DESC, object.translate(DESC)]] + [ + [Ransack::Constants::ASC, object.translate(Ransack::Constants::ASC)], + [Ransack::Constants::DESC, object.translate(Ransack::Constants::DESC)] + ] end def combinator_choices if Nodes::Condition === object - [[OR, Translate.word(:any)], [AND, Translate.word(:all)]] + [ + [Ransack::Constants::OR, Translate.word(:any)], + [Ransack::Constants::AND, Translate.word(:all)] + ] else - [[AND, Translate.word(:all)], [OR, Translate.word(:any)]] + [ + [Ransack::Constants::AND, Translate.word(:all)], + [Ransack::Constants::OR, Translate.word(:any)] + ] end end @@ -176,7 +187,7 @@ module Ransack ([prefix] + association_object(obj)) .compact .flatten - .map { |v| [prefix, v].compact.join(UNDERSCORE) } + .map { |v| [prefix, v].compact.join(Ransack::Constants::UNDERSCORE) } end def association_object(obj) @@ -196,7 +207,7 @@ module Ransack when Array, Hash association_array(value, key.to_s) else - [key.to_s, [key, value].join(UNDERSCORE)] + [key.to_s, [key, value].join(Ransack::Constants::UNDERSCORE)] end end end @@ -231,11 +242,13 @@ module Ransack end def attr_from_base_and_column(base, column) - [base, column].reject { |v| v.blank? }.join(UNDERSCORE) + [base, column].reject { |v| v.blank? } + .join(Ransack::Constants::UNDERSCORE) end def formbuilder_error_message(action) - "#{action.sub(SEARCH, ATTRIBUTE) + "#{ + action.sub(Ransack::Constants::SEARCH, Ransack::Constants::ATTRIBUTE) } must be called inside a search FormBuilder!" end diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index b949b2b..421c430 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -28,7 +28,7 @@ module Ransack "#{search.klass.to_s.underscore}_search", :method => :get } - options[:as] ||= DEFAULT_SEARCH_KEY + options[:as] ||= Ransack::Constants::DEFAULT_SEARCH_KEY options[:html].reverse_merge!(html_options) options[:builder] ||= FormBuilder @@ -88,19 +88,23 @@ module Ransack # if the user didn't specify the sort direction, detect the previous # sort direction on this field and reverse it - if ASC_DESC.none? { |d| d == new_dir } + if Ransack::Constants::ASC_DESC.none? { |d| d == new_dir } if existing_sort = search.sorts.detect { |s| s.name == attr_name } current_dir = existing_sort.dir end new_dir = - if current_dir - current_dir == DESC ? ASC : DESC - elsif default_order_is_a_hash - default_order[attr_name] || ASC - else - default_order || ASC - end + if current_dir + if current_dir == Ransack::Constants::DESC + Ransack::Constants::ASC + else + Ransack::Constants::DESC + end + elsif default_order_is_a_hash + default_order[attr_name] || Ransack::Constants::ASC + else + default_order || Ransack::Constants::ASC + end end sort_params << "#{attr_name} #{new_dir}" @@ -111,8 +115,10 @@ module Ransack sort_params = sort_params.first if sort_params.size == 1 html_options = args.first.is_a?(Hash) ? args.shift.dup : {} - css = [SORT_LINK, field_current_dir].compact.join(SPACE) - html_options[:class] = [css, html_options[:class]].compact.join(SPACE) + css = [Ransack::Constants::SORT_LINK, field_current_dir] + .compact.join(Ransack::Constants::SPACE) + html_options[:class] = [css, html_options[:class]] + .compact.join(Ransack::Constants::SPACE) query_hash = {} query_hash[search.context.search_key] = search_params @@ -137,15 +143,15 @@ module Ransack def link_name(label_text, dir) [ERB::Util.h(label_text), order_indicator_for(dir)] .compact - .join(NON_BREAKING_SPACE) + .join(Ransack::Constants::NON_BREAKING_SPACE) .html_safe end def order_indicator_for(dir) - if dir == ASC - ASC_ARROW - elsif dir == DESC - DESC_ARROW + if dir == Ransack::Constants::ASC + Ransack::Constants::ASC_ARROW + elsif dir == Ransack::Constants::DESC + Ransack::Constants::DESC_ARROW else nil end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 7b4c76d..d1b7246 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -105,7 +105,7 @@ module Ransack end def combinator=(val) - @combinator = AND_OR.detect { |v| v == val.to_s } || nil + @combinator = Ransack::Constants::AND_OR.detect { |v| v == val.to_s } || nil end alias :m= :combinator= alias :m :combinator @@ -180,9 +180,9 @@ module Ransack if predicates.size > 1 case combinator - when AND + when Ransack::Constants::AND Arel::Nodes::Grouping.new(Arel::Nodes::And.new(predicates)) - when OR + when Ransack::Constants::OR predicates.inject(&:or) end else @@ -228,12 +228,12 @@ module Ransack data = [ ['attributes'.freeze, a.try(:map, &:name)], ['predicate'.freeze, p], - [COMBINATOR, m], + [Ransack::Constants::COMBINATOR, m], ['values'.freeze, v.try(:map, &:value)] ] .reject { |e| e[1].blank? } .map { |v| "#{v[0]}: #{v[1]}" } - .join(COMMA_SPACE) + .join(Ransack::Constants::COMMA_SPACE) "Condition <#{data}>" end @@ -241,7 +241,7 @@ module Ransack def valid_combinator? attributes.size < 2 || - AND_OR.include?(combinator) + Ransack::Constants::AND_OR.include?(combinator) end end diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 98f0214..dc798d5 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -69,7 +69,7 @@ module Ransack def respond_to?(method_id) super or begin method_name = method_id.to_s - writer = method_name.sub!(/\=$/, EMPTY_STRING) + writer = method_name.sub!(/\=$/, Ransack::Constants::EMPTY) attribute_method?(method_name) ? true : false end end @@ -115,7 +115,7 @@ module Ransack def method_missing(method_id, *args) method_name = method_id.to_s - writer = method_name.sub!(/\=$/, EMPTY_STRING) + writer = method_name.sub!(/\=$/, Ransack::Constants::EMPTY) if attribute_method?(method_name) writer ? write_attribute(method_name, *args) : @@ -161,10 +161,13 @@ module Ransack end def inspect - data = [['conditions'.freeze, conditions], [COMBINATOR, combinator]] - .reject { |e| e[1].blank? } - .map { |v| "#{v[0]}: #{v[1]}" } - .join(COMMA_SPACE) + data = [ + ['conditions'.freeze, conditions], + [Ransack::Constants::COMBINATOR, combinator] + ] + .reject { |e| e[1].blank? } + .map { |v| "#{v[0]}: #{v[1]}" } + .join(Ransack::Constants::COMMA_SPACE) "Grouping <#{data}>" end diff --git a/lib/ransack/nodes/sort.rb b/lib/ransack/nodes/sort.rb index 66b3557..dc383d9 100644 --- a/lib/ransack/nodes/sort.rb +++ b/lib/ransack/nodes/sort.rb @@ -25,8 +25,8 @@ module Ransack def valid? bound? && attr && - context.klassify(parent).ransortable_attributes(context.auth_object) - .include?(attr_name) + context.klassify(parent).ransortable_attributes(context.auth_object) + .include?(attr_name) end def name=(name) @@ -36,7 +36,12 @@ module Ransack def dir=(dir) dir = dir.downcase if dir - @dir = ASC_DESC.include?(dir) ? dir : ASC + @dir = + if Ransack::Constants::ASC_DESC.include?(dir) + dir + else + Ransack::Constants::ASC + end end end diff --git a/lib/ransack/predicate.rb b/lib/ransack/predicate.rb index f583826..33a0d3c 100644 --- a/lib/ransack/predicate.rb +++ b/lib/ransack/predicate.rb @@ -19,7 +19,7 @@ module Ransack def detect_and_strip_from_string!(str) if p = detect_from_string(str) - str.sub! /_#{p}$/, EMPTY_STRING + str.sub! /_#{p}$/, Ransack::Constants::EMPTY p end end @@ -49,7 +49,7 @@ module Ransack lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? } @compound = opts[:compound] @wants_array = opts[:wants_array] == true || @compound || - IN_NOT_IN.include?(@arel_predicate) + Ransack::Constants::IN_NOT_IN.include?(@arel_predicate) end def eql?(other) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index 2e47180..72e5c29 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -88,7 +88,7 @@ module Ransack def method_missing(method_id, *args) method_name = method_id.to_s - getter_name = method_name.sub(/=$/, EMPTY_STRING) + getter_name = method_name.sub(/=$/, Ransack::Constants::EMPTY) if base.attribute_method?(getter_name) base.send(method_id, *args) elsif @context.ransackable_scope?(getter_name, @context.object) @@ -109,7 +109,7 @@ module Ransack [:base, base.inspect] ] .compact.map { |d| d.join(': '.freeze) } - .join(COMMA_SPACE) + .join(Ransack::Constants::COMMA_SPACE) "Ransack::Search<#{details}>" end @@ -127,14 +127,15 @@ module Ransack def collapse_multiparameter_attributes!(attrs) attrs.keys.each do |k| - if k.include?("(") + if k.include?('('.freeze) real_attribute, position = k.split(/\(|\)/) - cast = %w(a s i).include?(position.last) ? position.last : nil + cast = %w(a s i).freeze.include?(position.last) ? position.last : nil position = position.to_i - 1 value = attrs.delete(k) attrs[real_attribute] ||= [] - attrs[real_attribute][position] = if cast - (value.blank? && cast == 'i') ? nil : value.send("to_#{cast}") + attrs[real_attribute][position] = + if cast + value.blank? && cast == 'i'.freeze ? nil : value.send("to_#{cast}") else value end diff --git a/lib/ransack/translate.rb b/lib/ransack/translate.rb index 7bfbf7e..ffb059f 100644 --- a/lib/ransack/translate.rb +++ b/lib/ransack/translate.rb @@ -23,7 +23,8 @@ module Ransack |x| x.respond_to?(:model_name) } predicate = Predicate.detect_from_string(original_name) - attributes_str = original_name.sub(/_#{predicate}$/, EMPTY_STRING) + attributes_str = original_name + .sub(/_#{predicate}$/, Ransack::Constants::EMPTY) attribute_names = attributes_str.split(/_and_|_or_/) combinator = attributes_str.match(/_and_/) ? :and : :or defaults = base_ancestors.map do |klass| @@ -71,7 +72,7 @@ module Ransack def self.attribute_name(context, name, include_associations = nil) @context, @name = context, name @assoc_path = context.association_path(name) - @attr_name = @name.sub(/^#{@assoc_path}_/, EMPTY_STRING) + @attr_name = @name.sub(/^#{@assoc_path}_/, Ransack::Constants::EMPTY) associated_class = @context.traverse(@assoc_path) if @assoc_path.present? @include_associated = include_associations && associated_class diff --git a/lib/ransack/visitor.rb b/lib/ransack/visitor.rb index 66beb67..aabd4b1 100644 --- a/lib/ransack/visitor.rb +++ b/lib/ransack/visitor.rb @@ -18,7 +18,11 @@ module Ransack end def visit_Ransack_Nodes_Grouping(object) - object.combinator == OR ? visit_or(object) : visit_and(object) + if object.combinator == Ransack::Constants::OR + visit_or(object) + else + visit_and(object) + end end def visit_and(object) @@ -61,7 +65,9 @@ module Ransack end DISPATCH = Hash.new do |hash, klass| - hash[klass] = "visit_#{klass.name.gsub('::'.freeze, UNDERSCORE)}" + hash[klass] = "visit_#{ + klass.name.gsub('::'.freeze, Ransack::Constants::UNDERSCORE) + }" end end