calib-rails 0.1.2 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/calib/delegatable.rb +14 -0
- data/lib/calib/devise/friendly_forwardable.rb +32 -0
- data/lib/calib/devise/soft_deletion.rb +82 -0
- data/lib/calib/devise/soft_deletion_migration_enhancable.rb +15 -0
- data/lib/calib/illegal_access_error.rb +3 -0
- data/lib/calib/pundit/controller_nestable.rb +34 -0
- data/lib/calib/pundit/nestable.rb +27 -0
- data/lib/calib/rails/version.rb +1 -1
- data/lib/calib/rails.rb +22 -3
- data/lib/calib/service.rb +9 -0
- data/lib/calib/soft_deletion/migration_enhancable.rb +20 -0
- data/lib/calib/unauthenticated_error.rb +3 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a169448262ad90005944b50c2a715536f06f14ce4c3f7c9410b8c626cb06153d
|
4
|
+
data.tar.gz: 0a8a6d38c66d06ba4aa7cad08688627e912ee52913cfc5fe1f4654013726583e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aedee16b3782063afeb706fea29e76be213b916997b2b0ad8867f185aac994f5fb08fc1594711a2d5510c4ff7876732e6fa0df5ce6a8706d4412a69612f1da0
|
7
|
+
data.tar.gz: 681cf919772a6c90f154d6d539c8bb9dbebc1959d0e8a9c151e394b431fa0fcc34e9a5db07325be22f13aafdb98fd888f520feaeba2dcf0f3599044c4090e980
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Calib::Delegatable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
def self.delegate_all(targets)
|
6
|
+
targets = Array(targets)
|
7
|
+
define_method('method_missing') do |method, *args|
|
8
|
+
target = targets.find {|target| target.respond_to?(method) }
|
9
|
+
return target.send(method, *args) if target.present?
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Calib::Devise::FriendlyForwardable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
# @see https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update
|
6
|
+
before_action :store_access_location!, if: :storable_location?
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
# path with stored_location or root
|
12
|
+
def smart_path(r = devise_key_for_store_location)
|
13
|
+
stored_location_for(r) || root_path
|
14
|
+
end
|
15
|
+
|
16
|
+
def storable_location?
|
17
|
+
request.get? &&
|
18
|
+
is_navigational_format? &&
|
19
|
+
!devise_controller? &&
|
20
|
+
request.fullpath != root_path && # for return on creating new account
|
21
|
+
!request.xhr?
|
22
|
+
end
|
23
|
+
|
24
|
+
def store_access_location!
|
25
|
+
Rails.logger.debug("store_#{devise_key_for_store_location}_location!: #{request.fullpath}")
|
26
|
+
store_location_for(devise_key_for_store_location, request.fullpath)
|
27
|
+
end
|
28
|
+
|
29
|
+
def devise_key_for_store_location
|
30
|
+
Devise.mappings.keys[0]
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# on schema:
|
2
|
+
#
|
3
|
+
# with coulumn for soft_deleted_flag
|
4
|
+
# t.integer "alive", default: 1
|
5
|
+
#
|
6
|
+
# unique index
|
7
|
+
# [for postgresql] # partial indexes
|
8
|
+
# t.index ["email", "alive"], name: "index_users_on_email_and_alive", unique: true, where: "(alive = 1)"
|
9
|
+
# [for mysql] # ignoring null column
|
10
|
+
# t.index ["email", "alive"], name: "index_users_on_email_and_alive", unique: true
|
11
|
+
#
|
12
|
+
module Calib::Devise::SoftDeletion
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
included do
|
16
|
+
class_attribute :alive_column_name
|
17
|
+
|
18
|
+
def self.devise_soft_deletable(alive_column_name = :alive)
|
19
|
+
self.alive_column_name = alive_column_name
|
20
|
+
|
21
|
+
# @see http://blog.319ring.net/2016/05/24/devise-kakurenbo-puti/
|
22
|
+
# remove unique email Devise::validatable
|
23
|
+
# https://gist.github.com/brenes/4503386
|
24
|
+
self._validators.delete(:email)
|
25
|
+
self._validate_callbacks.each do |callback|
|
26
|
+
if callback.raw_filter.respond_to? :attributes
|
27
|
+
callback.raw_filter.attributes.delete :email
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# redefine email unique with column for soft deletation
|
32
|
+
self.validates :email, presence: true
|
33
|
+
self.validates_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email?
|
34
|
+
self.validates :email, uniqueness: { scope: alive_column_name }, if: :will_save_change_to_email?
|
35
|
+
|
36
|
+
self.scope :alive, -> { where(alive_column_name => 1) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.find_for_authentication(warden_conditions)
|
40
|
+
alive.where(email: warden_conditions[:email]).first
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def canceled?
|
45
|
+
self.alive_value == nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def soft_destroy
|
49
|
+
save if pre_destroy
|
50
|
+
end
|
51
|
+
|
52
|
+
def soft_destroy!
|
53
|
+
save! if pre_destroy
|
54
|
+
end
|
55
|
+
|
56
|
+
def alive_value
|
57
|
+
self.send(self.class.alive_column_name)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def pre_destroy
|
63
|
+
return false if canceled?
|
64
|
+
|
65
|
+
self.attributes = cancel_attributes
|
66
|
+
self.skip_reconfirmation!
|
67
|
+
true
|
68
|
+
end
|
69
|
+
|
70
|
+
def cancel_attributes
|
71
|
+
key = Devise.friendly_token.first(8)
|
72
|
+
|
73
|
+
attrs = {
|
74
|
+
email: "calceled_#{self.id}_#{key}@canceled.com",
|
75
|
+
password: key,
|
76
|
+
canceled_at: Time.zone.now
|
77
|
+
}
|
78
|
+
|
79
|
+
attrs[self.class.alive_column_name] = nil
|
80
|
+
attrs
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Calib::Devise::SoftDeletionMigrationEnhancable
|
2
|
+
include Calib::SoftDeletion::MigrationEnhancable
|
3
|
+
|
4
|
+
def change_for_device_soft_deletion(scope, filter_column = :alive, canceled_at_column = :canceled_at)
|
5
|
+
reversible do |dir|
|
6
|
+
dir.up { remove_index scope, :email }
|
7
|
+
dir.down { add_index scope, :email, unique: true }
|
8
|
+
end
|
9
|
+
|
10
|
+
add_column scope, canceled_at_column, :datetime
|
11
|
+
|
12
|
+
add_column_for_soft_deletion(scope, filter_column)
|
13
|
+
add_unique_index_for_soft_deletion(scope, :email, filter_column)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# @see https://stackoverflow.com/questions/43707745/pundit-with-namespaced-controllers
|
2
|
+
module Calib::Pundit::ControllerNestable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :namespace
|
7
|
+
|
8
|
+
def self.nested_namespace(namespace)
|
9
|
+
self.namespace = Array(namespace)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(user, record)
|
14
|
+
@user = user
|
15
|
+
@record = record.is_a?(Array) ? record.last : record
|
16
|
+
end
|
17
|
+
|
18
|
+
def scope
|
19
|
+
Pundit.policy_scope! user, self.class.namespace + [record.class]
|
20
|
+
end
|
21
|
+
|
22
|
+
class Scope # :nodoc:
|
23
|
+
attr_reader :user, :scope
|
24
|
+
|
25
|
+
def initialize(user, scope)
|
26
|
+
@user = user
|
27
|
+
@scope = scope.is_a?(Array) ? scope.last : scope
|
28
|
+
end
|
29
|
+
|
30
|
+
def resolve
|
31
|
+
scope
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# @see https://stackoverflow.com/questions/43707745/pundit-with-namespaced-controllers
|
2
|
+
# @see https://github.com/elabs/pundit/pull/392/files
|
3
|
+
module Calib::Pundit::Nestable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_attribute :namespace
|
8
|
+
|
9
|
+
def self.nested_namespace(namespace)
|
10
|
+
self.namespace = Array(namespace)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def policy_scope(scope)
|
15
|
+
super self.class.namespace + [scope]
|
16
|
+
end
|
17
|
+
|
18
|
+
def authorize(record, query = nil)
|
19
|
+
super self.class.namespace + [record], query
|
20
|
+
record
|
21
|
+
end
|
22
|
+
|
23
|
+
def policy(record)
|
24
|
+
# if record is Array, as already namespace array
|
25
|
+
super record.is_a?(Array) ? record : self.class.namespace + [record]
|
26
|
+
end
|
27
|
+
end
|
data/lib/calib/rails/version.rb
CHANGED
data/lib/calib/rails.rb
CHANGED
@@ -1,6 +1,25 @@
|
|
1
1
|
module Calib
|
2
|
-
module
|
2
|
+
module Controllers
|
3
|
+
autoload :BasicAuth, 'calib/controllers/basic_auth'
|
4
|
+
end
|
5
|
+
|
6
|
+
module Devise
|
7
|
+
autoload :FriendlyForwardable, 'calib/devise/friendly_forwardable'
|
8
|
+
autoload :SoftDeletionMigrationEnhancable, 'calib/devise/soft_deletion_migration_enhancable'
|
9
|
+
autoload :SoftDeletion, 'calib/devise/soft_deletion'
|
10
|
+
end
|
11
|
+
|
12
|
+
module Pundit
|
13
|
+
autoload :ControllerNestable, 'calib/pundit/controller_nestable'
|
14
|
+
autoload :Nestable, 'calib/pundit/nestable'
|
3
15
|
end
|
4
|
-
end
|
5
16
|
|
6
|
-
|
17
|
+
module SoftDeletion
|
18
|
+
autoload :MigrationEnhancable, 'calib/soft_deletion/migration_enhancable'
|
19
|
+
end
|
20
|
+
|
21
|
+
autoload :Delegatable, 'calib/delegatable'
|
22
|
+
autoload :IllegalAccessError, 'calib/illegal_access_error'
|
23
|
+
autoload :Service, 'calib/service'
|
24
|
+
autoload :UnauthenticatedError, 'calib/unauthenticated_error'
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Calib::SoftDeletion::MigrationEnhancable
|
2
|
+
def add_unique_column_for_soft_deletion(table, unique_column, filter_column = :alive)
|
3
|
+
add_column_for_soft_deletion(table, filter_column)
|
4
|
+
add_unique_index_for_soft_deletion(table, unique_column, filter_column)
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_column_for_soft_deletion(table, filter_column = :alive)
|
8
|
+
# @see https://qiita.com/yuba/items/70165875cfe02b03513d
|
9
|
+
add_column table, filter_column, :integer, default: 1 # if canceled, alive=NULL for MYSQL
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_unique_index_for_soft_deletion(table, unique_column, filter_column = :alive)
|
13
|
+
if connection.adapter_name == 'MySQL'
|
14
|
+
add_index table, [unique_column, filter_column], unique: true
|
15
|
+
else
|
16
|
+
# @see http://rny.io/rails/postgresql/2013/08/20/postgresql-indexing-in-rails.html
|
17
|
+
add_index table, [unique_column, filter_column], unique: true, where: "#{filter_column} = 1" # for Postgresql indexes partial
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calib-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ms2sato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -63,8 +63,18 @@ files:
|
|
63
63
|
- README.md
|
64
64
|
- Rakefile
|
65
65
|
- lib/calib/controllers/basic_auth.rb
|
66
|
+
- lib/calib/delegatable.rb
|
67
|
+
- lib/calib/devise/friendly_forwardable.rb
|
68
|
+
- lib/calib/devise/soft_deletion.rb
|
69
|
+
- lib/calib/devise/soft_deletion_migration_enhancable.rb
|
70
|
+
- lib/calib/illegal_access_error.rb
|
71
|
+
- lib/calib/pundit/controller_nestable.rb
|
72
|
+
- lib/calib/pundit/nestable.rb
|
66
73
|
- lib/calib/rails.rb
|
67
74
|
- lib/calib/rails/version.rb
|
75
|
+
- lib/calib/service.rb
|
76
|
+
- lib/calib/soft_deletion/migration_enhancable.rb
|
77
|
+
- lib/calib/unauthenticated_error.rb
|
68
78
|
- lib/tasks/calib/rails_tasks.rake
|
69
79
|
homepage: https://github.com/CircleAround/calib-rails
|
70
80
|
licenses:
|