Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 294015309b03240c6a9a9aea69118a24d7f737ac @ernie ernie committed Mar 31, 2011
Showing with 2,293 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +11 −0 Gemfile
  3. +20 −0 LICENSE
  4. +5 −0 README.rdoc
  5. +19 −0 Rakefile
  6. +24 −0 lib/ransack.rb
  7. +2 −0 lib/ransack/adapters/active_record.rb
  8. +17 −0 lib/ransack/adapters/active_record/base.rb
  9. +153 −0 lib/ransack/adapters/active_record/context.rb
  10. +39 −0 lib/ransack/configuration.rb
  11. +23 −0 lib/ransack/constants.rb
  12. +152 −0 lib/ransack/context.rb
  13. +2 −0 lib/ransack/helpers.rb
  14. +172 −0 lib/ransack/helpers/form_builder.rb
  15. +27 −0 lib/ransack/helpers/form_helper.rb
  16. +67 −0 lib/ransack/locale/en.yml
  17. +53 −0 lib/ransack/naming.rb
  18. +7 −0 lib/ransack/nodes.rb
  19. +8 −0 lib/ransack/nodes/and.rb
  20. +36 −0 lib/ransack/nodes/attribute.rb
  21. +209 −0 lib/ransack/nodes/condition.rb
  22. +207 −0 lib/ransack/nodes/grouping.rb
  23. +34 −0 lib/ransack/nodes/node.rb
  24. +8 −0 lib/ransack/nodes/or.rb
  25. +39 −0 lib/ransack/nodes/sort.rb
  26. +120 −0 lib/ransack/nodes/value.rb
  27. +57 −0 lib/ransack/predicate.rb
  28. +114 −0 lib/ransack/search.rb
  29. +92 −0 lib/ransack/translate.rb
  30. +3 −0 lib/ransack/version.rb
  31. +29 −0 ransack.gemspec
  32. +5 −0 spec/blueprints/articles.rb
  33. +5 −0 spec/blueprints/comments.rb
  34. +3 −0 spec/blueprints/notes.rb
  35. +4 −0 spec/blueprints/people.rb
  36. +3 −0 spec/blueprints/tags.rb
  37. +22 −0 spec/console.rb
  38. +2 −0 spec/helpers/ransack_helper.rb
  39. +37 −0 spec/playground.rb
  40. +30 −0 spec/ransack/adapters/active_record/base_spec.rb
  41. +29 −0 spec/ransack/adapters/active_record/context_spec.rb
  42. +11 −0 spec/ransack/configuration_spec.rb
  43. +39 −0 spec/ransack/helpers/form_builder_spec.rb
  44. 0 spec/ransack/nodes/compound_condition_spec.rb
  45. 0 spec/ransack/nodes/condition_spec.rb
  46. +13 −0 spec/ransack/nodes/grouping_spec.rb
  47. +25 −0 spec/ransack/predicate_spec.rb
  48. +182 −0 spec/ransack/search_spec.rb
  49. +28 −0 spec/spec_helper.rb
  50. +102 −0 spec/support/schema.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,11 @@
