Skip to content

Commit

Permalink
small refactor and not modifying opts that are passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
spilliton committed Feb 25, 2014
1 parent 726bd8f commit 60b14ad
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions lib/randumb/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def random(max_items = nil, opts={})
return random_by_id_shuffle(max_items, opts) if is_randumb_postges_case?(relation)
scope = relation.order_by_rand(opts)

if max_items && (!relation.limit_value || relation.limit_value > max_items)
scope = scope.limit(max_items)
end
scope = scope.limit(max_items) if override_limit?(max_items, relation)

# return first record if method was called without parameters
max_items ? scope.to_a : scope.first
Expand All @@ -32,9 +30,7 @@ def random_weighted(ranking_column, max_items = nil, opts={})
scope = relation.order_by_rand_weighted(ranking_column, opts)

# override the limit if they are requesting multiple records
if max_items && (!relation.limit_value || relation.limit_value > max_items)
scope = scope.limit(max_items)
end
scope = scope.limit(max_items) if override_limit?(max_items, relation)

# return first record if method was called without parameters
max_items ? scope.to_a : scope.first
Expand Down Expand Up @@ -65,23 +61,25 @@ def random_by_id_shuffle(max_items = nil, opts={})
return_first_record ? records.first : records
end

def order_by_rand(opts = {})
opts.reverse_merge!(connection: connection, table_name: table_name)

order_clause = Randumb::Syntax.random_order_clause(opts)
build_order_scope(order_clause)
def order_by_rand(opts={})
build_order_scope(opts)
end

def order_by_rand_weighted(ranking_column, opts = {})
opts.reverse_merge!(connection: connection, table_name: table_name)

order_clause = Randumb::Syntax.random_weighted_order_clause(ranking_column, opts)
build_order_scope(order_clause)
def order_by_rand_weighted(ranking_column, opts={})
build_order_scope(opts, ranking_column)
end

private

def build_order_scope(order_clause)
def build_order_scope(options, ranking_column=nil)
opts = options.reverse_merge(connection: connection, table_name: table_name)

order_clause = if ranking_column
Randumb::Syntax.random_weighted_order_clause(ranking_column, opts)
else
Randumb::Syntax.random_order_clause(opts)
end

if ::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR < 2
# AR 3.0.0 support
order(order_clause)
Expand Down Expand Up @@ -126,7 +124,6 @@ def fetch_random_ids(relation, max_ids, opts={})

id_results = connection.select_all(id_only_relation.to_sql)


rng = random_number_generator(opts)
if max_ids == 1 && id_results.count > 0
rand_index = rng.rand(id_results.count)
Expand All @@ -146,6 +143,10 @@ def random_number_generator(opts={})
end
end

def override_limit?(max_items, relation)
max_items && (!relation.limit_value || relation.limit_value > max_items)
end

end


Expand Down

0 comments on commit 60b14ad

Please sign in to comment.