Refactor preferences -> move them to subcontroller, subroute

This commit is contained in:
Luka Murn 2017-02-09 11:07:16 +01:00
parent e067c88a55
commit 5cb3eb5e09
8 changed files with 166 additions and 140 deletions

View file

@ -0,0 +1,124 @@
module Users
module Settings
module Account
class PreferencesController < ApplicationController
before_action :load_user, only: [
:index,
:update,
:tutorial,
:reset_tutorial,
:notifications_settings
]
def index
end
def update
respond_to do |format|
if @user.update(update_params)
flash[:notice] =
t('users.settings.account.preferences.update_flash')
format.json do
flash.keep
render json: { status: :ok }
end
else
format.json do
render json: @user.errors,
status: :unprocessable_entity
end
end
end
end
def tutorial
@teams =
@user
.user_teams
.includes(team: :users)
.where(role: 1..2)
.order(created_at: :asc)
.map(&:team)
@member_of = @teams.count
respond_to do |format|
format.json do
render json: {
status: :ok,
html: render_to_string(
partial: 'users/settings/account/preferences/' \
'repeat_tutorial_modal_body.html.erb'
)
}
end
end
end
def reset_tutorial
if @user.update(tutorial_status: 0) && params[:team][:id].present?
@user.update(current_team_id: params[:team][:id])
cookies.delete :tutorial_data
cookies.delete :current_tutorial_step
cookies[:repeat_tutorial_team_id] = {
value: params[:team][:id],
expires: 1.day.from_now
}
flash[:notice] = t(
'users.settings.account.preferences.tutorial.tutorial_reset_flash'
)
redirect_to root_path
else
flash[:alert] = t(
'users.settings.account.preferences.tutorial.tutorial_reset_error'
)
redirect_to :back
end
end
def notifications_settings
@user.assignments_notification =
params[:assignments_notification] ? true : false
@user.recent_notification =
params[:recent_notification] ? true : false
@user.recent_notification_email =
params[:recent_notification_email] ? true : false
@user.assignments_notification_email =
params[:assignments_notification_email] ? true : false
@user.system_message_notification_email =
params[:system_message_notification_email] ? true : false
if @user.save
respond_to do |format|
format.json do
render json: {
status: :ok
}
end
end
else
respond_to do |format|
format.json do
render json: {
status: :unprocessable_entity
}
end
end
end
end
private
def load_user
@user = current_user
end
def update_params
params.require(:user).permit(
:time_zone
)
end
end
end
end
end

View file

