active_median 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b551770177ca1b626f794e53a6076dcd2fd80f09
4
- data.tar.gz: 94f4f4e96beaefbadba494726b7809a5ff65e649
3
+ metadata.gz: f2e6418a4cb5e878f7f36dec69fe4bb169b666c4
4
+ data.tar.gz: b0da140de2cdb3c84d4f0e8408dc13d4339de6b0
5
5
  SHA512:
6
- metadata.gz: d8a51b06f6d07222bda15aeac042384bbf04d94cf1810f2bb85e32404c76b21b417912272653369c0a393d38b107eaa18ecec83418762f5b498303130511ba72
7
- data.tar.gz: 78a344daee6bc290764695fa6a7b6504aec0f6392d2788dcb3767b28e5f61c3b6d5499782719e0c236db2435f6c6315c9831e49a18c7d8ca5ded9af5333455a0
6
+ metadata.gz: f29c2c2002f6438ab8f2d9ff209481f1785d5f8fba668ec2c0ed73bfe17deac908ca31823fb0b26e55cf06b1f9d5bab1d34d5f6c03dcd1f093adfb21fcfb271c
7
+ data.tar.gz: 0201efbeb295a121298a153e67f4985fee623529fcca873bbcfa8e70f5f551c90af7009a7489941014c9a0b2644cc746f9b0c18bc32cb71d40274fa4caef5f10
@@ -12,11 +12,10 @@ notifications:
12
12
  on_failure: change
13
13
  gemfile:
14
14
  - Gemfile
15
- - test/gemfiles/activerecord50.gemfile
15
+ - test/gemfiles/activerecord42.gemfile
16
16
  - test/gemfiles/activerecord41.gemfile
17
17
  - test/gemfiles/activerecord40.gemfile
18
18
  - test/gemfiles/activerecord32.gemfile
19
19
  matrix:
20
20
  allow_failures:
21
- - gemfile: test/gemfiles/activerecord50.gemfile
22
21
  - gemfile: test/gemfiles/activerecord32.gemfile
@@ -1,3 +1,7 @@
1
+ ## 0.1.4
2
+
3
+ - Added `drop_function` method
4
+
1
5
  ## 0.1.3
2
6
 
3
7
  - Added support for ActiveRecord 5.0
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in active_median.gemspec
4
4
  gemspec
5
+
6
+ gem "activerecord", "~> 5.0.0"
data/README.md CHANGED
@@ -22,10 +22,22 @@ Add this line to your application’s Gemfile:
22
22
  gem 'active_median'
23
23
  ```
24
24
 
25
- And add the `median` function to the database from the Rails console.
25
+ And create a migration to add the `median` function to the database.
26
+
27
+ ```sh
28
+ rails g migration create_median_function
29
+ ```
30
+
31
+ with:
26
32
 
27
33
  ```ruby
