2014-04-16 08:12:06 -05:00
|
|
|
class BadgesController < ApplicationController
|
2014-04-25 13:58:41 -05:00
|
|
|
skip_before_filter :check_xhr, only: [:index, :show]
|
|
|
|
|
2014-04-16 08:12:06 -05:00
|
|
|
def index
|
2014-07-03 05:34:26 -05:00
|
|
|
badges = Badge.all
|
2014-07-14 02:40:01 -05:00
|
|
|
badges = badges.where(enabled: true, listable: true) if(params[:only_listable] == "true") || !request.xhr?
|
2014-07-03 05:34:26 -05:00
|
|
|
badges = badges.to_a
|
2014-07-16 02:53:54 -05:00
|
|
|
user_badges = nil
|
|
|
|
if current_user
|
|
|
|
user_badges = Set.new(current_user.user_badges.select('distinct badge_id').pluck(:badge_id))
|
|
|
|
end
|
|
|
|
serialized = MultiJson.dump(serialize_data(badges, BadgeSerializer, root: "badges", user_badges: user_badges))
|
2014-04-25 13:58:41 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded "badges", serialized
|
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
format.json { render json: serialized }
|
|
|
|
end
|
2014-04-16 08:12:06 -05:00
|
|
|
end
|
2014-04-16 09:56:11 -05:00
|
|
|
|
|
|
|
def show
|
|
|
|
params.require(:id)
|
2014-07-14 02:40:01 -05:00
|
|
|
badge = Badge.enabled.find(params[:id])
|
2014-06-14 02:55:06 -05:00
|
|
|
|
|
|
|
if current_user
|
|
|
|
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: badge.id)
|
|
|
|
if user_badge && user_badge.notification
|
|
|
|
user_badge.notification.update_attributes read: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-25 13:58:41 -05:00
|
|
|
serialized = MultiJson.dump(serialize_data(badge, BadgeSerializer, root: "badge"))
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded "badge", serialized
|
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
format.json { render json: serialized }
|
|
|
|
end
|
2014-04-16 09:56:11 -05:00
|
|
|
end
|
2014-04-16 08:12:06 -05:00
|
|
|
end
|