most_related 0.0.4 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b655ee05c62fde06ac5da9dbb806d5b2d46aa2e8
4
- data.tar.gz: b716b9ac1370e5fa5f31a1cb6c12a3fa0267c397
3
+ metadata.gz: 9df9224092e0d94960866ace4c1d4e9c451396a1
4
+ data.tar.gz: a96d26680751aa63103a65165d42f3bf63db21fb
5
5
  SHA512:
6
- metadata.gz: 652d59f88d1a0dfbf6b782dbfa6c9b4cd6d5b61d6eb812470aa5398771ec63f9b53f145a981d03c7d8bfbac1c087da72f73abada3fc2bd42b34ee7f61374ac32
7
- data.tar.gz: bd8d55d28bb3c534d440d41a5f447994ccc10ec5439730ed2434900fcc4e27d80134e1001a2080debefeb83e9aeaae3c5a03ba6c25c36d1890ed7274fd8a0664
6
+ metadata.gz: 9d4b6204200e7cf6518e7d2633d148b4c3830203c9e681aa872fcd83dfe782797a82e7a637c183cca9a762434adef82169e84a38f851ca7b926f0ae9adf6ab75
7
+ data.tar.gz: 70f89313fde9be828d433c2e65bcbd07bf107a062a2207987d8386578928b76bd0b464922a3e0854ed3f668376a05c51a4c01da2027dbffa12c1e5920e15a56f
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .byebug_history
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.3.3
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MostRelated
2
2
 
3
- `most_related` returns models that have the most many to many associated models in common
3
+ `most_related` returns models that have the most associated models in common
4
4
 
5
5
  ## Installation
6
6
 
@@ -23,7 +23,7 @@ Post example
23
23
  class Post < ActiveRecord::Base
24
24
  has_most_related :authors
25
25
  has_most_related :tags, as: :most_related_by_tags
26
- has_most_related [:authors, :tags], as: :most_related_by_author_or_tag
26
+ has_most_related :authors, :tags, as: :most_related_by_author_or_tag
27
27
 
28
28
  has_many :author_posts
29
29
  has_many :authors, through: :author_posts
@@ -38,28 +38,27 @@ Post example
38
38
  belongs_to :post
39
39
  end
40
40
 
41
- To return the posts with the most authors in common with post, in descending order:
41
+ To return the posts with the most authors in common with `post`, in descending order:
42
42
 
43
43
  post.most_related
44
44
 
45
- To return the posts with the most tags in common with post, in descending order:
45
+ To return the posts with the most tags in common with `post`, in descending order:
46
46
 
47
47
  post.most_related_by_tag
48
48
 
49
- To return the posts with the most authors and tags in common with post, in descending order:
49
+ To return the posts with the most authors and tags in common with `post`, in descending order:
50
50
 
51
51
  post.most_related_by_author_or_tag
52
52
 
53
- The count of the many to many associated models in common is accessible on each returned model
53
+ The count of the associated models in common is accessible on each returned model
54
54
 
55
55
  post.most_related_count
56
56
  post.most_related_by_tag_count
57
57
  post.most_related_by_author_or_tag_count
58
58
 
59
- If multiple many to many associations are used, the syntax is specific to MySql although
60
- it could be made to work with Postgres.
59
+ Note multiple associations do not work with sqlite.
61
60
 
62
- Because of the use of 'group', pagination is not supported.
61
+ Because of the use of `group`, pagination is not supported.
63
62
 
64
63
  ## Contributing
65
64
 
data/lib/most_related.rb CHANGED
@@ -3,14 +3,14 @@ require "most_related/version"
3
3
  module MostRelated
4
4
 
5
5
  # `most_related` returns those models that have the most
6
- # many to many associated models in common
6
+ # associated models in common
7
7
  #
8
8
  # Post example:
9
9
  #
10
10
  # class Post < ActiveRecord::Base
11
11
  # has_most_related :authors
12
12
  # has_most_related :tags, as: :most_related_by_tags
13
- # has_most_related [:authors, :tags], as: :most_related_by_author_or_tag
13
+ # has_most_related :authors, :tags, as: :most_related_by_author_or_tag
14
14
  #
15
15
  # has_many :author_posts
16
16
  # has_many :authors, through: :author_posts
@@ -25,31 +25,25 @@ module MostRelated
25
25
  # belongs_to :post
26
26
  # end
27
27
  #
28
- # To return the posts with the most authors in common with post, in descending order:
28
+ # To return the posts with the most authors in common with `post`, in descending order:
29
29
  # post.most_related
30
30
  #
31
- # To return the posts with the most tags in common with post, in descending order:
31
+ # To return the posts with the most tags in common with `post`, in descending order:
32
32
  # post.most_related_by_tag