@ -4,16 +4,11 @@ class Users::SettingsController < ApplicationController
include InputSanitizeHelper
before_action :load_user, only: [
:preferences,
:update_preferences,
:teams,
:team,
:create_team,
:teams_datatable,
:team_users_datatable,
:tutorial,
:reset_tutorial,
:notifications_settings,
:user_current_team,
:destroy_user_team
]
@ -34,26 +29,6 @@ class Users::SettingsController < ApplicationController
:destroy_user_team
]
def preferences
end
def update_preferences
respond_to do |format|
if @user.update(update_preferences_params)
flash[:notice] = t("users.settings.preferences.update_flash")
format.json {
flash.keep
render json: { status: :ok }
}
else
format.json {
render json: @user.errors,
status: :unprocessable_entity
}
end
end
end
def teams
@user_teams =
@user
@ -288,76 +263,6 @@ class Users::SettingsController < ApplicationController
end
end
def tutorial
@teams =
@user
.user_teams
.includes(team: :users)
.where(role: 1..2)
.order(created_at: :asc)
.map(&:team)
@member_of = @teams.count
respond_to do |format|
format.json {
render json: {
status: :ok,
html: render_to_string({
partial: "users/settings/repeat_tutorial_modal_body.html.erb"
})
}
}
end
end
def reset_tutorial
if @user.update(tutorial_status: 0) && params[:team][:id].present?
@user.update(current_team_id: params[:team][:id])
cookies.delete :tutorial_data
cookies.delete :current_tutorial_step
cookies[:repeat_tutorial_team_id] = {
value: params[:team][:id],
expires: 1.day.from_now
}
flash[:notice] = t("users.settings.preferences.tutorial.tutorial_reset_flash")
redirect_to root_path
else
flash[:alert] = t("users.settings.preferences.tutorial.tutorial_reset_error")
redirect_to :back
end
end
def notifications_settings
@user.assignments_notification =
params[:assignments_notification] ? true : false
@user.recent_notification = params[:recent_notification] ? true : false
@user.recent_notification_email =
params[:recent_notification_email] ? true : false
@user.assignments_notification_email =
params[:assignments_notification_email] ? true : false
@user.system_message_notification_email =
params[:system_message_notification_email] ? true : false
if @user.save
respond_to do |format|
format.json do
render json: {
status: :ok
}
end
end
else
respond_to do |format|
format.json do
render json: {
status: :unprocessable_entity
}
end
end
end
end
def user_current_team
team_id = params[:user][:current_team_id].to_i
if @user.teams_ids.include?(team_id)
@ -398,12 +303,6 @@ class Users::SettingsController < ApplicationController
end
end
def update_preferences_params
params.require(:user).permit(
:time_zone
)
end
def create_team_params
params.require(:team).permit(
:name,

View file

@ -1,10 +1,10 @@
<% if @member_of == 0 %>
<div class="row">
<span class="col-xs-12"><em><%=t 'users.settings.preferences.tutorial.no_teams' %></em></span>
<span class="col-xs-12"><em><%=t 'users.settings.account.preferences.tutorial.no_teams' %></em></span>
</div>
<% else %>
<div class="row">
<span class="col-xs-12"><%=t 'users.settings.preferences.tutorial.select_team_instruction' %></span>
<span class="col-xs-12"><%=t 'users.settings.account.preferences.tutorial.select_team_instruction' %></span>
</div>
<hr>
<div class="row">
@ -14,7 +14,7 @@
</div>
<div class="visible-xs">&nbsp;</div>
<div class="col-xs-4 col-sm-2">
<%= f.button t('users.settings.preferences.tutorial.repeat_tutorial'), class: "btn btn-primary" %>
<%= f.button t('users.settings.account.preferences.tutorial.repeat_tutorial'), class: "btn btn-primary" %>
</div>
<% end %>
</div>

View file

@ -1,4 +1,4 @@
<% provide(:head_title, t("users.settings.preferences.head_title")) %>
<% provide(:head_title, t("users.settings.account.preferences.head_title")) %>
<%= render partial: "users/settings/navigation.html.erb" %>
<div class="tab-content">
@ -10,7 +10,7 @@
html: { method: :put, 'data-for' => 'time_zone' }) do |f| %>
<div data-part="view">
<div class="form-group">
<%= f.label t("users.settings.preferences.edit.time_zone_label") %>
<%= f.label t("users.settings.account.preferences.edit.time_zone_label") %>
<div class="input-group" style="max-width: 500px;">
<input class="form-control"
disabled="disabled"
@ -23,17 +23,17 @@
<a href="#" class="btn btn-default" data-action="edit"><%=t "general.edit" %></a>
</span>
</div>
<small><%= t("users.settings.preferences.edit.time_zone_sublabel") %></small>
<small><%= t("users.settings.account.preferences.edit.time_zone_sublabel") %></small>
</div>
</div>
<div data-part="edit" style="display: none;">
<div class="well">
<h4><%=t "users.settings.preferences.edit.time_zone_title" %></h4>
<h4><%=t "users.settings.account.preferences.edit.time_zone_title" %></h4>
<div class="form-group" style="max-width: 500px;">
<%= f.select :time_zone, ActiveSupport::TimeZone.all.collect { |tz|
[tz.formatted_offset + " " + tz.name, tz.name]
}, {}, {class: 'form-control selectpicker', 'ata-role' => 'clear'} %>
<small><%= t("users.settings.preferences.edit.time_zone_sublabel") %></small>
<small><%= t("users.settings.account.preferences.edit.time_zone_sublabel") %></small>
</div>
<div>
<a href="#" class="btn btn-default" data-action="cancel"><%=t "general.cancel" %></a>
@ -45,7 +45,7 @@
<% if Rails.configuration.x.enable_tutorial %>
<hr>
<strong><%= t('tutorial.title') %></strong> <br>
<%= link_to t('users.settings.preferences.tutorial.repeat_tutorial'),
<%= link_to t('users.settings.account.preferences.tutorial.repeat_tutorial'),
tutorial_path(format: :json),
remote: true,
class: 'btn btn-primary',
@ -100,7 +100,7 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" ><%= t("users.settings.preferences.tutorial.modal_title") %></h4>
<h4 class="modal-title" ><%= t("users.settings.account.preferences.tutorial.modal_title") %></h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
@ -112,4 +112,4 @@
<%= javascript_include_tag "users/settings/preferences" %>
<%= javascript_include_tag "users/settings/account/preferences" %>

