localized ajax controllers

This commit is contained in:
pixeldesu 2015-06-07 18:24:01 +02:00
parent cd2685a179
commit e6e1d03187
11 changed files with 194 additions and 87 deletions

View file

@ -1,7 +1,7 @@
class Ajax::AnswerController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |titanic_param|
@status = :parameter_error
@message = "#{titanic_param.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: titanic_param.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -19,7 +19,7 @@ class Ajax::AnswerController < ApplicationController
unless current_user == inbox_entry.user
@status = :fail
@message = "question not in your inbox"
@message = I18n.t('messages.answer.create.fail')
@success = false
return
end
@ -28,7 +28,7 @@ class Ajax::AnswerController < ApplicationController
unless question.user.privacy_allow_stranger_answers
@status = :privacy_stronk
@message = "This user does not want other users to answer their question."
@message = I18n.t('messages.answer.create.privacy_stronk')
@success = false
return
end
@ -37,7 +37,7 @@ class Ajax::AnswerController < ApplicationController
# this should never trigger because empty params throw ParameterMissing
unless params[:answer].length > 0
@status = :peter_dinklage
@message = "Answer is too short"
@message = I18n.t('messages.answer.create.peter_dinklage')
@success = false
return
end
@ -52,7 +52,7 @@ class Ajax::AnswerController < ApplicationController
end
rescue
@status = :err
@message = "An error occurred"
@message = I18n.t('messages.error')
@success = false
return
end
@ -62,7 +62,7 @@ class Ajax::AnswerController < ApplicationController
@status = :okay
@message = "Successfully answered question."
@message = I18n.t('messages.answer.create.okay')
@success = true
unless inbox
@question = 1
@ -77,7 +77,7 @@ class Ajax::AnswerController < ApplicationController
unless (current_user == answer.user) or (privileged? answer.user)
@status = :nopriv
@message = "can't delete other people's answers"
@message = I18n.t('messages.answer.destroy.nopriv')
@success = false
return
end
@ -88,7 +88,7 @@ class Ajax::AnswerController < ApplicationController
answer.destroy
@status = :okay
@message = "Successfully deleted answer."
@message = I18n.t('messages.answer.destroy.okay')
@success = true
end
end

View file

