custom_sort 0.4.02
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/custom_sort-0.1.0.gem +0 -0
- data/custom_sort-0.1.01.gem +0 -0
- data/custom_sort-0.1.02.gem +0 -0
- data/custom_sort-0.1.03.gem +0 -0
- data/custom_sort-0.2.00.gem +0 -0
- data/custom_sort-0.2.01.gem +0 -0
- data/custom_sort-0.3.00.gem +0 -0
- data/custom_sort-0.4.00.gem +0 -0
- data/custom_sort-0.4.01.gem +0 -0
- data/custom_sort.gemspec +32 -0
- data/lib/custom_sort.rb +54 -0
- data/lib/custom_sort/active_record.rb +6 -0
- data/lib/custom_sort/enumerable.rb +51 -0
- data/lib/custom_sort/magic.rb +65 -0
- data/lib/custom_sort/magic_new.rb +65 -0
- data/lib/custom_sort/query_methods.rb +24 -0
- data/lib/custom_sort/relation.rb +16 -0
- data/lib/custom_sort/relation_builder.rb +156 -0
- data/lib/custom_sort/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e8a936142d2208136ff22002af705d7034650b934058600ea1e544a4cc5d5d0e
|
4
|
+
data.tar.gz: 3a8abf264ee95fe32ab0ef7163c6f7eb522d0f60413ebafa70abfa5795450d28
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 11076fe8473f6a7df30eae9b690063229a8d961200d3152df8d19537c90b49afede2ec0a9f899b1b61811e5d9025c6f2248062f46718225cdb51bd2739042a54
|
7
|
+
data.tar.gz: 4f687926e1a3aedee2070a873f626f91b1bf458b8ee25bfcc6761cebad28b32e62ec1ea311f7a0e3209f8da6989fc12c7ac7cf251f0d9099fd91e66266a05c1b
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at scott.merritt@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# CustomSort
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/custom_sort`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'custom_sort'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install custom_sort
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/custom_sort. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## Code of Conduct
|
38
|
+
|
39
|
+
Everyone interacting in the CustomSort project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/custom_sort/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "custom_sort"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/custom_sort.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "custom_sort/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "custom_sort"
|
7
|
+
spec.version = CustomSort::VERSION
|
8
|
+
spec.authors = ["Scott Merritt"]
|
9
|
+
spec.email = ["dev@meritocracyconsulting.com"]
|
10
|
+
|
11
|
+
spec.summary = "A gem for sorting data"
|
12
|
+
spec.description = "It allows you to sort by predefined basic algorithms"
|
13
|
+
spec.homepage = "https://github.com/scottcmerritt/custom_sort"
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/scottcmerritt/custom_sort.git"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/scottcmerritt/custom_sort/blob/master/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
end
|
data/lib/custom_sort.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# dependencies
|
2
|
+
require "active_support/core_ext/module/attribute_accessors"
|
3
|
+
#require "active_support/time"
|
4
|
+
|
5
|
+
# modules
|
6
|
+
require "custom_sort/magic"
|
7
|
+
require "custom_sort/magic_new"
|
8
|
+
require "custom_sort/relation_builder"
|
9
|
+
|
10
|
+
require "custom_sort/version"
|
11
|
+
|
12
|
+
|
13
|
+
module CustomSort
|
14
|
+
class Error < StandardError; end
|
15
|
+
# Your code goes here...
|
16
|
+
|
17
|
+
FIELDS = [:blended,:blended_recent,:quality,:interesting,:learned,:votes,:nofeedback,:nojoin] # [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :minute_of_hour, :day_of_month, :day_of_year, :month_of_year]
|
18
|
+
METHODS = FIELDS.map { |v| :"customsort_by_#{v}" } + [:customsort_by_field]
|
19
|
+
LAMBDA_VALUES = [-0.02445,-0.0489,-0.0990, nil]
|
20
|
+
|
21
|
+
mattr_accessor :data_table, :blended_columns
|
22
|
+
self.blended_columns = ["quality","interesting"]
|
23
|
+
self.data_table = "vote_caches"
|
24
|
+
|
25
|
+
class SortFields
|
26
|
+
def initialize
|
27
|
+
|
28
|
+
end
|
29
|
+
def self.time_decay table_name
|
30
|
+
"EXTRACT(EPOCH FROM (NOW()::timestamp - #{table_name}.created_at::timestamp))/(24*60*60)"
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.time_decay_adjusted table_name, lambda_key
|
34
|
+
lambda_val = CustomSort::LAMBDA_VALUES[lambda_key]
|
35
|
+
time_decay_query = CustomSort::SortFields.time_decay(table_name)
|
36
|
+
lambda_val.nil? ? "1" : "exp(#{time_decay_query}*#{lambda_val})"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# api for gems like ActiveMedian or Kaminari
|
41
|
+
def self.process_result(relation, result, **options)
|
42
|
+
if relation.customsort_values
|
43
|
+
result = CustomSort::Magic::Relation.process_result(relation, result, **options)
|
44
|
+
end
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
require "custom_sort/enumerable"
|
51
|
+
|
52
|
+
ActiveSupport.on_load(:active_record) do
|
53
|
+
require "custom_sort/active_record"
|
54
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Enumerable
|
2
|
+
=begin
|
3
|
+
CustomSort::FIELDS.each do |period|
|
4
|
+
define_method :"group_by_#{period}" do |*args, **options, &block|
|
5
|
+
if block
|
6
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" if args.any?
|
7
|
+
CustomSort::Magic::Enumerable.group_by(self, period, options, &block)
|
8
|
+
elsif respond_to?(:scoping)
|
9
|
+
scoping { @klass.group_by_period(period, *args, **options, &block) }
|
10
|
+
else
|
11
|
+
raise ArgumentError, "no block given"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def group_by_period(period, *args, **options, &block)
|
17
|
+
if block || !respond_to?(:scoping)
|
18
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size + 1}, expected 1)" if args.any?
|
19
|
+
|
20
|
+
Groupdate::Magic.validate_period(period, options.delete(:permit))
|
21
|
+
send("group_by_#{period}", **options, &block)
|
22
|
+
else
|
23
|
+
scoping { @klass.group_by_period(period, *args, **options, &block) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
=end
|
27
|
+
CustomSort::FIELDS.each do |query_name|
|
28
|
+
define_method :"customsort_by_#{query_name}" do |*args, **options, &block|
|
29
|
+
if block
|
30
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" if args.any?
|
31
|
+
CustomSort::Magic::Enumerable.customsort_by(self, query_name, options, &block)
|
32
|
+
elsif respond_to?(:scoping)
|
33
|
+
scoping { @klass.customsort_by_field(query_name, *args, **options, &block) }
|
34
|
+
else
|
35
|
+
raise ArgumentError, "no block given"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def customsort_by_field(query_name, *args, **options, &block)
|
41
|
+
if block || !respond_to?(:scoping)
|
42
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size + 1}, expected 1)" if args.any?
|
43
|
+
|
44
|
+
#CustomSort::Magic.validate_period(period, options.delete(:permit))
|
45
|
+
send("customsort_by_#{query_name}", **options, &block)
|
46
|
+
else
|
47
|
+
scoping { @klass.customsort_by_field(query_name, *args, **options, &block) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "i18n"
|
2
|
+
|
3
|
+
module CustomSort
|
4
|
+
class Magic
|
5
|
+
attr_accessor :query_name, :options
|
6
|
+
|
7
|
+
def initialize(query_name:, **options)
|
8
|
+
@query_name = query_name
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.validate_period(period, permit)
|
13
|
+
permitted_periods = ((permit || CustomSort::FIELDS).map(&:to_sym) & CustomSort::FIELDS).map(&:to_s)
|
14
|
+
raise ArgumentError, "Unpermitted period" unless permitted_periods.include?(period.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
class Enumerable < Magic
|
18
|
+
def sort_by(enum, &_block)
|
19
|
+
group = enum.group_by do |v|
|
20
|
+
v = yield(v)
|
21
|
+
raise ArgumentError, "Not a time" unless v.respond_to?(:to_time)
|
22
|
+
series_builder.round_time(v)
|
23
|
+
end
|
24
|
+
series_builder.generate(group, default_value: [], series_default: false)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.sort_by(enum, query_name, options, &block)
|
28
|
+
CustomSort::Magic::Enumerable.new(query_name: query_name, **options).group_by(enum, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Relation < Magic
|
33
|
+
def initialize(**options)
|
34
|
+
super(**options.reject { |k, _| [:default_value, :carry_forward, :last, :current].include?(k) })
|
35
|
+
@options = options
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.generate_relation(relation, field:, **options)
|
39
|
+
magic = CustomSort::Magic::Relation.new(**options)
|
40
|
+
|
41
|
+
# generate ActiveRecord relation
|
42
|
+
relation =
|
43
|
+
RelationBuilder.new(
|
44
|
+
relation,
|
45
|
+
column: field,
|
46
|
+
query_name: magic.query_name
|
47
|
+
).generate
|
48
|
+
|
49
|
+
# add Groupdate info
|
50
|
+
#magic.group_index = relation.group_values.size - 1
|
51
|
+
#(relation.groupdate_values ||= []) << magic
|
52
|
+
|
53
|
+
relation
|
54
|
+
end
|
55
|
+
|
56
|
+
# allow any options to keep flexible for future
|
57
|
+
def self.process_result(relation, result, **options)
|
58
|
+
relation.customsort_values.reverse.each do |gv|
|
59
|
+
result = gv.perform(relation, result, default_value: options[:default_value])
|
60
|
+
end
|
61
|
+
result
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "i18n"
|
2
|
+
|
3
|
+
module CustomSort
|
4
|
+
class MagicNew
|
5
|
+
attr_accessor :query_name, :recency_key, :options
|
6
|
+
|
7
|
+
def initialize(query_name:, **options)
|
8
|
+
@query_name = query_name
|
9
|
+
@options = options
|
10
|
+
|
11
|
+
@recency_key = options[:recency_key] ? options[:recency_key].to_i : 2
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.validate_period(period, permit)
|
15
|
+
permitted_periods = ((permit || CustomSort::FIELDS).map(&:to_sym) & CustomSort::FIELDS).map(&:to_s)
|
16
|
+
raise ArgumentError, "Unpermitted period" unless permitted_periods.include?(period.to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
class Enumerable < MagicNew
|
20
|
+
def customsort_by(enum, &_block)
|
21
|
+
group = enum.group_by do |v|
|
22
|
+
v = yield(v)
|
23
|
+
raise ArgumentError, "Not a time" unless v.respond_to?(:to_time)
|
24
|
+
series_builder.round_time(v)
|
25
|
+
end
|
26
|
+
series_builder.generate(group, default_value: [], series_default: false)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.customsort_by(enum, query_name, options, &block)
|
30
|
+
CustomSort::MagicNew::Enumerable.new(query_name: query_name, **options).group_by(enum, &block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Relation < MagicNew
|
35
|
+
def initialize(**options)
|
36
|
+
super(**options.reject { |k, _| [:default_value, :carry_forward, :last, :current].include?(k) })
|
37
|
+
@options = options
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.generate_relation(relation, field: nil, **options)
|
41
|
+
magic = CustomSort::MagicNew::Relation.new(**options)
|
42
|
+
|
43
|
+
# generate ActiveRecord relation
|
44
|
+
relation =
|
45
|
+
RelationBuilder.new(
|
46
|
+
relation,
|
47
|
+
column: field,
|
48
|
+
query_name: magic.query_name,
|
49
|
+
recency_key: magic.recency_key
|
50
|
+
).generate
|
51
|
+
|
52
|
+
relation
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
# allow any options to keep flexible for future
|
57
|
+
def self.process_result(relation, result, **options)
|
58
|
+
relation.customsort_values.reverse.each do |gv|
|
59
|
+
result = gv.perform(relation, result, default_value: options[:default_value])
|
60
|
+
end
|
61
|
+
result
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CustomSort
|
2
|
+
module QueryMethods
|
3
|
+
|
4
|
+
CustomSort::FIELDS.each do |query_name|
|
5
|
+
define_method :"customsort_by_#{query_name}" do |**options|
|
6
|
+
CustomSort::MagicNew::Relation.generate_relation(self,
|
7
|
+
query_name: query_name.to_s,
|
8
|
+
**options
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def customsort(query_name, **options)
|
15
|
+
send("customsort_by_#{query_name}", **options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def customsort_by_field(query_name, permit: nil, **options)
|
19
|
+
#CustomSort::Magic.validate_query(query_name, permit)
|
20
|
+
send("customsort_by_#{query_name}", **options)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module CustomSort
|
4
|
+
module Relation
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attr_accessor :customsort_values
|
9
|
+
end
|
10
|
+
|
11
|
+
def calculate(*args, &block)
|
12
|
+
default_value = [:count, :sum].include?(args[0]) ? 0 : nil
|
13
|
+
CustomSort.process_result(self, super, default_value: default_value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module CustomSort
|
2
|
+
class RelationBuilder
|
3
|
+
attr_reader :query_name, :column, :recency_key #, :period
|
4
|
+
#, :period, :day_start, :week_start, :n_seconds
|
5
|
+
|
6
|
+
#def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:)
|
7
|
+
def initialize(relation, column:, query_name:, recency_key:)
|
8
|
+
@relation = relation
|
9
|
+
@column = resolve_column(relation, column) unless column.nil?
|
10
|
+
@query_name = query_name
|
11
|
+
@table_name = resolve_table(relation) #"items"
|
12
|
+
@class_name = resolve_class(relation)
|
13
|
+
|
14
|
+
@recency = true
|
15
|
+
@lambda_key = recency_key #2
|
16
|
+
|
17
|
+
#if relation.default_timezone == :local
|
18
|
+
# raise CustomSort::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
|
19
|
+
#end
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate
|
23
|
+
#@relation.group(group_clause).where(*where_clause)
|
24
|
+
|
25
|
+
case query_name
|
26
|
+
when "nojoin"
|
27
|
+
@relation.where(*where_clause).order(order_clause)
|
28
|
+
else
|
29
|
+
@relation.joins(join_clause).where(*where_clause).order(order_clause)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
|
36
|
+
def time_decay
|
37
|
+
CustomSort::SortFields.time_decay_adjusted(@table_name,@lambda_key)
|
38
|
+
end
|
39
|
+
|
40
|
+
def quality_min
|
41
|
+
0
|
42
|
+
end
|
43
|
+
def interesting_min
|
44
|
+
0
|
45
|
+
end
|
46
|
+
def learned_min
|
47
|
+
0
|
48
|
+
end
|
49
|
+
def quality_col
|
50
|
+
"cached_weighted_quality_score"
|
51
|
+
end
|
52
|
+
def interesting_col
|
53
|
+
"cached_weighted_interesting_score"
|
54
|
+
end
|
55
|
+
def learned_col
|
56
|
+
"cached_weighted_learnedfrom_score"
|
57
|
+
end
|
58
|
+
def votes_col
|
59
|
+
"cached_votes_total"
|
60
|
+
end
|
61
|
+
def blended_col
|
62
|
+
|
63
|
+
#"(#{quality_col} + #{interesting_col})"
|
64
|
+
cols = []
|
65
|
+
CustomSort.blended_columns.each do |col|
|
66
|
+
cols.push send(col+"_col")
|
67
|
+
end
|
68
|
+
"(#{cols.join(" + ")})"
|
69
|
+
end
|
70
|
+
def blended_where vote_filter_col
|
71
|
+
cols = []
|
72
|
+
CustomSort.blended_columns.each do |col|
|
73
|
+
cols.push(send(col+"_col") + " > " + send(col+"_min").to_s)
|
74
|
+
end
|
75
|
+
|
76
|
+
#["#{vote_filter_col} = ? AND #{quality_col} > ? AND #{interesting_col} > ?",@class_name, quality_min, interesting_min]
|
77
|
+
["#{vote_filter_col} = ? AND #{cols.join(" AND ")}",@class_name]
|
78
|
+
end
|
79
|
+
|
80
|
+
def data_table
|
81
|
+
CustomSort.data_table
|
82
|
+
end
|
83
|
+
|
84
|
+
def order_clause
|
85
|
+
case query_name
|
86
|
+
when "quality"
|
87
|
+
sort_col = quality_col
|
88
|
+
when "interesting"
|
89
|
+
sort_col = interesting_col
|
90
|
+
when "learned"
|
91
|
+
sort_col = learned_col
|
92
|
+
when "blended"
|
93
|
+
sort_col = blended_col
|
94
|
+
when "blended_recent"
|
95
|
+
sort_col = "(#{time_decay}*#{blended_col})"
|
96
|
+
when "nofeedback","nojoin"
|
97
|
+
sort_col = "created_at"
|
98
|
+
else
|
99
|
+
sort_col = votes_col
|
100
|
+
end
|
101
|
+
sort_col + " DESC"
|
102
|
+
end
|
103
|
+
|
104
|
+
def join_clause
|
105
|
+
#tbl_name = @relation.class.name.pluralize.downcase #.klass.pluralize.downcase
|
106
|
+
"LEFT JOIN #{data_table} ON #{data_table}.resource_id = #{@table_name}.id"
|
107
|
+
end
|
108
|
+
|
109
|
+
def group_clause
|
110
|
+
#time_zone = @time_zone.tzinfo.name
|
111
|
+
#adapter_name = @relation.connection.adapter_name
|
112
|
+
query = ["id = ?",1]
|
113
|
+
clause = @relation.send(:sanitize_sql_array, query)
|
114
|
+
end
|
115
|
+
|
116
|
+
def where_clause
|
117
|
+
vote_filter_col = "#{data_table}.resource_type"
|
118
|
+
#base_query = ["vote_caches.resource_type = ?",@class_name]
|
119
|
+
case query_name
|
120
|
+
when "blended", "blended_recent"
|
121
|
+
query = blended_where vote_filter_col
|
122
|
+
when "interesting"
|
123
|
+
query = ["#{vote_filter_col} = ? AND #{interesting_col} > ?",@class_name, interesting_min]
|
124
|
+
when "learned"
|
125
|
+
query = ["#{vote_filter_col} = ? AND #{learned_col} > ?",@class_name, learned_min]
|
126
|
+
when "quality"
|
127
|
+
query = ["#{vote_filter_col} = ? AND #{quality_col} > ?",@class_name, quality_min]
|
128
|
+
when "nofeedback"
|
129
|
+
query = ["(#{vote_filter_col} = ? AND #{votes_col} = ?) OR #{data_table}.id is NULL",@class_name,0]
|
130
|
+
when "nojoin"
|
131
|
+
query = ["id > ?",0]
|
132
|
+
else
|
133
|
+
query = ["#{vote_filter_col} = ?",@class_name]
|
134
|
+
end
|
135
|
+
|
136
|
+
clause = @relation.send(:sanitize_sql_array, query)
|
137
|
+
end
|
138
|
+
|
139
|
+
# resolves eagerly
|
140
|
+
# need to convert both where_clause (easy)
|
141
|
+
# and group_clause (not easy) if want to avoid this
|
142
|
+
def resolve_column(relation, column)
|
143
|
+
node = relation.send(:relation).send(:arel_columns, [column]).first
|
144
|
+
node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
|
145
|
+
relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
|
146
|
+
end
|
147
|
+
|
148
|
+
def resolve_table(relation)
|
149
|
+
node = relation.send(:relation).table_name #relation.send(:relation).klass
|
150
|
+
node
|
151
|
+
end
|
152
|
+
def resolve_class(relation)
|
153
|
+
relation.send(:relation).klass
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_sort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.02
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Merritt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: It allows you to sort by predefined basic algorithms
|
42
|
+
email:
|
43
|
+
- dev@meritocracyconsulting.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CODE_OF_CONDUCT.md
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- custom_sort-0.1.0.gem
|
56
|
+
- custom_sort-0.1.01.gem
|
57
|
+
- custom_sort-0.1.02.gem
|
58
|
+
- custom_sort-0.1.03.gem
|
59
|
+
- custom_sort-0.2.00.gem
|
60
|
+
- custom_sort-0.2.01.gem
|
61
|
+
- custom_sort-0.3.00.gem
|
62
|
+
- custom_sort-0.4.00.gem
|
63
|
+
- custom_sort-0.4.01.gem
|
64
|
+
- custom_sort.gemspec
|
65
|
+
- lib/custom_sort.rb
|
66
|
+
- lib/custom_sort/active_record.rb
|
67
|
+
- lib/custom_sort/enumerable.rb
|
68
|
+
- lib/custom_sort/magic.rb
|
69
|
+
- lib/custom_sort/magic_new.rb
|
70
|
+
- lib/custom_sort/query_methods.rb
|
71
|
+
- lib/custom_sort/relation.rb
|
72
|
+
- lib/custom_sort/relation_builder.rb
|
73
|
+
- lib/custom_sort/version.rb
|
74
|
+
homepage: https://github.com/scottcmerritt/custom_sort
|
75
|
+
licenses: []
|
76
|
+
metadata:
|
77
|
+
allowed_push_host: https://rubygems.org
|
78
|
+
homepage_uri: https://github.com/scottcmerritt/custom_sort
|
79
|
+
source_code_uri: https://github.com/scottcmerritt/custom_sort.git
|
80
|
+
changelog_uri: https://github.com/scottcmerritt/custom_sort/blob/master/CHANGELOG.md
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.0.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A gem for sorting data
|
100
|
+
test_files: []
|