forked from ifournight/tower_homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Only Form Object AssignTodo, no request or controller - Related activity view
- Loading branch information
1 parent
5da9bbc
commit d279235
Showing
15 changed files
with
219 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
class AssignTodo | ||
include ActiveModel::Model | ||
|
||
attr_accessor( | ||
:todo_id, | ||
:authorizer_id, | ||
:member_id | ||
) | ||
|
||
validates_presence_of [:todo_id, :authorizer_id, :member_id] | ||
validate :valid_todo_id | ||
validate :valid_authorizer_id | ||
validate :valid_member_id | ||
|
||
def do | ||
return nil if invalid? | ||
|
||
valid_todo_id | ||
valid_authorizer_id | ||
valid_member_id | ||
|
||
@authorizer = User.find(authorizer_id) | ||
@member = User.find(member_id) | ||
@todo = Todo.find(todo_id) | ||
|
||
return nil if any_errors? | ||
|
||
check_authorizer_access | ||
check_already_member | ||
|
||
return nil if any_errors? | ||
|
||
assign_todo | ||
create_activity_assign_todo | ||
@todo | ||
end | ||
|
||
private | ||
|
||
def any_errors? | ||
errors.each do |_key, _value| | ||
return true | ||
end | ||
false | ||
end | ||
|
||
def valid_todo_id | ||
errors.add(:todo_id, 'must be valid') unless Todo.exists?(todo_id) | ||
end | ||
|
||
def valid_authorizer_id | ||
errors.add(:authorizer_id, 'must be valid') unless User.exists?(authorizer_id) | ||
end | ||
|
||
def valid_member_id | ||
errors.add(:member_id, 'must be valid') unless User.exists?(member_id) | ||
end | ||
|
||
def check_authorizer_access | ||
errors.add(:authorizer_id, "doesn't have access") unless Access.has_access?( | ||
@authorizer.id, | ||
@todo.project.id, | ||
@todo.project.class.name, | ||
Access::ACCESS_TYPE[:WRITE_PROJECT] | ||
) | ||
end | ||
|
||
def check_already_member | ||
errors.add(:member_id, 'already assigned') if @todo.members.any? && @todo.members.include?(@member) | ||
end | ||
|
||
def create_activity_assign_todo | ||
Activity.create( | ||
user_id: authorizer_id, | ||
subject_id: todo_id, | ||
subject_type: @todo.class.name, | ||
action: Activity::ACTION_TYPES[:ASSIGN_TODO], | ||
project_id: @todo.project_id, | ||
extra: { member_from_id: @member_from ? @member_from.id : '', member_to_id: member_id } | ||
) | ||
end | ||
|
||
def assign_todo | ||
if @todo.members.any? | ||
@member_before = @todo.members.first | ||
@todo.members.delete(@member_before) | ||
end | ||
|
||
TodoMember.create( | ||
todo_id: @todo.id, | ||
member_id: @member.id | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class TodoMember < ApplicationRecord | ||
belongs_to :member, class_name: 'User' | ||
belongs_to :todo | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<p data-role='activity-content'> | ||
<strong data-role='activity-username'><%= activity.user.name %></strong> | ||
<span data-role='activity-description'> | ||
<% member_from_id = activity.extra[:member_from_id] %> | ||
<% member_to_id = activity.extra[:member_to_id] %> | ||
<% member_from = User.exists?(member_from_id) ? User.find(member_from_id) : nil %> | ||
<% member_to = User.exists?(member_to_id) ? User.find(member_to_id) : nil %> | ||
|
||
<% if member_from.nil? %> | ||
给 <strong><%= member_to.name %></strong> 指派了任务: | ||
<% else %> | ||
将 <strong><%= member_from.name %></strong> 指派给 <strong><%= member_to.name %></strong>: | ||
<% end %> | ||
|
||
<%= link_to activity.subject.title, root_path, 'data-role': 'activity-subject' %> | ||
|
||
</span> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateTodoMembers < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :todo_members do |t| | ||
t.references :member, index: true, null: false | ||
t.references :todo, index: true, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.