effective_trash 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9aa7496caf63e3f09ad0778be9ef6c6b4a8667cd
4
- data.tar.gz: af51ec535165f1a72c6f1bcf930f4761033bbed3
3
+ metadata.gz: 1a76939023ec96d1a1a7616553bb4c985518e1f8
4
+ data.tar.gz: c1af33bc21f015d7eb486e5ba2b5e068b69bc222
5
5
  SHA512:
6
- metadata.gz: b8cf68d4f4fb439e7b99d6c687d93ac6def8e81707b26903d0de556defa9098c1953ca0ec2ce40c8d2a05f056f51582b179a799c13f4905c34df20e284deb9b6
7
- data.tar.gz: 88dede0f9ca2cb31a9b0a75273fecf8d5db4551768718b0e2c22f89bade54748caa5c3857b3804af2b6550ab507672e19292bc1639fe13d3054006d3620f6960
6
+ metadata.gz: 0431a81ded3b5a6b0b17a487fe47e9c013f671e1fc0add41a35e60f45e8815dfcc53950cfc7d5d3c29f5a14158aa1bdd1065d569a1cd6ca109c96aa8df20b559
7
+ data.tar.gz: 89576b17f5547c5949d86edd7eb9d7f78816c861b4a2ae34e66d9860370766234d7e8682aafbc1610cf8414a80aa2553e04ed5f79cc2a3add70f988943ac8a0d
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2017 Code and Effect Inc.
1
+ Copyright 2018 Code and Effect Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -2,31 +2,25 @@
2
2
 
3
3
  module Admin
4
4
  class TrashController < ApplicationController
5
- respond_to?(:before_action) ? before_action(:authenticate_user!) : before_filter(:authenticate_user!) # Devise
5
+ before_action :authenticate_user!
6
6
 
7
7
  layout (EffectiveTrash.layout.kind_of?(Hash) ? EffectiveTrash.layout[:admin_trash] : EffectiveTrash.layout)
8
8
 
9
- helper EffectiveTrashHelper
10
-
11
9
  def index
12
- if Gem::Version.new(EffectiveDatatables::VERSION) < Gem::Version.new('3.0')
13
- @datatable = Effective::Datatables::Trash.new
14
- else
15
- @datatable = EffectiveTrashDatatable.new(self)
16
- end
10
+ @datatable = EffectiveTrashDatatable.new(self)
17
11
 
18
12
  @page_title = 'Trash'
19
13
 
20
- EffectiveTrash.authorized?(self, :index, Effective::Trash)
21
- EffectiveTrash.authorized?(self, :admin, :effective_trash)
14
+ EffectiveTrash.authorize!(self, :index, Effective::Trash)
15
+ EffectiveTrash.authorize!(self, :admin, :effective_trash)
22
16
  end
23
17
 
24
18
  def show
25
19
  @trash = Effective::Trash.all.find(params[:id])
26
20
  @page_title = "Trash item - #{@trash.trashed_to_s}"
27
21
 
28
- EffectiveTrash.authorized?(self, :show, @trash)
29
- EffectiveTrash.authorized?(self, :admin, :effective_trash)
22
+ EffectiveTrash.authorize!(self, :show, @trash)
23
+ EffectiveTrash.authorize!(self, :admin, :effective_trash)
30
24
  end
31
25
  end
32
26
  end
@@ -1,23 +1,14 @@
1
1
  module Effective
2
2
  class TrashController < ApplicationController
3
- if respond_to?(:before_action) # Devise
4
- before_action :authenticate_user!
5
- else
6
- before_filter :authenticate_user!
7
- end
3
+ before_action :authenticate_user!
8
4
 
9
5
  # This is the User index event
10
6
  def index
11
-
12
- if Gem::Version.new(EffectiveDatatables::VERSION) < Gem::Version.new('3.0')
13
- @datatable = Effective::Datatables::Trash.new(user_id: current_user.id)
14
- else
15
- @datatable = EffectiveTrashDatatable.new(self, user_id: current_user.id)
16
- end
7
+ @datatable = EffectiveTrashDatatable.new(self, user_id: current_user.id)
17
8
 
18
9
  @page_title = 'Trash'
19
10
 