@ -1,7 +1,7 @@
class Ajax::CommentController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -16,13 +16,13 @@ class Ajax::CommentController < ApplicationController
current_user.comment(answer, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = "Your comment is too long."
@message = I18n.t('messages.comment.create.rec_inv')
@success = false
return
end
@status = :okay
@message = "Comment posted successfully."
@message = I18n.t('messages.comment.create.okay')
@success = true
@render = render_to_string(partial: 'shared/comments', locals: { a: answer })
@count = answer.comment_count
@ -37,7 +37,7 @@ class Ajax::CommentController < ApplicationController
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
@status = :nopriv
@message = "can't delete other people's comments"
@message = I18n.t('messages.comment.destroy.nopriv')
@success = false
return
end
@ -46,7 +46,7 @@ class Ajax::CommentController < ApplicationController
comment.destroy
@status = :okay
@message = "Successfully deleted comment."
@message = I18n.t('messages.comment.destroy.okay')
@success = true
end
end

View file

@ -1,7 +1,7 @@
class Ajax::FriendController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -15,13 +15,13 @@ class Ajax::FriendController < ApplicationController
current_user.follow target_user
rescue
@status = :fail
@message = "You are already following that user."
@message = I18n.t('messages.friend.create.fail')
@success = false
return
end
@status = :okay
@message = "Successfully followed user."
@message = I18n.t('messages.friend.create.okay')
@success = true
end
@ -34,13 +34,13 @@ class Ajax::FriendController < ApplicationController
current_user.unfollow target_user
rescue
@status = :fail
@message = "You are not following that user."
@message = I18n.t('messages.friend.destroy.fail')
@success = false
return
end
@status = :okay
@message = "Successfully unfollowed user."
@message = I18n.t('messages.friend.destroy.okay')
@success = true
end
end

View file

@ -1,7 +1,7 @@
class Ajax::GroupController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -12,7 +12,7 @@ class Ajax::GroupController < ApplicationController
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
@message = I18n.t('messages.noauth')
return
end
@ -20,7 +20,7 @@ class Ajax::GroupController < ApplicationController
params.require :name
rescue ActionController::ParameterMissing
@status = :toolong
@message = "Please give that group a name."
@message = I18n.t('messages.group.create.noname')
return
end
params.require :user
@ -30,21 +30,21 @@ class Ajax::GroupController < ApplicationController
group = Group.create! user: current_user, display_name: params[:name]
rescue ActiveRecord::RecordInvalid
@status = :toolong
@message = "Group name too long (30 characters max.)"
@message = I18n.t('messages.group.create.toolong')
return
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = "Could not find user."
@message = I18n.t('messages.group.create.notfound')
return
rescue ActiveRecord::RecordNotUnique
@status = :exists
@message = "Group already exists."
@message = I18n.t('messages.group.create.exists')
return
end
@status = :okay
@success = true
@message = "Successfully created group."
@message = I18n.t('messages.group.create.okay')
@render = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user })
end
@ -54,7 +54,7 @@ class Ajax::GroupController < ApplicationController
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
@message = I18n.t('messages.noauth')
return
end
@ -64,13 +64,13 @@ class Ajax::GroupController < ApplicationController
Group.where(user: current_user, name: params[:group]).first.destroy!
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = "Could not find group."
@message = I18n.t('messages.group.destroy.notfound')
return
end
@status = :okay
@success = true
@message = "Successfully deleted group."
@message = I18n.t('messages.group.destroy.okay')
end
def membership
@ -79,7 +79,7 @@ class Ajax::GroupController < ApplicationController
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
@message = I18n.t('messages.noauth')
return
end
@ -93,7 +93,7 @@ class Ajax::GroupController < ApplicationController
group = current_user.groups.find_by_name(params[:group])
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = "Group not found."
@message = I18n.t('messages.group.membership.notfound')
return
end
@ -102,11 +102,11 @@ class Ajax::GroupController < ApplicationController
if add
group.add_member target_user if group.members.find_by_user_id(target_user.id).nil?
@checked = true
@message = "Successfully added user to group."
@message = I18n.t('messages.group.membership.add')
else
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
@checked = false
@message = "Successfully removed user from group."
@message = I18n.t('messages.group.membership.remove')
end
@status = :okay

View file

@ -1,7 +1,7 @@
class Ajax::InboxController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -9,7 +9,7 @@ class Ajax::InboxController < ApplicationController
def create
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
@message = I18n.t('messages.noauth')
@success = false
return
end
@ -22,7 +22,7 @@ class Ajax::InboxController < ApplicationController
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
@status = :okay
@message = "Successfully added new question."
@message = I18n.t('messages.inbox.create.okay')
@success = true
@render = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
inbox.update(new: false)
@ -35,7 +35,7 @@ class Ajax::InboxController < ApplicationController
unless current_user == inbox.user
@status = :fail
@message = "question not in your inbox"
@message = I18n.t('messages.inbox.remove.fail')
@success = false
return
end
@ -44,13 +44,13 @@ class Ajax::InboxController < ApplicationController
inbox.remove
rescue
@status = :err
@message = "An error occurred"
@message = I18n.t('messages.error')
@success = false
return
end
@status = :okay
@message = "Successfully deleted question."
@message = I18n.t('messages.inbox.remove.okay')
@success = true
end
@ -59,13 +59,13 @@ class Ajax::InboxController < ApplicationController
Inbox.where(user: current_user).each { |i| i.remove }
rescue
@status = :err
@message = "An error occurred"
@message = I18n.t('messages.error')
@success = false
return
end
@status = :okay
@message = "Successfully deleted questions."
@message = I18n.t('messages.inbox.remove_all.okay')
@success = true
render 'ajax/inbox/remove'
end

View file

