4

Just got the Merit gem up and running with my rails 4 app.

So far, a user receives the first badge upon creating a subarticle.

The second badge I am trying to award is when that subarticle actually gets approved, which can only be done by an admin. I have tried using subarticle#update as the action, but it is not awarding the user the badge. Maybe it has to do with the admin updating the subarticle from the admin panel (rails admin gem)?

Here is my badge in the initializer:

Merit::Badge.create!(
  id: 2,
  name: "Serious Writer",
  description: "Things are gettin' serious! You have successfully written an approved article."
  )

and here are the rules:

grant_on ['subarticles#update','subarticles#destroy'], badge_id: 2, badge:'serious writer', to: :user, temporary: true do |subarticle|
  subarticle.user.subarticles.approved.count >= 1
end

Any suggestions? Still new to this gem so maybe its a simple mistake.

Kathan
  • 1,428
  • 2
  • 15
  • 31
  • You might need to override the rails admin controller, like we do with Devise https://github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-registration-using-Devise. Also, `grant_on` should probably be called for `'admin/subarticles#update'`. – TuteC Jul 09 '15 at 21:46
  • Do you know what class I would assign the controller? For devise its `Users::RegistrationsController < Devise::RegistrationsController` but not sure how this works with rails_admin. Thanks for the help! @TuteC – Kathan Jul 09 '15 at 23:36
  • It did not seem to work. The badge is awarded perfectly fine when updating the subarticle from the normal form (edit.html.erb), but something goes wrong when doing from admin panel @TuteC – Kathan Jul 10 '15 at 00:41
  • No idea exactly how to integrate with rails admin. Something to check in rails admin's internals. – TuteC Jul 10 '15 at 13:48

1 Answers1

2

What is your setup like for approving article? You could do it in your controllers, but I'd prefer to keep them skinny and have the logic done in your fat models.

Please note, there are other ways to do this but here's what I have done in the past.

class Subarticles < ActiveRecord::Base
  ...
  after_update :provide_badge_when_approved

  private

  def provide_badge_when_approved
    self.user.add_badge(2) if (self.approved_changed? && self.approved == true)
  end
  ...
end
  • This is what I use to approve a subarticle: `scope :approved, -> {where(:approved => true)}` which is inside of my subarticle model. Then it will only display if the approved box gets checked by an admin, inside of the admin panel (rails admin gem). Also, subarticles are nested into articles. Let me play around with what you gave me. I think it may work. Thank you! @Andrewcpkelley – Kathan Jul 19 '15 at 22:49
  • @Kathan Let me know if it works. I added a check to see if the approved attribute has been changed from false to true. –  Jul 20 '15 at 01:29