20
- EffectiveTrash.authorized?(self, :index, Effective::Trash.new(user_id: current_user.id))
11
+ EffectiveTrash.authorize!(self, :index, Effective::Trash.new(user_id: current_user.id))
21
12
  end
22
13
 
23
14
  # This is the User show event
@@ -25,12 +16,12 @@ module Effective
25
16
  @trash = Effective::Trash.where(user_id: current_user.id).find(params[:id])
26
17
  @page_title = "Trash item - #{@trash.to_s}"
27
18
 
28
- EffectiveTrash.authorized?(self, :show, @trash)
19
+ EffectiveTrash.authorize!(self, :show, @trash)
29
20
  end
30
21
 
31
22
  def restore
32
- @trash = Effective::Trash.all.find(params[:id])
33
- EffectiveTrash.authorized?(self, :update, @trash)
23
+ @trash = Effective::Trash.find(params[:id])
24
+ EffectiveTrash.authorize!(self, :update, @trash)
34
25
 
35
26
  Effective::Trash.transaction do
36
27
  begin
@@ -42,7 +33,12 @@ module Effective
42
33
  end
43
34
  end
44
35
 
45
- redirect_back(fallback_location: effective_trash.trash_path)
36
+ if request.referer.to_s.include?(effective_trash.admin_trash_index_path)
37
+ redirect_to effective_trash.admin_trash_index_path
38
+ else
39
+ redirect_to effective_trash.trash_index_path
40
+ end
41
+
46
42
  end
47
43
 
48
44
  end
@@ -1,47 +1,45 @@
1
- unless Gem::Version.new(EffectiveDatatables::VERSION) < Gem::Version.new('3.0')
2
- class EffectiveTrashDatatable < Effective::Datatable
3
- datatable do
4
- order :created_at, :desc
1
+ class EffectiveTrashDatatable < Effective::Datatable
2
+ datatable do
3
+ order :created_at, :desc
5
4
 
6
- col :created_at, label: 'Destroyed at'
7
- col :id, visible: false
5
+ col :created_at, label: 'Destroyed at'
6
+ col :id, visible: false
8
7
 
9
- unless attributes[:user_id] || attributes[:user] || (attributes[:user] == false)
10
- col :user, label: 'Destroyed by'
11
- end
12
-
13
- col :trashed_type, label: 'Type'
14
- col :trashed_id, label: 'Original Id', visible: false
15
- col :trashed_to_s, label: 'Item'
8
+ unless attributes[:user_id] || attributes[:user] || (attributes[:user] == false)
9
+ col :user, label: 'Destroyed by'
10
+ end
16
11
 
17
- col :details, visible: false do |trash|
18
- tableize_hash(trash.details)
19
- end
12
+ col :trashed_type, label: 'Type'
13
+ col :trashed_id, label: 'Original Id', visible: false
14
+ col :trashed_to_s, label: 'Item'
20
15
 
21
- unless attributes[:actions] == false
22
- actions_col partial: 'admin/trash/actions', partial_as: :trash
23
- end
16
+ col :details, visible: false do |trash|
17
+ tableize_hash(trash.details)
24
18
  end
25
19
 
26
- # A nil attributes[:log_id] means give me all the top level log entries
27
- # If we set a log_id then it's for sub trash
28
- collection do
29
- collection = Effective::Trash.all.includes(:user)
20
+ unless attributes[:actions] == false
21
+ actions_col partial: 'admin/trash/actions', partial_as: :trash
22
+ end
23
+ end
30
24
 
31
- if attributes[:user_id].present?
32
- collection = collection.where(user_id: attributes[:user_id])
33
- end
25
+ # A nil attributes[:log_id] means give me all the top level log entries
26
+ # If we set a log_id then it's for sub trash
27
+ collection do
28
+ collection = Effective::Trash.deep.all
34
29
 
35
- if attributes[:user].present?
36
- collection = collection.where(user: attributes[:user])
37
- end
30
+ if attributes[:user_id].present?
31
+ collection = collection.where(user_id: attributes[:user_id])
32
+ end
38
33
 