@ -1,7 +1,7 @@
class Ajax::ModerationController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -16,14 +16,14 @@ class Ajax::ModerationController < ApplicationController
current_user.report_vote(report, params[:upvote])
rescue
@status = :fail
@message = "You have already voted on this report."
@message = I18n.t('messages.moderation.vote.fail')
@success = false
return
end
@count = report.votes
@status = :okay
@message = "Successfully voted on report."
@message = I18n.t('messages.moderation.vote.okay')
@success = true
end
@ -36,14 +36,14 @@ class Ajax::ModerationController < ApplicationController
current_user.report_unvote report
rescue
@status = :fail
@message = "You have not voted on that report."
@message = I18n.t('messages.moderation.destroy_vote.fail')
@success = false
return
end
@count = report.votes
@status = :okay
@message = "Successfully removed vote from report."
@message = I18n.t('messages.moderation.destroy_vote.okay')
@success = true
end
@ -57,13 +57,13 @@ class Ajax::ModerationController < ApplicationController
report.save
rescue
@status = :fail
@message = "Something bad happened!"
@message = I18n.t('messages.moderation.destroy_report.fail')
@success = false
return
end
@status = :okay
@message = "WHERE DID IT GO??? OH NO!!!"
@message = I18n.t('messages.moderation.destroy_report.okay')
@success = true
end
@ -79,12 +79,12 @@ class Ajax::ModerationController < ApplicationController
current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = "Your comment is too long."
@message = I18n.t('messages.moderation.create_comment.rec_inv')
return
end
@status = :okay
@message = "Comment posted successfully."
@message = I18n.t('messages.moderation.create_comment.okay')
@success = true
@render = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@count = report.moderation_comments.all.count
@ -99,7 +99,7 @@ class Ajax::ModerationController < ApplicationController
unless current_user == comment.user
@status = :nopriv
@message = "can't delete other people's comments"
@message = I18n.t('messages.moderation.destroy_comment.nopriv')
@success = false
return
end
@ -107,13 +107,13 @@ class Ajax::ModerationController < ApplicationController
comment.destroy
@status = :okay
@message = "Successfully deleted comment."
@message = I18n.t('messages.moderation.destroy_comment.okay')
@success = true
end
def ban
@status = :err
@message = "Weird..."
@message = I18n.t('messages.moderation.ban.error')
@success = false
params.require :user
@ -129,21 +129,21 @@ class Ajax::ModerationController < ApplicationController
if not unban and target.admin?
@status = :nopriv
@message = "You cannot ban an administrator!"
@message = I18n.t('messages.moderation.ban.nopriv')
@success = false
return
end
if unban
target.unban
@message = "Unbanned user."
@message = I18n.t('messages.moderation.ban.unban')
@success = true
elsif perma
target.ban nil, reason
@message = "Permanently banned user."
@message = I18n.t('messages.moderation.ban.perma')
else
target.ban buntil, reason
@message = "Banned user until #{buntil.to_s}"
@message = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
end
target.save!
@ -163,12 +163,12 @@ class Ajax::ModerationController < ApplicationController
target_user = User.find_by_screen_name(params[:user])
@message = "nope!"
@message = I18n.t('messages.moderation.privilege.nope')
return unless %w(blogger supporter moderator admin contributor).include? params[:type].downcase
if %w(supporter moderator admin).include?(params[:type].downcase) and !current_user.admin?
@status = :nopriv
@message = "You'd better check YOUR privileges first!"
@message = I18n.t('messages.moderation.privilege.nopriv')
@success = false
return
end
@ -177,7 +177,7 @@ class Ajax::ModerationController < ApplicationController
target_user.send("#{params[:type]}=", status)
target_user.save!
@message = "Successfully checked this user's #{params[:type]} privilege."
@message = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
@status = :okay
@success = true

View file

