2014-03-05 07:52:20 -05:00
|
|
|
class Admin::BadgesController < Admin::AdminController
|
2014-07-24 04:28:09 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
data = {
|
2014-07-27 04:22:01 -04:00
|
|
|
badge_types: BadgeType.all.order(:id).to_a,
|
|
|
|
badge_groupings: BadgeGrouping.all.order(:position).to_a,
|
2014-07-29 18:35:15 -04:00
|
|
|
badges: Badge.includes(:badge_grouping)
|
|
|
|
.references(:badge_grouping)
|
|
|
|
.order('badge_groupings.position, badge_type_id, badges.name').to_a,
|
2014-07-24 04:28:09 -04:00
|
|
|
protected_system_fields: Badge.protected_system_fields,
|
|
|
|
triggers: Badge.trigger_hash
|
|
|
|
}
|
|
|
|
render_serialized(OpenStruct.new(data), AdminBadgesSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
|
|
|
render json: BadgeGranter.preview(params[:sql], target_posts: params[:target_posts] == "true")
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
def badge_types
|
|
|
|
badge_types = BadgeType.all.to_a
|
|
|
|
render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types")
|
|
|
|
end
|
|
|
|
|
2014-07-27 04:22:01 -04:00
|
|
|
def save_badge_groupings
|
|
|
|
|
|
|
|
badge_groupings = BadgeGrouping.all.order(:position).to_a
|
|
|
|
ids = params[:ids].map(&:to_i)
|
|
|
|
|
|
|
|
params[:names].each_with_index do |name,index|
|
|
|
|
id = ids[index].to_i
|
|
|
|
group = badge_groupings.find{|b| b.id == id} || BadgeGrouping.new()
|
|
|
|
group.name = name
|
|
|
|
group.position = index
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
|
|
|
|
badge_groupings.each do |g|
|
|
|
|
g.destroy unless g.system? || ids.include?(g.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
badge_groupings = BadgeGrouping.all.order(:position).to_a
|
2014-07-21 21:11:30 -04:00
|
|
|
render_serialized(badge_groupings, BadgeGroupingSerializer, root: "badge_groupings")
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
def create
|
|
|
|
badge = Badge.new
|
|
|
|
update_badge_from_params(badge)
|
2014-08-05 20:51:39 -04:00
|
|
|
badge.id = nil
|
2014-03-05 07:52:20 -05:00
|
|
|
badge.save!
|
|
|
|
render_serialized(badge, BadgeSerializer, root: "badge")
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
badge = find_badge
|
|
|
|
update_badge_from_params(badge)
|
|
|
|
badge.save!
|
|
|
|
render_serialized(badge, BadgeSerializer, root: "badge")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
find_badge.destroy
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def find_badge
|
|
|
|
params.require(:id)
|
|
|
|
Badge.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_badge_from_params(badge)
|
2014-07-24 04:28:09 -04:00
|
|
|
allowed = Badge.column_names.map(&:to_sym)
|
|
|
|
allowed -= [:id, :created_at, :updated_at, :grant_count]
|
|
|
|
allowed -= Badge.protected_system_fields if badge.system?
|
2014-07-21 21:11:30 -04:00
|
|
|
params.permit(*allowed)
|
|
|
|
|
|
|
|
allowed.each do |key|
|
|
|
|
badge.send("#{key}=" , params[key]) if params[key]
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
badge
|
|
|
|
end
|
|
|
|
end
|