Module: Redis::TextSearch::WpHelpers
- Defined in:
- lib/redis/text_search.rb
Overview
InstanceMethods
Instance Method Summary collapse
-
#wp_count(options, args, finder) ⇒ Object
Does the not-so-trivial job of finding out the total number of entries in the database.
Instance Method Details
#wp_count(options, args, finder) ⇒ Object
Does the not-so-trivial job of finding out the total number of entries in the database. It relies on the ActiveRecord count
method.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/redis/text_search.rb', line 338 def wp_count(, args, finder) excludees = [:count, :order, :limit, :offset, :readonly] if defined?(ActiveRecord::Calculations) excludees << :from unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:from) end # we may be in a model or an association proxy klass = (@owner and @reflection) ? @reflection.klass : self # Use :select from scope if it isn't already present. [:select] = scope(:find, :select) unless [:select] if [:select] and [:select] =~ /^\s*DISTINCT\b/i # Remove quoting and check for table_name.*-like statement. if [:select].gsub('`', '') =~ /\w+\.\*/ [:select] = "DISTINCT #{klass.table_name}.#{klass.primary_key}" end else excludees << :select # only exclude the select param if it doesn't begin with DISTINCT end # count expects (almost) the same options as find = .except *excludees # merge the hash found in :count # this allows you to specify :select, :order, or anything else just for the count query .update [:count] if [:count] # forget about includes if they are irrelevant (Rails 2.1) # if count_options[:include] and # klass.private_methods.include_method?(:references_eager_loaded_tables?) and # !klass.send(:references_eager_loaded_tables?, count_options) # count_options.delete :include # end # we may have to scope ... counter = Proc.new { count() } count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with')) # scope_out adds a 'with_finder' method which acts like with_scope, if it's present # then execute the count with the scoping provided by the with_finder send(scoper, &counter) elsif finder =~ /^find_(all_by|by)_([_a-zA-Z]\w*)$/ # extract conditions from calls like "paginate_by_foo_and_bar" attribute_names = $2.split('_and_') conditions = construct_attributes_from_arguments(attribute_names, args) with_scope(:find => { :conditions => conditions }, &counter) else counter.call end count.respond_to?(:length) ? count.length : count end |