2014-03-05 06:52:20 -06:00
|
|
|
class Admin::BadgesController < Admin::AdminController
|
2014-07-24 03:28:09 -05:00
|
|
|
|
|
|
|
def index
|
|
|
|
data = {
|
2014-07-27 03:22:01 -05:00
|
|
|
badge_types: BadgeType.all.order(:id).to_a,
|
|
|
|
badge_groupings: BadgeGrouping.all.order(:position).to_a,
|
2014-07-29 17:35:15 -05:00
|
|
|
badges: Badge.includes(:badge_grouping)
|
|
|
|
.references(:badge_grouping)
|
|
|
|
.order('badge_groupings.position, badge_type_id, badges.name').to_a,
|
2014-07-24 03:28:09 -05:00
|
|
|
protected_system_fields: Badge.protected_system_fields,
|
|
|
|
triggers: Badge.trigger_hash
|
|
|
|
}
|
|
|
|
render_serialized(OpenStruct.new(data), AdminBadgesSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 17:17:29 -05:00
|
|
|
render json: BadgeGranter.preview(params[:sql],
|
|
|
|
target_posts: params[:target_posts] == "true",
|
|
|
|
explain: params[:explain] == "true",
|
|
|
|
trigger: params[:trigger].to_i)
|
2014-07-24 03:28:09 -05:00
|
|
|
end
|
|
|
|
|
2014-10-17 13:27:40 -05:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
2014-03-05 06:52:20 -06:00
|
|
|
def badge_types
|
|
|
|
badge_types = BadgeType.all.to_a
|
|
|
|
render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types")
|
|
|
|
end
|
|
|
|
|
2014-07-27 03:22:01 -05: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 20:11:30 -05:00
|
|
|
render_serialized(badge_groupings, BadgeGroupingSerializer, root: "badge_groupings")
|
|
|
|
end
|
|
|
|
|
2014-03-05 06:52:20 -06:00
|
|
|
def create
|
|
|
|
badge = Badge.new
|
2014-09-02 15:22:52 -05:00
|
|
|
errors = update_badge_from_params(badge, new: true)
|
|
|
|
|
|
|
|
if errors.present?
|
|
|
|
render_json_error errors
|
|
|
|
else
|
|
|
|
render_serialized(badge, BadgeSerializer, root: "badge")
|
|
|
|
end
|
2014-03-05 06:52:20 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
badge = find_badge
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 17:17:29 -05:00
|
|
|
|
2014-09-02 15:22:52 -05:00
|
|
|
errors = update_badge_from_params(badge)
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 17:17:29 -05:00
|
|
|
|
2014-09-02 15:22:52 -05:00
|
|
|
if errors.present?
|
|
|
|
render_json_error errors
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 17:17:29 -05:00
|
|
|
else
|
|
|
|
render_serialized(badge, BadgeSerializer, root: "badge")
|
|
|
|
end
|
2014-03-05 06:52:20 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
find_badge.destroy
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def find_badge
|
|
|
|
params.require(:id)
|
|
|
|
Badge.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2014-09-02 15:22:52 -05:00
|
|
|
# Options:
|
|
|
|
# :new - reset the badge id to nil before saving
|
|
|
|
def update_badge_from_params(badge, opts={})
|
|
|
|
errors = []
|
|
|
|
Badge.transaction do
|
|
|
|
allowed = Badge.column_names.map(&:to_sym)
|
|
|
|
allowed -= [:id, :created_at, :updated_at, :grant_count]
|
|
|
|
allowed -= Badge.protected_system_fields if badge.system?
|
|
|
|
params.permit(*allowed)
|
|
|
|
|
|
|
|
allowed.each do |key|
|
|
|
|
badge.send("#{key}=" , params[key]) if params[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Badge query contract checks
|
|
|
|
begin
|
|
|
|
BadgeGranter.contract_checks!(badge.query, { target_posts: badge.target_posts, trigger: badge.trigger })
|
|
|
|
rescue => e
|
2014-10-17 13:27:40 -05:00
|
|
|
errors << e.message
|
2014-09-02 15:22:52 -05:00
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
|
|
|
|
badge.id = nil if opts[:new]
|
|
|
|
badge.save!
|
|
|
|
end
|
2014-07-21 20:11:30 -05:00
|
|
|
|
2014-10-17 13:27:40 -05:00
|
|
|
errors
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
errors.push(*badge.errors.full_messages)
|
2014-09-02 15:22:52 -05:00
|
|
|
errors
|
2014-03-05 06:52:20 -06:00
|
|
|
end
|
|
|
|
end
|