33
33
  #
34
- # To return the posts with the most authors and tags in common with post, in descending order:
34
+ # To return the posts with the most authors and tags in common with `post`, in descending order:
35
35
  # post.most_related_by_author_or_tag
36
36
  #
37
- # The count of the many to many associated models in common is accessible on each returned model
37
+ # The count of the associated models in common is accessible on each returned model
38
38
  # eg post.most_related_count, post.most_related_by_tag_count and post.most_related_by_author_or_tag_count
39
39
  #
40
- def has_most_related(many_to_many_associations, as: :most_related)
40
+ def has_most_related(*many_to_many_associations, as: :most_related)
41
41
 
42
42
  # defaults to 'def most_related'
43
43
  define_method as do
44
44
  table_name = self.class.table_name
45
45
  association_scopes = []
46
46
 
47
- many_to_many_associations = if many_to_many_associations.is_a?(Array)
48
- many_to_many_associations
49
- else
50
- [many_to_many_associations]
51
- end
52
-
53
47
  many_to_many_associations.each do |many_to_many_association|
54
48
  assocation = self.class.reflect_on_association(many_to_many_association)
55
49
  join_table, foreign_key, association_foreign_key = self.class.join_table_values(assocation)
@@ -60,17 +54,22 @@ module MostRelated
60
54
  joins("INNER JOIN #{join_table} ON #{join_table}.#{foreign_key} = #{table_name}.id")
61
55
  end
62
56
 
63
- # with postgres, multiple many-to-many associations doesn't work and here's why
64
- # http://dba.stackexchange.com/questions/88988/postgres-error-column-must-appear-in-the-group-by-clause-or-be-used-in-an-aggre
65
57
  scope = self.class.select("#{table_name}.*, count(#{table_name}.id) AS #{as}_count").
66
- where.not(id: self.id).group("#{table_name}.id").order("#{as}_count DESC")
58
+ where.not(id: self.id).order("#{as}_count DESC")
59
+ group_by_clause = 'id'
67
60
 
68
61
  # if there is only one many-to-many association no need to use UNION sql syntax
69
- if association_scopes.size == 1
70
- scope.merge(association_scopes.first)
62
+ if association_scopes.one?
63
+ scope.merge(association_scopes.first).group(group_by_clause)
71
64
  else
65
+ # with postgres the group by clause has to be different
66
+ # http://dba.stackexchange.com/questions/88988/postgres-error-column-must-appear-in-the-group-by-clause-or-be-used-in-an-aggre
67
+ if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
68
+ group_by_clause = self.class.column_names.join(', ')
69
+ end
70
+
72
71
  # see http://blog.ubersense.com/2013/09/27/tech-talk-unioning-scoped-queries-in-rails/
73
- scope.from("((#{association_scopes.map(&:to_sql).join(') UNION ALL (')})) AS #{table_name}")
72
+ scope.from("((#{association_scopes.map(&:to_sql).join(') UNION ALL (')})) AS #{table_name}").group(group_by_clause)
74
73
  end
75
74
  end
76
75
 
@@ -1,3 +1,3 @@
1
1
  module MostRelated
2
- VERSION = "0.0.4"
2
+ VERSION = '0.1.0'
3
3
  end
data/most_related.gemspec CHANGED
@@ -18,13 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.required_ruby_version = '>= 1.9.3'
22
- spec.add_runtime_dependency 'activerecord', '~> 4.0', '>= 4.0.0'
23
- spec.add_development_dependency 'bundler', '~> 1.3'
24
- spec.add_development_dependency 'rspec', '~> 2.14', '>= 2.14.1'
25
- spec.add_development_dependency 'database_cleaner', '~>1.4'
21
+ spec.required_ruby_version = '>= 2.1'
22
+ spec.add_runtime_dependency 'activerecord', '~> 4.2'
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rspec', '~> 3.5'
25
+ spec.add_development_dependency 'database_cleaner', '~>1.5'
26
26
  spec.add_development_dependency 'sqlite3', '~>1.3'
27
- spec.add_development_dependency 'mysql2', '~>0.3'
28
- spec.add_development_dependency 'pg', '~>0.18'
29
- spec.add_development_dependency 'byebug', '~>4.0'
27
+ spec.add_development_dependency 'mysql2', '~>0.4'
28
+ spec.add_development_dependency 'pg', '~>0.19'
29
+ spec.add_development_dependency 'byebug', '~>9.0'
30
30
  end
data/spec/spec_helper.rb CHANGED
@@ -21,7 +21,6 @@ DatabaseCleaner.strategy = :transaction
21
21
 