28
- ActiveMedian.create_function
34
+ def up
35
+ ActiveMedian.create_function
36
+ end
37
+
38
+ def down
39
+ ActiveMedian.drop_function
40
+ end
29
41
  ```
30
42
 
31
43
  ## Contributing
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
- task :default => :test
4
+ task default: :test
5
5
  Rake::TestTask.new do |t|
6
6
  t.libs << "test"
7
7
  t.pattern = "test/**/*_test.rb"
@@ -1,19 +1,19 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'active_median/version'
4
+ require "active_median/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "active_median"
8
8
  spec.version = ActiveMedian::VERSION
9
9
  spec.authors = ["Andrew Kane"]
10
10
  spec.email = ["acekane1@gmail.com"]
11
- spec.description = %q{Median for ActiveRecord}
12
- spec.summary = %q{Median for ActiveRecord}
11
+ spec.description = "Median for ActiveRecord"
12
+ spec.summary = "Median for ActiveRecord"
13
13
  spec.homepage = "https://github.com/ankane/active_median"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
@@ -42,6 +42,14 @@ module ActiveMedian
42
42
  SQL
43
43
  true
44
44
  end
45
+
46
+ def self.drop_function
47
+ ActiveRecord::Base.connection.execute <<-SQL
48
+ DROP AGGREGATE IF EXISTS median(anyelement);
49
+ DROP FUNCTION IF EXISTS median(anyarray);
50
+ SQL
51
+ true
52
+ end
45
53
  end
46
54
 
47
55
  module ActiveRecord
@@ -54,7 +62,7 @@ end
54
62
 
55
63
  module ActiveRecord
56
64
  module Querying
57
- delegate :median, :to => (Gem::Version.new(Arel::VERSION) >= Gem::Version.new("4.0.1") ? :all : :scoped)
65
+ delegate :median, to: (Gem::Version.new(Arel::VERSION) >= Gem::Version.new("4.0.1") ? :all : :scoped)
58
66
  end
59
67
  end
60
68
 
@@ -67,7 +75,7 @@ end
67
75
  module Arel
68
76
  module Expressions
69
77
  def median
70
- Nodes::Median.new [self], Nodes::SqlLiteral.new('median_id')
78
+ Nodes::Median.new [self], Nodes::SqlLiteral.new("median_id")
71
79
  end
72
80
  end
73
81
  end
@@ -75,15 +83,17 @@ end
75
83
  module Arel
76
84
  module Visitors
77
85
  class ToSql
78
- def visit_Arel_Nodes_Median o, a=nil
86
+ def visit_Arel_Nodes_Median(o, a = nil)
79
87
  if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("6.0.0")
80
88
  aggregate "MEDIAN", o, a
81
89
  elsif a
82
- "MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
83
- visit x, a }.join(', ')})#{o.alias ? " AS #{visit o.alias, a}" : ''}"
90
+ "MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map do |x|
91
+ visit x, a
92
+ end.join(', ')})#{o.alias ? " AS #{visit o.alias, a}" : ''}"
84
93
  else
85
- "MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
86
- visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
94
+ "MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map do |x|
95
+ visit x
96
+ end.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
87
97
  end
88
98
  end
89
99
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMedian
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -1,19 +1,18 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class TestActiveMedian < Minitest::Test
4
-
5
4
  def setup
6
5
  ActiveMedian.create_function
7
6
  User.delete_all
8
7
  end
9
8
 
10
9
  def test_even
11
- [1, 1, 2, 3, 4, 100].each {|n| User.create!(visits_count: n) }
10
+ [1, 1, 2, 3, 4, 100].each { |n| User.create!(visits_count: n) }
12
11
  assert_equal 2.5, User.median(:visits_count)
13
12
  end
14
13
 
15
14
  def test_odd
16
- [1, 1, 2, 4, 100].each {|n| User.create!(visits_count: n) }
15
+ [1, 1, 2, 4, 100].each { |n| User.create!(visits_count: n) }
17
16
  assert_equal 2, User.median(:visits_count)
18
17
  end
19
18
 
@@ -22,13 +21,18 @@ class TestActiveMedian < Minitest::Test
22
21
  end
23
22
 
24
23
  def test_decimal
25
- 6.times {|n| User.create!(latitude: n * 0.1) }
24
+ 6.times { |n| User.create!(latitude: n * 0.1) }
26
25
  assert_equal 0.25, User.median(:latitude)
27
26
  end
28
27
 
29
28
  def test_float
30
- 6.times {|n| User.create!(rating: n * 0.1) }
29
+ 6.times { |n| User.create!(rating: n * 0.1) }
31
30
  assert_equal 0.25, User.median(:rating)
32
31
  end
33
32
 
33
+ def test_drop
34
+ ActiveMedian.drop_function
35
+ error = assert_raises(ActiveRecord::StatementInvalid) { User.median(:visits_count) }
36
+ assert_includes error.message, "PG::UndefinedFunction"
37
+ end
34
38
  end
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in active_median.gemspec
4
4
  gemspec path: "../../"
5
5
 
6
- gem "activerecord", "~> 5.0.0.beta3"
6
+ gem "activerecord", "~> 4.2.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_median
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-22 00:00:00.000000000 Z
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -101,7 +101,7 @@ files:
101
101
  - test/gemfiles/activerecord32.gemfile
102
102
  - test/gemfiles/activerecord40.gemfile
103
103
  - test/gemfiles/activerecord41.gemfile
104
- - test/gemfiles/activerecord50.gemfile
104
+ - test/gemfiles/activerecord42.gemfile
105
105
  - test/test_helper.rb
106
106
  homepage: https://github.com/ankane/active_median
107
107
  licenses:
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 2.6.1
126
+ rubygems_version: 2.5.1
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: Median for ActiveRecord
@@ -132,6 +132,5 @@ test_files:
132
132
  - test/gemfiles/activerecord32.gemfile
133
133
  - test/gemfiles/activerecord40.gemfile
134
134
  - test/gemfiles/activerecord41.gemfile
135
- - test/gemfiles/activerecord50.gemfile
135
+ - test/gemfiles/activerecord42.gemfile
136
136
  - test/test_helper.rb
137
- has_rdoc: