Skip to content

Commit a47597e

Browse files
authored
Merge pull request #368 from coopdevs/delete_org
Delete a organization + organization policy
2 parents 3204d41 + 14fe6bc commit a47597e

22 files changed

+215
-83
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ gem 'rails-i18n'
77
gem "rdiscount"
88
gem 'activeadmin', '~> 1.2.1'
99
gem 'has_scope'
10-
gem 'pundit'
10+
gem 'pundit', '~> 2.0.0'
1111
gem 'pg', '0.17.1'
1212
gem 'hstore_translate'
1313
gem 'dalli'

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ GEM
238238
prawn-table (0.2.2)
239239
prawn (>= 1.3.0, < 3.0.0)
240240
public_suffix (2.0.5)
241-
pundit (0.3.0)
241+
pundit (2.0.0)
242242
activesupport (>= 3.0.0)
243243
rack (1.6.10)
244244
rack-protection (2.0.1)
@@ -428,7 +428,7 @@ DEPENDENCIES
428428
pg (= 0.17.1)
429429
prawn (~> 2.2.0)
430430
prawn-table (~> 0.2.2)
431-
pundit
431+
pundit (~> 2.0.0)
432432
rails (~> 4.2)
433433
rails-i18n
434434
rails_12factor (= 0.0.3)

app/admin/organization.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
f.actions
2828
end
2929

30+
controller do
31+
def destroy
32+
resource.destroy
33+
34+
if resource == current_organization
35+
sign_out(current_user)
36+
redirect_to root_path
37+
else
38+
redirect_to admin_organizations_path
39+
end
40+
end
41+
end
42+
3043
filter :name
3144
filter :city, as: :select, collection: -> { Organization.pluck(:city).uniq }
3245
filter :neighborhood

app/controllers/organizations_controller.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class OrganizationsController < ApplicationController
2-
before_filter :load_resource, only: [:show, :edit, :update, :destroy, :set_current]
2+
before_filter :load_resource, only: [:show, :edit, :update, :set_current]
33

44
def new
55
@organization = Organization.new
6+
7+
authorize @organization
68
end
79

810
def index
9-
@organizations = Organization.all
11+
@organizations = Organization.all.page(params[:page]).per(25)
1012
end
1113

1214
def show
@@ -21,8 +23,10 @@ def show
2123
def create
2224
@organization = Organization.new(organization_params)
2325

26+
authorize @organization
27+
2428
if @organization.save
25-
redirect_to @organization, status: :created
29+
redirect_to @organization
2630
else
2731
render action: :new, status: :unprocessable_entity
2832
end
@@ -36,11 +40,6 @@ def update
3640
end
3741
end
3842

39-
def destroy
40-
@organization.destroy
41-
redirect_to organizations_path, notice: "deleted"
42-
end
43-
4443
# POST /organizations/:organization_id/set_current
4544
#
4645
def set_current
@@ -54,6 +53,8 @@ def set_current
5453

5554
def load_resource
5655
@organization = Organization.find(params[:id])
56+
57+
authorize @organization
5758
end
5859

5960
def organization_params

app/models/event.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Event < ActiveRecord::Base
44
belongs_to :post
55
belongs_to :member
66
belongs_to :transfer
7+
has_many :push_notifications, dependent: :destroy
78

89
validates :action, presence: true
910
validate :resource_presence

app/models/member.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Member < ActiveRecord::Base
88
belongs_to :organization
99
has_one :account, as: :accountable
1010
has_many :movements, through: :account
11+
has_many :events, dependent: :destroy
1112

1213
delegate :balance, to: :account, prefix: true, allow_nil: true
1314
delegate :gender, :date_of_birth, to: :user, prefix: true, allow_nil: true

app/models/organization.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Organization < ActiveRecord::Base
22
has_many :members, dependent: :destroy
33
has_many :users, -> { order "members.created_at DESC" }, through: :members
4-
has_many :all_accounts, class_name: "Account", inverse_of: :organization
4+
has_many :all_accounts, class_name: "Account", inverse_of: :organization, dependent: :destroy
55
has_many :all_movements, class_name: "Movement", through: :all_accounts, source: :movements
66
has_many :all_transfers, class_name: "Transfer", through: :all_movements, source: :transfer
7-
has_one :account, as: :accountable
7+
has_one :account, as: :accountable, dependent: :destroy
88
has_many :member_accounts, through: :members, source: :account
9-
has_many :posts
9+
has_many :posts, dependent: :destroy
1010
has_many :offers
1111
has_many :inquiries
12-
has_many :documents, as: :documentable
12+
has_many :documents, as: :documentable, dependent: :destroy
1313

1414
validates :name, presence: true, uniqueness: true
1515

app/models/post.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ def self.inherited(child)
3939
attr_reader :member_id
4040

4141
belongs_to :category
42-
4342
belongs_to :user
4443
belongs_to :organization
4544
belongs_to :publisher, class_name: "User", foreign_key: "publisher_id"
46-
has_many :user_members, class_name: "Member", through: :user, source: :members
4745
has_many :transfers
4846
has_many :movements, through: :transfers
47+
has_many :events, dependent: :destroy
4948

5049
delegate :name, to: :category, prefix: true, allow_nil: true
5150

app/models/transfer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Transfer < ActiveRecord::Base
1414
attr_accessor :source, :destination, :amount, :hours, :minutes
1515

1616
belongs_to :post
17-
belongs_to :operator, class_name: "User"
18-
has_many :movements
17+
has_many :movements, dependent: :destroy
18+
has_many :events, dependent: :destroy
1919

2020
validate :different_source_and_destination
2121

app/policies/application_policy.rb

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def index?
1717
end
1818

1919
def show?
20-
scope.where(id: record.id).exists?
20+
record.class.where(id: record.id).exists?
2121
end
2222

2323
def create?
@@ -39,23 +39,4 @@ def edit?
3939
def destroy?
4040
false
4141
end
42-
43-
def scope
44-
Pundit.policy_scope!(member, record.class)
45-
end
46-
47-
class Scope
48-
attr_reader :member, :user, :organization, :scope
49-
50-
def initialize(member, scope)
51-
@member = member
52-
@user = member.user if member
53-
@organization = member.organization if member
54-
@scope = scope
55-
end
56-
57-
def resolve
58-
scope
59-
end
60-
end
6142
end

0 commit comments

Comments
 (0)