@ -3,7 +3,7 @@ class Ajax::QuestionController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -14,14 +14,14 @@ class Ajax::QuestionController < ApplicationController
question = Question.find params[:question]
if question.nil?
@status = :not_found
@message = "Question does not exist"
@message = I18n.t('messages.question.destroy.not_found')
@success = false
return
end
if not (current_user.mod? or question.user == current_user)
@status = :not_authorized
@message = "You are not allowed to delete this question"
@message = I18n.t('messages.question.destroy.not_authorized')
@success = false
return
end
@ -29,7 +29,7 @@ class Ajax::QuestionController < ApplicationController
question.destroy!
@status = :okay
@message = "Successfully deleted question."
@message = I18n.t('messages.question.destroy.okay')
@success = true
end
@ -44,7 +44,7 @@ class Ajax::QuestionController < ApplicationController
user: current_user)
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = "Your question is too long."
@message = I18n.t('messages.question.create.rec_inv')
@success = false
return
end
@ -67,7 +67,7 @@ class Ajax::QuestionController < ApplicationController
end
rescue ActiveRecord::RecordNotFound
@status = :not_found
@message = "Group not found"
@message = I18n.t('messages.question.create.not_found')
@success = false
return
end
@ -77,17 +77,17 @@ class Ajax::QuestionController < ApplicationController
end
@status = :okay
@message = "Question asked successfully."
@message = I18n.t('messages.question.create.okay')
@success = true
end
def preview
params.require :md
@message = "Failed to render markdown."
@message = I18n.t('messages.question.preview.fail')
begin
@markdown = markdown(params[:md], Time.new)
@message = "Successfully rendered markdown."
@message = I18n.t('messages.question.preview.okay')
rescue
@status = :fail
@success = false

View file

@ -1,7 +1,7 @@
class Ajax::ReportController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -14,12 +14,12 @@ class Ajax::ReportController < ApplicationController
@success = false
if current_user.nil?
@message = "login required"
@message = I18n.t('messages.report.create.login')
return
end
unless %w(answer comment question user).include? params[:type]
@message = "unknown type"
@message = I18n.t('messages.report.create.unknown')
return
end
@ -30,14 +30,14 @@ class Ajax::ReportController < ApplicationController
end
if object.nil?
@message = "Could not find #{params[:type]}"
@message = I18n.t('messages.report.create.not_found', parameter: params[:type])
return
end
current_user.report object, params[:reason]
@status = :okay
@message = "#{params[:type].capitalize} reported. A moderator will decide what happens with the #{params[:type]}."
@message = I18n.t('messages.report.create.okay', parameter: params[:type])
@success = true
end
end

View file

@ -1,7 +1,7 @@
class Ajax::SmileController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -15,13 +15,13 @@ class Ajax::SmileController < ApplicationController
current_user.smile answer
rescue
@status = :fail
@message = "You have already smiled that answer."
@message = I18n.t('messages.smile.create.fail')
@success = false
return
end
@status = :okay
@message = "Successfully smiled answer."
@message = I18n.t('messages.smile.create.okay')
@success = true
end
@ -34,13 +34,13 @@ class Ajax::SmileController < ApplicationController
current_user.unsmile answer
rescue
@status = :fail
@message = "You have not smiled that answer."
@message = I18n.t('messages.smile.destroy.fail')
@success = false
return
end
@status = :okay
@message = "Successfully unsmiled answer."
@message = I18n.t('messages.smile.destroy.okay')
@success = true
end
@ -53,13 +53,13 @@ class Ajax::SmileController < ApplicationController
current_user.smile_comment comment
rescue
@status = :fail
@message = "You have already smiled that comment."
@message = I18n.t('messages.smile.create_comment.fail')
@success = false
return
end
@status = :okay
@message = "Successfully smiled comment."
@message = I18n.t('messages.smile.create_comment.okay')
@success = true
end
@ -72,13 +72,13 @@ class Ajax::SmileController < ApplicationController
current_user.unsmile_comment comment
rescue
@status = :fail
@message = "You have not smiled that comment."
@message = I18n.t('messages.smile.destroy_comment.fail')
@success = false
return
end
@status = :okay
@message = "Successfully unsmiled comment."
@message = I18n.t('messages.smile.destroy_comment.okay')
@success = true
end
end

View file

