Skip to content

Commit

Permalink
Updated style
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Mar 11, 2015
1 parent 2b5c90d commit c9bbfdd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in hightop.gemspec
gemspec
2 changes: 1 addition & 1 deletion Rakefile
@@ -1,7 +1,7 @@
require "bundler/gem_tasks"
require "rake/testtask"

task :default => :test
task default: :test
Rake::TestTask.new do |t|
t.libs << "test"
t.pattern = "test/**/*_test.rb"
Expand Down
8 changes: 4 additions & 4 deletions hightop.gemspec
@@ -1,15 +1,15 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'hightop/version'
require "hightop/version"

Gem::Specification.new do |spec|
spec.name = "hightop"
spec.version = Hightop::VERSION
spec.authors = ["Andrew Kane"]
spec.email = ["andrew@chartkick.com"]
spec.summary = %q{A nice shortcut for group count queries}
spec.description = %q{A nice shortcut for group count queries}
spec.summary = "A nice shortcut for group count queries"
spec.description = "A nice shortcut for group count queries"
spec.homepage = "https://github.com/ankane/hightop"
spec.license = "MIT"

Expand Down
6 changes: 2 additions & 4 deletions lib/hightop.rb
Expand Up @@ -2,15 +2,14 @@
require "active_record"

module Hightop

def top(column, limit = nil, options = {})
if limit.is_a?(Hash)
options = limit
limit = nil
end

order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
relation = group(column).order("count_#{options[:uniq] || "all"} DESC, #{order_str}")
relation = group(column).order("count_#{options[:uniq] || 'all'} DESC, #{order_str}")
if limit
relation = relation.limit(limit)
end
Expand All @@ -22,7 +21,7 @@ def top(column, limit = nil, options = {})
end

if options[:min]
relation = relation.having("COUNT(#{options[:uniq] ? "DISTINCT #{options[:uniq]}" : "*"}) >= #{options[:min].to_i}")
relation = relation.having("COUNT(#{options[:uniq] ? "DISTINCT #{options[:uniq]}" : '*'}) >= #{options[:min].to_i}")
end

if options[:uniq]
Expand All @@ -31,7 +30,6 @@ def top(column, limit = nil, options = {})
relation.count
end
end

end

ActiveRecord::Base.send :extend, Hightop
10 changes: 4 additions & 6 deletions test/hightop_test.rb
@@ -1,7 +1,6 @@
require_relative "test_helper"

class TestHightop < Minitest::Test

def setup
Visit.delete_all
end
Expand Down Expand Up @@ -32,7 +31,7 @@ def test_nil_values
create_city("San Francisco", 3)
create_city(nil, 2)
expected = {
"San Francisco" => 3,
"San Francisco" => 3
}
assert_equal expected, Visit.top(:city)
end
Expand Down Expand Up @@ -64,8 +63,8 @@ def test_expressions
end

def test_uniq
create({city: "San Francisco", user_id: 1})
create({city: "San Francisco", user_id: 1})
create(city: "San Francisco", user_id: 1)
create(city: "San Francisco", user_id: 1)
expected = {
"San Francisco" => 1
}
Expand All @@ -86,7 +85,6 @@ def create_city(city, count = 1)
end

def create(attributes, count = 1)
count.times{ Visit.create!(attributes) }
count.times { Visit.create!(attributes) }
end

end

0 comments on commit c9bbfdd

Please sign in to comment.