-
Notifications
You must be signed in to change notification settings - Fork 2
/
72-granting-write-access-to-others.patch
133 lines (111 loc) · 4.66 KB
/
72-granting-write-access-to-others.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
granting-write-access-to-others
From: Bryan Larsen <[email protected]>
It's not enough just to allow others to view your projects, you need to allow some people to make changes, too. The goal of this part of the tutorial is to add a "Contributor" checkbox next to each person in the side-bar.
Implementing this is left as an exercise for the reader. The steps are:
1. Add a boolean `contributor` field to the `ProjectMembership` model.
2. Modify the permissions of stories and tasks so that users with `contributor=true` on their project membership have update permission for the project.
3. Use the `<editor>` tag to create an ajax editor for the `contributor` field in the ProjectMembership card.
That's all the hints we're going to give you for this one -- good luck!
Ok, one more hint, here's some associations that might be handy in the Project model:
has_many :contributor_memberships, :class_name => "ProjectMembership", :scope => :contributor
has_many :contributors, :through => :contributor_memberships, :source => :user
{: .ruby}
And a helper method that might come in handy when implementing your permission methods:
def accepts_changes_from?(user)
user.administrator? || user == owner || user.in?(contributors)
end
{: .ruby}
---
app/models/project.rb | 10 +++++++++-
app/models/project_membership.rb | 1 +
app/views/taglibs/application.dryml | 12 +++++++++++-
...01135804_hobo_migration_project_contributors.rb | 9 +++++++++
db/schema.rb | 3 ++-
5 files changed, 32 insertions(+), 3 deletions(-)
create mode 100644 db/migrate/20091201135804_hobo_migration_project_contributors.rb
diff --git a/app/models/project.rb b/app/models/project.rb
index be59f11..bfc7c8e 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -13,6 +13,14 @@ class Project < ActiveRecord::Base
belongs_to :owner, :class_name => "User", :creator => true
+ has_many :contributor_memberships, :class_name => "ProjectMembership", :scope => :contributor
+ has_many :contributors, :through => :contributor_memberships, :source => :user
+
+ # permission helper
+ def accepts_changes_from?(user)
+ user.administrator? || user == owner || user.in?(contributors)
+ end
+
# --- Permissions --- #
def create_permitted?
@@ -20,7 +28,7 @@ class Project < ActiveRecord::Base
end
def update_permitted?
- acting_user.administrator? || (owner_is?(acting_user) && !owner_changed?)
+ accepts_changes_from?(acting_user) && !owner_changed?
end
def destroy_permitted?
diff --git a/app/models/project_membership.rb b/app/models/project_membership.rb
index b7cc12e..33c2285 100644
--- a/app/models/project_membership.rb
+++ b/app/models/project_membership.rb
@@ -3,6 +3,7 @@ class ProjectMembership < ActiveRecord::Base
hobo_model # Don't put anything above this
fields do
+ contributor :boolean, :default => false
timestamps
end
diff --git a/app/views/taglibs/application.dryml b/app/views/taglibs/application.dryml
index e2bc28c..5681328 100644
--- a/app/views/taglibs/application.dryml
+++ b/app/views/taglibs/application.dryml
@@ -22,4 +22,14 @@
<old-form merge>
<field-list: fields="description, users"/>
</old-form>
-</extend>
\ No newline at end of file
+</extend>
+
+<extend tag="card" for="ProjectMembership">
+ <old-card merge>
+ <body:>
+ <span>Contributor?</span>
+ <editor:contributor/>
+ </body:>
+ </old-card>
+</extend>
+
diff --git a/db/migrate/20091201135804_hobo_migration_project_contributors.rb b/db/migrate/20091201135804_hobo_migration_project_contributors.rb
new file mode 100644
index 0000000..cd4429a
--- /dev/null
+++ b/db/migrate/20091201135804_hobo_migration_project_contributors.rb
@@ -0,0 +1,9 @@
+class HoboMigrationProjectContributors < ActiveRecord::Migration
+ def self.up
+ add_column :project_memberships, :contributor, :boolean, :default => false
+ end
+
+ def self.down
+ remove_column :project_memberships, :contributor
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 1a958c2..72bae99 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,13 +9,14 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20091201135345) do
+ActiveRecord::Schema.define(:version => 20091201135804) do
create_table "project_memberships", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "project_id"
t.integer "user_id"
+ t.boolean "contributor", :default => false
end
add_index "project_memberships", ["project_id"], :name => "index_project_memberships_on_project_id"