39
- if attributes[:trashed_id] && attributes[:trashed_type]
40
- collection = collection.where(trashed_id: attributes[:trashed_id], trashed_type: attributes[:trashed_type])
41
- end
34
+ if attributes[:user].present?
35
+ collection = collection.where(user: attributes[:user])
36
+ end
42
37
 
43
- collection
38
+ if attributes[:trashed_id] && attributes[:trashed_type]
39
+ collection = collection.where(trashed_id: attributes[:trashed_id], trashed_type: attributes[:trashed_type])
44
40
  end
45
41
 
42
+ collection
46
43
  end
44
+
47
45
  end
@@ -1,7 +1,7 @@
1
1
  module EffectiveTrashHelper
2
2
 
3
3
  def render_trash(trash)
4
- render(partial: 'effective/trash/trash', locals: {trash: trash})
4
+ render(partial: 'effective/trash/trash', locals: { trash: trash })
5
5
  end
6
6
 
7
7
  end
@@ -3,7 +3,7 @@ module Effective
3
3
  self.table_name = EffectiveTrash.trash_table_name.to_s
4
4
 
5
5
  belongs_to :trashed, polymorphic: true # The original item type and id. Note that this object will never exist as it's deleted.
6
- belongs_to :user, optional: true # The user that destroyed the original resource
6
+ belongs_to :user # The user that destroyed the original resource
7
7
 
8
8
  # Attributes
9
9
  # trashed_type :string
@@ -16,7 +16,8 @@ module Effective
16
16
 
17
17
  serialize :details, Hash
18
18
 
19
- default_scope -> { order(updated_at: :desc) }
19
+ scope :deep, -> { includes(:user, :trashed) }
20
+ scope :sorted, -> { order(:id) }
20
21
 
21
22
  def to_s
22
23
  trashed_to_s.presence || [trashed_type, trashed_id].join(' ').presence || 'New Trash item'
@@ -49,6 +50,7 @@ module Effective
49
50
  # When we delete ourselves, we restore this trash item first
50
51
  def restore!
51
52
  to_object.save!(validate: false)
53
+ destroy!
52
54
  end
53
55
 
54
56
  end
@@ -1,15 +1,11 @@
1
- :ruby
2
- show_path =
3
- if datatable.try(:admin_namespace?)
4
- effective_trash.admin_trash_path(trash)
5
- elsif datatables_admin_path?
6
- effective_trash.admin_trash_path(trash)
7
- else
8
- effective_trash.trash_path(trash)
9
- end
1
+ - show_path = (datatable.admin_namespace? ? effective_trash.admin_trash_path(trash) : effective_trash.trash_path(trash))
10
2
 
11
- = link_to show_path, title: 'view' do
12
- %span.glyphicon.glyphicon-eye-open
3
+ - if defined?(EffectiveBootstrap)
4
+ = show_icon_to(show_path)
5
+ = icon_to('repeat', effective_trash.restore_trash_path(trash), title: 'Restore', data: { confirm: 'Restore this item?'})
6
+ - else
7
+ = link_to show_path, title: 'view' do
8
+ %span.glyphicon.glyphicon-eye-open
13
9
 
14
- = link_to effective_trash.restore_trash_path(trash), title: 'Restore', data: {confirm: 'Restore this item?'} do
15
- %span.glyphicon.glyphicon-retweet
10
+ = link_to effective_trash.restore_trash_path(trash), title: 'Restore', data: {confirm: 'Restore this item?'} do
11
+ %span.glyphicon.glyphicon-retweet
@@ -1,7 +1,12 @@
1
- .panel.panel-default
2
- .panel-body
3
- %p= trash.trashed_to_s
1
+ .panel.panel-default.card
2
+ .panel-heading.card-header
3
+ .row
4
+ .col-md-10
5
+ %p= trash.trashed_to_s
6
+ .col-md-2.text-right
7
+ = link_to 'Restore', effective_trash.restore_trash_path(trash), class: 'btn btn-primary', data: { confirm: 'Restore this item?'}
4
8
 
9
+ .panel-body.card-body
5
10
  - if trash.trashed_extra.present?
6
11
  %p= trash.trashed_extra
7
12
 
@@ -1,6 +1,3 @@
1
1
  %h1.effective-heading= @page_title
2
2
 
