hello_code_active_record_utils 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +19 -0
- data/lib/hello_code/active_record_utils.rb +9 -0
- data/lib/hello_code/active_record_utils/concerns.rb +13 -0
- data/lib/hello_code/active_record_utils/concerns/deletable.rb +20 -0
- data/lib/hello_code/active_record_utils/concerns/filterable.rb +33 -0
- data/lib/hello_code/active_record_utils/concerns/paginable.rb +58 -0
- data/lib/hello_code/active_record_utils/concerns/relationship_scopes.rb +27 -0
- data/lib/hello_code/active_record_utils/concerns/sluggable.rb +62 -0
- data/lib/hello_code/active_record_utils/engine.rb +7 -0
- data/lib/hello_code/active_record_utils/loader.rb +21 -0
- data/lib/hello_code/active_record_utils/version.rb +5 -0
- data/lib/hello_code_active_record_utils.rb +1 -0
- data/lib/tasks/hello_code_active_record_utils_tasks.rake +4 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f0f3fd33a559a96b97434f86b16faafa95eecb8
|
4
|
+
data.tar.gz: c3919fe1cbd95bbce6a23d669fc80977db585c2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0d1945f8465710cc82a76d7567699a39e36bbf11ac9269af54c0f98bea1ffcbc95feced4303aa620ae93f0f4598991afd2bd6060b3da1bb49dbb83a4502d2ec
|
7
|
+
data.tar.gz: 8e3ed0200b7a7c4ac3e18e838f5ff291e9c1d12ec8adf9a07be35cdd1ebfbe180c843b696250699444a744c0a04b25e5f11a1439f8cfb8a37e84b3b7b1429292
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'HelloCodeActiveRecordUtils'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
load 'rails/tasks/statistics.rake'
|
18
|
+
|
19
|
+
Bundler::GemHelper.install_tasks
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
extend ActiveSupport::Autoload
|
5
|
+
|
6
|
+
autoload :Deletable, 'hello_code/active_record_utils/concerns/deletable'
|
7
|
+
autoload :Filterable, 'hello_code/active_record_utils/concerns/filterable'
|
8
|
+
autoload :Paginable, 'hello_code/active_record_utils/concerns/paginable'
|
9
|
+
autoload :Sluggable, 'hello_code/active_record_utils/concerns/sluggable'
|
10
|
+
autoload :RelationshipScopes, 'hello_code/active_record_utils/concerns/relationship_scopes'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
module Deletable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
default_scope { where(deleted_at: nil) }
|
9
|
+
|
10
|
+
scope :including_deleted, -> { unscope(where: :deleted_at) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete!
|
14
|
+
self.deleted_at = Time.current
|
15
|
+
self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
module Filterable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
scope :filtered, lambda { |values|
|
9
|
+
current = all
|
10
|
+
filters.each do |key, method|
|
11
|
+
if values.try(:key?, key)
|
12
|
+
current = current.send(method, values[key]) if
|
13
|
+
current.respond_to?(method)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
current
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def add_filter(filter_key, method_name, callable = nil)
|
22
|
+
scope(method_name, callable) if callable
|
23
|
+
filters[filter_key] = method_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def filters
|
27
|
+
@filters ||= {}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
module Paginable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
# Default items per page
|
9
|
+
items_per_page 25
|
10
|
+
|
11
|
+
scope :paginate, lambda { |page, items_per_page = @items_per_page|
|
12
|
+
limit(items_per_page)
|
13
|
+
.offset(([page.to_i, 1].max - 1) * items_per_page)
|
14
|
+
.extending do
|
15
|
+
|
16
|
+
def current_page
|
17
|
+
@current_page ||=
|
18
|
+
if total_pages.zero?
|
19
|
+
0
|
20
|
+
else
|
21
|
+
(offset_value / limit_value.to_f).ceil + 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# If pagination is used this
|
26
|
+
def total_items
|
27
|
+
@total_items ||=
|
28
|
+
begin
|
29
|
+
total_rows = except(:offset, :limit, :order)
|
30
|
+
total_rows = total_rows.except(:includes) if eager_loading?
|
31
|
+
total_rows.distinct(:id).count
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def total_pages
|
36
|
+
@total_pages ||=
|
37
|
+
begin
|
38
|
+
(total_items / limit_value.to_f).ceil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
def items_per_page(items)
|
47
|
+
@items_per_page = items
|
48
|
+
end
|
49
|
+
|
50
|
+
def pages
|
51
|
+
(all.except(:offset, :limit, :order, :includes)
|
52
|
+
.distinct(:id).count / @items_per_page.to_f).ceil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
module RelationshipScopes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def relationship_scopes(relationship, suffix = nil)
|
9
|
+
suffix ||= relationship
|
10
|
+
|
11
|
+
with_scope_name = "with_#{ suffix }".to_sym
|
12
|
+
include_scope_name = "include_#{ suffix }".to_sym
|
13
|
+
and_scope_name = "and_#{ suffix }".to_sym
|
14
|
+
|
15
|
+
scope with_scope_name, -> { joins(relationship) }
|
16
|
+
scope include_scope_name, -> { includes(relationship) }
|
17
|
+
scope and_scope_name, -> { joins(relationship) }
|
18
|
+
|
19
|
+
scope load_scope_name, -> { preload(relationship) }
|
20
|
+
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtils
|
3
|
+
module Concerns
|
4
|
+
module Sluggable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
# Be sure to create a valid slug before validating and saving.
|
9
|
+
before_validation :set_slug, on: [:create, :update]
|
10
|
+
after_validation :delete_slug_errors
|
11
|
+
|
12
|
+
# Scope to query by slug
|
13
|
+
scope :by_slug, ->(slug) { where(slug: slug) }
|
14
|
+
|
15
|
+
# Slug must be present
|
16
|
+
validates :slug, presence: true, length: { maximum: 256 }
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
# Find by slug
|
21
|
+
def find_by_slug(slug)
|
22
|
+
by_slug(slug).take
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_param
|
27
|
+
slug
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def set_slug
|
33
|
+
self.slug = qualify_slug(name.parameterize) if
|
34
|
+
name.present? && slug.blank?
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete_slug_errors
|
38
|
+
errors.delete(:slug)
|
39
|
+
end
|
40
|
+
|
41
|
+
def qualify_slug(base_slug)
|
42
|
+
tentative_slug = base_slug
|
43
|
+
while duplicate_slug?(tentative_slug)
|
44
|
+
tentative_slug = generate_slug_name(base_slug)
|
45
|
+
end
|
46
|
+
tentative_slug
|
47
|
+
end
|
48
|
+
|
49
|
+
def duplicate_slug?(slug)
|
50
|
+
self.class
|
51
|
+
.where.not(id: id)
|
52
|
+
.by_slug(slug)
|
53
|
+
.exists?
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_slug_name(slug)
|
57
|
+
"#{slug}-#{SecureRandom.random_number(10_000)}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module HelloCode
|
2
|
+
module ActiveRecordUtil
|
3
|
+
module Loader
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def utility! (*modules)
|
8
|
+
modules = modules.is_a?(Array) ? modules : [ modules ]
|
9
|
+
|
10
|
+
modules.each.map{|i| i.to_s.camelize }.each do |mod|
|
11
|
+
include HelloCode::ActiveRecordUtils::Concerns.const_get(mod)
|
12
|
+
end
|
13
|
+
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Base.send(:include, self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "hello_code/active_record_utils"
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hello_code_active_record_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francisco Soto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- bobby@hellocode.mx
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/hello_code/active_record_utils.rb
|
52
|
+
- lib/hello_code/active_record_utils/concerns.rb
|
53
|
+
- lib/hello_code/active_record_utils/concerns/deletable.rb
|
54
|
+
- lib/hello_code/active_record_utils/concerns/filterable.rb
|
55
|
+
- lib/hello_code/active_record_utils/concerns/paginable.rb
|
56
|
+
- lib/hello_code/active_record_utils/concerns/relationship_scopes.rb
|
57
|
+
- lib/hello_code/active_record_utils/concerns/sluggable.rb
|
58
|
+
- lib/hello_code/active_record_utils/engine.rb
|
59
|
+
- lib/hello_code/active_record_utils/loader.rb
|
60
|
+
- lib/hello_code/active_record_utils/version.rb
|
61
|
+
- lib/hello_code_active_record_utils.rb
|
62
|
+
- lib/tasks/hello_code_active_record_utils_tasks.rake
|
63
|
+
homepage: https://github.com/HelloCodeMX/active_record_utils
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: ActiveRecord extensions to make life easier.
|
87
|
+
test_files: []
|