active_record_batteries 1.0.0 → 2.0.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 +5 -5
- data/lib/active_record_batteries.rb +0 -2
- data/lib/active_record_batteries/concerns/filterable.rb +1 -1
- data/lib/active_record_batteries/concerns/paginable.rb +12 -8
- data/lib/active_record_batteries/version.rb +1 -1
- data/spec/active_record_batteries_spec.rb +7 -47
- data/spec/spec_helper.rb +11 -8
- metadata +10 -15
- data/lib/active_record_batteries/concerns/relationship_scopes.rb +0 -24
- data/lib/active_record_batteries/engine.rb +0 -5
- data/lib/active_record_batteries/loader.rb +0 -19
- data/lib/tasks/active_record_batteries_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 159a2a4a4681bde175fe4f840828e8cccc02730a920dfacfc90357dfb46dea45
|
4
|
+
data.tar.gz: 2688b3784eb100bd085a441a7ff75f60f26856d8fe2196af511ed2b6f6c6e1bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b70cd34525c873fe0ea4b554445e8139dc21ee1f491077952b5168ba5f814308515d8a04b8b47442a1101c5dfb673227ec4853e80dabc3cb9f8c5f1980dd866
|
7
|
+
data.tar.gz: 8426aa0d792c5c5c9682f8768df908509d683fa6f6bb459acd7a62f4fc529c3965f449d86ba0fa17fcc9d70172d10a55697bb52b8b4df58676c9d037ef83f860
|
@@ -5,11 +5,15 @@ module ActiveRecordBatteries
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
# Default items per page
|
8
|
-
|
8
|
+
items_per_page 25
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
klass = self
|
11
|
+
|
12
|
+
scope :paginate, lambda { |page, items_per_page = nil|
|
13
|
+
items_per_page ||= klass.instance_variable_get(:@items_per_page)
|
14
|
+
|
15
|
+
limit(items_per_page)
|
16
|
+
.offset(([page.to_i, 1].max - 1) * items_per_page)
|
13
17
|
.extending do
|
14
18
|
|
15
19
|
def current_page
|
@@ -42,13 +46,13 @@ module ActiveRecordBatteries
|
|
42
46
|
end
|
43
47
|
|
44
48
|
module ClassMethods
|
45
|
-
def
|
46
|
-
@
|
49
|
+
def items_per_page(items)
|
50
|
+
@items_per_page = items
|
47
51
|
end
|
48
52
|
|
49
|
-
def pages(
|
53
|
+
def pages(items_per_page = @items_per_page)
|
50
54
|
(all.except(:offset, :limit, :order, :includes)
|
51
|
-
.distinct(:id).count /
|
55
|
+
.distinct(:id).count / items_per_page.to_f).ceil
|
52
56
|
end
|
53
57
|
end
|
54
58
|
end
|
@@ -107,7 +107,7 @@ RSpec.describe ActiveRecordBatteries do
|
|
107
107
|
|
108
108
|
context "Paginable" do
|
109
109
|
it "should set items per page" do
|
110
|
-
expect(Author.
|
110
|
+
expect(Author.items_per_page(2)).to eql(2)
|
111
111
|
end
|
112
112
|
|
113
113
|
it "should count pages" do
|
@@ -147,54 +147,14 @@ RSpec.describe ActiveRecordBatteries do
|
|
147
147
|
end
|
148
148
|
end
|
149
149
|
|
150
|
-
context "
|
151
|
-
|
152
|
-
|
153
|
-
end
|
154
|
-
|
155
|
-
# Scopes should be added
|
156
|
-
[ :with_articles, :include_articles, :and_articles,
|
157
|
-
:pload_articles, :eload_articles ].each do |method|
|
158
|
-
it "should respond to #{method}" do
|
159
|
-
expect(Author.respond_to?(method)).to be true
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
it "should not have articles preloaded" do
|
164
|
-
author = Author.first
|
165
|
-
expect(author.articles.loaded?).to be false
|
166
|
-
end
|
167
|
-
|
168
|
-
it "should have articles included" do
|
169
|
-
author = Author.include_articles.first
|
170
|
-
|
171
|
-
expect(author.articles.loaded?).to be true
|
172
|
-
end
|
173
|
-
|
174
|
-
it "should have articles preloaded" do
|
175
|
-
author = Author.pload_articles.first
|
176
|
-
|
177
|
-
expect(author.articles.loaded?).to be true
|
178
|
-
end
|
179
|
-
|
180
|
-
it "should have articles eager loaded" do
|
181
|
-
author = Author.eload_articles.first
|
182
|
-
|
183
|
-
expect(author.articles.loaded?).to be true
|
184
|
-
end
|
185
|
-
|
186
|
-
it "should join but not preload" do
|
187
|
-
author = Author.with_articles.first
|
188
|
-
|
189
|
-
expect(author.articles.loaded?).to be false
|
190
|
-
end
|
191
|
-
|
192
|
-
it "should inner join" do
|
193
|
-
expect(Author.and_articles.where(name: "articleless")).to be_empty
|
150
|
+
context "Sluggable" do
|
151
|
+
it "should have a slug" do
|
152
|
+
expect(Article.create(title: "Hello, world").slug).to eq("hello-world")
|
194
153
|
end
|
195
154
|
|
196
|
-
it "should
|
197
|
-
expect
|
155
|
+
it "should qualify slugs" do
|
156
|
+
expect { Article.create!(title: "Hello, world") }.to_not raise_error
|
157
|
+
expect { Article.create!(title: "Hello, world") }.to_not raise_error
|
198
158
|
end
|
199
159
|
end
|
200
160
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -32,24 +32,27 @@ ActiveRecord::Schema.define do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
class Author < ActiveRecord::Base
|
35
|
-
|
35
|
+
include ActiveRecordBatteries::Concerns::Sluggable,
|
36
|
+
ActiveRecordBatteries::Concerns::Paginable,
|
37
|
+
ActiveRecordBatteries::Concerns::Filterable
|
36
38
|
|
37
39
|
has_many :articles
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
relationship_scopes :articles
|
41
|
+
filter_by :by_slug
|
42
42
|
end
|
43
43
|
|
44
44
|
class Article < ActiveRecord::Base
|
45
|
-
|
45
|
+
include ActiveRecordBatteries::Concerns::Sluggable,
|
46
|
+
ActiveRecordBatteries::Concerns::Paginable,
|
47
|
+
ActiveRecordBatteries::Concerns::Filterable,
|
48
|
+
ActiveRecordBatteries::Concerns::Deletable
|
46
49
|
|
47
50
|
belongs_to :author
|
48
51
|
|
49
52
|
slug_base_column :title
|
50
53
|
|
51
|
-
|
52
|
-
|
54
|
+
filter_by :by_slug
|
55
|
+
filter_by :by_title, ->(title) { where(title: title) }
|
53
56
|
|
54
|
-
|
57
|
+
items_per_page 5
|
55
58
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_batteries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francisco Soto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '5'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.3.10
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.3.10
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
description: Several small active record modules to empower models in a simple way.
|
56
56
|
email:
|
57
|
-
- ebobby@
|
57
|
+
- ebobby@ebobby.org
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
@@ -66,12 +66,8 @@ files:
|
|
66
66
|
- lib/active_record_batteries/concerns/deletable.rb
|
67
67
|
- lib/active_record_batteries/concerns/filterable.rb
|
68
68
|
- lib/active_record_batteries/concerns/paginable.rb
|
69
|
-
- lib/active_record_batteries/concerns/relationship_scopes.rb
|
70
69
|
- lib/active_record_batteries/concerns/sluggable.rb
|
71
|
-
- lib/active_record_batteries/engine.rb
|
72
|
-
- lib/active_record_batteries/loader.rb
|
73
70
|
- lib/active_record_batteries/version.rb
|
74
|
-
- lib/tasks/active_record_batteries_tasks.rake
|
75
71
|
- spec/active_record_batteries_spec.rb
|
76
72
|
- spec/spec_helper.rb
|
77
73
|
homepage: https://github.com/ebobby/active_record_batteries
|
@@ -93,8 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
89
|
- !ruby/object:Gem::Version
|
94
90
|
version: '0'
|
95
91
|
requirements: []
|
96
|
-
|
97
|
-
rubygems_version: 2.4.5
|
92
|
+
rubygems_version: 3.0.1
|
98
93
|
signing_key:
|
99
94
|
specification_version: 4
|
100
95
|
summary: ActiveRecord with Batteries. Extensions to make life easier.
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module ActiveRecordBatteries
|
2
|
-
module Concerns
|
3
|
-
module RelationshipScopes
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
module ClassMethods
|
7
|
-
def relationship_scopes(relationship, suffix = nil)
|
8
|
-
suffix ||= relationship
|
9
|
-
|
10
|
-
with_scope_name = "with_#{ suffix }".to_sym
|
11
|
-
include_scope_name = "include_#{ suffix }".to_sym
|
12
|
-
|
13
|
-
scope with_scope_name, -> { joins(relationship) }
|
14
|
-
scope include_scope_name, -> { includes(relationship) }
|
15
|
-
scope "and_#{ suffix }".to_sym, -> { send(with_scope_name).send(include_scope_name) }
|
16
|
-
scope "pload_#{ suffix }".to_sym, -> { preload(relationship) }
|
17
|
-
scope "eload_#{ suffix }".to_sym, -> { eager_load(relationship) }
|
18
|
-
|
19
|
-
nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module ActiveRecordBatteries
|
2
|
-
module Loader
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def batteries! (*modules)
|
7
|
-
modules = modules.is_a?(Array) ? modules : [ modules ]
|
8
|
-
|
9
|
-
modules.each.map{|i| i.to_s.camelize }.each do |mod|
|
10
|
-
include ActiveRecordBatteries::Concerns.const_get(mod)
|
11
|
-
end
|
12
|
-
|
13
|
-
nil
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
ActiveRecord::Base.send(:include, self)
|
18
|
-
end
|
19
|
-
end
|