22
22
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
23
  RSpec.configure do |config|
24
- config.treat_symbols_as_metadata_keys_with_true_values = true
25
24
  config.run_all_when_everything_filtered = true
26
25
  config.filter_run :focus
27
26
 
@@ -1,7 +1,7 @@
1
1
  class Post < ActiveRecord::Base
2
2
  has_most_related :authors
3
3
  has_most_related :tags, as: :most_related_by_tag
4
- has_most_related [:authors, :tags], as: :most_related_by_author_or_tag
4
+ has_most_related :authors, :tags, as: :most_related_by_author_or_tag
5
5
 
6
6
  has_and_belongs_to_many :tags
7
7
  has_many :author_posts
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: most_related
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jolyon Pawlyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-09 00:00:00.000000000 Z
11
+ date: 2016-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,68 +16,56 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 4.0.0
19
+ version: '4.2'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '4.0'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 4.0.0
26
+ version: '4.2'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: bundler
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
- version: '1.3'
33
+ version: '1.13'
40
34
  type: :development
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
38
  - - "~>"
45
39
  - !ruby/object:Gem::Version
46
- version: '1.3'
40
+ version: '1.13'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: rspec
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '2.14'
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 2.14.1
47
+ version: '3.5'
57
48
  type: :development
58
49
  prerelease: false
59
50
  version_requirements: !ruby/object:Gem::Requirement
60
51
  requirements:
61
52
  - - "~>"
62
53
  - !ruby/object:Gem::Version
63
- version: '2.14'
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: 2.14.1
54
+ version: '3.5'
67
55
  - !ruby/object:Gem::Dependency
68
56
  name: database_cleaner
69
57
  requirement: !ruby/object:Gem::Requirement
70
58
  requirements:
71
59
  - - "~>"
72
60
  - !ruby/object:Gem::Version
73
- version: '1.4'
61
+ version: '1.5'
74
62
  type: :development
75
63
  prerelease: false
76
64
  version_requirements: !ruby/object:Gem::Requirement
77
65
  requirements:
78
66
  - - "~>"
79
67
  - !ruby/object:Gem::Version
80
- version: '1.4'
68
+ version: '1.5'
81
69
  - !ruby/object:Gem::Dependency
82
70
  name: sqlite3
83
71
  requirement: !ruby/object:Gem::Requirement
@@ -98,42 +86,42 @@ dependencies:
98
86
  requirements:
99
87
  - - "~>"
100
88
  - !ruby/object:Gem::Version
101
- version: '0.3'
89
+ version: '0.4'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
93
  requirements:
106
94
  - - "~>"
107
95
  - !ruby/object:Gem::Version
108
- version: '0.3'
96
+ version: '0.4'
109
97
  - !ruby/object:Gem::Dependency
110
98
  name: pg
111
99
  requirement: !ruby/object:Gem::Requirement
112
100
  requirements:
113
101
  - - "~>"
114
102
  - !ruby/object:Gem::Version
115
- version: '0.18'
103
+ version: '0.19'
116
104
  type: :development
117
105
  prerelease: false
118
106
  version_requirements: !ruby/object:Gem::Requirement
119
107
  requirements:
120
108
  - - "~>"
121
109
  - !ruby/object:Gem::Version
122
- version: '0.18'
110
+ version: '0.19'
123
111
  - !ruby/object:Gem::Dependency
124
112
  name: byebug
125
113
  requirement: !ruby/object:Gem::Requirement
126
114
  requirements:
127
115
  - - "~>"
128
116
  - !ruby/object:Gem::Version
129
- version: '4.0'
117
+ version: '9.0'
130
118
  type: :development
131
119
  prerelease: false
132
120
  version_requirements: !ruby/object:Gem::Requirement
133
121
  requirements:
134
122
  - - "~>"
135
123
  - !ruby/object:Gem::Version
136
- version: '4.0'
124
+ version: '9.0'
137
125
  description: Adds an instance method to a active record model that returns the most
138
126
  related models based on associated models in common
139
127
  email:
@@ -168,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
156
  requirements:
169
157
  - - ">="
170
158
  - !ruby/object:Gem::Version
171
- version: 1.9.3
159
+ version: '2.1'
172
160
  required_rubygems_version: !ruby/object:Gem::Requirement
173
161
  requirements:
174
162
  - - ">="
@@ -176,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
164
  version: '0'
177
165
  requirements: []
178
166
  rubyforge_project:
179
- rubygems_version: 2.4.5
167
+ rubygems_version: 2.5.2
180
168
  signing_key:
181
169
  specification_version: 4
182
170
  summary: Returns models that have the most associated models in common