View file

@ -13,7 +13,8 @@ Rails.application.config.assets.precompile += %w(underscore.js)
Rails.application.config.assets.precompile += %w(jsPlumb-2.0.4-min.js)
Rails.application.config.assets.precompile += %w(jsnetworkx.js)
Rails.application.config.assets.precompile += %w(handsontable.full.min.js)
Rails.application.config.assets.precompile += %w(users/settings/preferences.js)
Rails.application.config.assets.precompile +=
%w(users/settings/account/preferences.js)
Rails.application.config.assets.precompile += %w(users/settings/teams.js)
Rails.application.config.assets.precompile +=
%w(users/settings/teams_datatable.js)

View file

@ -1068,24 +1068,25 @@ en:
changed_team_in_search: "The searched item is not in your current team. You will be redirected to %{team} team!"
navigation:
preferences: "My preferences"
teams: "My teams"
preferences:
head_title: "Settings | My preferences"
edit:
time_zone_label: "Time zone"
time_zone_sublabel: "Time zone setting affects all time & date fields throughout application."
time_zone_title: "Time zone"
update_flash: "Preferences successfully updated."
tutorial:
modal_title: "Repeat tutorial"
no_teams: "Seems like you are not a member of any team. Tutorials can be run only by team members."
repeat_tutorial: "Repeat tutorial"
select_team_instruction: "Select the team where you wish to create the demo project."
select_team: "Select team"
tutorial_reset_flash: "Tutorial can now be repeated."
tutorial_reset_error: "Tutorial could not be repeated."
teams: "Teams"
account:
preferences:
head_title: "Settings | My preferences"
edit:
time_zone_label: "Time zone"
time_zone_sublabel: "Time zone setting affects all time & date fields throughout application."
time_zone_title: "Time zone"
update_flash: "Preferences successfully updated."
tutorial:
modal_title: "Repeat tutorial"
no_teams: "Seems like you are not a member of any team. Tutorials can be run only by team members."
repeat_tutorial: "Repeat tutorial"
select_team_instruction: "Select the team where you wish to create the demo project."
select_team: "Select team"
tutorial_reset_flash: "Tutorial can now be repeated."
tutorial_reset_error: "Tutorial could not be repeated."
teams:
head_title: "Settings | My teams"
head_title: "Settings | Teams"
breadcrumbs:
all: "All teams"
new_team: "New team"

View file

@ -23,22 +23,23 @@ Rails.application.routes.draw do
get 'not_found', to: 'application#not_found', as: 'not_found'
# Settings
get 'users/settings/preferences',
to: 'users/settings#preferences',
get 'users/settings/account/preferences',
to: 'users/settings/account/preferences#index',
as: 'preferences'
put 'users/settings/preferences',
to: 'users/settings#update_preferences',
put 'users/settings/account/preferences',
to: 'users/settings/account/preferences#update',
as: 'update_preferences'
get 'users/settings/preferences/tutorial',
to: 'users/settings#tutorial',
get 'users/settings/account/preferences/tutorial',
to: 'users/settings/account/preferences#tutorial',
as: 'tutorial'
post 'users/settings/preferences/reset_tutorial/',
to: 'users/settings#reset_tutorial',
post 'users/settings/account/preferences/reset_tutorial/',
to: 'users/settings/account/preferences#reset_tutorial',
as: 'reset_tutorial'
post 'users/settings/preferences/notifications_settings',
to: 'users/settings#notifications_settings',
post 'users/settings/account/preferences/notifications_settings',
to: 'users/settings/account/preferences#notifications_settings',
as: 'notifications_settings',
defaults: { format: 'json' }
post 'users/settings/user_current_team',
to: 'users/settings#user_current_team',
as: 'user_current_team'