@ -2,7 +2,7 @@ class Ajax::SubscriptionController < ApplicationController
before_filter :authenticate_user!
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = "#{param_miss_ex.param.capitalize} is required"
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
@ -10,7 +10,7 @@ class Ajax::SubscriptionController < ApplicationController
def subscribe
params.require :answer
@status = 418
@message = "418 I'm a torpedo"
@message = I18n.t('messages.subscription.torpedo')
state = Subscription.subscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false
end
@ -18,7 +18,7 @@ class Ajax::SubscriptionController < ApplicationController
def unsubscribe
params.require :answer
@status = 418
@message = "418 I'm a torpedo"
@message = I18n.t('messages.subscription.torpedo')
state = Subscription.unsubscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false
end

View file

@ -20,9 +20,116 @@
# available at http://guides.rubyonrails.org/i18n.html.
en:
messages:
noscript: "Please activate JavaScript."
error: "An error occurred."
parameter_error: "%{parameter} is required."
noauth: "requires authentication"
answer:
create:
fail: "Question is not in your inbox."
privacy_stronk: "This user does not want other users to answer their question."
peter_dinklage: "Answer is too short."
okay: "Successfully answered question."
destroy:
nopriv: "can't delete other people's answers"
okay: "Successfully deleted answer."
comment:
create:
rec_inv: "Your comment is too long."
okay: "Comment posted successfully."
destroy:
nopriv: "can't delete other people's comments"
okay: "Successfully deleted comment."
friend:
create:
fail: "You are already following that user."
okay: "Successfully followed user."
destroy:
fail: "You are not following that user."
okay: "Successfully unfollowed user."
group:
create:
noname: "Please give that group a name."
toolong: "Group name too long (30 characters max.)"
notfound: "Could not find user."
exists: "Group already exists."
okay: "Successfully created group."
destroy:
notfound: "Could not find group."
okay: "Successfully deleted group."
membership:
notfound: "Group not found."
add: "Successfully added user to group."
remove: "Successfully removed user from group."
inbox:
create:
okay: "Successfully added new question."
remove:
fail: "question not in your inbox"
okay: "Successfully deleted question."
remove_all:
okay: "Successfully deleted questions."
moderation:
vote:
fail: "You have already voted on this report."
okay: "Successfully voted on report."
destroy_vote:
fail: "You have not voted on that report."
okay: "Successfully removed vote from report."
destroy_report:
fail: "Something bad happened!"
okay: "WHERE DID IT GO??? OH NO!!!"
create_comment:
rec_inv: "Your comment is too long."
okay: "Comment posted successfully."
destroy_comment:
nopriv: "can't delete other people's comments"
okay: "Successfully deleted comment."
ban:
error: "Weird..."
nopriv: "You cannot ban an administrator!"
unban: "Unbanned user."
perma: "Permanently banned user."
temp: "Banned user until %{date}"
privilege:
nope: "nope!"
nopriv: "You'd better check YOUR privileges first!"
checked: "Successfully checked this user's %{privilege} privilege."
question:
destroy:
not_found: "Question does not exist"
not_authorized: "You are not allowed to delete this question"
okay: "Successfully deleted question."
create:
rec_inv: "Your question is too long."
not_found: "Group not found"
okay: "Question asked successfully."
preview:
fail: "Failed to render markdown."
okay: "Successfully rendered markdown."
report:
create:
login: "login required"
unknown: "unknown type"
not_found: "Could not find %{parameter}"
okay: "%{parameter} reported. A moderator will decide what happens with the #{parameter}."
smile:
create:
fail: "You have already smiled that answer."
okay: "Successfully smiled answer."
destroy:
fail: "You have not smiled that answer."
okay: "Successfully unsmiled answer."
create_comment:
fail: "You have already smiled that comment."
okay: "Successfully smiled comment."
destroy_comment:
fail: "You have not smiled that comment."
okay: "Successfully unsmiled comment."
subscription:
torpedo: "418 I'm a torpedo"
views:
messages:
noscript: "Please activate JavaScript."
notifications:
show: "Show all notifications"
mark: " and mark them as read"