+source "http://rubygems.org"
+gemspec
+
+gem 'arel', :git => 'git://github.com/rails/arel.git'
+gem 'rack', :git => 'git://github.com/rack/rack.git'
+
+git 'git://github.com/rails/rails.git' do
+ gem 'activesupport'
+ gem 'activerecord'
+ gem 'actionpack'
+end
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Ernie Miller
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
+= Ransack
+
+Don't use me.
+
+Seriously, I'm not anywhere close to ready for public consumption, yet.
@@ -0,0 +1,19 @@
+require 'bundler'
+require 'rspec/core/rake_task'
+
+Bundler::GemHelper.install_tasks
+
+RSpec::Core::RakeTask.new(:spec) do |rspec|
+ rspec.rspec_opts = ['--backtrace']
+end
+
+task :default => :spec
+
+desc "Open an irb session with Ransack and the sample data used in specs"
+task :console do
+ require 'irb'
+ require 'irb/completion'
+ require 'console'
+ ARGV.clear
+ IRB.start
+end
@@ -0,0 +1,24 @@
+require 'ransack/configuration'
+
+module Ransack
+ extend Configuration
+end
+
+Ransack.configure do |config|
+ Ransack::Constants::AREL_PREDICATES.each do |name|
+ config.add_predicate name, :arel_predicate => name
+ end
+
+ Ransack::Constants::DERIVED_PREDICATES.each do |args|
+ config.add_predicate *args
+ end
+end
+
+require 'ransack/translate'
+require 'ransack/search'
+require 'ransack/adapters/active_record'
+require 'ransack/helpers'
+require 'action_controller'
+
+ActiveRecord::Base.extend Ransack::Adapters::ActiveRecord::Base
+ActionController::Base.helper Ransack::Helpers::FormHelper
@@ -0,0 +1,2 @@
+require 'ransack/adapters/active_record/base'
+require 'ransack/adapters/active_record/context'
@@ -0,0 +1,17 @@
+module Ransack
+ module Adapters
+ module ActiveRecord
+ module Base
+
+ def self.extended(base)
+ alias :search :ransack unless base.method_defined? :search
+ end
+
+ def ransack(params = {})
+ Search.new(self, params)
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,153 @@
+require 'ransack/context'
+require 'active_record'
+
+module Ransack
+ module Adapters
+ module ActiveRecord
+ class Context < ::Ransack::Context
+ # Because the AR::Associations namespace is insane
+ JoinDependency = ::ActiveRecord::Associations::JoinDependency
+ JoinPart = JoinDependency::JoinPart
+ JoinAssociation = JoinDependency::JoinAssociation
+
+ def evaluate(search, opts = {})
+ relation = @object.where(accept(search.base)).order(accept(search.sorts))
+ opts[:distinct] ? relation.group(@klass.arel_table[@klass.primary_key]) : relation
+ end
+
+ def attribute_method?(str, klass = @klass)
+ exists = false
+
+ if column = get_column(str, klass)
+ exists = true
+ elsif (segments = str.split(/_/)).size > 1
+ remainder = []
+ found_assoc = nil
+ while !found_assoc && remainder.unshift(segments.pop) && segments.size > 0 do
+ if found_assoc = get_association(segments.join('_'), klass)
+ exists = attribute_method?(remainder.join('_'), found_assoc.klass)
+ end
+ end
+ end
+
+ exists
+ end
+
+ def type_for(attr)
+ return nil unless attr
+ name = attr.name.to_s
+ table = attr.relation.table_name
+
+ unless @engine.connection_pool.table_exists?(table)
+ raise "No table named #{table} exists"
+ end
+
+ @engine.connection_pool.columns_hash[table][name].type
+ end
+
+ private
+
+ def klassify(obj)
+ if Class === obj && ::ActiveRecord::Base > obj
+ obj
+ elsif obj.respond_to? :klass
+ obj.klass
+ elsif obj.respond_to? :active_record
+ obj.active_record
+ else
+ raise ArgumentError, "Don't know how to klassify #{obj}"
+ end
+ end
+
+ def get_attribute(str, parent = @base)
+ attribute = nil
+
+ if column = get_column(str, parent)
+ attribute = parent.table[str]
+ elsif (segments = str.split(/_/)).size > 1
+ remainder = []
+ found_assoc = nil
+ while remainder.unshift(segments.pop) && segments.size > 0 && !found_assoc do
+ if found_assoc = get_association(segments.join('_'), parent)
+ join = build_or_find_association(found_assoc.name, parent)
+ attribute = get_attribute(remainder.join('_'), join)
+ end
+ end
+ end
+
+ attribute
+ end
+
+ def get_column(str, parent = @base)
+ klassify(parent).columns_hash[str]
+ end
+
+ def get_association(str, parent = @base)
+ klassify(parent).reflect_on_all_associations.detect {|a| a.name.to_s == str}
+ end
+
+ def join_dependency(relation)
+ if relation.respond_to?(:join_dependency) # MetaWhere will enable this
+ relation.join_dependency
+ else
+ build_join_dependency(relation)
+ end
+ end
+
+ def build_join_dependency(relation)
+ buckets = relation.joins_values.group_by do |join|
+ case join
+ when String
+ 'string_join'
+ when Hash, Symbol, Array
+ 'association_join'
+ when ActiveRecord::Associations::JoinDependency::JoinAssociation
+ 'stashed_join'
+ when Arel::Nodes::Join
+ '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
+
+ join_list = relation.send :custom_join_ast, relation.table.from(relation.table), string_joins
+
+ join_dependency = JoinDependency.new(
+ relation.klass,
+ association_joins,
+ join_list
+ )
+
+ join_nodes.each do |join|
+ join_dependency.table_aliases[join.left.name.downcase] = 1
+ end
+
+ join_dependency.graft(*stashed_association_joins)
+ end
+
+ def build_or_find_association(name, parent = @base)
+ found_association = @join_dependency.join_associations.detect do |assoc|
+ assoc.reflection.name == name &&
+ assoc.parent == parent
+ end
+ unless found_association
+ @join_dependency.send(:build, name.to_sym, parent, Arel::Nodes::OuterJoin)
+ found_association = @join_dependency.join_associations.last
+ # Leverage the stashed association functionality in AR
+ @object = @object.joins(found_association)
+ end
+
+ found_association
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,39 @@
+require 'ransack/constants'
+require 'ransack/predicate'
+
+module Ransack
+ module Configuration
+
+ mattr_accessor :predicates
+ self.predicates = {}
+
+ def self.predicate_keys
+ predicates.keys.sort {|a,b| b.length <=> a.length}
+ end
+
+ def configure
+ yield self
+ end
+
+ def add_predicate(name, opts = {})
+ name = name.to_s
+ opts[:name] = name
+ compounds = opts.delete(:compounds)
+ compounds = true if compounds.nil?
+ opts[:arel_predicate] = opts[:arel_predicate].to_s
+
+ self.predicates[name] = Predicate.new(opts)
+
+ ['_any', '_all'].each do |suffix|
+ self.predicates[name + suffix] = Predicate.new(
+ opts.merge(
+ :name => name + suffix,
+ :arel_predicate => opts[:arel_predicate] + suffix,
+ :compound => true
+ )
+ )
+ end if compounds
+ end
+
+ end
+end
@@ -0,0 +1,23 @@
+module Ransack
+ module Constants
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
+
+ AREL_PREDICATES = %w(eq not_eq matches does_not_match lt lteq gt gteq in not_in)
+
+ DERIVED_PREDICATES = [
+ ['cont', {:arel_predicate => 'matches', :formatter => proc {|v| "%#{v}%"}}],
+ ['not_cont', {:arel_predicate => 'does_not_match', :formatter => proc {|v| "%#{v}%"}}],
+ ['start', {:arel_predicate => 'matches', :formatter => proc {|v| "#{v}%"}}],
+ ['not_start', {:arel_predicate => 'does_not_match', :formatter => proc {|v| "#{v}%"}}],
+ ['end', {:arel_predicate => 'matches', :formatter => proc {|v| "%#{v}"}}],
+ ['not_end', {:arel_predicate => 'does_not_match', :formatter => proc {|v| "%#{v}"}}],
+ ['true', {:arel_predicate => 'eq', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}}],
+ ['false', {:arel_predicate => 'eq', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}, :formatter => proc {|v| !v}}],
+ ['present', {:arel_predicate => 'not_eq_all', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}, :formatter => proc {|v| [nil, '']}}],
+ ['blank', {:arel_predicate => 'eq_any', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}, :formatter => proc {|v| [nil, '']}}],
+ ['null', {:arel_predicate => 'eq', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}, :formatter => proc {|v| nil}}],
+ ['not_null', {:arel_predicate => 'not_eq', :compounds => false, :type => :boolean, :validator => proc {|v| TRUE_VALUES.include?(v)}, :formatter => proc {|v| nil}}]
+ ]
+ end
+end
Oops, something went wrong.

0 comments on commit 2940153

Please sign in to comment.