From 984059e63f2cee1295a34e5a4419cfc60fb004dd Mon Sep 17 00:00:00 2001 From: jonatack Date: Mon, 9 Dec 2013 11:05:10 +0100 Subject: [PATCH] Code cleanup --- lib/ransack/nodes/grouping.rb | 19 ++++++++++++------- lib/ransack/predicate.rb | 15 +++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 6835618..08a0d6a 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -21,7 +21,9 @@ module Ransack end def translate(key, options = {}) - super or Translate.attribute(key.to_s, options.merge(:context => context)) + super or Translate.attribute( + key.to_s, options.merge(:context => context) + ) end def conditions @@ -48,7 +50,7 @@ module Ransack alias :c= :conditions= def [](key) - if condition = conditions.detect {|c| c.key == key.to_s} + if condition = conditions.detect { |c| c.key == key.to_s } condition else nil @@ -56,7 +58,7 @@ module Ransack end def []=(key, value) - conditions.reject! {|c| c.key == key.to_s} + conditions.reject! { |c| c.key == key.to_s } self.conditions << value end @@ -127,7 +129,9 @@ module Ransack when /^(g|c|m|groupings|conditions|combinator)=?$/ true else - name.split(/_and_|_or_/).select {|n| !@context.attribute_method?(n)}.empty? + name.split(/_and_|_or_/) + .select { |n| !@context.attribute_method?(n) } + .empty? end end @@ -155,9 +159,10 @@ module Ransack end def inspect - data =[['conditions', conditions], ['combinator', combinator]].reject { |e| - e[1].blank? - }.map { |v| "#{v[0]}: #{v[1]}" }.join(', ') + data = [['conditions', conditions], ['combinator', combinator]] + .reject { |e| e[1].blank? } + .map { |v| "#{v[0]}: #{v[1]}" } + .join(', ') "Grouping <#{data}>" end diff --git a/lib/ransack/predicate.rb b/lib/ransack/predicate.rb index 23405fe..6b839d2 100644 --- a/lib/ransack/predicate.rb +++ b/lib/ransack/predicate.rb @@ -1,6 +1,7 @@ module Ransack class Predicate - attr_reader :name, :arel_predicate, :type, :formatter, :validator, :compound, :wants_array + attr_reader :name, :arel_predicate, :type, :formatter, :validator, + :compound, :wants_array class << self @@ -24,11 +25,11 @@ module Ransack end def detect_from_string(str) - names_by_decreasing_length.detect {|p| str.end_with?("_#{p}")} + names_by_decreasing_length.detect { |p| str.end_with?("_#{p}") } end def name_from_attribute_name(attribute_name) - names_by_decreasing_length.detect {|p| attribute_name.to_s.match(/_#{p}$/)} + names_by_decreasing_length.detect { |p| attribute_name.to_s.match(/_#{p}$/) } end def for_attribute_name(attribute_name) @@ -42,9 +43,11 @@ module Ransack @arel_predicate = opts[:arel_predicate] @type = opts[:type] @formatter = opts[:formatter] - @validator = opts[:validator] || lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? } + @validator = opts[:validator] || + 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) + @wants_array = opts[:wants_array] == true || @compound || + ['in', 'not_in'].include?(@arel_predicate) end def eql?(other) @@ -66,7 +69,7 @@ module Ransack end def validate(vals, type = @type) - vals.select {|v| validator.call(type ? v.cast(type) : v.value)}.any? + vals.select { |v| validator.call(type ? v.cast(type) : v.value) }.any? end end