Kathan
Kathan

Reputation: 1458

Merit rails 4 not working for update action

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.

Upvotes: 4

Views: 198

Answers (1)

user1724295
user1724295

Reputation:

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

Upvotes: 2

Related Questions