3
- - if @datatable.present?
4
- = render_datatable(@datatable)
5
- - else
6
- %p You have no previously trashed items
3
+ = render_datatable(@datatable)
@@ -2,12 +2,11 @@ module EffectiveTrash
2
2
  module SetCurrentUser
3
3
  module ActionController
4
4
 
5
+ # Add me to your ApplicationController
6
+ # before_action :set_effective_trash_current_user
7
+
5
8
  def set_effective_trash_current_user
6
- if respond_to?(:current_user)
7
- EffectiveTrash.current_user = current_user
8
- else
9
- raise "(effective_trash) set_effective_trash_current_user expects a current_user() method to be available"
10
- end
9
+ EffectiveTrash.current_user = current_user
11
10
  end
12
11
 
13
12
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveTrash
2
- VERSION = '0.2.6'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -16,10 +16,20 @@ module EffectiveTrash
16
16
  end
17
17
 
18
18
  def self.authorized?(controller, action, resource)
19
- if authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol)
20
- raise Effective::AccessDenied.new() unless (controller || self).instance_exec(controller, action, resource, &authorization_method)
19
+ @_exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact
20
+
21
+ return !!authorization_method unless authorization_method.respond_to?(:call)
22
+ controller = controller.controller if controller.respond_to?(:controller)
23
+
24
+ begin
25
+ !!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
26
+ rescue *@_exceptions
27
+ false
21
28
  end
22
- true
29
+ end
30
+
31
+ def self.authorize!(controller, action, resource)
32
+ raise Effective::AccessDenied unless authorized?(controller, action, resource)
23
33
  end
24
34
 
25
35
  # This is set by the "set_effective_trash_current_user" before_filter.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_trash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-07 00:00:00.000000000 Z
11
+ date: 2018-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.0
19
+ version: 4.0.0
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: 3.2.0
26
+ version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: effective_datatables
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: coffee-rails
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: devise
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -95,7 +81,6 @@ files:
95
81
  - app/helpers/effective_trash_helper.rb
96
82
  - app/models/concerns/acts_as_trashable.rb
97
83
  - app/models/effective/access_denied.rb
98
- - app/models/effective/datatables/trash.rb
99
84
  - app/models/effective/trash.rb
100
85
  - app/views/admin/trash/_actions.html.haml
101
86
  - app/views/admin/trash/index.html.haml
@@ -1,53 +0,0 @@
1
- if Gem::Version.new(EffectiveDatatables::VERSION) < Gem::Version.new('3.0')
2
- module Effective
3
- module Datatables
4
- class Trash < Effective::Datatable
5
- include EffectiveTrashHelper
6
-
7
- datatable do
8
- default_order :created_at, :desc
9
-
10
- table_column :created_at, label: 'Destroyed at'
11
- table_column :id, visible: false
12
-
13
- unless attributes[:user_id] || attributes[:user] || (attributes[:user] == false)
14
- table_column :user, label: 'Destroyed by'
15
- end
16
-
17
- table_column :trashed_type, label: 'Type'
18
- table_column :trashed_id, label: 'Original Id', visible: false
19
- table_column :trashed_to_s, label: 'Item'
20
-
21
- table_column :details, visible: false, sortable: false do |trash|
22
- tableize_hash(trash.details, th: true, sub_th: false, width: '100%')
23
- end
24
-
25
- unless attributes[:actions] == false
26
- actions_column partial: 'admin/trash/actions', partial_local: :trash
27
- end
28
- end
29
-
30
- # A nil attributes[:log_id] means give me all the top level log entries
31
- # If we set a log_id then it's for sub trash
32
- def collection
33
- collection = Effective::Trash.all.includes(:user)
34
-
35
- if attributes[:user_id].present?
36
- collection = collection.where(user_id: attributes[:user_id])
37
- end
38
-
39
- if attributes[:user].present?
40
- collection = collection.where(user: attributes[:user])
41
- end
42
-
43
- if attributes[:trashed_id] && attributes[:trashed_type]
44
- collection = collection.where(trashed_id: attributes[:trashed_id], trashed_type: attributes[:trashed_type])
45
- end
46
-
47
- collection
48
- end
49
-
50
- end
51
- end
52
- end
53
- end