Skip to content

Commit

Permalink
Create assign_todo
Browse files Browse the repository at this point in the history
- Only Form Object AssignTodo, no request or controller
- Related activity view
  • Loading branch information
ifournight committed Apr 7, 2017
1 parent 5da9bbc commit d279235
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 25 deletions.
11 changes: 6 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
source 'https://rubygems.org'

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use sqlite3 as the database for Active Record
Expand All @@ -27,7 +26,7 @@ gem 'jquery-rails'
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
gem "active_model_serializers"
gem 'active_model_serializers'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
Expand All @@ -38,9 +37,9 @@ gem "active_model_serializers"

gem 'record_tag_helper', '~> 1.0'

gem "bulma-rails", "~> 0.4.0"
gem 'bulma-rails', '~> 0.4.0'

gem "font-awesome-rails"
gem 'font-awesome-rails'

gem 'flatpickr_rails'

Expand All @@ -58,6 +57,8 @@ group :development, :test do
gem 'factory_girl_rails'

gem 'rack_session_access'

gem 'guard-rspec', require: false
end

group :development do
Expand Down
32 changes: 32 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
coderay (1.1.1)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
Expand All @@ -77,10 +78,25 @@ GEM
railties (>= 3.0)
font-awesome-rails (4.7.0.1)
railties (>= 3.2, < 5.1)
formatador (0.2.5)
globalid (0.3.7)
activesupport (>= 4.1.0)
groupdate (3.2.0)
activesupport (>= 3)
guard (2.14.1)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
i18n (0.8.1)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
Expand All @@ -91,6 +107,7 @@ GEM
rb-inotify (~> 0.9, >= 0.9.7)
loofah (2.0.3)
nokogiri (>= 1.5.9)
lumberjack (1.0.11)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
Expand All @@ -99,9 +116,17 @@ GEM
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
nenv (0.3.0)
nio4r (2.0.0)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
notiffany (0.1.1)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
public_suffix (2.0.5)
puma (3.8.2)
rack (2.0.1)
Expand Down Expand Up @@ -139,6 +164,10 @@ GEM
ffi (>= 0.5.0)
record_tag_helper (1.0.0)
actionview (~> 5.x)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
Expand All @@ -163,8 +192,10 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shellany (0.0.1)
shoulda-matchers (3.1.1)
activesupport (>= 4.0.0)
slop (3.6.0)
spring (2.0.1)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -213,6 +244,7 @@ DEPENDENCIES
flatpickr_rails
font-awesome-rails
groupdate
guard-rspec
jquery-rails
listen (~> 3.0.5)
puma (~> 3.0)
Expand Down
20 changes: 12 additions & 8 deletions app/assets/stylesheets/todo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
margin-right: 0.5em;
}

.complete-todo-icon {
color: hsl(0, 0%, 96%);
.complete-todo-icon {
color: hsl(0, 0%, 96%);

transition: color 0.5s ease;
&:hover {
color: hsl(0, 0%, 86%);
transition: color 0.5s ease;
&:hover {
color: hsl(0, 0%, 86%);
}
}

.complete-todo-icon.is-completed {
color: hsl(171, 100%, 41%);
}
}

.complete-todo-icon.is-completed {
color: hsl(171, 100%, 41%);
.todo-tag {
margin-right: 0.25em;
}
12 changes: 11 additions & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
project_ids = projects.map(&:id)
@activities = Activity.where(project_id: project_ids).order('created_at DESC').to_a
else
@activities = Activity.order('created_at DESC')
@activities = Activity.order('created_at DESC').to_a
end
by_day = @activities.group_by_day(&:created_at)
@activities_by_day = {}
Expand All @@ -17,5 +17,15 @@ def index
@activities_by_day[day][id] = subgroup
end
end

@by_day_groups = []
@activities_by_day.each do |day, group|
@by_day_groups << { day: day, group: group }
end

@by_day_groups.sort do |a, b|
(a[:day] <=> b[:day])
end
@by_day_groups.reverse!
end
end
3 changes: 2 additions & 1 deletion app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Activity < ApplicationRecord
CREATE_TODO: 'create_todo',
COMPLETE_TODO: 'complete_todo',
REOPEN_TODO: 'reopen_todo',
SET_DUE_TODO: 'set_due_todo'
SET_DUE_TODO: 'set_due_todo',
ASSIGN_TODO: 'assign_todo'
}.freeze
end
94 changes: 94 additions & 0 deletions app/models/assign_todo.rb
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
11 changes: 8 additions & 3 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ class Todo < ApplicationRecord
belongs_to :creator, class_name: 'User', optional: true
belongs_to :project

has_many :todo_members
has_many :members, through: :todo_members

validates :title, presence: true

def over_due?
unless deadline.nil?
return deadline < Time.zone.now
end
return deadline < Time.zone.now unless deadline.nil?

false
end

def assigned_member
members.any? ? members[0] : nil
end
end
4 changes: 4 additions & 0 deletions app/models/todo_member.rb
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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class User < ApplicationRecord
has_many :team_memberships, foreign_key: 'member_id'
has_many :owned_teams, class_name: 'Team', foreign_key: 'owner_id'
has_many :joined_teams, through: :team_memberships, source: :team
has_many :todo_members, foreign_key: 'member_id'
has_many :working_todos, through: :todo_members, source: :todo

validates :name, presence: true
validates :name, uniqueness: true
Expand Down
18 changes: 18 additions & 0 deletions app/views/activities/_activity_assign_todo.html.erb
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>
6 changes: 3 additions & 3 deletions app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<section class='section' id="todo-list-section">
<% if @activities.any? %>
<% @activities_by_day.each do |day, by_day_group| %>
<% @by_day_groups.each do |by_day_group| %>
<section class='section'>
<h2 class='title is-h2'><%= day %></h2>
<% by_day_group.each do |project_id, by_project_group| %>
<h2 class='title is-h2'><%= by_day_group[:day] %></h2>
<% by_day_group[:group].each do |project_id, by_project_group| %>
<% project = Project.find(project_id) %>
<h3 class='title is-h3'><%= link_to project.name, project %></h3>
<ul>
Expand Down
8 changes: 6 additions & 2 deletions app/views/todos/_todo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
<span class='level-item'><%= todo.title %> </span>
<% unless todo.deadline.nil? %>
<% if todo.over_due? %>
<span class="tag is-danger is-primary"><%= todo.deadline.to_date %></span>
<span class="tag is-danger is-primary todo-tag"><%= todo.deadline.to_date %></span>
<% else %>
<span class="tag is-info is-primary"><%= todo.deadline.to_date %></span>
<span class="tag is-info is-primary todo-tag"><%= todo.deadline.to_date %></span>
<% end %>
<% end %>

<% unless todo.assigned_member.nil? %>
<span class="tag is-info is-primary todo-tag"><%= todo.assigned_member.name %></span>
<% end %>
</span>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20170407074847_create_todo_members.rb
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
Loading

0 comments on commit d279235

Please sign in to comment.