active_similar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/lib/active_similar/association.rb +42 -0
- data/lib/active_similar/query.rb +103 -0
- data/lib/active_similar/version.rb +5 -0
- data/lib/active_similar.rb +29 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b84485dfe06d69d270e19a691a5fac3c17256b1c75ec7a568985ec316751151d
|
4
|
+
data.tar.gz: c38a0965fc70a4dbeb5617d06b6997ef70fc89cbc9d25803ff75f243ac4236d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac4ac2655afc7135e898d1c97cb168e811e23a27546ac69521d47de6be99dc9178a657933abb610afc218e872eec04de657125d115919ae2a7acdd61e61eed20
|
7
|
+
data.tar.gz: 75ce8ec70f92b2063ed498887c242e4778f65518a2a48417339a8ca9c8099c4454626c4729e8dbc12c4e20c38d81b358438f5f30b0ed741d2b5786c46353fe86
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Code of Conduct
|
2
|
+
|
3
|
+
All participants of this project are expected to abide by our Code of Conduct, both online and during in-person events that are hosted and/or associated with this project.
|
4
|
+
|
5
|
+
## The Pledge
|
6
|
+
|
7
|
+
In the interest of fostering an open and welcoming environment, we pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
8
|
+
|
9
|
+
## The Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to creating a positive environment include:
|
12
|
+
|
13
|
+
* Using welcoming and inclusive language
|
14
|
+
* Being respectful of differing viewpoints and experiences
|
15
|
+
* Referring to people by their preferred pronouns and using gender-neutral pronouns when uncertain
|
16
|
+
* Gracefully accepting constructive criticism
|
17
|
+
* Focusing on what is best for the community
|
18
|
+
* Showing empathy towards other community members
|
19
|
+
|
20
|
+
Examples of unacceptable behavior by participants include:
|
21
|
+
|
22
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
23
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
26
|
+
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
27
|
+
* Dismissing or attacking inclusion-oriented requests
|
28
|
+
|
29
|
+
## Enforcement
|
30
|
+
|
31
|
+
Violations of the Code of Conduct may be reported by sending an email to info@hardpixel.eu. All reports will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. Further details of specific enforcement policies may be posted separately.
|
32
|
+
|
33
|
+
We hold the right and responsibility to remove comments or other contributions that are not aligned to this Code of Conduct, or to suspend temporarily or permanently any members for other behaviors that are inappropriate, threatening, offensive, or harmful.
|
34
|
+
|
35
|
+
## Attribution
|
36
|
+
|
37
|
+
This Code of Conduct is adapted from [dev.to](https://dev.to/code-of-conduct).
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Jonian Guveli
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Active Similar
|
2
|
+
|
3
|
+
Ruby gem to find similar Active Record models through most common associations.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/active_similar.svg)](https://badge.fury.io/rb/active_similar)
|
6
|
+
[![Build](https://github.com/hardpixel/active-similar/actions/workflows/build.yml/badge.svg)](https://github.com/hardpixel/active-similar/actions/workflows/build.yml)
|
7
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/9070ea120ceeddbcc1d2/maintainability)](https://codeclimate.com/github/hardpixel/active-similar/maintainability)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'active_similar'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install active_similar
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'active_similar'
|
29
|
+
|
30
|
+
class Post < ActiveRecord::Base
|
31
|
+
include ActiveSimilar
|
32
|
+
|
33
|
+
has_many :categories
|
34
|
+
has_many :tags
|
35
|
+
|
36
|
+
scope :published, -> { where status: 'published' }
|
37
|
+
scope :featured, -> { where featured: true }
|
38
|
+
|
39
|
+
has_similar :related, through: %i[categories tags], scope: :published
|
40
|
+
end
|
41
|
+
|
42
|
+
post = Post.find(1)
|
43
|
+
post.related # Returns similar posts to post #1
|
44
|
+
|
45
|
+
similar = ActiveSimilar::Query.new(Post.published, through: %i[categories tags])
|
46
|
+
similar.with(2) # Returns similar posts to post #2
|
47
|
+
|
48
|
+
similar = ActiveSimilar::Query.new(Post.featured, through: %i[categories])
|
49
|
+
similar.with(3) # Returns similar posts to post #3
|
50
|
+
```
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
55
|
+
|
56
|
+
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).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/active-similar.
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSimilar
|
4
|
+
# Builds association subquery.
|
5
|
+
class Association
|
6
|
+
attr_reader :klass, :reflection, :other
|
7
|
+
|
8
|
+
delegate :foreign_key, :through_reflection,
|
9
|
+
to: :reflection
|
10
|
+
|
11
|
+
delegate :klass, :name, :table_name, :foreign_key,
|
12
|
+
to: :through_reflection,
|
13
|
+
prefix: :through
|
14
|
+
|
15
|
+
# Returns a new instance of Association.
|
16
|
+
# @param name [Symbol]
|
17
|
+
# @param klass [ActiveRecord::Base]
|
18
|
+
# @param other [Integer ActiveRecord::Base]
|
19
|
+
|
20
|
+
def initialize(name, klass, other)
|
21
|
+
@klass = klass
|
22
|
+
@reflection = klass.reflect_on_association(name)
|
23
|
+
@other = other
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the association join scope.
|
27
|
+
# @return [ActiveRecord::Relation]
|
28
|
+
def scope
|
29
|
+
klass
|
30
|
+
.joins(through_name)
|
31
|
+
.where(through_table_name => { foreign_key => subquery })
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def subquery
|
37
|
+
through_klass
|
38
|
+
.select(foreign_key)
|
39
|
+
.where(through_foreign_key => other)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_similar/association'
|
4
|
+
|
5
|
+
module ActiveSimilar
|
6
|
+
# Queries similar models through most common associations.
|
7
|
+
class Query
|
8
|
+
attr_reader :scope, :through, :prefix
|
9
|
+
|
10
|
+
delegate :klass, :table, :select_values,
|
11
|
+
to: :scope
|
12
|
+
|
13
|
+
delegate :primary_key, :column_names,
|
14
|
+
to: :klass
|
15
|
+
|
16
|
+
# Returns a new instance of Query.
|
17
|
+
# @param scope [ActiveRecord::Relation]
|
18
|
+
# @param through [Symbol Array<Symbol>]
|
19
|
+
# @param prefix [String]
|
20
|
+
# @raise [TypeError] if scope has invalid type
|
21
|
+
|
22
|
+
def initialize(scope, through:, prefix: 'similar')
|
23
|
+
relation = ActiveRecord::Relation
|
24
|
+
scope.is_a?(relation) || raise(TypeError, "scope must be #{relation}")
|
25
|
+
|
26
|
+
@scope = scope
|
27
|
+
@through = Array(through)
|
28
|
+
@prefix = prefix
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns records similar to other record.
|
32
|
+
# @param other [Integer ActiveRecord::Base]
|
33
|
+
# @return [ActiveRecord::Relation]
|
34
|
+
|
35
|
+
def with(other)
|
36
|
+
scope
|
37
|
+
.merge(subquery(other))
|
38
|
+
.where.not(primary_key => other)
|
39
|
+
.select(*select_columns)
|
40
|
+
.group(*group_columns)
|
41
|
+
.order(count_column => :desc)
|
42
|
+
end
|
43
|
+
|
44
|
+
def inspect # :nodoc:
|
45
|
+
"#<#{self.class.name} model=#{klass.name} through=#{through} prefix=#{prefix}>"
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def count_column
|
51
|
+
"#{prefix}_#{table.name}_count"
|
52
|
+
end
|
53
|
+
|
54
|
+
def columns
|
55
|
+
names = column_names
|
56
|
+
names &= select_values.map(&:to_s) if select_values.any?
|
57
|
+
|
58
|
+
names
|
59
|
+
end
|
60
|
+
|
61
|
+
def group_columns
|
62
|
+
columns.map { |col| table[col] }
|
63
|
+
end
|
64
|
+
|
65
|
+
def select_columns
|
66
|
+
counter = table[primary_key].count.as(count_column)
|
67
|
+
clauses = [counter.to_sql]
|
68
|
+
clauses << table[Arel.star] if select_values.none?
|
69
|
+
|
70
|
+
clauses
|
71
|
+
end
|
72
|
+
|
73
|
+
def build_association(name, other)
|
74
|
+
assoc = Association.new(name, klass, other)
|
75
|
+
return assoc.scope if select_values.none?
|
76
|
+
|
77
|
+
assoc.scope.select(*group_columns)
|
78
|
+
end
|
79
|
+
|
80
|
+
def build_union(left, right)
|
81
|
+
left = Arel::Nodes::Grouping.new(left.arel.ast)
|
82
|
+
right = Arel::Nodes::Grouping.new(right.arel.ast)
|
83
|
+
|
84
|
+
klass.unscoped.from(
|
85
|
+
Arel::Nodes::TableAlias.new(
|
86
|
+
Arel::Nodes::UnionAll.new(left, right),
|
87
|
+
table.name
|
88
|
+
)
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
def subquery(other)
|
93
|
+
items = through.map { |name| build_association(name, other) }
|
94
|
+
query = items.delete_at(0)
|
95
|
+
|
96
|
+
items.each do |item|
|
97
|
+
query = build_union(query, item)
|
98
|
+
end
|
99
|
+
|
100
|
+
query
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
require 'active_similar/query'
|
6
|
+
require 'active_similar/version'
|
7
|
+
|
8
|
+
# Find similar Active Record models through most common associations.
|
9
|
+
module ActiveSimilar
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
# Defines method to retrieve similar records.
|
14
|
+
# @param name [Symbol]
|
15
|
+
# @param through [Symbol Array<Symbol>]
|
16
|
+
# @param class_name [ActiveRecord::Base]
|
17
|
+
# @param scope [Symbol]
|
18
|
+
|
19
|
+
def has_similar(name, through:, class_name: self, scope: :all)
|
20
|
+
klass = const_get(class_name.to_s)
|
21
|
+
scope = klass.send(scope)
|
22
|
+
|
23
|
+
define_method(name) do
|
24
|
+
similar = Query.new(scope, through: through, prefix: name)
|
25
|
+
similar.with(id)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_similar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonian Guveli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
description: Find similar Active Record models through most common associations.
|
70
|
+
email:
|
71
|
+
- jonian@hardpixel.eu
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- CODE_OF_CONDUCT.md
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- lib/active_similar.rb
|
80
|
+
- lib/active_similar/association.rb
|
81
|
+
- lib/active_similar/query.rb
|
82
|
+
- lib/active_similar/version.rb
|
83
|
+
homepage: https://github.com/hardpixel/active_similar
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '2.6'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.3.7
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Find similar Active Record models through their associations
|
106
|
+
test_files: []
|